vscode-xml/src/extension.ts

102 lines
3.8 KiB
TypeScript
Raw Normal View History

2018-04-29 00:47:12 -04:00
import {
2018-05-03 22:01:29 -04:00
commands, languages, window, workspace, ExtensionContext, Memento,
TextEditor, TextEditorSelectionChangeEvent, TextEditorSelectionChangeKind
} from "vscode";
2018-01-27 20:30:03 -05:00
2018-05-03 22:36:54 -04:00
import { createDocumentSelector, ExtensionState, Configuration } from "./common";
2018-05-03 22:01:29 -04:00
import { XQueryCompletionItemProvider } from "./completion";
import { XmlFormatterFactory, XmlFormattingEditProvider } from "./formatting";
import { formatAsXml, minifyXml } from "./formatting/commands";
import { XQueryLinter } from "./linting";
import { XmlTreeDataProvider } from "./tree-view";
import { evaluateXPath, getCurrentXPath } from "./xpath/commands";
2018-05-03 22:01:29 -04:00
import { executeXQuery } from "./xquery-execution/commands";
2018-01-28 01:19:06 -05:00
import * as constants from "./constants";
2018-01-27 20:30:03 -05:00
export function activate(context: ExtensionContext) {
2018-05-03 22:08:12 -04:00
ExtensionState.configure(context);
2018-04-28 00:10:51 -04:00
const xmlXsdDocSelector = [...createDocumentSelector(constants.languageIds.xml), ...createDocumentSelector(constants.languageIds.xsd)];
const xqueryDocSelector = createDocumentSelector(constants.languageIds.xquery);
2018-03-01 21:50:18 -05:00
/* Completion Features */
context.subscriptions.push(
languages.registerCompletionItemProvider(xqueryDocSelector, new XQueryCompletionItemProvider(), ":", "$")
2018-03-01 21:50:18 -05:00
);
2018-01-28 01:19:06 -05:00
/* Formatting Features */
2018-05-03 22:36:54 -04:00
const xmlFormattingEditProvider = new XmlFormattingEditProvider(XmlFormatterFactory.getXmlFormatter());
2018-01-27 20:30:03 -05:00
2018-01-28 01:19:06 -05:00
context.subscriptions.push(
2018-04-28 20:08:28 -04:00
commands.registerTextEditorCommand(constants.commands.formatAsXml, formatAsXml),
commands.registerTextEditorCommand(constants.commands.minifyXml, minifyXml),
languages.registerDocumentFormattingEditProvider(xmlXsdDocSelector, xmlFormattingEditProvider),
languages.registerDocumentRangeFormattingEditProvider(xmlXsdDocSelector, xmlFormattingEditProvider)
2018-01-28 01:19:06 -05:00
);
2018-02-13 23:25:07 -05:00
/* Linting Features */
context.subscriptions.push(
window.onDidChangeActiveTextEditor(_handleChangeActiveTextEditor),
window.onDidChangeTextEditorSelection(_handleChangeTextEditorSelection)
);
2018-04-27 23:10:55 -04:00
/* Tree View Features */
2018-04-29 00:47:12 -04:00
const treeViewDataProvider = new XmlTreeDataProvider(context);
const treeView = window.createTreeView<Node>(constants.views.xmlTreeView, {
treeDataProvider: treeViewDataProvider
});
2018-05-03 22:36:54 -04:00
if (Configuration.enableXmlTreeViewCursorSync) {
2018-04-29 00:47:12 -04:00
window.onDidChangeTextEditorSelection(x => {
if (x.kind === TextEditorSelectionChangeKind.Mouse && x.selections.length > 0) {
treeView.reveal(treeViewDataProvider.getNodeAtPosition(x.selections[0].start));
}
});
}
2018-04-27 23:10:55 -04:00
context.subscriptions.push(
2018-04-29 00:47:12 -04:00
treeView
2018-04-27 23:10:55 -04:00
);
2018-04-28 00:10:51 -04:00
/* XPath Features */
context.subscriptions.push(
commands.registerTextEditorCommand(constants.commands.evaluateXPath, evaluateXPath),
commands.registerTextEditorCommand(constants.commands.getCurrentXPath, getCurrentXPath)
2018-04-28 00:10:51 -04:00
);
2018-04-28 20:36:58 -04:00
/* XQuery Features */
context.subscriptions.push(
commands.registerTextEditorCommand(constants.commands.executeXQuery, executeXQuery)
);
2018-01-27 20:30:03 -05:00
}
2018-01-28 01:19:06 -05:00
export function deactivate() {
// do nothing
}
2018-02-13 23:25:07 -05:00
function _handleContextChange(editor: TextEditor): void {
const supportedSchemes = [constants.uriSchemes.file, constants.uriSchemes.untitled];
if (!editor || !editor.document || supportedSchemes.indexOf(editor.document.uri.scheme) === -1) {
2018-02-13 23:25:07 -05:00
return;
}
2018-02-13 23:30:01 -05:00
2018-02-13 23:25:07 -05:00
switch (editor.document.languageId) {
2018-04-28 20:08:28 -04:00
case constants.languageIds.xquery:
languages
.createDiagnosticCollection(constants.diagnosticCollections.xquery)
.set(editor.document.uri, new XQueryLinter().lint(editor.document.getText()));
2018-02-13 23:25:07 -05:00
break;
}
}
function _handleChangeActiveTextEditor(editor: TextEditor): void {
_handleContextChange(editor);
}
function _handleChangeTextEditorSelection(e: TextEditorSelectionChangeEvent): void {
_handleContextChange(e.textEditor);
}