vscode-xml/src/Extension.ts

77 lines
3 KiB
TypeScript
Raw Normal View History

'use strict';
import * as vsc from 'vscode';
import { TextEditorCommands } from './Commands';
import { XmlFormattingEditProvider } from './providers/Formatting';
import { XQueryLintingFeatureProvider } from './providers/Linting';
import { XQueryCompletionItemProvider } from './providers/Completion';
2016-02-16 18:42:23 -05:00
import { XmlTreeDocumentContentProvider } from './providers/Content';
export var GlobalState: vsc.Memento;
export var WorkspaceState: vsc.Memento;
const LANG_XML: string = 'xml';
const LANG_XQUERY: string = 'xquery;'
2016-01-07 11:19:29 -05:00
const MEM_QUERY_HISTORY: string = 'xpathQueryHistory';
export function activate(ctx: vsc.ExtensionContext) {
2016-01-18 15:59:58 -05:00
console.log('activate extension');
// expose global and workspace state to the entire extension
GlobalState = ctx.globalState;
WorkspaceState = ctx.workspaceState;
// register palette commands
ctx.subscriptions.push(
vsc.commands.registerTextEditorCommand('xmlTools.minifyXml', TextEditorCommands.minifyXml),
2016-01-08 12:35:50 -05:00
vsc.commands.registerTextEditorCommand('xmlTools.evaluateXPath', TextEditorCommands.evaluateXPath),
2016-02-16 18:42:23 -05:00
vsc.commands.registerTextEditorCommand('xmlTools.executeXQuery', TextEditorCommands.executeXQuery),
2016-02-16 18:42:43 -05:00
vsc.commands.registerTextEditorCommand('xmlTools.viewXmlTree', TextEditorCommands.viewXmlTree)
);
// register language feature providers
ctx.subscriptions.push(
vsc.languages.registerDocumentFormattingEditProvider(LANG_XML, new XmlFormattingEditProvider()),
vsc.languages.registerDocumentRangeFormattingEditProvider(LANG_XML, new XmlFormattingEditProvider()),
vsc.languages.registerCompletionItemProvider(LANG_XQUERY, new XQueryCompletionItemProvider(), ':', '$')
);
2016-02-16 18:42:23 -05:00
// register workspace feature providers
ctx.subscriptions.push(
vsc.workspace.registerTextDocumentContentProvider(XmlTreeDocumentContentProvider.SCHEME, new XmlTreeDocumentContentProvider())
);
// listen to editor events (for linting)
ctx.subscriptions.push(
vsc.window.onDidChangeActiveTextEditor(_handleChangeActiveTextEditor),
vsc.window.onDidChangeTextEditorSelection(_handleChangeTextEditorSelection)
);
2016-01-07 11:19:29 -05:00
}
export function deactivate() {
// clean up xpath history
let memento: vsc.Memento = WorkspaceState || GlobalState;
let history = memento.get<any[]>(MEM_QUERY_HISTORY, []);
history.splice(0);
memento.update(MEM_QUERY_HISTORY, history);
}
function _handleContextChange(editor: vsc.TextEditor): void {
2016-01-08 21:50:30 -05:00
if (!editor || !editor.document) {
return;
}
switch (editor.document.languageId) {
case 'xquery':
XQueryLintingFeatureProvider.provideXQueryDiagnostics(editor);
break;
}
}
function _handleChangeActiveTextEditor(editor: vsc.TextEditor): void {
_handleContextChange(editor);
}
function _handleChangeTextEditorSelection(e: vsc.TextEditorSelectionChangeEvent): void {
_handleContextChange(e.textEditor);
}