From 0a9fe4acfe364bec6b79ddc50813fa4516273fe3 Mon Sep 17 00:00:00 2001 From: Sandeep Somavarapu Date: Wed, 10 Jan 2018 10:44:02 +0100 Subject: [PATCH] Use own context key to enable/disable outline view --- tree-view-sample/package.json | 6 ++++-- tree-view-sample/src/jsonOutline.ts | 25 ++++++++++++++++++++----- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/tree-view-sample/package.json b/tree-view-sample/package.json index a1b18932..e6e68b66 100644 --- a/tree-view-sample/package.json +++ b/tree-view-sample/package.json @@ -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", diff --git a/tree-view-sample/src/jsonOutline.ts b/tree-view-sample/src/jsonOutline.ts index f36f2d85..f33634a2 100644 --- a/tree-view-sample/src/jsonOutline.ts +++ b/tree-view-sample/src/jsonOutline.ts @@ -14,15 +14,14 @@ export class JsonOutlineProvider implements vscode.TreeDataProvider { 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 { } 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) {