From 21139390cd097520c2ab430cd2fb1a85b4dbf7c0 Mon Sep 17 00:00:00 2001 From: Matthieu Sozeau Date: Thu, 24 Dec 2020 17:20:37 +0100 Subject: [PATCH] Use the word under the cursor as default for git grep search --- .vscode/launch.json | 27 ++++++++------------------- .vscode/tasks.json | 10 ++-------- package.json | 8 ++++---- src/extension.ts | 15 ++++++++++++++- 4 files changed, 28 insertions(+), 32 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index cd6b87b..fabf24e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,28 +1,17 @@ // A launch configuration that compiles the extension and then opens it inside a new window { - "version": "0.1.0", "configurations": [ { + "args": [ + "--extensionDevelopmentPath=${workspaceFolder}" + ], "name": "Launch Extension", - "type": "extensionHost", + "outFiles": [ + "${workspaceFolder}/out/**/*.js" + ], + "preLaunchTask": "npm", "request": "launch", - "runtimeExecutable": "${execPath}", - "args": ["--extensionDevelopmentPath=${workspaceRoot}" ], - "stopOnEntry": false, - "sourceMaps": true, - "outFiles": [ "${workspaceRoot}/out/src/**/*.js" ], - "preLaunchTask": "npm" - }, - { - "name": "Launch Tests", - "type": "extensionHost", - "request": "launch", - "runtimeExecutable": "${execPath}", - "args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ], - "stopOnEntry": false, - "sourceMaps": true, - "outFiles": [ "${workspaceRoot}/out/test/**/*.js" ], - "preLaunchTask": "npm" + "type": "pwa-extensionHost" } ] } diff --git a/.vscode/tasks.json b/.vscode/tasks.json index fb7f662..7a2fbb2 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -8,22 +8,16 @@ // A task runner that calls a custom npm script that compiles the extension. { - "version": "0.1.0", + "version": "2.0.0", // we want to run npm "command": "npm", - // the command is a shell script - "isShellCommand": true, - - // show the output window only if unrecognized errors occur. - "showOutput": "silent", - // we run the custom script "compile" as defined in package.json "args": ["run", "compile", "--loglevel", "silent"], // The tsc compiler is started in watching mode - "isWatching": true, + "isBackground": true, // use the standard tsc in watch mode problem matcher to find compile problems in the output. "problemMatcher": "$tsc-watch" diff --git a/package.json b/package.json index c34cb69..c187fe5 100644 --- a/package.json +++ b/package.json @@ -40,11 +40,11 @@ "test": "node ./node_modules/vscode/bin/test" }, "devDependencies": { - "typescript": "^2.3.0", - "vscode": "^1.0.0", - "mocha": "^2.3.3", + "@types/mocha": "^2.2.32", "@types/node": "^6.0.40", - "@types/mocha": "^2.2.32" + "mocha": "^2.3.3", + "typescript": "^2.3.0", + "vscode": "^1.0.0" }, "dependencies": { "shell-escape": "^0.2.0", diff --git a/src/extension.ts b/src/extension.ts index 9cecf52..de1a08c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -13,7 +13,20 @@ const projectRoot = workspace.rootPath ? workspace.rootPath : '.'; export function activate(context: ExtensionContext) { (async () => { const disposable = commands.registerCommand('extension.gitGrep', async () => { - const query = await window.showInputBox({ prompt: 'Please input search word.' }) + // Get the current editor + let wordText = '' + let editor = window.activeTextEditor; + if (editor) { + // Get word under cursor position + let wordRange = editor.document.getWordRangeAtPosition(editor.selection.start); + if (!wordRange) { + wordText = ''; + } + // Get word text + wordText = editor.document.getText(wordRange); + } + + const query = await window.showInputBox({ prompt: 'Please input search word.', value: wordText }) const command = quote(['git', 'grep', '-H', '-n', '-e', query]); const fetchItems = (): Promise => new Promise((resolve, reject) => { exec(command, { cwd: projectRoot, maxBuffer: 2000 * 1024 }, (err, stdout, stderr) => {