Skip to content

Commit

Permalink
feat: new command Search in Git Changed Files
Browse files Browse the repository at this point in the history
  • Loading branch information
YuTengjing authored and YuTengjing committed Apr 20, 2024
1 parent 9713787 commit 89cd3be
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "power-edit",
"displayName": "Power Edit",
"version": "1.3.0",
"packageManager": "pnpm@9.0.4",
"description": "a vscode extension enhance edit experience",
"publisher": "YuTengjing",
"private": true,
Expand Down Expand Up @@ -58,19 +59,23 @@
"commands": [
{
"command": "power-edit.selectBracketContent",
"title": "Power Edit: Select Bracket Content"
"title": "Select Bracket Content"
},
{
"command": "power-edit.selectBracket",
"title": "Power Edit: Select Bracket"
"title": "Select Bracket"
},
{
"command": "power-edit.selectByIndent",
"title": "Power Edit: Select by Indent"
"title": "Select by Indent"
},
{
"command": "power-edit.goToSymbolInEditor",
"title": "Power Edit: Go to Symbol in Editor"
"title": "Go to Symbol in Editor"
},
{
"command": "power-edit.searchInGitChangedFiles",
"title": "Search in Git Changed Files"
}
]
},
Expand Down
8 changes: 8 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ export function activate(context: ExtensionContext) {
registerTextEditorCommand('goToSymbolInEditor', (editor: TextEditor) =>
import('./features/goToSymbolInEditor').then((mod) => mod.goToSymbolInEditor(editor)),
);

context.subscriptions.push(
commands.registerCommand('power-edit.searchInGitChangedFiles', () =>
import('./features/searchInGitChangedFiles').then((mod) =>
mod.searchInGitChangedFiles(),
),
),
);
}

export function deactivate() {}
38 changes: 38 additions & 0 deletions src/features/searchInGitChangedFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import cp from 'node:child_process';
import util from 'node:util';

import vscode from 'vscode';

const exec = util.promisify(cp.exec);

export async function searchInGitChangedFiles() {
let cwd = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
if (!cwd) {
return vscode.window.showErrorMessage('No workspace is opened');
}

let { stdout: gitRepoDir } = await exec('git rev-parse --show-toplevel', { cwd });
gitRepoDir = gitRepoDir.trim();
if (!gitRepoDir) {
return vscode.window.showErrorMessage('Not a Git repository');
}

cwd = gitRepoDir;
// Step 1: Detect Git changes
const [modifiedFilesOutput, addedFilesOutput] = await Promise.all([
exec('git diff --name-only', { cwd }),
exec('git ls-files --others --exclude-standard', { cwd }),
]);
if (modifiedFilesOutput.stderr || addedFilesOutput.stderr) {
return vscode.window.showErrorMessage('Failed to detect Git changes');
}

const changedFiles = `${modifiedFilesOutput.stdout}\n${addedFilesOutput.stdout}`
.split(/\r?\n/)
.filter(Boolean);

return vscode.commands.executeCommand('workbench.action.findInFiles', {
triggerSearch: true,
filesToInclude: changedFiles.join(', '),
});
}

0 comments on commit 89cd3be

Please sign in to comment.