Skip to content

Commit a1740a6

Browse files
SoonIterHerringtonDarkholme
authored andcommitted
feat(command): ast-grep.executeAutofix lsp
1 parent 9b9b1f8 commit a1740a6

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
"activationEvents": ["onStartupFinished"],
3636
"contributes": {
3737
"commands": [
38+
{
39+
"command": "ast-grep.executeAutofix",
40+
"title": "executeAutofix",
41+
"category": "ast-grep"
42+
},
3843
{
3944
"command": "ast-grep.restartLanguageServer",
4045
"title": "restart Language Server",

src/extension/lsp.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
type LanguageClientOptions,
55
type ServerOptions,
66
type Executable,
7+
ExecuteCommandRequest,
78
} from 'vscode-languageclient/node'
89
import { resolveBinary, testBinaryExist } from './common'
910

@@ -49,6 +50,27 @@ async function fileExists(pathFromRoot: string): Promise<boolean> {
4950
}
5051
}
5152

53+
async function applyAllFixes() {
54+
const textEditor = window.activeTextEditor
55+
if (!textEditor) {
56+
return
57+
}
58+
const textDocument = {
59+
uri: textEditor.document.uri.toString(),
60+
version: textEditor.document.version,
61+
text: textEditor.document.getText(),
62+
}
63+
const params = {
64+
command: 'ast-grep.applyAllFixes',
65+
arguments: [textDocument],
66+
}
67+
client.sendRequest(ExecuteCommandRequest.type, params).then(undefined, () => {
68+
void window.showErrorMessage(
69+
'Failed to apply Ast-grep fixes to the document. Please consider upgrading ast-grep version or opening an issue with steps to reproduce.',
70+
)
71+
})
72+
}
73+
5274
interface Found {
5375
found: boolean
5476
// empty path means default config file
@@ -76,6 +98,7 @@ async function findConfigFile(): Promise<Found> {
7698
*/
7799
export async function activateLsp(context: ExtensionContext) {
78100
context.subscriptions.push(
101+
commands.registerCommand('ast-grep.executeAutofix', applyAllFixes),
79102
commands.registerCommand('ast-grep.restartLanguageServer', async () => {
80103
console.log(
81104
'Restart the ast-grep language server by ast-grep.restart command',

0 commit comments

Comments
 (0)