Skip to content

Commit 09981a7

Browse files
feat: add yaml codelens
1 parent 4bb8831 commit 09981a7

File tree

6 files changed

+71
-20
lines changed

6 files changed

+71
-20
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@
100100
{
101101
"command": "ast-grep.searchByCode",
102102
"title": "ast-grep: Search by Code"
103+
},
104+
{
105+
"command": "ast-grep.runRule",
106+
"title": "Run ast-grep Rule"
103107
}
104108
],
105109
"menus": {

src/extension/codelens.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import * as vscode from 'vscode'
2+
import { parentPort } from './common'
3+
4+
export function activateCodeLens(context: vscode.ExtensionContext) {
5+
// Register the CodeLens provider for YAML files
6+
const codelensProvider = new CodelensProvider()
7+
context.subscriptions.push(
8+
vscode.languages.registerCodeLensProvider(
9+
{ language: 'yaml', pattern: '**/*.yml' }, // Only trigger for YAML files
10+
codelensProvider,
11+
),
12+
)
13+
14+
// Register the command that the CodeLens will execute
15+
const runRuleCommand = vscode.commands.registerCommand(
16+
'ast-grep.runRule',
17+
(ruleId: string, text: string) => {
18+
parentPort.postMessage('searchByYAML', {
19+
text,
20+
})
21+
},
22+
)
23+
24+
context.subscriptions.push(runRuleCommand)
25+
}
26+
27+
class CodelensProvider implements vscode.CodeLensProvider {
28+
provideCodeLenses(
29+
document: vscode.TextDocument,
30+
_token: vscode.CancellationToken,
31+
): vscode.CodeLens[] {
32+
const codeLenses: vscode.CodeLens[] = []
33+
const text = document.getText()
34+
35+
// Look for rule definitions in YAML files (sgconfig.yml)
36+
const ruleMatches = text.matchAll(/^(\s*)id:\s*(.+)$/gm)
37+
38+
for (const match of ruleMatches) {
39+
const ruleId = match[2].trim()
40+
const line = document.positionAt(match.index!).line
41+
const range = new vscode.Range(line, 0, line, 0)
42+
43+
const command: vscode.Command = {
44+
title: `Run rule: ${ruleId}`,
45+
command: 'ast-grep.runRule',
46+
arguments: [ruleId, text],
47+
}
48+
49+
codeLenses.push(new vscode.CodeLens(range, command))
50+
}
51+
52+
return codeLenses
53+
}
54+
55+
resolveCodeLens(codeLens: vscode.CodeLens, _token: vscode.CancellationToken): vscode.CodeLens {
56+
return codeLens
57+
}
58+
}

src/extension/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ExtensionContext } from 'vscode'
2+
import { activateCodeLens } from './codelens'
23
import { detectDefaultBinaryAtStart } from './common'
34
import { activateLsp } from './lsp'
45
import { activatePreview } from './preview'
@@ -11,4 +12,5 @@ export async function activate(context: ExtensionContext) {
1112
activateWebview(context)
1213
activateSearch(context)
1314
activatePreview(context)
15+
activateCodeLens(context)
1416
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export interface ParentToChild {
7171
toggleAllSearch: unknown
7272
searchByCode: { text: string }
7373
enableYAML: boolean
74+
searchByYAML: { text: string }
7475
}
7576

7677
export interface Diff {

src/webview/SearchSidebar/SearchWidgetContainer/YamlWidget.tsx

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,6 @@ export default function YamlWidget() {
2222
const [value, setValue] = useState('')
2323
return (
2424
<div {...stylex.props(styles.yaml)}>
25-
<VSCodeTextArea
26-
{...stylex.props(styles.editor)}
27-
value={value}
28-
placeholder="YAML configuration"
29-
resize="vertical"
30-
onInput={(e: any) => {
31-
const newValue = e.target.value || ''
32-
setValue(newValue)
33-
}}
34-
/>
35-
<VSCodeButton
36-
{...stylex.props(styles.searchButton)}
37-
appearance="primary"
38-
onClick={() => {
39-
// Handle save or apply logic here
40-
postYAML({ yaml: value, includeFile: '' })
41-
}}
42-
>
43-
Search
44-
</VSCodeButton>
4525
</div>
4626
)
4727
}

src/webview/hooks/useSearch.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ childPort.onMessage('refreshSearchResult', event => {
125125
grouped.sort(byFilePath)
126126
notify()
127127
})
128+
childPort.onMessage('searchByYAML', event => {
129+
postYAML({
130+
yaml: event.text,
131+
includeFile: '',
132+
})
133+
})
128134

129135
function groupBy(matches: DisplayResult[]) {
130136
const groups = new Map<string, DisplayResult[]>()

0 commit comments

Comments
 (0)