Skip to content

Commit b56f580

Browse files
authored
feat: update atom-languageclient + use pylsp (atom-community#334)
2 parents 342af79 + 62049b9 commit b56f580

File tree

5 files changed

+2131
-1988
lines changed

5 files changed

+2131
-1988
lines changed

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ coverage
77
build
88
dist
99
VendorLib
10+
.mypy_cache

lib/main.js

+57-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const cp = require("child_process")
21
const { shell } = require("electron")
2+
const whichSync = require("which").sync
33
const { AutoLanguageClient } = require("atom-languageclient")
44
const { detectVirtualEnv, detectPipEnv, replacePipEnvPathVar, sanitizeConfig } = require("./utils")
55

@@ -10,6 +10,21 @@ import { createDebuggerProvider, activate as debuggerActivate, dispose as debugg
1010
const PYTHON_REGEX = /(([^\d\W]|[\u00A0-\uFFFF])[\w.\u00A0-\uFFFF]*)|\.$/
1111

1212
class PythonLanguageClient extends AutoLanguageClient {
13+
activate() {
14+
super.activate()
15+
if (!atom.packages.isPackageLoaded("atom-ide-base")) {
16+
// install if not installed
17+
// eslint-disable-next-line @typescript-eslint/no-var-requires
18+
require("atom-package-deps")
19+
.install("ide-python", true)
20+
.then(() => {
21+
// enable if disabled
22+
atom.packages.enablePackage("atom-ide-base")
23+
atom.notifications.addSuccess("ide-pyhon: atom-ide-base was installed and enabled...")
24+
})
25+
}
26+
}
27+
1328
getGrammarScopes() {
1429
return ["source.python", "python"]
1530
}
@@ -50,45 +65,52 @@ class PythonLanguageClient extends AutoLanguageClient {
5065
pylsEnvironment["VIRTUAL_ENV"] = venvPath
5166
}
5267
const python = replacePipEnvPathVar(atom.config.get("ide-python.python"), venvPath)
53-
const childProcess = cp.spawn(python, ["-m", "pyls"], {
68+
69+
let pyls = atom.config.get("ide-python.pyls") || "pylsp"
70+
// check if it exists
71+
if (whichSync(pyls, { nothrow: true }) === null) {
72+
pyls = "pyls"
73+
}
74+
const childProcess = super.spawn(python, ["-m", pyls], {
5475
cwd: projectPath,
5576
env: pylsEnvironment,
5677
})
57-
childProcess.on("error", (err) => {
58-
const description =
59-
err.code == "ENOENT"
60-
? `No Python interpreter found at \`${python}\`.`
61-
: `Could not spawn the Python interpreter \`${python}\`.`
62-
atom.notifications.addError("`ide-python` could not launch your Python runtime.", {
63-
dismissable: true,
64-
description: `${description}<p>If you have Python installed please set "Python Executable" setting correctly. If you do not please install Python.</p>`,
65-
})
66-
})
78+
return childProcess
79+
}
6780

68-
childProcess.on("close", (code, signal) => {
69-
if (code !== 0 && signal == null) {
70-
atom.notifications.addError("Unable to start the Python language server.", {
71-
dismissable: true,
72-
buttons: [
73-
{
74-
text: "Install Instructions",
75-
onDidClick: () => atom.workspace.open("atom://config/packages/ide-python"),
76-
},
77-
{
78-
text: "Download Python",
79-
onDidClick: () => shell.openExternal("https://www.python.org/downloads/"),
80-
},
81-
],
82-
description:
83-
"Make sure to install `pyls` 0.19 or newer by running:\n" +
84-
"```\n" +
85-
`${python} -m pip install 'python-language-server[all]'\n` +
86-
`${python} -m pip install git+https://github.com/tomv564/pyls-mypy.git\n` +
87-
"```",
88-
})
89-
}
81+
onSpawnError(err) {
82+
const description =
83+
err.code == "ENOENT"
84+
? `No Python interpreter found at \`${python}\`.`
85+
: `Could not spawn the Python interpreter \`${python}\`.`
86+
atom.notifications.addError("`ide-python` could not launch your Python runtime.", {
87+
dismissable: true,
88+
description: `${description}<p>If you have Python installed please set "Python Executable" setting correctly. If you do not please install Python.</p>`,
9089
})
91-
return childProcess
90+
}
91+
92+
onSpawnClose(code, signal) {
93+
if (code !== 0 && signal == null) {
94+
atom.notifications.addError("Unable to start the Python language server.", {
95+
dismissable: true,
96+
buttons: [
97+
{
98+
text: "Install Instructions",
99+
onDidClick: () => atom.workspace.open("atom://config/packages/ide-python"),
100+
},
101+
{
102+
text: "Download Python",
103+
onDidClick: () => shell.openExternal("https://www.python.org/downloads/"),
104+
},
105+
],
106+
description:
107+
"Make sure to install `pylsp` 0.19 or newer by running:\n" +
108+
"```\n" +
109+
`${python} -m pip install 'python-lsp-server[all]'\n` +
110+
`${python} -m pip install git+https://github.com/tomv564/pyls-mypy.git\n` +
111+
"```",
112+
})
113+
}
92114
}
93115

94116
async getSuggestions(request) {

package.json

+18-6
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,16 @@
4040
"build-commit": "build-commit -o dist",
4141
"prepare": "npm run build"
4242
},
43+
"package-deps": [
44+
"atom-ide-base"
45+
],
4346
"dependencies": {
4447
"@atom-ide-community/nuclide-commons": "^0.8.2",
4548
"@atom-ide-community/nuclide-commons-atom": "^0.8.2",
4649
"@atom-ide-community/nuclide-debugger-common": "^0.8.2",
4750
"arch": "2.1.0",
48-
"atom-languageclient": "^1.2.2",
51+
"atom-languageclient": "^1.10.0",
52+
"atom-package-deps": "^7.2.3",
4953
"dotenv": "5.0.1",
5054
"fs-extra": "4.0.3",
5155
"getos": "3.1.0",
@@ -56,7 +60,7 @@
5660
"log4js": "1.1.1",
5761
"minimatch": "3.0.4",
5862
"nullthrows": "1.0.0",
59-
"react": "16.6.3",
63+
"react": "16.14.0",
6064
"reflect-metadata": "0.1.12",
6165
"rxjs": "5.5.9",
6266
"rxjs-compat": "6.3.3",
@@ -67,18 +71,20 @@
6771
"untildify": "3.0.2",
6872
"vscode-debugadapter": "1.24.0",
6973
"vscode-debugprotocol": "1.24.0",
74+
"which": "^2.0.2",
7075
"xml2js": "0.4.17"
7176
},
7277
"devDependencies": {
73-
"@babel/cli": "7.13.10",
74-
"@babel/core": "7.13.10",
75-
"babel-preset-atomic": "^3.0.3",
78+
"@babel/cli": "7.14.3",
79+
"@babel/core": "7.14.3",
80+
"babel-preset-atomic": "^4.1.0",
7681
"build-commit": "0.1.4",
7782
"cross-env": "^7.0.3",
7883
"mock-spawn": "0.2.6",
79-
"prettier": "^2.2.1",
84+
"prettier-config-atomic": "^2.0.5",
8085
"shx": "^0.3.3"
8186
},
87+
"prettier": "prettier-config-atomic",
8288
"enhancedScopes": [
8389
"source.python"
8490
],
@@ -93,6 +99,12 @@
9399
"default": "python",
94100
"description": "Absolute path of your Python binary. This is used to launch the Python language server. Make sure to install `pyls` for this version of Python. Changes will take effect after a restart of the language server. Use `$PIPENV_PATH/bin/python` if you want to use the pipenv path of your project"
95101
},
102+
"pyls-path": {
103+
"title": "Path to pyls executable",
104+
"order": 2,
105+
"type": "string",
106+
"default": "pylsp"
107+
},
96108
"pylsConfigurationSources": {
97109
"order": 2,
98110
"type": "array",

0 commit comments

Comments
 (0)