connect MCP language queries to project LSP
This commit is contained in:
+6
@@ -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();
|
||||
|
||||
+15
-3
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package p.studio.lsp.services;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.lsp.messages.*;
|
||||
import java.nio.file.Path;
|
||||
import java.net.Socket;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class LspServerLifecycleTest {
|
||||
@TempDir Path temporary;
|
||||
|
||||
@Test void projectEndpointsAreIndependentAndShutdownClosesConnectedSockets() throws Exception {
|
||||
var lifecycle = new LspServerLifecycleImpl();
|
||||
var first = new LspProjectContext("first", "pbs", temporary);
|
||||
var second = new LspProjectContext("second", "pbs", temporary.resolve("second"));
|
||||
var configuration = LspServerConfiguration.builder().endpoint(new LspServerEndpoint("127.0.0.1", 0)).build();
|
||||
lifecycle.bootServer(configuration, first); lifecycle.bootServer(configuration, second);
|
||||
try {
|
||||
var endpoint = lifecycle.endpoint(first).orElseThrow();
|
||||
assertNotEquals(endpoint.port(), lifecycle.endpoint(second).orElseThrow().port());
|
||||
assertTrue(lifecycle.endpoint(new LspProjectContext("first", "other-language", temporary)).isEmpty());
|
||||
try (var socket = new Socket(endpoint.host(), endpoint.port())) {
|
||||
socket.setSoTimeout(2000);
|
||||
String request = "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{\"rootUri\":\"" + temporary.toUri() + "\",\"capabilities\":{}}}";
|
||||
byte[] body = request.getBytes(java.nio.charset.StandardCharsets.UTF_8);
|
||||
socket.getOutputStream().write(("Content-Length: " + body.length + "\r\n\r\n").getBytes(java.nio.charset.StandardCharsets.US_ASCII));
|
||||
socket.getOutputStream().write(body); socket.getOutputStream().flush();
|
||||
assertNotEquals(-1, socket.getInputStream().read());
|
||||
lifecycle.shutdownServer(first);
|
||||
socket.getInputStream().readAllBytes(); // Buffered reply drains, then EOF; no abandoned client connection.
|
||||
assertTrue(lifecycle.endpoint(first).isEmpty());
|
||||
assertTrue(lifecycle.endpoint(second).isPresent());
|
||||
}
|
||||
} finally { lifecycle.shutdownServer(first); lifecycle.shutdownServer(second); }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user