Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure server commands can be executed #368

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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