-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PyrightLSDescriptor.kt
68 lines (51 loc) · 2.54 KB
/
PyrightLSDescriptor.kt
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
package com.insyncwithfoo.pyrightls.server
import com.insyncwithfoo.pyrightls.configuration.project.WorkspaceFolders
import com.insyncwithfoo.pyrightls.message
import com.insyncwithfoo.pyrightls.path
import com.insyncwithfoo.pyrightls.pyrightLSConfigurations
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.BaseProjectDirectories.Companion.getBaseDirectories
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.modules
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.platform.lsp.api.LspServerDescriptor
import java.nio.file.Path
private fun Project.getModuleSourceRoots(): Collection<VirtualFile> =
modules.flatMap { module ->
ModuleRootManager.getInstance(module).sourceRoots.asIterable()
}
private fun Project.getWorkspaceFolders(type: WorkspaceFolders): Collection<VirtualFile> {
return when (type) {
WorkspaceFolders.PROJECT_BASE -> this.getBaseDirectories()
WorkspaceFolders.SOURCE_ROOTS -> this.getModuleSourceRoots()
}
}
private fun Project.getWorkspaceFolders(): Collection<VirtualFile> {
val configurations = this.pyrightLSConfigurations
return this.getWorkspaceFolders(configurations.workspaceFolders)
}
@Suppress("UnstableApiUsage")
internal class PyrightLSDescriptor(project: Project, private val executable: Path) :
LspServerDescriptor(project, PRESENTABLE_NAME, *project.getWorkspaceFolders().toTypedArray()) {
private val configurations = project.pyrightLSConfigurations
override val lspServerListener = Listener(project)
override val lspGoToDefinitionSupport = configurations.goToDefinitionSupport
override val lspHoverSupport = configurations.hoverSupport
override val lspCompletionSupport = CompletionSupport().takeIf { configurations.completionSupport }
override val lspDiagnosticsSupport = DiagnosticsSupport(project)
init {
LOGGER.info(configurations.toString())
}
override fun isSupportedFile(file: VirtualFile) = file.extension == "py"
override fun createCommandLine() =
GeneralCommandLine(executable.toString(), "--stdio").apply {
withWorkDirectory(project.path?.toString())
withCharset(Charsets.UTF_8)
}
companion object {
val PRESENTABLE_NAME = message("languageServer.representableName")
private val LOGGER = Logger.getInstance(DiagnosticsSupport::class.java)
}
}