|
1 | | -import { promises as fsPromises } from 'node:fs'; |
| 1 | +import { ExtensionContext, StatusBarAlignment, StatusBarItem, ThemeColor, window, workspace } from 'vscode'; |
2 | 2 |
|
3 | 3 | import { |
4 | | - CodeAction, |
5 | | - Command, |
6 | | - commands, |
7 | | - ExtensionContext, |
8 | | - StatusBarAlignment, |
9 | | - StatusBarItem, |
10 | | - ThemeColor, |
11 | | - window, |
12 | | - workspace, |
13 | | -} from 'vscode'; |
14 | | - |
15 | | -import { |
16 | | - CodeActionRequest, |
17 | | - CodeActionTriggerKind, |
| 4 | + Executable, |
| 5 | + LanguageClient, |
| 6 | + LanguageClientOptions, |
18 | 7 | MessageType, |
19 | | - Position, |
20 | | - Range, |
| 8 | + ServerOptions, |
21 | 9 | ShowMessageNotification, |
22 | | -} from 'vscode-languageclient'; |
23 | | - |
24 | | -import { Executable, LanguageClient, LanguageClientOptions, ServerOptions } from 'vscode-languageclient/node'; |
| 10 | +} from 'vscode-languageclient/node'; |
25 | 11 |
|
26 | | -import { join } from 'node:path'; |
| 12 | +import { |
| 13 | + applyAllFixesFileCommand, |
| 14 | + OxcCommands, |
| 15 | + restartServerCommand, |
| 16 | + showOutputChannelCommand, |
| 17 | + toggleEnabledCommand, |
| 18 | +} from './commands'; |
27 | 19 | import { ConfigService } from './ConfigService'; |
| 20 | +import findBinary from './findBinary'; |
28 | 21 |
|
29 | 22 | const languageClientName = 'oxc'; |
30 | 23 | const outputChannelName = 'Oxc'; |
31 | | -const commandPrefix = 'oxc'; |
32 | | - |
33 | | -const enum OxcCommands { |
34 | | - RestartServer = `${commandPrefix}.restartServer`, |
35 | | - ApplyAllFixesFile = `${commandPrefix}.applyAllFixesFile`, |
36 | | - ShowOutputChannel = `${commandPrefix}.showOutputChannel`, |
37 | | - ToggleEnable = `${commandPrefix}.toggleEnable`, |
38 | | -} |
39 | 24 |
|
40 | 25 | let client: LanguageClient; |
41 | 26 |
|
42 | 27 | let myStatusBarItem: StatusBarItem; |
43 | 28 |
|
44 | 29 | export async function activate(context: ExtensionContext) { |
45 | 30 | const configService = new ConfigService(); |
46 | | - const restartCommand = commands.registerCommand( |
47 | | - OxcCommands.RestartServer, |
48 | | - async () => { |
49 | | - if (!client) { |
50 | | - window.showErrorMessage('oxc client not found'); |
51 | | - return; |
52 | | - } |
53 | | - |
54 | | - try { |
55 | | - if (client.isRunning()) { |
56 | | - await client.restart(); |
57 | | - |
58 | | - window.showInformationMessage('oxc server restarted.'); |
59 | | - } else { |
60 | | - await client.start(); |
61 | | - } |
62 | | - } catch (err) { |
63 | | - client.error('Restarting client failed', err, 'force'); |
64 | | - } |
65 | | - }, |
66 | | - ); |
67 | | - |
68 | | - const showOutputCommand = commands.registerCommand( |
69 | | - OxcCommands.ShowOutputChannel, |
70 | | - () => { |
71 | | - client?.outputChannel?.show(); |
72 | | - }, |
73 | | - ); |
74 | | - |
75 | | - const toggleEnable = commands.registerCommand( |
76 | | - OxcCommands.ToggleEnable, |
77 | | - () => { |
78 | | - configService.config.updateEnable(!configService.config.enable); |
79 | | - }, |
80 | | - ); |
81 | | - |
82 | | - const applyAllFixesFile = commands.registerCommand( |
83 | | - OxcCommands.ApplyAllFixesFile, |
84 | | - async () => { |
85 | | - if (!client) { |
86 | | - window.showErrorMessage('oxc client not found'); |
87 | | - return; |
88 | | - } |
89 | | - const textEditor = window.activeTextEditor; |
90 | | - if (!textEditor) { |
91 | | - window.showErrorMessage('active text editor not found'); |
92 | | - return; |
93 | | - } |
94 | | - |
95 | | - const lastLine = textEditor.document.lineAt(textEditor.document.lineCount - 1); |
96 | | - const codeActionResult = await client.sendRequest(CodeActionRequest.type, { |
97 | | - textDocument: { |
98 | | - uri: textEditor.document.uri.toString(), |
99 | | - }, |
100 | | - range: Range.create(Position.create(0, 0), lastLine.range.end), |
101 | | - context: { |
102 | | - diagnostics: [], |
103 | | - only: [], |
104 | | - triggerKind: CodeActionTriggerKind.Invoked, |
105 | | - }, |
106 | | - }); |
107 | | - const commandsOrCodeActions = await client.protocol2CodeConverter.asCodeActionResult(codeActionResult || []); |
108 | | - |
109 | | - await Promise.all( |
110 | | - commandsOrCodeActions |
111 | | - .map(async (codeActionOrCommand) => { |
112 | | - // Commands are always applied. Regardless of whether it's a Command or CodeAction#command. |
113 | | - if (isCommand(codeActionOrCommand)) { |
114 | | - await commands.executeCommand(codeActionOrCommand.command, codeActionOrCommand.arguments); |
115 | | - } else { |
116 | | - // Only preferred edits are applied |
117 | | - // LSP states edits must be run first, then commands |
118 | | - if (codeActionOrCommand.edit && codeActionOrCommand.isPreferred) { |
119 | | - await workspace.applyEdit(codeActionOrCommand.edit); |
120 | | - } |
121 | | - if (codeActionOrCommand.command) { |
122 | | - await commands.executeCommand( |
123 | | - codeActionOrCommand.command.command, |
124 | | - codeActionOrCommand.command.arguments, |
125 | | - ); |
126 | | - } |
127 | | - } |
128 | | - }), |
129 | | - ); |
130 | | - |
131 | | - function isCommand(codeActionOrCommand: CodeAction | Command): codeActionOrCommand is Command { |
132 | | - return typeof codeActionOrCommand.command === 'string'; |
133 | | - } |
134 | | - }, |
135 | | - ); |
136 | 31 |
|
137 | 32 | context.subscriptions.push( |
138 | | - applyAllFixesFile, |
139 | | - restartCommand, |
140 | | - showOutputCommand, |
141 | | - toggleEnable, |
| 33 | + applyAllFixesFileCommand(client), |
| 34 | + restartServerCommand(client), |
| 35 | + showOutputChannelCommand(client), |
| 36 | + toggleEnabledCommand(configService.config), |
142 | 37 | configService, |
143 | 38 | ); |
144 | 39 |
|
145 | 40 | const outputChannel = window.createOutputChannel(outputChannelName, { log: true }); |
146 | 41 |
|
147 | | - async function findBinary(): Promise<string> { |
148 | | - let bin = configService.config.binPath; |
149 | | - if (bin) { |
150 | | - try { |
151 | | - await fsPromises.access(bin); |
152 | | - return bin; |
153 | | - } catch {} |
154 | | - } |
155 | | - |
156 | | - const workspaceFolders = workspace.workspaceFolders; |
157 | | - const isWindows = process.platform === 'win32'; |
158 | | - |
159 | | - if (workspaceFolders?.length && !isWindows) { |
160 | | - try { |
161 | | - return await Promise.any( |
162 | | - workspaceFolders.map(async (folder) => { |
163 | | - const binPath = join( |
164 | | - folder.uri.fsPath, |
165 | | - 'node_modules', |
166 | | - '.bin', |
167 | | - 'oxc_language_server', |
168 | | - ); |
169 | | - |
170 | | - await fsPromises.access(binPath); |
171 | | - return binPath; |
172 | | - }), |
173 | | - ); |
174 | | - } catch {} |
175 | | - } |
176 | | - |
177 | | - const ext = isWindows ? '.exe' : ''; |
178 | | - // NOTE: The `./target/release` path is aligned with the path defined in .github/workflows/release_vscode.yml |
179 | | - return ( |
180 | | - process.env.SERVER_PATH_DEV ?? |
181 | | - join(context.extensionPath, `./target/release/oxc_language_server${ext}`) |
182 | | - ); |
183 | | - } |
184 | | - |
185 | | - const command = await findBinary(); |
| 42 | + const command = await findBinary(context, configService.config); |
186 | 43 | const run: Executable = { |
187 | 44 | command: command!, |
188 | 45 | options: { |
@@ -232,6 +89,7 @@ export async function activate(context: ExtensionContext) { |
232 | 89 | serverOptions, |
233 | 90 | clientOptions, |
234 | 91 | ); |
| 92 | + |
235 | 93 | client.onNotification(ShowMessageNotification.type, (params) => { |
236 | 94 | switch (params.type) { |
237 | 95 | case MessageType.Debug: |
|
0 commit comments