|
4 | 4 | type LanguageClientOptions, |
5 | 5 | type ServerOptions, |
6 | 6 | type Executable, |
| 7 | + ExecuteCommandRequest, |
7 | 8 | } from 'vscode-languageclient/node' |
8 | 9 | import { resolveBinary, testBinaryExist } from './common' |
9 | 10 |
|
@@ -49,6 +50,27 @@ async function fileExists(pathFromRoot: string): Promise<boolean> { |
49 | 50 | } |
50 | 51 | } |
51 | 52 |
|
| 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 | + |
52 | 74 | interface Found { |
53 | 75 | found: boolean |
54 | 76 | // empty path means default config file |
@@ -76,6 +98,7 @@ async function findConfigFile(): Promise<Found> { |
76 | 98 | */ |
77 | 99 | export async function activateLsp(context: ExtensionContext) { |
78 | 100 | context.subscriptions.push( |
| 101 | + commands.registerCommand('ast-grep.executeAutofix', applyAllFixes), |
79 | 102 | commands.registerCommand('ast-grep.restartLanguageServer', async () => { |
80 | 103 | console.log( |
81 | 104 | 'Restart the ast-grep language server by ast-grep.restart command', |
|
0 commit comments