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

Simplify vscode-playground setup and fix Python discovery #374

Merged
merged 4 commits into from
Nov 15, 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 .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.vscode/settings.json
.idea
.dir-locals.el

Expand Down Expand Up @@ -117,3 +116,4 @@ venv.bak/

# VS Code settings
.vscode/
!examples/servers/.vscode
18 changes: 18 additions & 0 deletions examples/servers/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
// Uncomment to override Python interpreter used.
// "pygls.server.pythonPath": "/path/to/python",
"pygls.server.launchScript": "json_server.py",
"pygls.trace.server": "off",
"pygls.client.documentSelector": [
{
"scheme": "file",
"language": "json"
}
// Uncomment to use code_actions or inlay_hints servers
// {
// "scheme": "file",
// "language": "plaintext"
// }
],
// "pygls.jsonServer.exampleConfiguration": "some value here",
}
16 changes: 11 additions & 5 deletions examples/servers/json_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,23 @@ def workspace_diagnostic(
params: lsp.WorkspaceDiagnosticParams,
) -> lsp.WorkspaceDiagnosticReport:
"""Returns diagnostic report."""
first = list(json_server.workspace.text_documents.keys())[0]
document = json_server.workspace.get_document(first)
return lsp.WorkspaceDiagnosticReport(
items=[
documents = json_server.workspace.text_documents.keys()

if len(documents) == 0:
items = []
else:
first = list(documents)[0]
document = json_server.workspace.get_document(first)
items = [
lsp.WorkspaceFullDocumentDiagnosticReport(
uri=document.uri,
version=document.version,
items=_validate_json(document.source),
kind=lsp.DocumentDiagnosticReportKind.Full,
)
]
)

return lsp.WorkspaceDiagnosticReport(items=items)


@json_server.feature(
Expand Down
File renamed without changes.
File renamed without changes.
9 changes: 4 additions & 5 deletions examples/vscode-playground/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceRoot}",
"--folder-uri=${workspaceRoot}/../workspace",
"--folder-uri=${workspaceRoot}/../servers",
],
"outFiles": [
"${workspaceRoot}/out/**/*.js"
],
// "preLaunchTask": {
// "type": "npm",
// "script": "compile"
// },
"preLaunchTask": {
"type": "npm",
"script": "watch"
},
},
],
}
9 changes: 3 additions & 6 deletions examples/vscode-playground/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,11 @@ Open terminal in the same directory as this file and execute following commands:

1. Select `Launch Client` and press `F5`, this will open a second VSCode window with the `vscode-playground` extension enabled.

1. You will need to make sure that VSCode is using a virtual environment that contains an installation of `pygls`.
The `Python: Select Interpreter` command can be used to pick the correct one
1. You will need to make sure that VSCode is using a virtual environment that contains an installation of `pygls`.
The `Python: Select Interpreter` command can be used to pick the correct one.

If you see a window like the following
Alternatively, you can set the `pygls.server.pythonPath` option in the `.vscode/settings.json` file

![Screenshot of the VSCode workspace folder selection dialog](https://user-images.githubusercontent.com/2675694/262779751-367c568e-37d7-490a-b83e-910da1596298.png)

be sure to select the one corresponding with the `pygls/examples/workspace` folder.

#### Selecting the document language

Expand Down
15 changes: 11 additions & 4 deletions examples/vscode-playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,26 @@
"title": "Server Configuration",
"properties": {
"pygls.server.cwd": {
"scope": "window",
"scope": "resource",
"type": "string",
"description": "The working directory from which to launch the server.",
"markdownDescription": "The working directory from which to launch the server.\nIf blank, this will default to the `examples/servers` directory."
},
"pygls.server.launchScript": {
"scope": "window",
"scope": "resource",
"type": "string",
"default": "json_server.py",
"description": "The python script to run when launching the server."
"description": "The python script to run when launching the server.",
"markdownDescription": "The python script to run when launching the server.\n Relative to #pygls.server.cwd#"
},
"pygls.server.pythonPath": {
"scope": "resource",
"type": "string",
"default": "",
"description": "The python interpreter to use to run the server.\nBy default, this extension will attempt to use the Python interpreter configured via the Python extension, setting this setting will override this behavior."
},
"pygls.trace.server": {
"scope": "window",
"scope": "resource",
"type": "string",
"default": "off",
"enum": [
Expand Down
30 changes: 21 additions & 9 deletions examples/vscode-playground/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,19 @@
await stopLangServer()
}

const pythonPath = await getPythonPath()
if (!pythonPath) {
clientStarting = false
return
}

const cwd = getCwd()
const serverPath = getServerPath()

logger.info(`cwd: '${cwd}'`)
logger.info(`server: '${serverPath}'`)

const resource = vscode.Uri.joinPath(vscode.Uri.file(cwd), serverPath)
const pythonPath = await getPythonPath(resource)
if (!pythonPath) {
clientStarting = false
return
}

const serverOptions: ServerOptions = {
command: pythonPath,
args: [serverPath],
Expand Down Expand Up @@ -189,7 +190,7 @@
function getClientOptions(): LanguageClientOptions {
const config = vscode.workspace.getConfiguration('pygls.client')
const options = {
documentSelector: config.get<any>('documentSelector'),

Check warning on line 193 in examples/vscode-playground/src/extension.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
outputChannel: logger,
connectionOptions: {
maxRestartCount: 0 // don't restart on server failure.
Expand All @@ -199,7 +200,7 @@
return options
}

function startLangServerTCP(addr: number): LanguageClient {

Check warning on line 203 in examples/vscode-playground/src/extension.ts

View workflow job for this annotation

GitHub Actions / build

'startLangServerTCP' is defined but never used
const serverOptions: ServerOptions = () => {
return new Promise((resolve /*, reject */) => {
const clientSocket = new net.Socket();
Expand Down Expand Up @@ -283,14 +284,25 @@
*
* @returns The python interpreter to use to launch the server
*/
async function getPythonPath(): Promise<string | undefined> {
async function getPythonPath(resource?: vscode.Uri): Promise<string | undefined> {

const config = vscode.workspace.getConfiguration("pygls.server", resource)
const pythonPath = config.get<string>('pythonPath')
if (pythonPath) {
logger.info(`Using user configured python environment: '${pythonPath}'`)
return pythonPath
}

if (!python) {
return
}

if (resource) {
logger.info(`Looking for environment in which to execute: '${resource.toString()}'`)
}
// Use whichever python interpreter the user has configured.
const activeEnvPath = python.environments.getActiveEnvironmentPath()
logger.info(`Using environment: ${activeEnvPath.id}: ${activeEnvPath.path}`)
const activeEnvPath = python.environments.getActiveEnvironmentPath(resource)
logger.info(`Found environment: ${activeEnvPath.id}: ${activeEnvPath.path}`)

const activeEnv = await python.environments.resolveEnvironment(activeEnvPath)
if (!activeEnv) {
Expand Down
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ def client_server(request):
@pytest.fixture(scope="session")
def uri_for():
"""Returns the uri corresponsing to a file in the example workspace."""
base_dir = pathlib.Path(__file__, "..", "..", "examples", "workspace").resolve()
base_dir = pathlib.Path(
__file__, "..", "..", "examples", "servers", "workspace"
).resolve()

def fn(*args):
fpath = pathlib.Path(base_dir, *args)
Expand Down
Loading