Use own context key to enable/disable outline view

This commit is contained in:
Sandeep Somavarapu
2018-01-10 10:44:02 +01:00
parent 9247d4d523
commit 0a9fe4acfe
2 changed files with 24 additions and 7 deletions

View File

@ -13,7 +13,9 @@
"activationEvents": [
"onView:nodeDependencies",
"onView:ftpExplorer",
"onView:jsonOutline"
"onView:jsonOutline",
"onLanguage:json",
"onLanguage:jsonc"
],
"main": "./out/src/extension",
"icon": "media/dep.png",
@ -27,7 +29,7 @@
{
"id": "jsonOutline",
"name": "Json Outline",
"when": "resourceLangId == 'json'"
"when": "jsonOutlineEnabled"
},
{
"id": "ftpExplorer",

View File

@ -14,15 +14,14 @@ export class JsonOutlineProvider implements vscode.TreeDataProvider<string> {
private autoRefresh: boolean = true;
constructor(private context: vscode.ExtensionContext) {
vscode.window.onDidChangeActiveTextEditor(editor => {
this.refresh();
});
vscode.window.onDidChangeActiveTextEditor(() => this.onActiveEditorChanged());
vscode.workspace.onDidChangeTextDocument(e => this.onDocumentChanged(e));
this.parseTree();
this.autoRefresh = vscode.workspace.getConfiguration('jsonOutline').get('autorefresh');
vscode.workspace.onDidChangeConfiguration(() => {
this.autoRefresh = vscode.workspace.getConfiguration('jsonOutline').get('autorefresh');
});
this.onActiveEditorChanged();
}
refresh(offset?: string): void {
@ -46,13 +45,29 @@ export class JsonOutlineProvider implements vscode.TreeDataProvider<string> {
}
const range = new vscode.Range(this.editor.document.positionAt(propertyNode.offset), this.editor.document.positionAt(propertyNode.offset + propertyNode.length));
editBuilder.replace(range, `"${value}"`);
this.parseTree();
this.refresh(offset);
setTimeout(() => {
this.parseTree();
this.refresh(offset);
}, 100)
});
}
});
}
private onActiveEditorChanged(): void {
if (vscode.window.activeTextEditor) {
if (vscode.window.activeTextEditor.document.uri.scheme === 'file') {
const enabled = vscode.window.activeTextEditor.document.languageId === 'json' || vscode.window.activeTextEditor.document.languageId === 'jsonc';
vscode.commands.executeCommand('setContext', 'jsonOutlineEnabled', enabled);
if (enabled) {
this.refresh();
}
}
} else {
vscode.commands.executeCommand('setContext', 'jsonOutlineEnabled', false);
}
}
private onDocumentChanged(changeEvent: vscode.TextDocumentChangeEvent): void {
if (this.autoRefresh && changeEvent.document.uri.toString() === this.editor.document.uri.toString()) {
for (const change of changeEvent.contentChanges) {