-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmain.js
139 lines (122 loc) · 4.33 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const { shell } = require("electron")
const whichSync = require("which").sync
const { AutoLanguageClient } = require("atom-languageclient")
const { detectVirtualEnv, detectPipEnv, replacePipEnvPathVar, sanitizeConfig } = require("./utils")
import { createDebuggerProvider, activate as debuggerActivate, dispose as debuggerDispose } from "./debugger/main"
// Ref: https://github.com/nteract/hydrogen/blob/master/lib/autocomplete-provider.js#L33
// adapted from http://stackoverflow.com/q/5474008
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"]
}
getLanguageName() {
return "Python"
}
getServerName() {
return "pyls"
}
getRootConfigurationKey() {
return "ide-python"
}
activate() {
// Remove deprecated option
atom.config.unset("ide-python.pylsPath")
super.activate()
debuggerActivate()
}
mapConfigurationObject(configuration) {
return {
pyls: {
configurationSources: configuration.pylsConfigurationSources,
rope: sanitizeConfig(configuration.rope),
plugins: configuration.pylsPlugins,
},
}
}
async startServerProcess(projectPath) {
const venvPath = (await detectPipEnv(projectPath)) || (await detectVirtualEnv(projectPath))
const pylsEnvironment = Object.assign({}, process.env)
if (venvPath) {
pylsEnvironment["VIRTUAL_ENV"] = venvPath
}
const python = replacePipEnvPathVar(atom.config.get("ide-python.python"), venvPath)
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,
})
return childProcess
}
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>`,
})
}
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) {
if (!PYTHON_REGEX.test(request.prefix)) return null
return super.getSuggestions(request)
}
deactivate() {
debuggerDispose()
return Promise.race([super.deactivate(), this.createTimeoutPromise(2000)])
}
createTimeoutPromise(milliseconds) {
return new Promise((resolve, reject) => {
let timeout = setTimeout(() => {
clearTimeout(timeout)
this.logger.error(`Server failed to shutdown in ${milliseconds}ms, forcing termination`)
resolve()
}, milliseconds)
})
}
}
const pythonClient = new PythonLanguageClient()
pythonClient.createDebuggerProvider = createDebuggerProvider // add the debugger
module.exports = pythonClient