Skip to content

Commit 7ddc41f

Browse files
feat: add search in folder command
part of #186
1 parent bcbb8b9 commit 7ddc41f

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,21 @@
3232
"command": "ast-grep.restartLanguageServer",
3333
"title": "restart Language Server",
3434
"category": "ast-grep"
35+
},
36+
{
37+
"command": "ast-grep.searchInFolder",
38+
"title": "Find ast-grep Pattern in Folder"
3539
}
3640
],
41+
"menus": {
42+
"explorer/context": [
43+
{
44+
"command": "ast-grep.searchInFolder",
45+
"group": "4_search",
46+
"when": "explorerResourceIsFolder"
47+
}
48+
]
49+
},
3750
"configuration": {
3851
"type": "object",
3952
"title": "ast-grep",

src/extension.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
Executable
88
} from 'vscode-languageclient/node'
99

10-
import { activate as activateWebview } from './view'
10+
import { activate as activateWebview, findInFolder } from './view'
1111

1212
let client: LanguageClient
1313
const diagnosticCollectionName = 'ast-grep-diagnostics'
@@ -35,6 +35,7 @@ function getExecutable(isDebug: boolean): Executable {
3535

3636
function activateLsp(context: ExtensionContext) {
3737
context.subscriptions.push(
38+
commands.registerCommand('ast-grep.searchInFolder', findInFolder),
3839
commands.registerCommand('ast-grep.restartLanguageServer', async () => {
3940
console.log(
4041
'Restart the ast-grep language server by ast-grep.restart command'

src/view.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'path'
12
import type {
23
Definition,
34
ParentPort,
@@ -7,14 +8,14 @@ import type {
78
} from './types'
89
import { Unport, ChannelMessage } from 'unport'
910
import * as vscode from 'vscode'
10-
import { workspace } from 'vscode'
11+
import { workspace, window } from 'vscode'
1112
import { type ChildProcessWithoutNullStreams, spawn } from 'node:child_process'
1213

1314
export function activate(context: vscode.ExtensionContext) {
1415
const provider = new SearchSidebarProvider(context.extensionUri)
1516

1617
context.subscriptions.push(
17-
vscode.window.registerWebviewViewProvider(
18+
window.registerWebviewViewProvider(
1819
SearchSidebarProvider.viewType,
1920
provider,
2021
{ webviewOptions: { retainContextWhenHidden: true } }
@@ -288,3 +289,14 @@ function getNonce() {
288289
}
289290
return text
290291
}
292+
293+
export function findInFolder(data: any) {
294+
const workspacePath = workspace.workspaceFolders?.[0].uri.fsPath
295+
// compute relative path to the workspace folder
296+
const relative = workspacePath && path.relative(workspacePath, data.fsPath)
297+
if (!relative) {
298+
window.showErrorMessage('ast-grep Error: folder is not in the workspace')
299+
return
300+
}
301+
window.showInformationMessage('ast-grep: searching in ' + relative)
302+
}

0 commit comments

Comments
 (0)