Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,44 @@
}
],
"contributes": {
"submenus": [
{
"id": "laravel.wrapWithHelper.submenu",
"label": "Wrap selection with helpers"
}
],
"menus": {
"commandPalette": [
{
"command": "laravel.wrapWithHelper",
"when": "editorHasSelection",
"group": "laravel"
},
{
"command": "laravel.wrapWithHelper.unwrap",
"when": "editorHasSelection",
"group": "laravel"
},
{
"command": "laravel.wrapWithHelper.dd",
"when": "false",
"group": "laravel"
},
{
"command": "laravel.wrapWithHelper.dump",
"when": "false",
"group": "laravel"
},
{
"command": "laravel.wrapWithHelper.collect",
"when": "false",
"group": "laravel"
},
{
"command": "laravel.wrapWithHelper.str",
"when": "false",
"group": "laravel"
},
{
"command": "laravel.refactorSelectedHtmlClassToBladeDirective",
"when": "editorHasSelection && resourceLangId == blade",
Expand All @@ -56,6 +92,30 @@
}
],
"editor/context": [
{
"submenu": "laravel.wrapWithHelper.submenu",
"when": "editorHasSelection",
"group": "laravel"
},
{
"command": "laravel.wrapWithHelper.unwrap",
"when": "editorHasSelection",
"group": "laravel"
}
],
"laravel.wrapWithHelper.submenu": [
{
"command": "laravel.wrapWithHelper.dd"
},
{
"command": "laravel.wrapWithHelper.dump"
},
{
"command": "laravel.wrapWithHelper.collect"
},
{
"command": "laravel.wrapWithHelper.str"
},
{
"command": "laravel.refactorSelectedHtmlClassToBladeDirective",
"when": "editorHasSelection && resourceLangId == blade",
Expand Down Expand Up @@ -109,6 +169,36 @@
"title": "Run Pint on Current File",
"category": "Laravel"
},
{
"command": "laravel.wrapWithHelper",
"title": "Wrap selection with helper",
"category": "Laravel"
},
{
"command": "laravel.wrapWithHelper.unwrap",
"title": "Unwrap selection",
"category": "Laravel"
},
{
"command": "laravel.wrapWithHelper.dd",
"title": "with dd(...)",
"category": "Laravel"
},
{
"command": "laravel.wrapWithHelper.dump",
"title": "with dump(...)",
"category": "Laravel"
},
{
"command": "laravel.wrapWithHelper.collect",
"title": "with collect(...)",
"category": "Laravel"
},
{
"command": "laravel.wrapWithHelper.str",
"title": "with str(...)",
"category": "Laravel"
},
{
"command": "laravel.refactorSelectedHtmlClassToBladeDirective",
"title": "Refactor selected HTML class attribute to Blade directive",
Expand Down
80 changes: 80 additions & 0 deletions src/commands/wrapWithHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import * as vscode from "vscode";

type SubCommand = "dd" | "dump" | "collect" | "str" | "unwrap";

export const helpers: SubCommand[] = ["dd", "dump", "collect", "str"];

export const wrapWithHelperCommandName = "laravel.wrapWithHelper";

export const wrapHelperCommandNameSubCommandName = (command: SubCommand) =>
`${wrapWithHelperCommandName}.${command}`;

export const openSubmenuCommand = async () => {
const choice = await vscode.window.showQuickPick(
helpers.map((helper) => ({
label: `${helper}(...)`,
command: wrapHelperCommandNameSubCommandName(helper),
})),
{
placeHolder: "Wrap with...",
},
);

if (choice) {
vscode.commands.executeCommand(choice.command);
}
};

export const wrapSelectionCommand = (wrapper: string) => {
const editor = vscode.window.activeTextEditor;

if (!editor) {
return;
}

const selection = editor.selection;

let selectedText = editor.document.getText(selection);

const lastChar = selectedText.at(-1) ?? "";
const hasSeparator = [";", ","].includes(lastChar);

if (hasSeparator) {
selectedText = selectedText.slice(0, -1);
}

let transformed = `${wrapper}(${selectedText})`;

if (hasSeparator) {
transformed += lastChar;
}

editor.edit((editBuilder) => {
editBuilder.replace(selection, transformed);
});
};

export const unwrapSelectionCommand = () => {
const editor = vscode.window.activeTextEditor;

if (!editor) {
return;
}

const selection = editor.selection;
const selectedText = editor.document.getText(selection);
const match = selectedText.match(/^([a-zA-Z0-9_]+)\(/);

if (!match || !helpers.includes(match[1] as SubCommand)) {
return;
}

const transformed = selectedText.replace(
/[a-zA-Z0-9_]+\(([\s\S]+)\)(?!\))/,
"$1",
);

editor.edit((editBuilder) => {
editBuilder.replace(selection, transformed);
});
};
22 changes: 22 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ import {
runPintOnDirtyFiles,
runPintOnSave,
} from "./commands";
import {
helpers,
openSubmenuCommand,
unwrapSelectionCommand,
wrapHelperCommandNameSubCommandName,
wrapSelectionCommand,
wrapWithHelperCommandName,
} from "./commands/wrapWithHelper";
import {
refactorAllHtmlClassesToBladeDirectives,
refactorSelectedHtmlClassToBladeDirective,
Expand Down Expand Up @@ -217,6 +225,20 @@ export async function activate(context: vscode.ExtensionContext) {
providedCodeActionKinds: [vscode.CodeActionKind.QuickFix],
},
),
vscode.commands.registerCommand(
wrapWithHelperCommandName,
openSubmenuCommand,
),
vscode.commands.registerCommand(
wrapHelperCommandNameSubCommandName("unwrap"),
unwrapSelectionCommand,
),
...helpers.map((helper) => {
return vscode.commands.registerCommand(
wrapHelperCommandNameSubCommandName(helper),
() => wrapSelectionCommand(helper),
);
}),
vscode.commands.registerCommand(
"laravel.refactorSelectedHtmlClassToBladeDirective",
refactorSelectedHtmlClassToBladeDirective,
Expand Down