Consolidate Constants

This commit is contained in:
Josh Johnson 2018-04-28 20:08:28 -04:00
parent bce1b3dd87
commit df45b80085
7 changed files with 53 additions and 26 deletions

View File

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

View File

@ -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;
}
}

View File

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

View File

@ -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({

View File

@ -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);

View File

@ -18,9 +18,9 @@ export class XmlFormattingOptionsFactory {
return {
editorOptions: formattingOptions,
newLine: (eol === EndOfLine.CRLF) ? "\r\n" : "\n",
removeCommentsOnMinify: config.get<boolean>("removeCommentsOnMinify"),
splitAttributesOnFormat: config.get<boolean>("splitAttributesOnFormat"),
splitXmlnsOnFormat: config.get<boolean>("splitXmlnsOnFormat")
removeCommentsOnMinify: config.get<boolean>(constants.configKeys.removeCommentsOnMinify),
splitAttributesOnFormat: config.get<boolean>(constants.configKeys.splitAttributesOnFormat),
splitXmlnsOnFormat: config.get<boolean>(constants.configKeys.splitXmlnsOnFormat)
};
}
}

View File

@ -41,7 +41,7 @@ export class XmlTreeDataProvider implements TreeDataProvider<any> {
}
treeItem.command = {
command: "revealLine",
command: constants.nativeCommands.revealLine,
title: "",
arguments: [{
lineNumber: element.lineNumber - 1,
@ -120,8 +120,8 @@ export class XmlTreeDataProvider implements TreeDataProvider<any> {
}
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<any> {
const config = workspace.getConfiguration(constants.extensionPrefix);
const enableTreeView = config.get<boolean>(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");