diff --git a/package.json b/package.json index 13ec194..4a1b606 100644 --- a/package.json +++ b/package.json @@ -89,6 +89,18 @@ "when": "editorTextFocus && editorLangId == 'azcli'" } ], + "configuration": { + "type": "object", + "title": "Azure CLI Tools Configuration", + "properties": { + "azureCLI.showResultInNewEditor": { + "type": "boolean", + "default": false, + "scope": "resource", + "description": "Controls whether showing the result from running an Azure CLI command in an editor should always create a new editor." + } + } + }, "menus": { "editor/context": [ { diff --git a/src/extension.ts b/src/extension.ts index 4e526d7..daef6c6 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -3,8 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import * as jmespath from 'jmespath'; - -import { HoverProvider, Hover, SnippetString, StatusBarAlignment, StatusBarItem, ExtensionContext, TextDocument, TextDocumentChangeEvent, Disposable, TextEditor, Selection, languages, commands, Range, ViewColumn, Position, CancellationToken, ProviderResult, CompletionItem, CompletionList, CompletionItemKind, CompletionItemProvider, window, workspace, env, Uri } from 'vscode'; +import { HoverProvider, Hover, SnippetString, StatusBarAlignment, StatusBarItem, ExtensionContext, TextDocument, TextDocumentChangeEvent, Disposable, TextEditor, Selection, languages, commands, Range, ViewColumn, Position, CancellationToken, ProviderResult, CompletionItem, CompletionList, CompletionItemKind, CompletionItemProvider, window, workspace, env, Uri, WorkspaceEdit } from 'vscode'; import { AzService, CompletionKind, Arguments, Status } from './azService'; import { parse, findNode } from './parser'; @@ -19,7 +18,6 @@ export function activate(context: ExtensionContext) { context.subscriptions.push(new RunLineInTerminal()); context.subscriptions.push(new RunLineInEditor(status)); context.subscriptions.push(commands.registerCommand('ms-azurecli.installAzureCLI', installAzureCLI)); - } const completionKinds: Record = { @@ -173,8 +171,8 @@ class RunLineInEditor { .then(() => exec(line)) .then(({ stdout }) => stdout, ({ stdout, stderr }) => JSON.stringify({ stderr, stdout }, null, ' ')) .then(content => replaceContent(target, content) - .then(() => this.parsedResult = JSON.parse(content)) - .then(undefined, err => {}) + .then(() => this.parsedResult = JSON.parse(content)) + .then(undefined, err => {}) ) ) .then(undefined, console.error); @@ -188,7 +186,8 @@ class RunLineInEditor { } private findResultDocument() { - if (this.resultDocument) { + const showResultInNewEditor = workspace.getConfiguration('azureCLI', null).get('showResultInNewEditor', false) + if (this.resultDocument && !showResultInNewEditor) { return Promise.resolve(this.resultDocument); } return workspace.openTextDocument({ language: 'json' }) @@ -307,7 +306,9 @@ function allMatches(regex: RegExp, string: string, group: number) { function replaceContent(editor: TextEditor, content: string) { const document = editor.document; const all = new Range(new Position(0, 0), document.lineAt(document.lineCount - 1).range.end); - return editor.edit(builder => builder.replace(all, content)) + const edit = new WorkspaceEdit(); + edit.replace(document.uri, all, content); + return workspace.applyEdit(edit) .then(() => editor.selections = [new Selection(0, 0, 0, 0)]); }