Skip to content

feat: update atom-languageclient + use pylsp #334

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

Merged
merged 9 commits into from
Jun 9, 2021
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
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ coverage
build
dist
VendorLib
.mypy_cache
92 changes: 57 additions & 35 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const cp = require("child_process")
const { shell } = require("electron")
const whichSync = require("which").sync
const { AutoLanguageClient } = require("atom-languageclient")
const { detectVirtualEnv, detectPipEnv, replacePipEnvPathVar, sanitizeConfig } = require("./utils")

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

class PythonLanguageClient extends AutoLanguageClient {
activate() {
super.activate()
if (!atom.packages.isPackageLoaded("atom-ide-base")) {
// install if not installed
// eslint-disable-next-line @typescript-eslint/no-var-requires
require("atom-package-deps")
.install("ide-python", true)
.then(() => {
// enable if disabled
atom.packages.enablePackage("atom-ide-base")
atom.notifications.addSuccess("ide-pyhon: atom-ide-base was installed and enabled...")
})
}
}

getGrammarScopes() {
return ["source.python", "python"]
}
Expand Down Expand Up @@ -50,45 +65,52 @@ class PythonLanguageClient extends AutoLanguageClient {
pylsEnvironment["VIRTUAL_ENV"] = venvPath
}
const python = replacePipEnvPathVar(atom.config.get("ide-python.python"), venvPath)
const childProcess = cp.spawn(python, ["-m", "pyls"], {

let pyls = atom.config.get("ide-python.pyls") || "pylsp"
// check if it exists
if (whichSync(pyls, { nothrow: true }) === null) {
pyls = "pyls"
}
const childProcess = super.spawn(python, ["-m", pyls], {
cwd: projectPath,
env: pylsEnvironment,
})
childProcess.on("error", (err) => {
const description =
err.code == "ENOENT"
? `No Python interpreter found at \`${python}\`.`
: `Could not spawn the Python interpreter \`${python}\`.`
atom.notifications.addError("`ide-python` could not launch your Python runtime.", {
dismissable: true,
description: `${description}<p>If you have Python installed please set "Python Executable" setting correctly. If you do not please install Python.</p>`,
})
})
return childProcess
}

childProcess.on("close", (code, signal) => {
if (code !== 0 && signal == null) {
atom.notifications.addError("Unable to start the Python language server.", {
dismissable: true,
buttons: [
{
text: "Install Instructions",
onDidClick: () => atom.workspace.open("atom://config/packages/ide-python"),
},
{
text: "Download Python",
onDidClick: () => shell.openExternal("https://www.python.org/downloads/"),
},
],
description:
"Make sure to install `pyls` 0.19 or newer by running:\n" +
"```\n" +
`${python} -m pip install 'python-language-server[all]'\n` +
`${python} -m pip install git+https://github.com/tomv564/pyls-mypy.git\n` +
"```",
})
}
onSpawnError(err) {
const description =
err.code == "ENOENT"
? `No Python interpreter found at \`${python}\`.`
: `Could not spawn the Python interpreter \`${python}\`.`
atom.notifications.addError("`ide-python` could not launch your Python runtime.", {
dismissable: true,
description: `${description}<p>If you have Python installed please set "Python Executable" setting correctly. If you do not please install Python.</p>`,
})
return childProcess
}

onSpawnClose(code, signal) {
if (code !== 0 && signal == null) {
atom.notifications.addError("Unable to start the Python language server.", {
dismissable: true,
buttons: [
{
text: "Install Instructions",
onDidClick: () => atom.workspace.open("atom://config/packages/ide-python"),
},
{
text: "Download Python",
onDidClick: () => shell.openExternal("https://www.python.org/downloads/"),
},
],
description:
"Make sure to install `pylsp` 0.19 or newer by running:\n" +
"```\n" +
`${python} -m pip install 'python-lsp-server[all]'\n` +
`${python} -m pip install git+https://github.com/tomv564/pyls-mypy.git\n` +
"```",
})
}
}

async getSuggestions(request) {
Expand Down
24 changes: 18 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@
"build-commit": "build-commit -o dist",
"prepare": "npm run build"
},
"package-deps": [
"atom-ide-base"
],
"dependencies": {
"@atom-ide-community/nuclide-commons": "^0.8.2",
"@atom-ide-community/nuclide-commons-atom": "^0.8.2",
"@atom-ide-community/nuclide-debugger-common": "^0.8.2",
"arch": "2.1.0",
"atom-languageclient": "^1.2.2",
"atom-languageclient": "^1.10.0",
"atom-package-deps": "^7.2.3",
"dotenv": "5.0.1",
"fs-extra": "4.0.3",
"getos": "3.1.0",
Expand All @@ -56,7 +60,7 @@
"log4js": "1.1.1",
"minimatch": "3.0.4",
"nullthrows": "1.0.0",
"react": "16.6.3",
"react": "16.14.0",
"reflect-metadata": "0.1.12",
"rxjs": "5.5.9",
"rxjs-compat": "6.3.3",
Expand All @@ -67,18 +71,20 @@
"untildify": "3.0.2",
"vscode-debugadapter": "1.24.0",
"vscode-debugprotocol": "1.24.0",
"which": "^2.0.2",
"xml2js": "0.4.17"
},
"devDependencies": {
"@babel/cli": "7.13.10",
"@babel/core": "7.13.10",
"babel-preset-atomic": "^3.0.3",
"@babel/cli": "7.14.3",
"@babel/core": "7.14.3",
"babel-preset-atomic": "^4.1.0",
"build-commit": "0.1.4",
"cross-env": "^7.0.3",
"mock-spawn": "0.2.6",
"prettier": "^2.2.1",
"prettier-config-atomic": "^2.0.5",
"shx": "^0.3.3"
},
"prettier": "prettier-config-atomic",
"enhancedScopes": [
"source.python"
],
Expand All @@ -93,6 +99,12 @@
"default": "python",
"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"
},
"pyls-path": {
"title": "Path to pyls executable",
"order": 2,
"type": "string",
"default": "pylsp"
},
"pylsConfigurationSources": {
"order": 2,
"type": "array",
Expand Down
Loading