mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-04-27 16:55:44 +08:00
Use own context key to enable/disable outline view
This commit is contained in:
@ -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",
|
||||
|
||||
@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user