From df45b8008503cc00a625c1b9aa4b42dc34580c69 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Sat, 28 Apr 2018 20:08:28 -0400 Subject: [PATCH] Consolidate Constants --- src/constants.ts | 30 +++++++++++++++++++++++- src/extension.ts | 22 +++++++++-------- src/formatting/commands/formatAsXml.ts | 6 ++--- src/formatting/commands/minifyXml.ts | 2 -- src/formatting/xml-formatter.ts | 5 ++-- src/formatting/xml-formatting-options.ts | 6 ++--- src/tree-view/xml-tree-data-provider.ts | 8 +++---- 7 files changed, 53 insertions(+), 26 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 9616133..a97ec0b 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -2,7 +2,8 @@ export const extensionPrefix = "xmlTools"; export namespace commands { export const evaluateXPath = "xmlTools.evaluateXPath"; - export const setContext = "setContext"; + export const formatAsXml = "xmlTools.formatAsXml"; + export const minifyXml = "xmlTools.minifyXml"; } export namespace contextKeys { @@ -13,9 +14,36 @@ export namespace configKeys { export const enableXmlTreeView = "enableXmlTreeView"; export const ignoreDefaultNamespace = "ignoreDefaultNamespace"; export const persistXPathQuery = "persistXPathQuery"; + export const removeCommentsOnMinify = "removeCommentsOnMinify"; + export const splitAttributesOnFormat = "splitAttributesOnFormat"; + export const splitXmlnsOnFormat = "splitXmlnsOnFormat"; +} + +export namespace diagnosticCollections { + export const xquery = "XQueryDiagnostics"; +} + +export namespace languageIds { + export const xml = "xml"; + export const xquery = "xquery"; +} + +export namespace nativeCommands { + export const cursorMove = "cursorMove"; + export const revealLine = "revealLine"; + export const setContext = "setContext"; } export namespace stateKeys { export const xpathQueryHistory = "xpathQueryHistory"; export const xPathQueryLast = "xPathQueryLast"; } + +export namespace views { + export const xmlTreeView = "xmlTreeView"; +} + +export namespace xmlFormatterImplementations { + export const classic = "classic"; + export const v2 = "v2"; +} diff --git a/src/extension.ts b/src/extension.ts index 4aacf73..54c1dc7 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -2,8 +2,8 @@ import { languages, window, workspace, commands } from "vscode"; import { ExtensionContext, Memento, TextEditor, TextEditorSelectionChangeEvent, WorkspaceConfiguration } from "vscode"; import { XQueryCompletionItemProvider } from "./completion/xquery-completion-item-provider"; -import { FormatAsXmlCommandName, formatAsXml } from "./formatting/commands/formatAsXml"; -import { MinifyXmlCommandName, minifyXml } from "./formatting/commands/minifyXml"; +import { formatAsXml } from "./formatting/commands/formatAsXml"; +import { minifyXml } from "./formatting/commands/minifyXml"; import { XmlFormatterFactory } from "./formatting/xml-formatter"; import { XmlFormattingEditProvider } from "./formatting/xml-formatting-edit-provider"; import { XQueryLinter } from "./linting/xquery-linter"; @@ -22,17 +22,17 @@ export function activate(context: ExtensionContext) { /* Completion Features */ context.subscriptions.push( - languages.registerCompletionItemProvider("xquery", new XQueryCompletionItemProvider(), ":", "$") + languages.registerCompletionItemProvider(constants.languageIds.xquery, new XQueryCompletionItemProvider(), ":", "$") ); /* Formatting Features */ const xmlFormattingEditProvider = new XmlFormattingEditProvider(config, XmlFormatterFactory.getXmlFormatter()); context.subscriptions.push( - commands.registerTextEditorCommand(FormatAsXmlCommandName, formatAsXml), - commands.registerTextEditorCommand(MinifyXmlCommandName, minifyXml), - languages.registerDocumentFormattingEditProvider("xml", xmlFormattingEditProvider), - languages.registerDocumentRangeFormattingEditProvider("xml", xmlFormattingEditProvider) + commands.registerTextEditorCommand(constants.commands.formatAsXml, formatAsXml), + commands.registerTextEditorCommand(constants.commands.minifyXml, minifyXml), + languages.registerDocumentFormattingEditProvider(constants.languageIds.xml, xmlFormattingEditProvider), + languages.registerDocumentRangeFormattingEditProvider(constants.languageIds.xml, xmlFormattingEditProvider) ); /* Linting Features */ @@ -43,7 +43,7 @@ export function activate(context: ExtensionContext) { /* Tree View Features */ context.subscriptions.push( - window.registerTreeDataProvider("xmlTreeView", new XmlTreeDataProvider(context)) + window.registerTreeDataProvider(constants.views.xmlTreeView, new XmlTreeDataProvider(context)) ); /* XPath Features */ @@ -63,8 +63,10 @@ function _handleContextChange(editor: TextEditor): void { } switch (editor.document.languageId) { - case "xquery": - languages.createDiagnosticCollection("XQueryDiagnostics").set(editor.document.uri, new XQueryLinter().lint(editor.document.getText())); + case constants.languageIds.xquery: + languages + .createDiagnosticCollection(constants.diagnosticCollections.xquery) + .set(editor.document.uri, new XQueryLinter().lint(editor.document.getText())); break; } } diff --git a/src/formatting/commands/formatAsXml.ts b/src/formatting/commands/formatAsXml.ts index cfec9ea..a1b589e 100644 --- a/src/formatting/commands/formatAsXml.ts +++ b/src/formatting/commands/formatAsXml.ts @@ -7,8 +7,6 @@ import { XmlFormatterFactory } from "../xml-formatter"; import { XmlFormattingEditProvider } from "../xml-formatting-edit-provider"; import { XmlFormattingOptionsFactory } from "../xml-formatting-options"; -export const FormatAsXmlCommandName = "xmlTools.formatAsXml"; - export function formatAsXml(editor: TextEditor, edit: TextEditorEdit): void { const xmlFormattingEditProvider = new XmlFormattingEditProvider(workspace.getConfiguration(constants.extensionPrefix), XmlFormatterFactory.getXmlFormatter()); const formattingOptions = { @@ -40,11 +38,11 @@ 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("cursorMove", { + await commands.executeCommand(constants.nativeCommands.cursorMove, { to: "left", by: "character" }); - await commands.executeCommand("cursorMove", { + await commands.executeCommand(constants.nativeCommands.cursorMove, { to: "right", by: "character" }); diff --git a/src/formatting/commands/minifyXml.ts b/src/formatting/commands/minifyXml.ts index a73ea87..7be0078 100644 --- a/src/formatting/commands/minifyXml.ts +++ b/src/formatting/commands/minifyXml.ts @@ -7,8 +7,6 @@ import { XmlFormatterFactory } from "../xml-formatter"; import { XmlFormattingEditProvider } from "../xml-formatting-edit-provider"; import { XmlFormattingOptionsFactory } from "../xml-formatting-options"; -export const MinifyXmlCommandName = "xmlTools.minifyXml"; - export function minifyXml(editor: TextEditor, edit: TextEditorEdit): void { const xmlFormatter = XmlFormatterFactory.getXmlFormatter(); const xmlFormattingOptions = XmlFormattingOptionsFactory.getXmlFormattingOptions({ diff --git a/src/formatting/xml-formatter.ts b/src/formatting/xml-formatter.ts index ee16c1d..b87b2b1 100644 --- a/src/formatting/xml-formatter.ts +++ b/src/formatting/xml-formatter.ts @@ -23,8 +23,9 @@ export class XmlFormatterFactory { let xmlFormatterImplementation: XmlFormatter; switch (xmlFormatterImplementationSetting) { - case "classic": xmlFormatterImplementation = new ClassicXmlFormatter(); break; - case "v2": default: xmlFormatterImplementation = new V2XmlFormatter(); break; + case constants.xmlFormatterImplementations.classic: xmlFormatterImplementation = new ClassicXmlFormatter(); break; + case constants.xmlFormatterImplementations.v2: + default: xmlFormatterImplementation = new V2XmlFormatter(); break; } return (XmlFormatterFactory._xmlFormatter = xmlFormatterImplementation); diff --git a/src/formatting/xml-formatting-options.ts b/src/formatting/xml-formatting-options.ts index bcaf173..4e37c24 100644 --- a/src/formatting/xml-formatting-options.ts +++ b/src/formatting/xml-formatting-options.ts @@ -18,9 +18,9 @@ export class XmlFormattingOptionsFactory { return { editorOptions: formattingOptions, newLine: (eol === EndOfLine.CRLF) ? "\r\n" : "\n", - removeCommentsOnMinify: config.get("removeCommentsOnMinify"), - splitAttributesOnFormat: config.get("splitAttributesOnFormat"), - splitXmlnsOnFormat: config.get("splitXmlnsOnFormat") + removeCommentsOnMinify: config.get(constants.configKeys.removeCommentsOnMinify), + splitAttributesOnFormat: config.get(constants.configKeys.splitAttributesOnFormat), + splitXmlnsOnFormat: config.get(constants.configKeys.splitXmlnsOnFormat) }; } } diff --git a/src/tree-view/xml-tree-data-provider.ts b/src/tree-view/xml-tree-data-provider.ts index 347b8f8..c21d3a6 100644 --- a/src/tree-view/xml-tree-data-provider.ts +++ b/src/tree-view/xml-tree-data-provider.ts @@ -41,7 +41,7 @@ export class XmlTreeDataProvider implements TreeDataProvider { } treeItem.command = { - command: "revealLine", + command: constants.nativeCommands.revealLine, title: "", arguments: [{ lineNumber: element.lineNumber - 1, @@ -120,8 +120,8 @@ export class XmlTreeDataProvider implements TreeDataProvider { } private _refreshTree(): void { - if (!this.activeEditor || this.activeEditor.document.languageId !== "xml") { - commands.executeCommand(constants.commands.setContext, constants.contextKeys.xmlTreeViewEnabled, false); + if (!this.activeEditor || this.activeEditor.document.languageId !== constants.languageIds.xml) { + commands.executeCommand(constants.nativeCommands.setContext, constants.contextKeys.xmlTreeViewEnabled, false); this._xmlDocument = null; this._onDidChangeTreeData.fire(); @@ -131,7 +131,7 @@ export class XmlTreeDataProvider implements TreeDataProvider { const config = workspace.getConfiguration(constants.extensionPrefix); const enableTreeView = config.get(constants.configKeys.enableXmlTreeView, true); - commands.executeCommand(constants.commands.setContext, constants.contextKeys.xmlTreeViewEnabled, enableTreeView); + commands.executeCommand(constants.nativeCommands.setContext, constants.contextKeys.xmlTreeViewEnabled, enableTreeView); const xml = this.activeEditor.document.getText(); this._xmlDocument = new DOMParser().parseFromString(xml, "text/xml");