Skip to content

Commit

Permalink
feat: Add setting to configure the language server path (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrayburn-tech authored Oct 23, 2024
1 parent a5bc844 commit c84aa51
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.github.oxc.project.oxcintellijplugin.lsp

import com.github.oxc.project.oxcintellijplugin.settings.OxcSettingsComponent
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.openapi.components.PathMacroManager
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.platform.lsp.api.ProjectWideLspServerDescriptor
import com.intellij.platform.lsp.api.customization.LspDiagnosticsSupport
Expand All @@ -26,9 +30,9 @@ class OxcLspServerDescriptor(project: Project) : ProjectWideLspServerDescriptor(
}

override fun createCommandLine(): GeneralCommandLine {
thisLogger().debug("Start oxc language server")

return GeneralCommandLine(findBinary())
val binary = findBinary()
thisLogger().debug("Creating Oxc command with binary: $binary")
return GeneralCommandLine(binary)
}

override fun createInitializeParams(): InitializeParams {
Expand All @@ -38,6 +42,12 @@ class OxcLspServerDescriptor(project: Project) : ProjectWideLspServerDescriptor(
}

private fun findBinary(): String {
val configuredBinaryPath = project.service<OxcSettingsComponent>().binaryPath
if (!configuredBinaryPath.isNullOrBlank()) {
return FileUtil.toSystemDependentName(
project.service<PathMacroManager>().expandPath(configuredBinaryPath))
}

val binaryName = "oxc_language_server"
val foundBinaries = roots.map {
return@map it.findFileByRelativePath("node_modules/.bin/$binaryName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@ import com.intellij.openapi.project.Project
class OxcSettingsComponent(private val project: Project) :

Check warning on line 13 in src/main/kotlin/com/github/oxc/project/oxcintellijplugin/settings/OxcSettingsComponent.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused symbol

Property "project" is never used
SimplePersistentStateComponent<OxcSettingsState>(OxcSettingsState()) {

var binaryPath
get() = state.binaryPath
set(value) {
if (value.isNullOrBlank()) {
state.binaryPath = null
return
}
state.binaryPath = value
}

var enable
get() = state.enable
set(value) {
state.enable = value
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ package com.github.oxc.project.oxcintellijplugin.settings
import com.github.oxc.project.oxcintellijplugin.MyBundle
import com.github.oxc.project.oxcintellijplugin.lsp.OxcLspServerSupportProvider
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.PathMacroManager
import com.intellij.openapi.components.service
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory
import com.intellij.openapi.options.BoundSearchableConfigurable
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogPanel
import com.intellij.openapi.ui.TextFieldWithBrowseButton
import com.intellij.openapi.util.io.FileUtil
import com.intellij.platform.lsp.api.LspServerManager
import com.intellij.ui.dsl.builder.AlignX
import com.intellij.ui.dsl.builder.bindSelected
import com.intellij.ui.dsl.builder.bindText
import com.intellij.ui.dsl.builder.panel

class OxcSettingsConfigurable(private val project: Project) :
Expand All @@ -20,17 +26,47 @@ class OxcSettingsConfigurable(private val project: Project) :

return panel {
row {
checkBox("Enabled").bindSelected(settings::enable)
checkBox(MyBundle.message("oxc.settings.enabled")).bindSelected(settings::enable)
}
row(MyBundle.message("oxc.settings.languageServerPath")) {
val textField = TextFieldWithBrowseButton()
cell(textField).align(AlignX.FILL).applyToComponent {
val fileChooser = FileChooserDescriptorFactory.createSingleFileNoJarsDescriptor()
addBrowseFolderListener(null,
MyBundle.message("oxc.settings.selectPathToLanguageServer"), null,
fileChooser)
}.bindText({
return@bindText expandToSystemDependentPath(settings.binaryPath)
}, {
settings.binaryPath = collapseToSystemIndependentPath(it)
})
}
}
}

override fun apply() {
super.apply()
@Suppress("UnstableApiUsage")
ApplicationManager.getApplication().invokeLater {
@Suppress("UnstableApiUsage") ApplicationManager.getApplication().invokeLater {
project.service<LspServerManager>()
.stopAndRestartIfNeeded(OxcLspServerSupportProvider::class.java)
}
}

private fun collapseToSystemIndependentPath(path: String?): String {
if (path.isNullOrBlank()) {
return ""
}

val pathMacroManager = project.service<PathMacroManager>()
return FileUtil.toSystemIndependentName(pathMacroManager.collapsePath(path))
}

private fun expandToSystemDependentPath(path: String?): String {
if (path.isNullOrBlank()) {
return ""
}

val pathMacroManager = project.service<PathMacroManager>()
return FileUtil.toSystemDependentName(pathMacroManager.expandPath(path))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import com.intellij.util.xml.Attribute

class OxcSettingsState : BaseState() {

@get:Attribute("binaryPath")
var binaryPath by string()

@get:Attribute("enable")
var enable by property(true)
}
3 changes: 3 additions & 0 deletions src/main/resources/messages/MyBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ action.com.github.oxc.project.oxcintellijplugin.actions.RestartLanguageServer.te

oxc.name=Oxc
oxc.schema.name=Oxc
oxc.settings.enabled=Enabled
oxc.settings.languageServerPath=Oxc Language Server Path
oxc.settings.selectPathToLanguageServer=Select path to Oxc language server

0 comments on commit c84aa51

Please sign in to comment.