Skip to content

Commit 1febc36

Browse files
Merge pull request #431 from N1ebieski/Add-a-command-that-wraps-selected-text-in-a-helper-#51
Add a command that wraps selected text with a helper
2 parents d177d94 + a2f152b commit 1febc36

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed

package.json

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,44 @@
4242
}
4343
],
4444
"contributes": {
45+
"submenus": [
46+
{
47+
"id": "laravel.wrapWithHelper.submenu",
48+
"label": "Wrap selection with helpers"
49+
}
50+
],
4551
"menus": {
4652
"commandPalette": [
53+
{
54+
"command": "laravel.wrapWithHelper",
55+
"when": "editorHasSelection",
56+
"group": "laravel"
57+
},
58+
{
59+
"command": "laravel.wrapWithHelper.unwrap",
60+
"when": "editorHasSelection",
61+
"group": "laravel"
62+
},
63+
{
64+
"command": "laravel.wrapWithHelper.dd",
65+
"when": "false",
66+
"group": "laravel"
67+
},
68+
{
69+
"command": "laravel.wrapWithHelper.dump",
70+
"when": "false",
71+
"group": "laravel"
72+
},
73+
{
74+
"command": "laravel.wrapWithHelper.collect",
75+
"when": "false",
76+
"group": "laravel"
77+
},
78+
{
79+
"command": "laravel.wrapWithHelper.str",
80+
"when": "false",
81+
"group": "laravel"
82+
},
4783
{
4884
"command": "laravel.refactorSelectedHtmlClassToBladeDirective",
4985
"when": "editorHasSelection && resourceLangId == blade",
@@ -56,6 +92,30 @@
5692
}
5793
],
5894
"editor/context": [
95+
{
96+
"submenu": "laravel.wrapWithHelper.submenu",
97+
"when": "editorHasSelection",
98+
"group": "laravel"
99+
},
100+
{
101+
"command": "laravel.wrapWithHelper.unwrap",
102+
"when": "editorHasSelection",
103+
"group": "laravel"
104+
}
105+
],
106+
"laravel.wrapWithHelper.submenu": [
107+
{
108+
"command": "laravel.wrapWithHelper.dd"
109+
},
110+
{
111+
"command": "laravel.wrapWithHelper.dump"
112+
},
113+
{
114+
"command": "laravel.wrapWithHelper.collect"
115+
},
116+
{
117+
"command": "laravel.wrapWithHelper.str"
118+
},
59119
{
60120
"command": "laravel.refactorSelectedHtmlClassToBladeDirective",
61121
"when": "editorHasSelection && resourceLangId == blade",
@@ -109,6 +169,36 @@
109169
"title": "Run Pint on Current File",
110170
"category": "Laravel"
111171
},
172+
{
173+
"command": "laravel.wrapWithHelper",
174+
"title": "Wrap selection with helper",
175+
"category": "Laravel"
176+
},
177+
{
178+
"command": "laravel.wrapWithHelper.unwrap",
179+
"title": "Unwrap selection",
180+
"category": "Laravel"
181+
},
182+
{
183+
"command": "laravel.wrapWithHelper.dd",
184+
"title": "with dd(...)",
185+
"category": "Laravel"
186+
},
187+
{
188+
"command": "laravel.wrapWithHelper.dump",
189+
"title": "with dump(...)",
190+
"category": "Laravel"
191+
},
192+
{
193+
"command": "laravel.wrapWithHelper.collect",
194+
"title": "with collect(...)",
195+
"category": "Laravel"
196+
},
197+
{
198+
"command": "laravel.wrapWithHelper.str",
199+
"title": "with str(...)",
200+
"category": "Laravel"
201+
},
112202
{
113203
"command": "laravel.refactorSelectedHtmlClassToBladeDirective",
114204
"title": "Refactor selected HTML class attribute to Blade directive",

src/commands/wrapWithHelper.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import * as vscode from "vscode";
2+
3+
type SubCommand = "dd" | "dump" | "collect" | "str" | "unwrap";
4+
5+
export const helpers: SubCommand[] = ["dd", "dump", "collect", "str"];
6+
7+
export const wrapWithHelperCommandName = "laravel.wrapWithHelper";
8+
9+
export const wrapHelperCommandNameSubCommandName = (command: SubCommand) =>
10+
`${wrapWithHelperCommandName}.${command}`;
11+
12+
export const openSubmenuCommand = async () => {
13+
const choice = await vscode.window.showQuickPick(
14+
helpers.map((helper) => ({
15+
label: `${helper}(...)`,
16+
command: wrapHelperCommandNameSubCommandName(helper),
17+
})),
18+
{
19+
placeHolder: "Wrap with...",
20+
},
21+
);
22+
23+
if (choice) {
24+
vscode.commands.executeCommand(choice.command);
25+
}
26+
};
27+
28+
export const wrapSelectionCommand = (wrapper: string) => {
29+
const editor = vscode.window.activeTextEditor;
30+
31+
if (!editor) {
32+
return;
33+
}
34+
35+
const selection = editor.selection;
36+
37+
let selectedText = editor.document.getText(selection);
38+
39+
const lastChar = selectedText.at(-1) ?? "";
40+
const hasSeparator = [";", ","].includes(lastChar);
41+
42+
if (hasSeparator) {
43+
selectedText = selectedText.slice(0, -1);
44+
}
45+
46+
let transformed = `${wrapper}(${selectedText})`;
47+
48+
if (hasSeparator) {
49+
transformed += lastChar;
50+
}
51+
52+
editor.edit((editBuilder) => {
53+
editBuilder.replace(selection, transformed);
54+
});
55+
};
56+
57+
export const unwrapSelectionCommand = () => {
58+
const editor = vscode.window.activeTextEditor;
59+
60+
if (!editor) {
61+
return;
62+
}
63+
64+
const selection = editor.selection;
65+
const selectedText = editor.document.getText(selection);
66+
const match = selectedText.match(/^([a-zA-Z0-9_]+)\(/);
67+
68+
if (!match || !helpers.includes(match[1] as SubCommand)) {
69+
return;
70+
}
71+
72+
const transformed = selectedText.replace(
73+
/[a-zA-Z0-9_]+\(([\s\S]+)\)(?!\))/,
74+
"$1",
75+
);
76+
77+
editor.edit((editBuilder) => {
78+
editBuilder.replace(selection, transformed);
79+
});
80+
};

src/extension.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ import {
1414
runPintOnDirtyFiles,
1515
runPintOnSave,
1616
} from "./commands";
17+
import {
18+
helpers,
19+
openSubmenuCommand,
20+
unwrapSelectionCommand,
21+
wrapHelperCommandNameSubCommandName,
22+
wrapSelectionCommand,
23+
wrapWithHelperCommandName,
24+
} from "./commands/wrapWithHelper";
1725
import {
1826
refactorAllHtmlClassesToBladeDirectives,
1927
refactorSelectedHtmlClassToBladeDirective,
@@ -217,6 +225,20 @@ export async function activate(context: vscode.ExtensionContext) {
217225
providedCodeActionKinds: [vscode.CodeActionKind.QuickFix],
218226
},
219227
),
228+
vscode.commands.registerCommand(
229+
wrapWithHelperCommandName,
230+
openSubmenuCommand,
231+
),
232+
vscode.commands.registerCommand(
233+
wrapHelperCommandNameSubCommandName("unwrap"),
234+
unwrapSelectionCommand,
235+
),
236+
...helpers.map((helper) => {
237+
return vscode.commands.registerCommand(
238+
wrapHelperCommandNameSubCommandName(helper),
239+
() => wrapSelectionCommand(helper),
240+
);
241+
}),
220242
vscode.commands.registerCommand(
221243
"laravel.refactorSelectedHtmlClassToBladeDirective",
222244
refactorSelectedHtmlClassToBladeDirective,

0 commit comments

Comments
 (0)