From 4b33ad5e681bef8f57196991adc9e6f298c2986c Mon Sep 17 00:00:00 2001 From: Sandeep Somavarapu Date: Sun, 3 Dec 2017 19:46:09 +0100 Subject: [PATCH] Rename and refresh commands --- tree-view-sample/package.json | 29 +++++++++++++++++++++- tree-view-sample/src/extension.ts | 2 ++ tree-view-sample/src/jsonOutline.ts | 37 +++++++++++++++++++++++++---- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/tree-view-sample/package.json b/tree-view-sample/package.json index 770d13b5..d616eba6 100644 --- a/tree-view-sample/package.json +++ b/tree-view-sample/package.json @@ -63,6 +63,14 @@ "light": "resources/light/refresh.svg", "dark": "resources/dark/refresh.svg" } + }, + { + "command": "jsonOutline.refreshNode", + "title": "Refresh" + }, + { + "command": "jsonOutline.renameNode", + "title": "Rename" } ], "menus": { @@ -86,9 +94,28 @@ { "command": "nodeDependencies.deleteEntry", "when": "view == nodeDependencies && viewItem == dependency" + }, + { + "command": "jsonOutline.renameNode", + "when": "view == jsonOutline" + }, + { + "command": "jsonOutline.refreshNode", + "when": "view == jsonOutline" } ] - } + }, + "configuration": [ + { + "title": "JSON Outline", + "properties": { + "jsonOutline.autorefresh": { + "type": "boolean", + "default": true + } + } + } + ] }, "scripts": { "vscode:prepublish": "tsc -p ./", diff --git a/tree-view-sample/src/extension.ts b/tree-view-sample/src/extension.ts index 1ffa8768..31e16c8f 100644 --- a/tree-view-sample/src/extension.ts +++ b/tree-view-sample/src/extension.ts @@ -15,6 +15,8 @@ export function activate(context: vscode.ExtensionContext) { vscode.window.registerTreeDataProvider('nodeDependencies', nodeDependenciesProvider); vscode.commands.registerCommand('jsonOutline.refresh', () => jsonOutlineProvider.refresh()); + vscode.commands.registerCommand('jsonOutline.refreshNode', offset => jsonOutlineProvider.refresh(offset)); + vscode.commands.registerCommand('jsonOutline.renameNode', offset => jsonOutlineProvider.rename(offset)); vscode.window.registerTreeDataProvider('jsonOutline', jsonOutlineProvider); vscode.window.registerTreeDataProvider('ftpExplorer', ftpExplorerProvider); diff --git a/tree-view-sample/src/jsonOutline.ts b/tree-view-sample/src/jsonOutline.ts index 2fa6535d..c05ac648 100644 --- a/tree-view-sample/src/jsonOutline.ts +++ b/tree-view-sample/src/jsonOutline.ts @@ -1,6 +1,7 @@ import * as vscode from 'vscode'; import * as json from 'jsonc-parser'; import * as path from 'path'; +import { isNumber } from 'util'; export class JsonOutlineProvider implements vscode.TreeDataProvider { @@ -10,6 +11,7 @@ export class JsonOutlineProvider implements vscode.TreeDataProvider { private tree: json.Node; private text: string; private editor: vscode.TextEditor; + private autoRefresh: boolean = true; constructor(private context: vscode.ExtensionContext) { vscode.window.onDidChangeActiveTextEditor(editor => { @@ -17,15 +19,42 @@ export class JsonOutlineProvider implements vscode.TreeDataProvider { }); 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'); + }); } - refresh(): void { - this.parseTree(); - this._onDidChangeTreeData.fire(); + refresh(offset?: string): void { + if (isNumber(offset)) { + this._onDidChangeTreeData.fire(offset); + } else { + this.parseTree(); + this._onDidChangeTreeData.fire(); + } + } + + rename(offset: string): void { + vscode.window.showInputBox({ placeHolder: 'Enter the new label' }) + .then(value => { + if (value !== null && value !== undefined) { + this.editor.edit(editBuilder => { + const path = json.getLocation(this.text, parseInt(offset)).path + let propertyNode = json.findNodeAtLocation(this.tree, path); + if (propertyNode.parent.type !== 'array') { + propertyNode = propertyNode.parent.children[0]; + } + 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); + }); + } + }); } private onDocumentChanged(changeEvent: vscode.TextDocumentChangeEvent): void { - if (changeEvent.document.uri.toString() === this.editor.document.uri.toString()) { + if (this.autoRefresh && changeEvent.document.uri.toString() === this.editor.document.uri.toString()) { for (const change of changeEvent.contentChanges) { const path = json.getLocation(this.text, this.editor.document.offsetAt(change.range.start)).path; path.pop();