|
| 1 | +// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package software.aws.toolkits.jetbrains.services.amazonq.lsp |
| 5 | + |
| 6 | +import com.google.gson.ToNumberPolicy |
| 7 | +import com.intellij.execution.configurations.GeneralCommandLine |
| 8 | +import com.intellij.execution.impl.ExecutionManagerImpl |
| 9 | +import com.intellij.execution.process.KillableColoredProcessHandler |
| 10 | +import com.intellij.execution.process.KillableProcessHandler |
| 11 | +import com.intellij.execution.process.ProcessEvent |
| 12 | +import com.intellij.execution.process.ProcessListener |
| 13 | +import com.intellij.execution.process.ProcessOutputType |
| 14 | +import com.intellij.openapi.Disposable |
| 15 | +import com.intellij.openapi.components.Service |
| 16 | +import com.intellij.openapi.components.service |
| 17 | +import com.intellij.openapi.project.Project |
| 18 | +import com.intellij.openapi.util.Key |
| 19 | +import com.intellij.util.io.await |
| 20 | +import kotlinx.coroutines.CoroutineScope |
| 21 | +import kotlinx.coroutines.launch |
| 22 | +import org.eclipse.lsp4j.InitializeParams |
| 23 | +import org.eclipse.lsp4j.InitializedParams |
| 24 | +import org.eclipse.lsp4j.jsonrpc.Launcher |
| 25 | +import org.eclipse.lsp4j.launch.LSPLauncher |
| 26 | +import org.slf4j.event.Level |
| 27 | +import software.aws.toolkits.core.utils.getLogger |
| 28 | +import software.aws.toolkits.core.utils.warn |
| 29 | +import software.aws.toolkits.jetbrains.isDeveloperMode |
| 30 | +import java.io.IOException |
| 31 | +import java.io.OutputStreamWriter |
| 32 | +import java.io.PipedInputStream |
| 33 | +import java.io.PipedOutputStream |
| 34 | +import java.io.PrintWriter |
| 35 | +import java.io.StringWriter |
| 36 | +import java.nio.charset.StandardCharsets |
| 37 | +import java.util.concurrent.Future |
| 38 | + |
| 39 | +// https://github.com/redhat-developer/lsp4ij/blob/main/src/main/java/com/redhat/devtools/lsp4ij/server/LSPProcessListener.java |
| 40 | +// JB impl and redhat both use a wrapper to handle input buffering issue |
| 41 | +internal class LSPProcessListener : ProcessListener { |
| 42 | + private val outputStream = PipedOutputStream() |
| 43 | + private val outputStreamWriter = OutputStreamWriter(outputStream, StandardCharsets.UTF_8) |
| 44 | + val inputStream = PipedInputStream(outputStream) |
| 45 | + |
| 46 | + override fun onTextAvailable(event: ProcessEvent, outputType: Key<*>) { |
| 47 | + if (ProcessOutputType.isStdout(outputType)) { |
| 48 | + try { |
| 49 | + this.outputStreamWriter.write(event.text) |
| 50 | + this.outputStreamWriter.flush() |
| 51 | + } catch (_: IOException) { |
| 52 | + ExecutionManagerImpl.stopProcess(event.processHandler) |
| 53 | + } |
| 54 | + } else if (ProcessOutputType.isStderr(outputType)) { |
| 55 | + LOG.warn { "LSP process stderr: ${event.text}" } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + override fun processTerminated(event: ProcessEvent) { |
| 60 | + try { |
| 61 | + this.outputStreamWriter.close() |
| 62 | + this.outputStream.close() |
| 63 | + } catch (_: IOException) { |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + companion object { |
| 68 | + private val LOG = getLogger<LSPProcessListener>() |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +@Service(Service.Level.PROJECT) |
| 73 | +class AmazonQLspService(project: Project, private val cs: CoroutineScope) : Disposable { |
| 74 | + private val launcher: Launcher<AmazonQLanguageServer> |
| 75 | + |
| 76 | + private val languageServer: AmazonQLanguageServer |
| 77 | + get() = launcher.remoteProxy |
| 78 | + |
| 79 | + @Suppress("ForbiddenVoid") |
| 80 | + private val launcherFuture: Future<Void> |
| 81 | + private val launcherHandler: KillableProcessHandler |
| 82 | + |
| 83 | + init { |
| 84 | + val cmd = GeneralCommandLine("amazon-q-lsp") |
| 85 | + |
| 86 | + launcherHandler = KillableColoredProcessHandler.Silent(cmd) |
| 87 | + val inputWrapper = LSPProcessListener() |
| 88 | + launcherHandler.addProcessListener(inputWrapper) |
| 89 | + launcherHandler.startNotify() |
| 90 | + |
| 91 | + launcher = LSPLauncher.Builder<AmazonQLanguageServer>() |
| 92 | + .setLocalService(AmazonQLanguageClientImpl()) |
| 93 | + .setRemoteInterface(AmazonQLanguageServer::class.java) |
| 94 | + .configureGson { |
| 95 | + // TODO: maybe need adapter for initialize: |
| 96 | + // https://github.com/aws/amazon-q-eclipse/blob/b9d5bdcd5c38e1dd8ad371d37ab93a16113d7d4b/plugin/src/software/aws/toolkits/eclipse/amazonq/lsp/QLspTypeAdapterFactory.java |
| 97 | + |
| 98 | + // otherwise Gson treats all numbers as double which causes deser issues |
| 99 | + it.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE) |
| 100 | + }.traceMessages( |
| 101 | + PrintWriter( |
| 102 | + object : StringWriter() { |
| 103 | + private val traceLogger = LOG.atLevel(if (isDeveloperMode()) Level.INFO else Level.DEBUG) |
| 104 | + |
| 105 | + override fun flush() { |
| 106 | + traceLogger.log { buffer.toString() } |
| 107 | + buffer.setLength(0) |
| 108 | + } |
| 109 | + } |
| 110 | + ) |
| 111 | + ) |
| 112 | + .setInput(inputWrapper.inputStream) |
| 113 | + .setOutput(launcherHandler.process.outputStream) |
| 114 | + .create() |
| 115 | + |
| 116 | + launcherFuture = launcher.startListening() |
| 117 | + |
| 118 | + cs.launch { |
| 119 | + val initializeResult = languageServer.initialize( |
| 120 | + InitializeParams().apply { |
| 121 | + // does this work on windows |
| 122 | + processId = ProcessHandle.current().pid().toInt() |
| 123 | + // capabilities |
| 124 | + // client info |
| 125 | + // trace? |
| 126 | + // workspace folders? |
| 127 | + // anything else we need? |
| 128 | + } |
| 129 | + // probably need a timeout |
| 130 | + ).await() |
| 131 | + |
| 132 | + // then if this succeeds then we can allow the client to send requests |
| 133 | + if (initializeResult == null) { |
| 134 | + LOG.warn { "LSP initialization failed" } |
| 135 | + launcherHandler.destroyProcess() |
| 136 | + } |
| 137 | + languageServer.initialized(InitializedParams()) |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + override fun dispose() { |
| 142 | + if (!launcherFuture.isDone) { |
| 143 | + try { |
| 144 | + languageServer.apply { |
| 145 | + shutdown().thenRun { exit() } |
| 146 | + } |
| 147 | + } catch (e: Exception) { |
| 148 | + LOG.warn(e) { "LSP shutdown failed" } |
| 149 | + launcherHandler.destroyProcess() |
| 150 | + } |
| 151 | + } else if (!launcherHandler.isProcessTerminated) { |
| 152 | + launcherHandler.destroyProcess() |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + companion object { |
| 157 | + private val LOG = getLogger<AmazonQLspService>() |
| 158 | + fun getInstance(project: Project) = project.service<AmazonQLspService>() |
| 159 | + } |
| 160 | +} |
0 commit comments