From 9c89e7c727b702e0cb82e69de41d63dbf62a640f Mon Sep 17 00:00:00 2001 From: azerr Date: Thu, 17 Nov 2022 18:55:15 +0100 Subject: [PATCH] Wrap selection in XML element Fixes #794 Signed-off-by: azerr --- package.json | 5 ++++ src/commands/clientCommandConstants.ts | 7 ++++- src/commands/registerCommands.ts | 37 ++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index a94a7f64..9663bc7c 100644 --- a/package.json +++ b/package.json @@ -678,6 +678,11 @@ "command": "xml.restart.language.server", "title": "Restart XML Language Server", "category": "XML" + }, + { + "command": "xml.refactor.wrap.element", + "title": "Surround with Tags (Wrap)", + "category": "XML" } ], "menus": { diff --git a/src/commands/clientCommandConstants.ts b/src/commands/clientCommandConstants.ts index fc3b1bc6..8042cde4 100644 --- a/src/commands/clientCommandConstants.ts +++ b/src/commands/clientCommandConstants.ts @@ -65,4 +65,9 @@ export const EXECUTE_WORKSPACE_COMMAND = 'xml.workspace.executeCommand'; /** * Command to restart connection to language server. */ - export const RESTART_LANGUAGE_SERVER = 'xml.restart.language.server'; \ No newline at end of file + export const RESTART_LANGUAGE_SERVER = 'xml.restart.language.server'; + +/** + * Command to wrap element. + */ + export const REFACTOR_WRAP_ELEMENT = 'xml.refactor.wrap.element'; \ No newline at end of file diff --git a/src/commands/registerCommands.ts b/src/commands/registerCommands.ts index be0bf5f4..5442e127 100644 --- a/src/commands/registerCommands.ts +++ b/src/commands/registerCommands.ts @@ -1,5 +1,5 @@ import * as path from 'path'; -import { commands, ConfigurationTarget, env, ExtensionContext, OpenDialogOptions, Position, QuickPickItem, TextDocument, Uri, window, workspace, WorkspaceEdit } from "vscode"; +import { commands, ConfigurationTarget, env, ExtensionContext, OpenDialogOptions, Position, QuickPickItem, SnippetString, TextDocument, Uri, window, workspace, WorkspaceEdit, Selection } from "vscode"; import { CancellationToken, ExecuteCommandParams, ExecuteCommandRequest, ReferencesRequest, TextDocumentEdit, TextDocumentIdentifier } from "vscode-languageclient"; import { LanguageClient } from 'vscode-languageclient/node'; import { markdownPreviewProvider } from "../markdownPreviewProvider"; @@ -29,6 +29,7 @@ export async function registerClientServerCommands(context: ExtensionContext, la registerCodeLensReferencesCommands(context, languageClient); registerValidationCommands(context); + registerRefactorCommands(context); registerAssociationCommands(context, languageClient); registerRestartLanguageServerCommand(context, languageClient); @@ -182,7 +183,7 @@ async function grammarAssociationCommand(documentURI: Uri, languageClient: Langu if (!predefinedUrl || !predefinedUrl.startsWith('http')) { predefinedUrl = ''; } - grammarURI = await window.showInputBox({title:'Fill with schema / grammar URL' , value:predefinedUrl}); + grammarURI = await window.showInputBox({ title: 'Fill with schema / grammar URL', value: predefinedUrl }); } else { // step 2.1: Open a dialog to select the XSD, DTD, RelaxNG file to bind. const options: OpenDialogOptions = { @@ -366,3 +367,35 @@ function registerRestartLanguageServerCommand(context: ExtensionContext, languag })); } + +/** + * Register commands used for refactoring XML files + * + * @param context the extension context + */ +function registerRefactorCommands(context: ExtensionContext) { + // Wrap element + context.subscriptions.push(commands.registerCommand(ClientCommandConstants.REFACTOR_WRAP_ELEMENT, () => { + const editor = window.activeTextEditor; + if (!editor) { + return; + } + const selection = editor.selections[0]; + if (!selection) { + return; + } + const startTag = ''; + const endTag = ''; + + const pos = new Position(selection.start.line, selection.start.character + 1); + editor.edit((selectedText) => { + selectedText.insert(selection.start, startTag); + selectedText.insert(selection.end, endTag); + }) + + const s = new Selection(pos, pos); + editor.selections = [s]; + + commands.executeCommand("editor.action.triggerSuggest"); + })); +}