Skip to content

Commit dc9d3ef

Browse files
committed
fix: infinite indexing / intellisense blocked
1 parent 896a96f commit dc9d3ef

File tree

2 files changed

+57
-25
lines changed

2 files changed

+57
-25
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pluginUntilBuild = 243.*
1111

1212
# IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension
1313
platformType = WS
14-
platformVersion = 2024.1.1
14+
platformVersion = 2024.3
1515

1616
# Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
1717
# Example: platformPlugins = com.jetbrains.php:203.4449.22, org.intellij.scala:2023.3.27@EAP

src/main/kotlin/com/github/biomejs/intellijbiome/lsp/BiomeLspServerSupportProvider.kt

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,30 @@ package com.github.biomejs.intellijbiome.lsp
33
import com.github.biomejs.intellijbiome.BiomeBundle
44
import com.github.biomejs.intellijbiome.BiomeIcons
55
import com.github.biomejs.intellijbiome.BiomePackage
6-
import com.github.biomejs.intellijbiome.extensions.runBiomeCLI
6+
import com.github.biomejs.intellijbiome.extensions.isNodeScript
77
import com.github.biomejs.intellijbiome.listeners.BIOME_CONFIG_RESOLVED_TOPIC
88
import com.github.biomejs.intellijbiome.services.BiomeServerService
99
import com.github.biomejs.intellijbiome.settings.BiomeConfigurable
1010
import com.github.biomejs.intellijbiome.settings.BiomeSettings
1111
import com.intellij.execution.ExecutionException
1212
import com.intellij.execution.configurations.GeneralCommandLine
13+
import com.intellij.execution.process.OSProcessHandler
14+
import com.intellij.javascript.nodejs.execution.NodeTargetRun
15+
import com.intellij.javascript.nodejs.execution.NodeTargetRunOptions.Companion.of
16+
import com.intellij.javascript.nodejs.execution.withInvisibleProgress
17+
import com.intellij.javascript.nodejs.interpreter.NodeJsInterpreterManager
18+
import com.intellij.javascript.nodejs.interpreter.local.NodeJsLocalInterpreter
19+
import com.intellij.javascript.nodejs.interpreter.wsl.WslNodeInterpreter
20+
import com.intellij.lang.javascript.JavaScriptBundle
1321
import com.intellij.openapi.components.service
1422
import com.intellij.openapi.project.Project
1523
import com.intellij.openapi.vfs.VirtualFile
1624
import com.intellij.platform.lsp.api.*
1725
import com.intellij.platform.lsp.api.customization.LspFormattingSupport
1826
import com.intellij.platform.lsp.api.lsWidget.LspServerWidgetItem
1927
import com.intellij.util.SmartList
28+
import java.io.File
29+
import kotlin.io.path.Path
2030

2131

2232
@Suppress("UnstableApiUsage")
@@ -38,29 +48,32 @@ class BiomeLspServerSupportProvider : LspServerSupportProvider {
3848
serverStarter.ensureServerStarted(BiomeLspServerDescriptor(project, executable, configPath))
3949
}
4050

41-
override fun createLspServerWidgetItem(lspServer: LspServer, currentFile: VirtualFile?) = LspServerWidgetItem(
51+
override fun createLspServerWidgetItem(lspServer: LspServer,
52+
currentFile: VirtualFile?) = LspServerWidgetItem(
4253
lspServer, currentFile,
4354
BiomeIcons.BiomeIcon, BiomeConfigurable::class.java
4455
)
4556
}
4657

4758
@Suppress("UnstableApiUsage")
48-
class BiomeLspServerManagerListener(val project: Project) : LspServerManagerListener {
49-
override fun serverStateChanged(lspServer: LspServer) {
50-
if (lspServer.descriptor is BiomeLspServerDescriptor && lspServer.state == LspServerState.ShutdownUnexpectedly) {
51-
// restart again if the server was shutdown unexpectedly.
52-
// This can be caused by race condition, when we restart LSP server because of config change,
53-
// but Intellij also tried to send a request to it at the same time.
54-
// Unfortunate There is no way prevent IDEA send requests after LSP started.
55-
project.service<BiomeServerService>().restartBiomeServer()
59+
private class BiomeLspServerDescriptor(project: Project,
60+
val executable: String,
61+
val configPath: String?) :
62+
ProjectWideLspServerDescriptor(project, "Biome") {
63+
private val biomePackage = BiomePackage(project)
64+
private val executableFile = File(executable)
65+
private val params: SmartList<String> by lazy {
66+
val params = SmartList("lsp-proxy")
67+
if (!configPath.isNullOrEmpty()) {
68+
params.add("--config-path")
69+
params.add(configPath)
5670
}
71+
return@lazy params
5772
}
58-
}
5973

60-
@Suppress("UnstableApiUsage")
61-
private class BiomeLspServerDescriptor(project: Project, val executable: String, val configPath: String?) :
62-
ProjectWideLspServerDescriptor(project, "Biome") {
63-
private val biomePackage = BiomePackage(project)
74+
init {
75+
biomePackage.versionNumber()?.let { project.messageBus.syncPublisher(BIOME_CONFIG_RESOLVED_TOPIC).resolved(it) }
76+
}
6477

6578
override fun isSupportedFile(file: VirtualFile): Boolean {
6679
val settings = BiomeSettings.getInstance(project)
@@ -72,25 +85,44 @@ private class BiomeLspServerDescriptor(project: Project, val executable: String,
7285
}
7386

7487
override fun createCommandLine(): GeneralCommandLine {
75-
val params = SmartList("lsp-proxy")
76-
77-
if (!configPath.isNullOrEmpty()) {
78-
params.add("--config-path")
79-
params.add(configPath)
88+
// we're here only if we specify an executable file
89+
return GeneralCommandLine().apply {
90+
withExePath(executable)
91+
withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE)
92+
project.basePath?.let {
93+
withWorkingDirectory(Path(it))
94+
}
95+
addParameters(params)
8096
}
8197

98+
}
99+
100+
override fun startServerProcess(): OSProcessHandler {
82101
if (executable.isEmpty()) {
83102
throw ExecutionException(BiomeBundle.message("biome.language.server.not.found"))
84103
}
85104

86-
val version = biomePackage.versionNumber()
105+
if (!(executableFile.isFile && executableFile.isNodeScript())) {
106+
return super.startServerProcess()
107+
}
87108

88-
version?.let { project.messageBus.syncPublisher(BIOME_CONFIG_RESOLVED_TOPIC).resolved(it) }
109+
val interpreter = NodeJsInterpreterManager.getInstance(project).interpreter
110+
if (interpreter !is NodeJsLocalInterpreter && interpreter !is WslNodeInterpreter) {
111+
throw ExecutionException(JavaScriptBundle.message("lsp.interpreter.error"))
112+
}
89113

90-
return GeneralCommandLine().runBiomeCLI(project, executable).apply {
114+
val target = NodeTargetRun(interpreter, project, null, of(false))
115+
target.commandLineBuilder.apply {
116+
project.basePath?.let { this.setWorkingDirectory(target.path(it)) }
117+
addParameter(target.path(executable))
91118
addParameters(params)
92-
withWorkDirectory(configPath)
119+
addParameter("--stdio")
120+
charset = Charsets.UTF_8
93121
}
122+
123+
val process = withInvisibleProgress { target.startProcessEx() }
124+
125+
return process.processHandler
94126
}
95127

96128
override val lspGoToDefinitionSupport = false

0 commit comments

Comments
 (0)