diff --git a/src/common/index.ts b/src/common/index.ts index ead1cdb..3089050 100644 --- a/src/common/index.ts +++ b/src/common/index.ts @@ -1,3 +1,4 @@ export * from "./configuration"; export * from "./create-document-selector"; export * from "./extension-state"; +export * from "./native-commands"; diff --git a/src/common/native-commands.ts b/src/common/native-commands.ts new file mode 100644 index 0000000..7703cbc --- /dev/null +++ b/src/common/native-commands.ts @@ -0,0 +1,18 @@ +import { commands } from "vscode"; + +export class NativeCommands { + static async cursorMove(to: string, by: string): Promise { + await commands.executeCommand("cursorMove", { + to: to, + by: by + }); + } + + static openGlobalSettings(): void { + commands.executeCommand("workbench.action.openGlobalSettings"); + } + + static setContext(key: string, value: any): void { + commands.executeCommand("setContext", key, value); + } +} diff --git a/src/constants.ts b/src/constants.ts index 5732939..28ac902 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,5 +1,3 @@ -export const extensionPrefix = "xmlTools"; - export namespace commands { export const evaluateXPath = "xmlTools.evaluateXPath"; export const executeXQuery = "xmlTools.executeXQuery"; @@ -22,10 +20,7 @@ export namespace languageIds { } export namespace nativeCommands { - export const cursorMove = "cursorMove"; - export const openGlobalSettings = "workbench.action.openGlobalSettings"; export const revealLine = "revealLine"; - export const setContext = "setContext"; } export namespace stateKeys { diff --git a/src/formatting/commands/formatAsXml.ts b/src/formatting/commands/formatAsXml.ts index c0d64a1..643df5c 100644 --- a/src/formatting/commands/formatAsXml.ts +++ b/src/formatting/commands/formatAsXml.ts @@ -1,6 +1,7 @@ -import { commands, workspace } from "vscode"; +import { workspace } from "vscode"; import { ProviderResult, Range, TextEdit, TextEditor, TextEditorEdit } from "vscode"; +import { NativeCommands } from "../../common"; import * as constants from "../../constants"; import { XmlFormatterFactory } from "../xml-formatter"; @@ -38,14 +39,8 @@ export function formatAsXml(editor: TextEditor, edit: TextEditorEdit): void { editBuilder.replace(textEdit.range, textEdit.newText); // wiggle the cursor to deselect the formatted XML (is there a non-hacky way to go about this?) - await commands.executeCommand(constants.nativeCommands.cursorMove, { - to: "left", - by: "character" - }); - await commands.executeCommand(constants.nativeCommands.cursorMove, { - to: "right", - by: "character" - }); + await NativeCommands.cursorMove("left", "character"); + await NativeCommands.cursorMove("right", "character"); }); } } diff --git a/src/formatting/commands/minifyXml.ts b/src/formatting/commands/minifyXml.ts index f75e8f3..f6ee759 100644 --- a/src/formatting/commands/minifyXml.ts +++ b/src/formatting/commands/minifyXml.ts @@ -1,4 +1,4 @@ -import { commands, workspace } from "vscode"; +import { workspace } from "vscode"; import { ProviderResult, Range, TextEdit, TextEditor, TextEditorEdit } from "vscode"; import * as constants from "../../constants"; diff --git a/src/tree-view/xml-tree-data-provider.ts b/src/tree-view/xml-tree-data-provider.ts index 5986d6d..1adf469 100644 --- a/src/tree-view/xml-tree-data-provider.ts +++ b/src/tree-view/xml-tree-data-provider.ts @@ -1,4 +1,4 @@ -import { commands, window, workspace } from "vscode"; +import { window, workspace } from "vscode"; import { Event, EventEmitter, ExtensionContext, Position, TextEditor, TreeDataProvider, TreeItem, TreeItemCollapsibleState @@ -7,7 +7,7 @@ import { import * as path from "path"; import { DOMParser } from "xmldom"; -import { Configuration } from "../common"; +import { Configuration, NativeCommands } from "../common"; import * as constants from "../constants"; export class XmlTreeDataProvider implements TreeDataProvider { @@ -199,7 +199,7 @@ export class XmlTreeDataProvider implements TreeDataProvider { private _refreshTree(): void { if (!this.activeEditor || this.activeEditor.document.languageId !== constants.languageIds.xml) { - commands.executeCommand(constants.nativeCommands.setContext, constants.contextKeys.xmlTreeViewEnabled, false); + NativeCommands.setContext(constants.contextKeys.xmlTreeViewEnabled, false); this._xmlDocument = null; this._onDidChangeTreeData.fire(); @@ -208,7 +208,7 @@ export class XmlTreeDataProvider implements TreeDataProvider { const enableTreeView = Configuration.enableXmlTreeView; - commands.executeCommand(constants.nativeCommands.setContext, constants.contextKeys.xmlTreeViewEnabled, enableTreeView); + NativeCommands.setContext(constants.contextKeys.xmlTreeViewEnabled, enableTreeView); const xml = this.activeEditor.document.getText(); diff --git a/src/xquery-execution/commands/executeXQuery.ts b/src/xquery-execution/commands/executeXQuery.ts index da61812..90c0525 100644 --- a/src/xquery-execution/commands/executeXQuery.ts +++ b/src/xquery-execution/commands/executeXQuery.ts @@ -1,10 +1,10 @@ -import { commands, window, workspace } from "vscode"; +import { window, workspace } from "vscode"; import { Disposable, Range, TextEditor, TextEditorEdit, Uri } from "vscode"; import * as constants from "../../constants"; import { ChildProcess } from "../child-process"; -import { Configuration } from "../../common"; +import { Configuration, NativeCommands } from "../../common"; export async function executeXQuery(editor: TextEditor, edit: TextEditorEdit): Promise { // this disposable will be used for creating status bar messages @@ -22,7 +22,7 @@ export async function executeXQuery(editor: TextEditor, edit: TextEditorEdit): P const action = await window.showWarningMessage("An XQuery execution engine has not been defined.", "Define Now"); if (action === "Define Now") { - commands.executeCommand(constants.nativeCommands.openGlobalSettings); + NativeCommands.openGlobalSettings(); } return;