@@ -7,7 +7,7 @@ import com.intellij.collaboration.async.launchNow
7
7
import com.intellij.ide.actions.ShowSettingsUtilImpl
8
8
import com.intellij.notification.*
9
9
import com.intellij.openapi.Disposable
10
- import com.intellij.openapi.diagnostic.Logger
10
+ import com.intellij.openapi.diagnostic.logger
11
11
import com.intellij.openapi.diagnostic.runAndLogException
12
12
import com.intellij.openapi.editor.Editor
13
13
import com.intellij.openapi.fileEditor.FileDocumentManager
@@ -51,13 +51,14 @@ import java.util.concurrent.ConcurrentHashMap
51
51
import java.util.concurrent.TimeUnit
52
52
import java.util.concurrent.atomic.AtomicInteger
53
53
54
+ private const val MAX_FAILED_STARTS = 2
55
+
54
56
class LanguageServerEndpoint (
55
57
private val coroutineScope : CoroutineScope ,
56
58
private val languageHostConnectionManager : LanguageHostConnectionManager ,
57
59
private val project : Project
58
60
) : Disposable {
59
61
60
- private val LOG : Logger = Logger .getInstance(javaClass)
61
62
private var client: PSLanguageClientImpl = PSLanguageClientImpl (project)
62
63
private var languageServer: LanguageServer ? = null
63
64
private val textDocumentServiceQueue: TextDocumentServiceQueue
@@ -69,14 +70,11 @@ class LanguageServerEndpoint(
69
70
private val rootPath = project.basePath
70
71
private var crashCount: Int = 0
71
72
private val myFailedStarts = AtomicInteger ()
72
- private val MAX_FAILED_STARTS = 2
73
73
74
74
override fun toString (): String {
75
75
val consoleString = if (isConsoleConnection()) " Console" else " "
76
76
return " [PowerShell Editor Services host$consoleString connection, project: $rootPath ]"
77
77
}
78
- // private val dumpFile = File(FileUtil.toCanonicalPath(PSLanguageHostUtils.getLanguageHostLogsDir() + "/protocol_messages_IJ.log"))
79
- // private var fileWriter: PrintWriter? = null
80
78
81
79
init {
82
80
Disposer .register(PluginProjectRoot .getInstance(project), this )
@@ -100,17 +98,18 @@ class LanguageServerEndpoint(
100
98
101
99
fun connectEditor (editor : Editor ? ) {
102
100
if (editor == null ) {
103
- LOG .warn(" Editor is null for $languageServer " )
101
+ logger .warn(" Editor is null for $languageServer " )
104
102
return
105
103
}
106
104
val file = FileDocumentManager .getInstance().getFile(editor.document) ? : return
107
105
val uri = VfsUtil .toUri(File (file.path))
106
+ @Suppress(" DeferredResultUnused" )
108
107
connectedEditors.computeIfAbsent(uri) {
109
108
coroutineScope.async {
110
109
ensureStarted()
111
110
val capabilities = getServerCapabilities()
112
111
if (capabilities != null ) {
113
- LOG .runAndLogException {
112
+ logger .runAndLogException {
114
113
val syncOptions: Either <TextDocumentSyncKind , TextDocumentSyncOptions > = capabilities.textDocumentSync
115
114
val syncKind: TextDocumentSyncKind ? = if (syncOptions.isRight) syncOptions.right.change else syncOptions.left
116
115
val mouseListener = EditorMouseListenerImpl ()
@@ -132,12 +131,12 @@ class LanguageServerEndpoint(
132
131
selectionListener.setManager(manager)
133
132
manager.registerListeners()
134
133
coroutineScope.launchNow { manager.documentOpened() }
135
- LOG .debug(" Created manager for $uri " )
134
+ logger .debug(" Created manager for $uri " )
136
135
return @async manager
137
136
}
138
137
}
139
138
} else {
140
- LOG .warn(" Capabilities are null for $languageServer " )
139
+ logger .warn(" Capabilities are null for $languageServer " )
141
140
}
142
141
143
142
return @async null
@@ -171,13 +170,11 @@ class LanguageServerEndpoint(
171
170
shutdown?.get(100 , TimeUnit .MILLISECONDS )// otherwise lsp4j stream IOException
172
171
languageServer?.exit()
173
172
} catch (ignored: Exception ) {
174
- LOG .debug(" PowerShell language host shutdown exception: $ignored " )
173
+ logger .debug(" PowerShell language host shutdown exception: $ignored " )
175
174
}
176
175
languageServer = null
177
- // fileWriter?.close()
178
- // fileWriter = null
179
176
destroyLanguageHostProcess()
180
- LOG .info(" Connection to PowerShell language host closed for $rootPath ." )
177
+ logger .info(" Connection to PowerShell language host closed for $rootPath ." )
181
178
}
182
179
183
180
fun disconnectEditor (uri : URI ) {
@@ -216,7 +213,7 @@ class LanguageServerEndpoint(
216
213
try {
217
214
val (inStream, outStream) = languageHostConnectionManager.establishConnection()
218
215
if (inStream == null || outStream == null ) {
219
- LOG .warn(" Connection creation to PowerShell language host failed for $rootPath " )
216
+ logger .warn(" Connection creation to PowerShell language host failed for $rootPath " )
220
217
onStartFailure()
221
218
return @job null
222
219
}
@@ -226,7 +223,7 @@ class LanguageServerEndpoint(
226
223
227
224
val server = languageServer
228
225
if (server == null ) {
229
- LOG .warn(" Language server is null for $launcher " )
226
+ logger .warn(" Language server is null for $launcher " )
230
227
onStartFailure()
231
228
return @job null
232
229
}
@@ -235,26 +232,26 @@ class LanguageServerEndpoint(
235
232
launcher.startListening() // NOTE: no need to await here; the future only terminates together with the server
236
233
languageHostConnectionManager.connectServer(this @LanguageServerEndpoint)
237
234
val result = async { initialize(server) }
238
- LOG .debug(" Sent initialize request to server" )
235
+ logger .debug(" Sent initialize request to server" )
239
236
240
237
if (isConsoleConnection()) addRemoteFilesChangeListener()// register vfs listener for saving remote files
241
238
return @job result.await()
242
239
} catch (e: Exception ) {
243
240
when (e) {
244
241
is PowerShellExtensionError -> {
245
- LOG .warn(" PowerShell extension error: ${e.message} " )
242
+ logger .warn(" PowerShell extension error: ${e.message} " )
246
243
showPowerShellNotConfiguredNotification()
247
244
}
248
245
is PowerShellExtensionNotFound -> {
249
- LOG .warn(" PowerShell extension not found" , e)
246
+ logger .warn(" PowerShell extension not found" , e)
250
247
showPowerShellNotConfiguredNotification()
251
248
}
252
249
is PowerShellNotInstalled -> {
253
- LOG .warn(" PowerShell is not installed" , e)
250
+ logger .warn(" PowerShell is not installed" , e)
254
251
showPowerShellNotInstalledNotification()
255
252
}
256
253
else -> {
257
- LOG .warn(" Can not start language server: " , e)
254
+ logger .warn(" Can not start language server: " , e)
258
255
}
259
256
}
260
257
@@ -322,17 +319,16 @@ class LanguageServerEndpoint(
322
319
workspaceClientCapabilities.didChangeConfiguration = DidChangeConfigurationCapabilities ()
323
320
workspaceClientCapabilities.didChangeWatchedFiles = DidChangeWatchedFilesCapabilities ()
324
321
workspaceClientCapabilities.executeCommand = ExecuteCommandCapabilities ()
325
- workspaceClientCapabilities.workspaceEdit = WorkspaceEditCapabilities (true )
322
+ workspaceClientCapabilities.workspaceEdit = WorkspaceEditCapabilities ().apply {
323
+ documentChanges = true
324
+ }
326
325
workspaceClientCapabilities.symbol = SymbolCapabilities ()
327
326
val textDocumentClientCapabilities = TextDocumentClientCapabilities ()
328
327
textDocumentClientCapabilities.codeAction = CodeActionCapabilities ()
329
- // textDocumentClientCapabilities.setCodeLens(new CodeLensCapabilities)
330
328
textDocumentClientCapabilities.completion = CompletionCapabilities (CompletionItemCapabilities (false ))
331
329
textDocumentClientCapabilities.definition = DefinitionCapabilities ()
332
330
textDocumentClientCapabilities.documentHighlight = DocumentHighlightCapabilities ()
333
331
textDocumentClientCapabilities.synchronization = SynchronizationCapabilities (false , false , true )
334
- // textDocumentClientCapabilities.setDocumentLink(new DocumentLinkCapabilities)
335
- // textDocumentClientCapabilities.setDocumentSymbol(new DocumentSymbolCapabilities)
336
332
textDocumentClientCapabilities.formatting = FormattingCapabilities ()
337
333
textDocumentClientCapabilities.hover = HoverCapabilities ()
338
334
textDocumentClientCapabilities.onTypeFormatting = OnTypeFormattingCapabilities ()
@@ -342,11 +338,11 @@ class LanguageServerEndpoint(
342
338
textDocumentClientCapabilities.signatureHelp = SignatureHelpCapabilities ()
343
339
textDocumentClientCapabilities.synchronization = SynchronizationCapabilities (true , true , true )
344
340
val initParams = InitializeParams ()
345
- initParams.rootUri = rootPath
341
+ initParams.workspaceFolders = listOf ( WorkspaceFolder ( rootPath, " root " ))
346
342
initParams.capabilities = ClientCapabilities (workspaceClientCapabilities, textDocumentClientCapabilities, null )
347
343
initParams.initializationOptions = null
348
344
val result = server.initialize(initParams).await()
349
- LOG .info(" Got server initialize result for $rootPath " )
345
+ logger .info(" Got server initialize result for $rootPath " )
350
346
return result
351
347
}
352
348
@@ -359,7 +355,7 @@ class LanguageServerEndpoint(
359
355
get() = synchronized(serverInitializationLock) { serverInitialization } != null
360
356
361
357
fun crashed (e : Throwable ) {
362
- LOG .warn(" Crashed: $e " )
358
+ logger .warn(" Crashed: $e " )
363
359
crashCount + = 1
364
360
if (crashCount < 5 ) {
365
361
val editors = connectedEditors.toMap().keys
@@ -404,3 +400,5 @@ class LanguageServerEndpoint(
404
400
}
405
401
}
406
402
}
403
+
404
+ private val logger = logger<LanguageServerEndpoint >()
0 commit comments