Skip to content

Commit

Permalink
fix: ensure server commands can be executed
Browse files Browse the repository at this point in the history
This brings back the ability to execute commands provided by the
language server in the `vscode-playground` extension.

Instead of encoding all the commands in the `package.json` file, the
extension inspects the `ServerCapabilities` of the `initialize` response
to determine which commands a server provides. It then presents a quick
pick list of commands the user can choose to run.

This should mean commands (that don't take any arguments at least) now
"just work" with the playground extension
  • Loading branch information
alcarney authored and tombh committed Sep 1, 2023
1 parent d888d7f commit fd6e948
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/servers/json_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class JsonLanguageServer(LanguageServer):
CMD_SHOW_CONFIGURATION_THREAD = "showConfigurationThread"
CMD_UNREGISTER_COMPLETIONS = "unregisterCompletions"

CONFIGURATION_SECTION = "jsonServer"
CONFIGURATION_SECTION = "pygls.jsonServer"

def __init__(self, *args):
super().__init__(*args)
Expand Down
11 changes: 11 additions & 0 deletions examples/vscode-playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@
}
],
"configuration": [
{
"type": "object",
"title": "Json Server Configuration",
"properties": {
"pygls.jsonServer.exampleConfiguration": {
"scope": "resource",
"type": "string",
"default": "You can override this message"
}
}
},
{
"type": "object",
"title": "Server Configuration",
Expand Down
37 changes: 36 additions & 1 deletion examples/vscode-playground/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const MIN_PYTHON = semver.parse("3.7.9")

// Some other nice to haves.
// TODO: Check selected env satisfies pygls' requirements - if not offer to run the select env command.
// TODO: Inspect ServerCapabilities and present a quick pick list of runnable commands.
// TODO: Start a debug session for the currently configured server.
// TODO: TCP Transport
// TODO: WS Transport
Expand Down Expand Up @@ -62,6 +61,13 @@ export async function activate(context: vscode.ExtensionContext) {
})
)

// Execute command... command
context.subscriptions.push(
vscode.commands.registerCommand("pygls.server.executeCommand", async () => {
await executeServerCommand()
})
)

// Restart the language server if the user switches Python envs...
context.subscriptions.push(
python.environments.onDidChangeActiveEnvironmentPath(async () => {
Expand Down Expand Up @@ -213,6 +219,35 @@ function startLangServerTCP(addr: number): LanguageClient {
);
}

/**
* Execute a command provided by the language server.
*/
async function executeServerCommand() {
if (!client || client.state !== State.Running) {
await vscode.window.showErrorMessage("There is no language server running.")
return
}

const knownCommands = client.initializeResult.capabilities.executeCommandProvider?.commands
if (!knownCommands || knownCommands.length === 0) {
const info = client.initializeResult.serverInfo
const name = info?.name || "Server"
const version = info?.version || ""

await vscode.window.showInformationMessage(`${name} ${version} does not implement any commands.`)
return
}

const commandName = await vscode.window.showQuickPick(knownCommands, { canPickMany: false })
if (!commandName) {
return
}
logger.info(`executing command: '${commandName}'`)

const result = await vscode.commands.executeCommand(commandName /* if your command accepts arguments you can pass them here */)
logger.info(`${commandName} result: ${JSON.stringify(result, undefined, 2)}`)
}

/**
* If the user has explicitly provided a src directory use that.
* Otherwise, fallback to the examples/servers directory.
Expand Down

0 comments on commit fd6e948

Please sign in to comment.