-
-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(vscode): move commands and
findBinary
to separate files (#…
…8605) pure refactor. wanted to add tests but needs to mocks :/
- Loading branch information
Showing
5 changed files
with
178 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { CodeAction, Command, commands, Disposable, window, workspace } from 'vscode'; | ||
|
||
import { CodeActionRequest, CodeActionTriggerKind, LanguageClient, Position, Range } from 'vscode-languageclient/node'; | ||
import { Config } from './Config'; | ||
|
||
const commandPrefix = 'oxc'; | ||
|
||
export const enum OxcCommands { | ||
RestartServer = `${commandPrefix}.restartServer`, | ||
ApplyAllFixesFile = `${commandPrefix}.applyAllFixesFile`, | ||
ShowOutputChannel = `${commandPrefix}.showOutputChannel`, | ||
ToggleEnable = `${commandPrefix}.toggleEnable`, | ||
} | ||
|
||
export const restartServerCommand = (client: LanguageClient): Disposable => { | ||
return commands.registerCommand( | ||
OxcCommands.RestartServer, | ||
async () => { | ||
if (!client) { | ||
window.showErrorMessage('oxc client not found'); | ||
return; | ||
} | ||
|
||
try { | ||
if (client.isRunning()) { | ||
await client.restart(); | ||
|
||
window.showInformationMessage('oxc server restarted.'); | ||
} else { | ||
await client.start(); | ||
} | ||
} catch (err) { | ||
client.error('Restarting client failed', err, 'force'); | ||
} | ||
}, | ||
); | ||
}; | ||
|
||
export const showOutputChannelCommand = (client: LanguageClient): Disposable => { | ||
return commands.registerCommand( | ||
OxcCommands.ShowOutputChannel, | ||
() => { | ||
client.outputChannel.show(); | ||
}, | ||
); | ||
}; | ||
|
||
export const toggleEnabledCommand = (config: Config): Disposable => { | ||
return commands.registerCommand( | ||
OxcCommands.ToggleEnable, | ||
() => { | ||
config.updateEnable(!config.enable); | ||
}, | ||
); | ||
}; | ||
|
||
export const applyAllFixesFileCommand = (client: LanguageClient): Disposable => { | ||
return commands.registerCommand( | ||
OxcCommands.ApplyAllFixesFile, | ||
async () => { | ||
if (!client) { | ||
window.showErrorMessage('oxc client not found'); | ||
return; | ||
} | ||
const textEditor = window.activeTextEditor; | ||
if (!textEditor) { | ||
window.showErrorMessage('active text editor not found'); | ||
return; | ||
} | ||
|
||
const lastLine = textEditor.document.lineAt(textEditor.document.lineCount - 1); | ||
const codeActionResult = await client.sendRequest(CodeActionRequest.type, { | ||
textDocument: { | ||
uri: textEditor.document.uri.toString(), | ||
}, | ||
range: Range.create(Position.create(0, 0), lastLine.range.end), | ||
context: { | ||
diagnostics: [], | ||
only: [], | ||
triggerKind: CodeActionTriggerKind.Invoked, | ||
}, | ||
}); | ||
const commandsOrCodeActions = await client.protocol2CodeConverter.asCodeActionResult(codeActionResult || []); | ||
|
||
await Promise.all( | ||
commandsOrCodeActions | ||
.map(async (codeActionOrCommand) => { | ||
// Commands are always applied. Regardless of whether it's a Command or CodeAction#command. | ||
if (isCommand(codeActionOrCommand)) { | ||
await commands.executeCommand(codeActionOrCommand.command, codeActionOrCommand.arguments); | ||
} else { | ||
// Only preferred edits are applied | ||
// LSP states edits must be run first, then commands | ||
if (codeActionOrCommand.edit && codeActionOrCommand.isPreferred) { | ||
await workspace.applyEdit(codeActionOrCommand.edit); | ||
} | ||
if (codeActionOrCommand.command) { | ||
await commands.executeCommand( | ||
codeActionOrCommand.command.command, | ||
codeActionOrCommand.command.arguments, | ||
); | ||
} | ||
} | ||
}), | ||
); | ||
|
||
function isCommand(codeActionOrCommand: CodeAction | Command): codeActionOrCommand is Command { | ||
return typeof codeActionOrCommand.command === 'string'; | ||
} | ||
}, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { promises as fsPromises } from 'node:fs'; | ||
import { join } from 'node:path'; | ||
|
||
import { ExtensionContext, workspace } from 'vscode'; | ||
|
||
import { Config } from './Config'; | ||
|
||
export default async function findBinary(context: ExtensionContext, config: Config): Promise<string> { | ||
let bin = config.binPath; | ||
if (bin) { | ||
try { | ||
await fsPromises.access(bin); | ||
return bin; | ||
} catch {} | ||
} | ||
|
||
const workspaceFolders = workspace.workspaceFolders; | ||
const isWindows = process.platform === 'win32'; | ||
|
||
if (workspaceFolders?.length && !isWindows) { | ||
try { | ||
return await Promise.any( | ||
workspaceFolders.map(async (folder) => { | ||
const binPath = join( | ||
folder.uri.fsPath, | ||
'node_modules', | ||
'.bin', | ||
'oxc_language_server', | ||
); | ||
|
||
await fsPromises.access(binPath); | ||
return binPath; | ||
}), | ||
); | ||
} catch {} | ||
} | ||
|
||
const ext = isWindows ? '.exe' : ''; | ||
// NOTE: The `./target/release` path is aligned with the path defined in .github/workflows/release_vscode.yml | ||
return ( | ||
process.env.SERVER_PATH_DEV ?? | ||
join(context.extensionPath, `./target/release/oxc_language_server${ext}`) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters