Skip to content

Commit

Permalink
Open results in new editor (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
mburleigh authored and chrmarti committed Jun 26, 2019
1 parent 47ba5ab commit 5986bd8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
15 changes: 8 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<CompletionKind, CompletionItemKind> = {
Expand Down Expand Up @@ -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);
Expand All @@ -188,7 +186,8 @@ class RunLineInEditor {
}

private findResultDocument() {
if (this.resultDocument) {
const showResultInNewEditor = workspace.getConfiguration('azureCLI', null).get<boolean>('showResultInNewEditor', false)
if (this.resultDocument && !showResultInNewEditor) {
return Promise.resolve(this.resultDocument);
}
return workspace.openTextDocument({ language: 'json' })
Expand Down Expand Up @@ -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)]);
}

Expand Down

0 comments on commit 5986bd8

Please sign in to comment.