Skip to content

Commit bf1aaa0

Browse files
committed
(#291) LSPMain: refactor into PowerShellSettings (app service) and LanguageServer (project service)
1 parent 21c4aad commit bf1aaa0

File tree

14 files changed

+148
-173
lines changed

14 files changed

+148
-173
lines changed

src/main/java/com/intellij/plugin/powershell/lang/lsp/ide/settings/FormUIUtil.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import com.intellij.openapi.ui.TextFieldWithBrowseButton
99
import com.intellij.openapi.util.io.FileUtil
1010
import com.intellij.openapi.util.text.StringUtil
1111
import com.intellij.plugin.powershell.ide.MessagesBundle
12-
import com.intellij.plugin.powershell.lang.lsp.LSPInitMain
12+
import com.intellij.plugin.powershell.lang.lsp.PowerShellSettings
1313
import com.intellij.plugin.powershell.lang.lsp.languagehost.PSLanguageHostUtils.getEditorServicesModuleVersion
1414
import com.intellij.plugin.powershell.lang.lsp.languagehost.PSLanguageHostUtils.getEditorServicesStartupScript
1515
import com.intellij.plugin.powershell.lang.lsp.languagehost.PSLanguageHostUtils.getPSExtensionModulesDir
@@ -76,5 +76,5 @@ object FormUIUtil {
7676
}
7777

7878
val globalSettingsExecutablePath: String?
79-
get() = LSPInitMain.getInstance().state.powerShellExePath
79+
get() = PowerShellSettings.getInstance().state.powerShellExePath
8080
}

src/main/java/com/intellij/plugin/powershell/lang/lsp/ide/settings/PowerShellConfigurable.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.intellij.openapi.options.ConfigurationException;
55
import com.intellij.openapi.options.SearchableConfigurable;
66
import com.intellij.openapi.util.text.StringUtil;
7-
import com.intellij.plugin.powershell.lang.lsp.LSPInitMain;
7+
import com.intellij.plugin.powershell.lang.lsp.PowerShellSettings;
88
import org.jetbrains.annotations.Nls;
99
import org.jetbrains.annotations.NotNull;
1010
import org.jetbrains.annotations.Nullable;
@@ -49,8 +49,8 @@ public boolean isModified() {
4949

5050
@Override
5151
public void reset() {
52-
LSPInitMain lspInitMain = LSPInitMain.getInstance();
53-
LSPInitMain.PowerShellInfo powerShellInfo = lspInitMain.getState();
52+
PowerShellSettings powerShellSettings = PowerShellSettings.getInstance();
53+
PowerShellSettings.PowerShellInfo powerShellInfo = powerShellSettings.getState();
5454
String psEsPathFromSettings = powerShellInfo.getPowerShellExtensionPath();
5555
String exePathFromSettings = powerShellInfo.getPowerShellExePath();
5656
boolean isEnabledInSettings = powerShellInfo.isUseLanguageServer();
@@ -65,8 +65,8 @@ public void apply() throws ConfigurationException {
6565
String psExtensionPath = getPSExtensionPathFromForm();
6666
String powerShellExePath = getPowerShellExePathFromForm();
6767
boolean isEnabled = getPSJpanel().getIsUseLanguageServer();
68-
LSPInitMain lspInitMain = LSPInitMain.getInstance();
69-
LSPInitMain.PowerShellInfo powerShellInfo = lspInitMain.getState();
68+
PowerShellSettings powerShellSettings = PowerShellSettings.getInstance();
69+
PowerShellSettings.PowerShellInfo powerShellInfo = powerShellSettings.getState();
7070
FormUIUtil.validatePowerShellExecutablePath(powerShellExePath);
7171
String powerShellVersion = getPSJpanel().getPowerShellVersionValue();
7272
if (StringUtil.isEmpty(powerShellVersion)) throw new ConfigurationException("Can not detect PowerShell version");

src/main/java/com/intellij/plugin/powershell/lang/lsp/ide/settings/PowerShellExecutableChooserPanel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
66
import com.intellij.openapi.util.text.StringUtil;
77
import com.intellij.plugin.powershell.ide.MessagesBundle;
8-
import com.intellij.plugin.powershell.lang.lsp.LSPInitMain;
8+
import com.intellij.plugin.powershell.lang.lsp.PowerShellSettings;
99
import com.intellij.plugin.powershell.lang.lsp.languagehost.PSLanguageHostUtils;
1010
import com.intellij.ui.components.JBTextField;
1111
import org.jetbrains.annotations.NotNull;
@@ -23,7 +23,7 @@ public class PowerShellExecutableChooserPanel extends JComponent {
2323
private JPanel myJpanel;
2424

2525
public PowerShellExecutableChooserPanel(@Nullable String executablePath) {
26-
String globalSettingsPath = LSPInitMain.getInstance().getState().getPowerShellExePath();
26+
String globalSettingsPath = PowerShellSettings.getInstance().getState().getPowerShellExePath();
2727
updateExecutablePath(StringUtil.isEmpty(executablePath) ? globalSettingsPath : executablePath);
2828
}
2929

src/main/java/com/intellij/plugin/powershell/lang/lsp/ide/settings/PowerShellJPanelComponent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import com.intellij.openapi.util.text.StringUtil;
99
import com.intellij.openapi.vfs.VirtualFile;
1010
import com.intellij.plugin.powershell.ide.MessagesBundle;
11-
import com.intellij.plugin.powershell.lang.lsp.LSPInitMain;
11+
import com.intellij.plugin.powershell.lang.lsp.PowerShellSettings;
1212
import com.intellij.plugin.powershell.lang.lsp.languagehost.PSLanguageHostUtils;
1313
import com.intellij.plugin.powershell.lang.lsp.languagehost.PowerShellNotInstalled;
1414
import com.intellij.ui.HyperlinkAdapter;
@@ -177,7 +177,7 @@ private void setBundledPowerShellExtensionPath() throws ConfigurationException {
177177
Companion.setUseBundledPowerShellExtension(true);
178178
}
179179

180-
void fillPowerShellInfo(@NotNull LSPInitMain.PowerShellInfo powerShellInfo) {
180+
void fillPowerShellInfo(@NotNull PowerShellSettings.PowerShellInfo powerShellInfo) {
181181
setEditorServicesVersionLabelValue(powerShellInfo.getEditorServicesModuleVersion());
182182
setPowerShellExtensionPath(powerShellInfo.getPowerShellExtensionPath());
183183
setPowerShellExePath(powerShellInfo.getPowerShellExePath());

src/main/kotlin/com/intellij/plugin/powershell/ide/actions/PowerShellConsoleAction.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ import com.intellij.openapi.progress.ProgressIndicator
66
import com.intellij.openapi.progress.ProgressManager
77
import com.intellij.openapi.progress.Task
88
import com.intellij.plugin.powershell.PowerShellIcons
9-
import com.intellij.plugin.powershell.lang.lsp.LSPInitMain
9+
import com.intellij.plugin.powershell.lang.lsp.LanguageServer
1010

1111
class PowerShellConsoleAction : AnAction(PowerShellIcons.FILE) {
1212

1313
override fun actionPerformed(e: AnActionEvent) {
1414
val project = e.project ?: return
15-
val server = LSPInitMain.getServerWithConsoleProcess(project)
15+
val server = LanguageServer.getInstance(project).serverWithConsoleProcess.value
1616
ProgressManager.getInstance().run(object : Task.Backgroundable(project, "Starting PowerShell terminal console", false) {
1717
override fun run(indicator: ProgressIndicator) {
1818
indicator.text = "Starting PowerShell terminal console..."
1919
server.start()
2020
}
2121
})
2222
}
23-
}
23+
}

src/main/kotlin/com/intellij/plugin/powershell/ide/run/PowerShellScriptCommandLineState.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import com.intellij.openapi.util.io.NioFiles.toPath
1919
import com.intellij.openapi.util.text.StringUtil
2020
import com.intellij.openapi.vfs.LocalFileSystem
2121
import com.intellij.plugin.powershell.ide.runAndLogException
22-
import com.intellij.plugin.powershell.lang.lsp.LSPInitMain
22+
import com.intellij.plugin.powershell.lang.lsp.PowerShellSettings
2323
import com.intellij.plugin.powershell.lang.lsp.languagehost.PowerShellNotInstalled
2424
import com.intellij.terminal.TerminalExecutionConsole
2525
import com.intellij.util.text.nullize
@@ -54,7 +54,7 @@ class PowerShellScriptCommandLineState(
5454
private fun startProcess(): ProcessHandler {
5555
try {
5656
val command = buildCommand(
57-
runConfiguration.executablePath ?: LSPInitMain.getInstance().getPowerShellExecutable(),
57+
runConfiguration.executablePath ?: PowerShellSettings.getInstance().getPowerShellExecutable(),
5858
runConfiguration.scriptPath,
5959
runConfiguration.getCommandOptions(),
6060
runConfiguration.scriptParameters

src/main/kotlin/com/intellij/plugin/powershell/lang/lsp/LSPInitMain.kt

Lines changed: 0 additions & 140 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* adopted from https://github.com/gtache/intellij-lsp
3+
*/
4+
package com.intellij.plugin.powershell.lang.lsp
5+
6+
import com.intellij.openapi.application.ApplicationManager
7+
import com.intellij.openapi.components.Service
8+
import com.intellij.openapi.components.service
9+
import com.intellij.openapi.diagnostic.Logger
10+
import com.intellij.openapi.diagnostic.logger
11+
import com.intellij.openapi.editor.Editor
12+
import com.intellij.openapi.fileEditor.FileDocumentManager
13+
import com.intellij.openapi.project.Project
14+
import com.intellij.openapi.util.io.toNioPathOrNull
15+
import com.intellij.openapi.vfs.VirtualFile
16+
import com.intellij.plugin.powershell.PowerShellFileType
17+
import com.intellij.plugin.powershell.ide.PluginProjectRoot
18+
import com.intellij.plugin.powershell.lang.lsp.languagehost.EditorServicesLanguageHostStarter
19+
import com.intellij.plugin.powershell.lang.lsp.languagehost.LanguageServerEndpoint
20+
import com.intellij.plugin.powershell.lang.lsp.languagehost.terminal.PowerShellConsoleTerminalRunner
21+
import com.intellij.plugin.powershell.lang.lsp.util.isRemotePath
22+
23+
@Service(Service.Level.PROJECT)
24+
class LanguageServer(private val project: Project) {
25+
companion object {
26+
private val LOG: Logger = logger<LanguageServer>()
27+
28+
fun getInstance(project: Project): LanguageServer {
29+
return project.service<LanguageServer>()
30+
}
31+
32+
fun editorOpened(editor: Editor) {
33+
if (!PowerShellSettings.getInstance().state.isUseLanguageServer) return
34+
35+
val project = editor.project ?: return
36+
val file = FileDocumentManager.getInstance().getFile(editor.document)
37+
if (file == null || file.fileType !is PowerShellFileType && !isRemotePath(file.path)) return
38+
ApplicationManager.getApplication().executeOnPooledThread {
39+
val server = getServer(file, project)
40+
server.connectEditor(editor)
41+
LOG.info("Registered ${file.path} script for server: $server")
42+
}
43+
}
44+
45+
private fun getServer(file: VirtualFile, project: Project): LanguageServerEndpoint {
46+
return findServerForRemoteFile(file, project) ?: getInstance(project).editorLanguageServer.value
47+
}
48+
49+
fun editorClosed(editor: Editor) {
50+
val project = editor.project ?: return
51+
val vfile = FileDocumentManager.getInstance().getFile(editor.document) ?: return
52+
if (vfile.fileType !is PowerShellFileType) return
53+
val server = findServer(vfile, project) ?: return
54+
server.disconnectEditor(vfile.path.toNioPathOrNull()?.toUri() ?: return)
55+
LOG.debug("Removed ${vfile.name} script from server: $server")
56+
}
57+
58+
private fun findServer(file: VirtualFile, project: Project): LanguageServerEndpoint? {
59+
return findServerForRemoteFile(file, project)
60+
?: getInstance(project).editorLanguageServer.takeIf { it.isInitialized() }?.value
61+
}
62+
63+
private fun findServerForRemoteFile(file: VirtualFile, project: Project): LanguageServerEndpoint? {
64+
val consoleServer = getInstance(project).serverWithConsoleProcess.takeIf { it.isInitialized() }?.value ?: return null
65+
return if (consoleServer.isRunning && isRemotePath(file.canonicalPath)) consoleServer else null
66+
}
67+
}
68+
69+
val serverWithConsoleProcess: Lazy<LanguageServerEndpoint> = lazy {
70+
val scope = PluginProjectRoot.getInstance(project).coroutineScope
71+
LanguageServerEndpoint(scope, PowerShellConsoleTerminalRunner(project), project)
72+
}
73+
74+
val editorLanguageServer: Lazy<LanguageServerEndpoint> = lazy {
75+
val scope = PluginProjectRoot.getInstance(project).coroutineScope
76+
LanguageServerEndpoint(scope, EditorServicesLanguageHostStarter(project), project)
77+
}
78+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* adopted from https://github.com/gtache/intellij-lsp
3+
*/
4+
package com.intellij.plugin.powershell.lang.lsp
5+
6+
import com.intellij.openapi.components.*
7+
import com.intellij.plugin.powershell.ide.run.findPsExecutable
8+
9+
@Service
10+
@State(name = "PowerShellSettings", storages = [Storage(value = "powerShellSettings.xml", roamingType = RoamingType.DISABLED)])
11+
class PowerShellSettings : PersistentStateComponent<PowerShellSettings.PowerShellInfo> {
12+
13+
companion object {
14+
@JvmStatic
15+
fun getInstance(): PowerShellSettings = service()
16+
}
17+
18+
data class PowerShellInfo(
19+
var editorServicesStartupScript: String = "",
20+
var powerShellExePath: String? = null,
21+
var powerShellVersion: String? = null,
22+
var powerShellExtensionPath: String? = null,
23+
var editorServicesModuleVersion: String? = null,
24+
var isUseLanguageServer: Boolean = true
25+
)
26+
27+
fun getPowerShellExecutable(): String {
28+
val psExecutable = myPowerShellInfo.powerShellExePath ?: findPsExecutable()
29+
myPowerShellInfo.powerShellExePath = psExecutable
30+
return psExecutable
31+
}
32+
33+
private var myPowerShellInfo: PowerShellInfo = PowerShellInfo()
34+
35+
override fun loadState(powerShellInfo: PowerShellInfo) {
36+
myPowerShellInfo = powerShellInfo
37+
}
38+
39+
override fun getState(): PowerShellInfo {
40+
return myPowerShellInfo
41+
}
42+
}

0 commit comments

Comments
 (0)