connect MCP language queries to project LSP

This commit is contained in:
2026-09-29 09:21:59 +01:00
parent feb57c5298
commit c1834bad1f
24 changed files with 757 additions and 21 deletions
@@ -47,6 +47,12 @@ public final class LspServerLifecycleImpl implements LspServerLifecycle {
closeQuietly(host);
}
@Override
public java.util.Optional<p.studio.lsp.messages.LspServerEndpoint> endpoint(LspProjectContext context) {
var host = activeServers.get(context.projectKey());
return host == null || !host.matches(context) ? java.util.Optional.empty() : host.endpoint();
}
private static void closeQuietly(final TcpLspServerHost host) {
try {
host.close();
@@ -24,6 +24,7 @@ public final class TcpLspServerHost {
private volatile boolean closed;
private volatile ServerSocket serverSocket;
private volatile Thread acceptThread;
private final java.util.Set<Socket> clients = java.util.concurrent.ConcurrentHashMap.newKeySet();
public TcpLspServerHost(
final LspServerConfiguration serverConfiguration,
@@ -59,6 +60,8 @@ public final class TcpLspServerHost {
while (!closed) {
try {
final Socket socket = serverSocket.accept();
clients.add(socket);
if (closed) { clients.remove(socket); socket.close(); break; }
clientExecutor.submit(() -> handleClient(socket));
} catch (IOException exception) {
if (!closed) {
@@ -74,14 +77,21 @@ public final class TcpLspServerHost {
final Launcher<LanguageClient> launcher = LSPLauncher.createServerLauncher(
server,
socket.getInputStream(),
socket.getOutputStream());
socket.getOutputStream(), clientExecutor, consumer -> consumer);
server.connect(launcher.getRemoteProxy());
launcher.startListening().get();
} catch (Exception exception) {
throw new IllegalStateException("LSP client failed", exception);
}
if (!closed && !socket.isClosed()) throw new IllegalStateException("LSP client failed", exception);
} finally { clients.remove(socket); }
}
public synchronized java.util.Optional<p.studio.lsp.messages.LspServerEndpoint> endpoint() {
return closed || serverSocket == null ? java.util.Optional.empty() : java.util.Optional.of(
new p.studio.lsp.messages.LspServerEndpoint(serverSocket.getInetAddress().getHostAddress(), serverSocket.getLocalPort()));
}
public boolean matches(LspProjectContext project) { return context.equals(project); }
public synchronized void close() throws Exception {
if (closed) {
return;
@@ -93,6 +103,8 @@ public final class TcpLspServerHost {
if (acceptThread != null) {
acceptThread.interrupt();
}
for (var socket : clients) { try { socket.close(); } catch (IOException ignored) {} }
clients.clear();
clientExecutor.shutdownNow();
}
}