diff --git a/package.json b/package.json index 5a57eadd..b51d8016 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,49 @@ } ], "contributes": { + "commands": [ + { + "command": "laravel.refactorSelectedClass", + "title": "Laravel: Refactor selected class attribute" + }, + { + "command": "laravel.refactorAllClasses", + "title": "Laravel: Refactor all class attributes" + } + ], + "keybindings": [ + { + "command": "laravel.refactorSelectedClass", + "key": "shift+alt+c", + "when": "editorHasSelection && resourceLangId == blade" + } + ], + "menus": { + "commandPalette": [ + { + "command": "laravel.refactorSelectedClass", + "when": "editorHasSelection && resourceLangId == blade", + "group": "laravel" + }, + { + "command": "laravel.refactorAllClasses", + "when": "resourceLangId == blade", + "group": "laravel" + } + ], + "editor/context": [ + { + "command": "laravel.refactorSelectedClass", + "when": "editorHasSelection && resourceLangId == blade", + "group": "laravel" + }, + { + "command": "laravel.refactorAllClasses", + "when": "resourceLangId == blade", + "group": "laravel" + } + ] + }, "languages": [ { "id": "blade", diff --git a/src/commands/refactorClass.ts b/src/commands/refactorClass.ts new file mode 100644 index 00000000..0a175728 --- /dev/null +++ b/src/commands/refactorClass.ts @@ -0,0 +1,58 @@ +import * as vscode from "vscode"; + +const transformClass = (classList: string) => { + const classes = classList + .trim() + .split(/\s+/) + .map(cls => `'${cls}'`) + .join(', '); + + return `@class([${classes}])`; +}; + +export const refactorAllClassesCommand = () => { + const editor = vscode.window.activeTextEditor; + + if (!editor) { + return; + } + + const document = editor.document; + const fullText = document.getText(); + + const transformed = fullText.replace(/(? { + return transformClass(classList); + }); + + const entireRange = new vscode.Range( + document.positionAt(0), + document.positionAt(fullText.length) + ); + + editor.edit(editBuilder => { + editBuilder.replace(entireRange, transformed); + }); +}; + +export const refactorSelectedClassCommand = () => { + const editor = vscode.window.activeTextEditor; + + if (!editor) { + return; + } + + const selection = editor.selection; + const selectedText = editor.document.getText(selection); + + const match = selectedText.match(/(? { + editBuilder.replace(selection, transformed); + }); +}; \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index 0cb41b68..96b9c439 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -7,6 +7,7 @@ import { LanguageClient } from "vscode-languageclient/node"; import { bladeSpacer } from "./blade/bladeSpacer"; import { initClient } from "./blade/client"; import { openFileCommand } from "./commands"; +import { refactorAllClassesCommand, refactorSelectedClassCommand } from "./commands/refactorClass"; import { configAffected } from "./support/config"; import { collectDebugInfo } from "./support/debug"; import { @@ -188,6 +189,8 @@ export async function activate(context: vscode.ExtensionContext) { }, ), vscode.commands.registerCommand("laravel.open", openFileCommand), + vscode.commands.registerCommand("laravel.refactorSelectedClass", refactorSelectedClassCommand), + vscode.commands.registerCommand("laravel.refactorAllClasses", refactorAllClassesCommand), ); collectDebugInfo();