Skip to content

Commit

Permalink
Wrap selection in XML element
Browse files Browse the repository at this point in the history
Fixes redhat-developer#794

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Dec 1, 2022
1 parent eef51c1 commit 7bea068
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,11 @@
"command": "xml.restart.language.server",
"title": "Restart XML Language Server",
"category": "XML"
},
{
"command": "xml.refactor.wrap.element",
"title": "Wrap element",
"category": "XML"
}
],
"menus": {
Expand Down
7 changes: 6 additions & 1 deletion src/commands/clientCommandConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
export const RESTART_LANGUAGE_SERVER = 'xml.restart.language.server';

/**
* Command to wrap element.
*/
export const REFACTOR_WRAP_ELEMENT = 'xml.refactor.wrap.element';
37 changes: 35 additions & 2 deletions src/commands/registerCommands.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -29,6 +29,7 @@ export async function registerClientServerCommands(context: ExtensionContext, la

registerCodeLensReferencesCommands(context, languageClient);
registerValidationCommands(context);
registerRefactorCommands(context);
registerAssociationCommands(context, languageClient);
registerRestartLanguageServerCommand(context, languageClient);

Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = '<elt>';
const endTag = '</elt>';

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");
}));
}

0 comments on commit 7bea068

Please sign in to comment.