Skip to content

Commit

Permalink
Migrate setting classes to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
JojOatXGME committed Feb 3, 2025
1 parent 095f5e9 commit 79a43ab
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 367 deletions.
77 changes: 0 additions & 77 deletions src/main/java/org/nixos/idea/lsp/NixLspSettings.java

This file was deleted.

52 changes: 52 additions & 0 deletions src/main/java/org/nixos/idea/lsp/NixLspSettings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.nixos.idea.lsp

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.BaseState
import com.intellij.openapi.components.RoamingType
import com.intellij.openapi.components.SimplePersistentStateComponent
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.util.text.Strings
import org.nixos.idea.settings.NixStoragePaths
import org.nixos.idea.settings.SimplePersistentStateComponentHelper.delegate
import java.util.ArrayDeque
import java.util.Collections
import java.util.Deque

@State(name = "NixLspSettings", storages = [Storage(value = NixStoragePaths.TOOLS, roamingType = RoamingType.LOCAL)])
class NixLspSettings : SimplePersistentStateComponent<NixLspSettings.State>(State()) {

class State : BaseState() {
var enabled by property(false)
var command by string()
var history: Deque<String> by property(ArrayDeque(), { it.isEmpty() })
}

var isEnabled: Boolean by delegate(State::enabled)
var command: String
get() = Strings.notNullize(state.command)
set(command) {
state.command = command
addToHistory(command)
}
val commandHistory: Collection<String>
get() = Collections.unmodifiableCollection(state.history)

private fun addToHistory(command: String) {
val history = state.history
history.remove(command)
history.addFirst(command)
while (history.size > MAX_HISTORY_SIZE) {
history.removeLast()
}
}

companion object {
private const val MAX_HISTORY_SIZE = 5

@JvmStatic
fun getInstance(): NixLspSettings {
return ApplicationManager.getApplication().getService(NixLspSettings::class.java)
}
}
}
106 changes: 0 additions & 106 deletions src/main/java/org/nixos/idea/lsp/NixLspSettingsConfigurable.java

This file was deleted.

70 changes: 70 additions & 0 deletions src/main/java/org/nixos/idea/lsp/NixLspSettingsConfigurable.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.nixos.idea.lsp

import com.intellij.openapi.options.BoundSearchableConfigurable
import com.intellij.openapi.options.Configurable
import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.ui.DialogPanel
import com.intellij.platform.lsp.api.LspServerManager
import com.intellij.ui.RawCommandLineEditor
import com.intellij.ui.components.JBCheckBox
import com.intellij.ui.dsl.builder.AlignX
import com.intellij.ui.dsl.builder.Cell
import com.intellij.ui.dsl.builder.bindSelected
import com.intellij.ui.dsl.builder.panel
import com.intellij.ui.dsl.builder.selected
import com.intellij.ui.dsl.builder.toMutableProperty
import org.nixos.idea.lsp.ui.CommandSuggestionsPopup

class NixLspSettingsConfigurable :
BoundSearchableConfigurable("Language Server (LSP)", "org.nixos.idea.lsp.NixLspSettingsConfigurable"),
Configurable.Beta {

override fun createPanel(): DialogPanel {
val settings = NixLspSettings.getInstance()
lateinit var enabledCheckBox: Cell<JBCheckBox>
return panel {
row {
enabledCheckBox = checkBox("Enable language server")
.bindSelected(settings::isEnabled)
}
groupRowsRange("Language Server Configuration") {
row("Command:") {
cell(RawCommandLineEditor()).applyToComponent {
editorField.emptyText.setText("Command to start Language Server")
editorField.accessibleContext.accessibleName = "Command to start Language Server"
// TODO What about the following line?
//editorField.margin = myEnabled!!.margin
CommandSuggestionsPopup(
this,
settings.commandHistory,
BUILTIN_SUGGESTIONS
).install()
}.bind(
RawCommandLineEditor::getText,
RawCommandLineEditor::setText,
settings::command.toMutableProperty()
).align(AlignX.FILL)
}
}.enabledIf(enabledCheckBox.selected)
}
}

@Suppress("UnstableApiUsage")
override fun apply() {
super.apply()
for (project in ProjectManager.getInstance().openProjects) {
LspServerManager.getInstance(project).stopAndRestartIfNeeded(NixLspServerSupportProvider::class.java)
}
}
}

private val BUILTIN_SUGGESTIONS: List<CommandSuggestionsPopup.Suggestion> = listOf(
CommandSuggestionsPopup.Suggestion.builtin(
"<html>Use <b>nil</b> from nixpkgs</html>",
"nix --extra-experimental-features \"nix-command flakes\" run nixpkgs#nil"
),
CommandSuggestionsPopup.Suggestion.builtin(
"<html>Use <b>nixd</b> from nixpkgs</html>",
"nix --extra-experimental-features \"nix-command flakes\" run nixpkgs#nixd"
)
)

This file was deleted.

Loading

0 comments on commit 79a43ab

Please sign in to comment.