wip: Add Document Context
This commit is contained in:
parent
f2cfe160ba
commit
53e01de1a6
6 changed files with 89 additions and 9 deletions
13
package-lock.json
generated
13
package-lock.json
generated
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "xml",
|
||||
"version": "2.0.0-preview.2",
|
||||
"version": "2.3.1",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
@ -2733,6 +2733,14 @@
|
|||
"glob": "7.1.2"
|
||||
}
|
||||
},
|
||||
"rxjs": {
|
||||
"version": "6.2.1",
|
||||
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.2.1.tgz",
|
||||
"integrity": "sha512-OwMxHxmnmHTUpgO+V7dZChf3Tixf4ih95cmXjzzadULziVl/FKhHScGLj4goEw9weePVOH2Q0+GcCBUhKCZc/g==",
|
||||
"requires": {
|
||||
"tslib": "1.9.0"
|
||||
}
|
||||
},
|
||||
"safe-buffer": {
|
||||
"version": "5.1.1",
|
||||
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.1.tgz",
|
||||
|
@ -3046,8 +3054,7 @@
|
|||
"tslib": {
|
||||
"version": "1.9.0",
|
||||
"resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.0.tgz",
|
||||
"integrity": "sha512-f/qGG2tUkrISBlQZEjEqoZ3B2+npJjIf04H1wuAv9iA8i04Icp+61KRXxFdha22670NJopsZCIjhC3SnjPRKrQ==",
|
||||
"dev": true
|
||||
"integrity": "sha512-f/qGG2tUkrISBlQZEjEqoZ3B2+npJjIf04H1wuAv9iA8i04Icp+61KRXxFdha22670NJopsZCIjhC3SnjPRKrQ=="
|
||||
},
|
||||
"tslint": {
|
||||
"version": "5.9.1",
|
||||
|
|
|
@ -253,6 +253,7 @@
|
|||
"vscode": "^1.1.16"
|
||||
},
|
||||
"dependencies": {
|
||||
"rxjs": "^6.2.1",
|
||||
"xmldom": "^0.1.27",
|
||||
"xpath": "0.0.27",
|
||||
"xqlint": "^0.4.1"
|
||||
|
|
73
src/common/document-context.ts
Normal file
73
src/common/document-context.ts
Normal file
|
@ -0,0 +1,73 @@
|
|||
import { BehaviorSubject, Observable, merge } from "rxjs";
|
||||
import { ExtensionContext, TextDocument, Uri, workspace } from "vscode";
|
||||
|
||||
export class DocumentContext {
|
||||
|
||||
private static _contexts = new Array<DocumentContext>();
|
||||
|
||||
private _data = new Map<string, any>();
|
||||
private _dataChangesSubject = new BehaviorSubject<DocumentContext>(this);
|
||||
private _documentChangesSubject = new BehaviorSubject<DocumentContext>(this);
|
||||
|
||||
constructor(private _document: TextDocument) { }
|
||||
|
||||
static allDocumentChanges(): Observable<DocumentContext> {
|
||||
return merge(...this._contexts.map(x => x.documentChanges));
|
||||
}
|
||||
|
||||
get dataChanges(): Observable<DocumentContext> {
|
||||
return this._dataChangesSubject.asObservable();
|
||||
}
|
||||
|
||||
get documentChanges(): Observable<any> {
|
||||
return this._documentChangesSubject.asObservable();
|
||||
}
|
||||
|
||||
get document(): TextDocument {
|
||||
return this._document;
|
||||
}
|
||||
|
||||
static configure(context: ExtensionContext): void {
|
||||
context.subscriptions.push(
|
||||
workspace.onDidChangeTextDocument(e => {
|
||||
const uri = e.document.uri;
|
||||
let docContext = this.get(uri);
|
||||
|
||||
if (!docContext) {
|
||||
docContext = new DocumentContext(e.document);
|
||||
}
|
||||
|
||||
else {
|
||||
docContext._document = e.document;
|
||||
}
|
||||
|
||||
docContext._documentChangesSubject.next(docContext);
|
||||
}),
|
||||
|
||||
workspace.onDidCloseTextDocument(e => {
|
||||
const docContext = this.get(e.uri);
|
||||
|
||||
if (docContext) {
|
||||
this._contexts.splice(this._contexts.indexOf(docContext), 1);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
static get(uri: Uri): DocumentContext | undefined {
|
||||
return this._contexts.find(x => x.document.uri.toString() === uri.toString());
|
||||
}
|
||||
|
||||
getContextData<T>(key: string): T {
|
||||
return this._data.get(key);
|
||||
}
|
||||
|
||||
setContextData(key: string, value: any, suppressChangeEvent?: boolean): void {
|
||||
this._data.set(key, value);
|
||||
|
||||
if (!suppressChangeEvent) {
|
||||
this._dataChangesSubject.next(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
export * from "./configuration";
|
||||
export * from "./create-document-selector";
|
||||
export * from "./document-context";
|
||||
export * from "./extension-state";
|
||||
export * from "./native-commands";
|
||||
export * from "./xml-traverser";
|
||||
|
|
|
@ -3,7 +3,7 @@ import {
|
|||
TextEditor, TextEditorSelectionChangeEvent, TextEditorSelectionChangeKind
|
||||
} from "vscode";
|
||||
|
||||
import { createDocumentSelector, ExtensionState, Configuration } from "./common";
|
||||
import { createDocumentSelector, DocumentContext, ExtensionState, Configuration } from "./common";
|
||||
import { XQueryCompletionItemProvider } from "./completion";
|
||||
import { XmlFormatterFactory, XmlFormattingEditProvider } from "./formatting";
|
||||
import { formatAsXml, minifyXml } from "./formatting/commands";
|
||||
|
@ -16,6 +16,9 @@ import * as constants from "./constants";
|
|||
|
||||
export function activate(context: ExtensionContext) {
|
||||
ExtensionState.configure(context);
|
||||
DocumentContext.configure(context);
|
||||
|
||||
/* Document Change Handlers */
|
||||
|
||||
const xmlXsdDocSelector = [...createDocumentSelector(constants.languageIds.xml), ...createDocumentSelector(constants.languageIds.xsd)];
|
||||
const xqueryDocSelector = createDocumentSelector(constants.languageIds.xquery);
|
||||
|
|
|
@ -13,11 +13,6 @@
|
|||
},
|
||||
"eofline": true,
|
||||
"forin": true,
|
||||
"import-blacklist": [
|
||||
true,
|
||||
"rxjs",
|
||||
"rxjs/Rx"
|
||||
],
|
||||
"import-spacing": true,
|
||||
"indent": [
|
||||
true,
|
||||
|
|
Loading…
Add table
Reference in a new issue