diff --git a/package.json b/package.json index f944076..0c2a69c 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,14 @@ "mac": "cmd+shift+j", "when": "editorTextFocus && editorLangId == 'json'" }], + "menus": { + "commandPalette": [ + { + "command": "jmespath.query", + "when": "editorLangId == json" + } + ] + }, "configuration": { "title": "", "properties": { diff --git a/src/command/jmespathQueryCommandHandler.ts b/src/command/jmespathQueryCommandHandler.ts index 56d0640..32016ba 100644 --- a/src/command/jmespathQueryCommandHandler.ts +++ b/src/command/jmespathQueryCommandHandler.ts @@ -12,6 +12,7 @@ export default class JMESPathQueryCommandHandler { private extensionContext: vscode.ExtensionContext; private queryInputUi: QueryInput; + private defaultExpression: string = "*"; private queryService: JMESPathQueryService; private resultViewer: ResultViewer; private timeoutId: number; @@ -39,7 +40,8 @@ export default class JMESPathQueryCommandHandler { } try { - let expression = await this.queryInputUi.presentInputBox(Strings.UI_QUERY_PROMPT, Strings.UI_QUERY_PROMPT_PLACEHOLDER); + let expression = await this.queryInputUi.presentInputBox(Strings.UI_QUERY_PROMPT, Strings.UI_QUERY_PROMPT_PLACEHOLDER, this.defaultExpression); + this.defaultExpression = expression; if (expression === undefined) { return Promise.resolve(); } diff --git a/src/ui/outputChannelResultViewer.ts b/src/ui/outputChannelResultViewer.ts index 1db3d0f..6a6dd52 100644 --- a/src/ui/outputChannelResultViewer.ts +++ b/src/ui/outputChannelResultViewer.ts @@ -1,25 +1,46 @@ "use strict"; import * as vscode from "vscode"; +import { Uri, ViewColumn, Selection, Position } from "vscode"; +import * as fs from "fs"; +import * as os from "os"; +import * as path from "path"; import { ResultViewer } from "./resultViewer"; +import JMESPathQueryCommandHandler from "./../command/jmespathQueryCommandHandler"; export default class OutputChannelResultViewer implements ResultViewer { - private outputChannel: vscode.OutputChannel; private indentString: string; + private resultsFilePath: string; + private resultsDoc: vscode.TextDocument; constructor(channelName: string, indent: string = " ") { - this.outputChannel = vscode.window.createOutputChannel(channelName); this.indentString = indent; - this.outputChannel.show(); + this.resultsFilePath = path.join(os.tmpdir(), "Query Results.json"); } public viewResult(queryResult: any) { - this.outputChannel.clear(); - this.outputChannel.appendLine(JSON.stringify(queryResult, null, this.indentString)); - this.outputChannel.show(); + fs.writeFileSync(this.resultsFilePath, ""); + const resultsFileUri = Uri.file(this.resultsFilePath); + const resultsFileUri2 = Uri.file(path.join(os.tmpdir(), "Query Results2.json")); + if (this.resultsDoc) + this.resultsDoc.save(); + fs.writeFileSync(this.resultsFilePath, JSON.stringify(queryResult, null, this.indentString)); + vscode.workspace.openTextDocument(resultsFileUri).then(doc => { + this.resultsDoc = doc; + vscode.window.showTextDocument(doc, getViewColumn(), true); + } + ); } public dispose() { - this.outputChannel.dispose(); + fs.unlink(this.resultsFilePath); } } + +function getViewColumn(): ViewColumn { + const activeEditor = vscode.window.activeTextEditor; + if (!activeEditor) { + return ViewColumn.One; + } + return activeEditor.viewColumn + 1; +} diff --git a/src/ui/queryInput.ts b/src/ui/queryInput.ts index a1989ba..5068aa1 100644 --- a/src/ui/queryInput.ts +++ b/src/ui/queryInput.ts @@ -29,10 +29,11 @@ export default class QueryInput { * @param placeholder An optional string to show as place holder in the input box to guide the user what to type. * @return A string representing the user-entered expression */ - public async presentInputBox(prompt: string, placeholder?: string): Promise { + public async presentInputBox(prompt: string, placeholder?: string, defaultVal?: string): Promise { return vscode.window.showInputBox({ prompt: prompt, placeHolder: placeholder, + value: defaultVal, validateInput: (expr) => { return this.validateExpression(expr); } }); }