clean up and simplify
This commit is contained in:
+4
-4
@@ -6,11 +6,11 @@ import p.studio.lsp.messages.BaselineServerDescription;
|
||||
import p.studio.lsp.messages.LspProjectContext;
|
||||
|
||||
public interface LanguageServiceBridge {
|
||||
BaselineServerDescription describeServer(LspProjectContext project);
|
||||
BaselineServerDescription describeServer(LspProjectContext context);
|
||||
|
||||
BaselineDocumentAnalysis analyzeDocument(LspProjectContext project, String documentUri, String text);
|
||||
BaselineDocumentAnalysis analyzeDocument(LspProjectContext context, String documentUri, String text);
|
||||
|
||||
BaselineHover hover(LspProjectContext project, String documentUri, int line, int character);
|
||||
BaselineHover hover(LspProjectContext context, String documentUri, int line, int character);
|
||||
|
||||
String onSave(LspProjectContext project, String documentUri);
|
||||
String onSave(LspProjectContext context, String documentUri);
|
||||
}
|
||||
|
||||
+15
-22
@@ -2,43 +2,36 @@ package p.studio.lsp.services;
|
||||
|
||||
import p.studio.lsp.api.LspServerLifecycle;
|
||||
import p.studio.lsp.messages.LspProjectContext;
|
||||
import p.studio.lsp.messages.LspServerBootRequest;
|
||||
import p.studio.lsp.messages.LspServerHandle;
|
||||
import p.studio.lsp.messages.LspServerConfiguration;
|
||||
import p.studio.lsp.services.compiler.CompilerLanguageServiceBridge;
|
||||
import p.studio.lsp.services.host.ProjectScopedLspServerHost;
|
||||
import p.studio.lsp.services.host.ProjectScopedLspServerHostFactory;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
public final class LspServerLifecycleImpl implements LspServerLifecycle {
|
||||
private final ProjectScopedLspServerHostFactory serverHostFactory;
|
||||
private final TcpLspServerHostFactory serverHostFactory;
|
||||
private final CompilerLanguageServiceBridge compilerBridge;
|
||||
private final ConcurrentMap<String, ProjectScopedLspServerHost> activeServers;
|
||||
private final ConcurrentMap<String, TcpLspServerHost> activeServers;
|
||||
|
||||
public LspServerLifecycleImpl(
|
||||
final ProjectScopedLspServerHostFactory serverHostFactory,
|
||||
final CompilerLanguageServiceBridge compilerBridge) {
|
||||
this.serverHostFactory = Objects.requireNonNull(serverHostFactory, "serverHostFactory");
|
||||
this.compilerBridge = Objects.requireNonNull(compilerBridge, "compilerBridge");
|
||||
public LspServerLifecycleImpl() {
|
||||
this.compilerBridge = new CompilerLanguageServiceBridge();
|
||||
this.serverHostFactory = new TcpLspServerHostFactory();
|
||||
this.activeServers = new ConcurrentHashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public LspServerHandle bootServer(final LspServerBootRequest request) {
|
||||
final LspServerBootRequest safeRequest = Objects.requireNonNull(request, "request");
|
||||
final ProjectScopedLspServerHost host = serverHostFactory.create(safeRequest, compilerBridge);
|
||||
final ProjectScopedLspServerHost previous = activeServers.putIfAbsent(safeRequest.project().projectKey(), host);
|
||||
public void bootServer(final LspServerConfiguration serverConfiguration, LspProjectContext context) {
|
||||
final var host = serverHostFactory.create(serverConfiguration, context, compilerBridge);
|
||||
final var previous = activeServers.putIfAbsent(context.projectKey(), host);
|
||||
if (previous != null) {
|
||||
throw new IllegalStateException("LSP server already booted for project " + safeRequest.project().projectKey());
|
||||
throw new IllegalStateException("LSP server already booted for project " + context.projectKey());
|
||||
}
|
||||
|
||||
try {
|
||||
final var endpoint = host.start();
|
||||
return new LspServerHandle(safeRequest.project(), endpoint);
|
||||
host.start();
|
||||
} catch (RuntimeException runtimeException) {
|
||||
activeServers.remove(safeRequest.project().projectKey(), host);
|
||||
activeServers.remove(context.projectKey(), host);
|
||||
closeQuietly(host);
|
||||
throw runtimeException;
|
||||
}
|
||||
@@ -46,15 +39,15 @@ public final class LspServerLifecycleImpl implements LspServerLifecycle {
|
||||
|
||||
@Override
|
||||
public void shutdownServer(final LspProjectContext project) {
|
||||
final LspProjectContext safeProject = Objects.requireNonNull(project, "project");
|
||||
final ProjectScopedLspServerHost host = activeServers.remove(safeProject.projectKey());
|
||||
final var safeProject = Objects.requireNonNull(project, "project");
|
||||
final var host = activeServers.remove(safeProject.projectKey());
|
||||
if (host == null) {
|
||||
return;
|
||||
}
|
||||
closeQuietly(host);
|
||||
}
|
||||
|
||||
private static void closeQuietly(final ProjectScopedLspServerHost host) {
|
||||
private static void closeQuietly(final TcpLspServerHost host) {
|
||||
try {
|
||||
host.close();
|
||||
} catch (Exception exception) {
|
||||
|
||||
+16
-19
@@ -1,10 +1,10 @@
|
||||
package p.studio.lsp.services.host;
|
||||
package p.studio.lsp.services;
|
||||
|
||||
import org.eclipse.lsp4j.jsonrpc.Launcher;
|
||||
import org.eclipse.lsp4j.launch.LSPLauncher;
|
||||
import org.eclipse.lsp4j.services.LanguageClient;
|
||||
import p.studio.lsp.messages.LspServerBootRequest;
|
||||
import p.studio.lsp.messages.LspServerEndpoint;
|
||||
import p.studio.lsp.messages.LspProjectContext;
|
||||
import p.studio.lsp.messages.LspServerConfiguration;
|
||||
import p.studio.lsp.services.compiler.CompilerLanguageServiceBridge;
|
||||
import p.studio.lsp.services.protocol.PrometeuLanguageServer;
|
||||
|
||||
@@ -16,19 +16,21 @@ import java.util.Objects;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public final class TcpLspServerHost implements ProjectScopedLspServerHost {
|
||||
private final LspServerBootRequest request;
|
||||
public final class TcpLspServerHost {
|
||||
private final LspServerConfiguration serverConfiguration;
|
||||
private final LspProjectContext context;
|
||||
private final CompilerLanguageServiceBridge compilerBridge;
|
||||
private final ExecutorService clientExecutor;
|
||||
private volatile boolean closed;
|
||||
private volatile ServerSocket serverSocket;
|
||||
private volatile Thread acceptThread;
|
||||
private volatile LspServerEndpoint endpoint;
|
||||
|
||||
public TcpLspServerHost(
|
||||
final LspServerBootRequest request,
|
||||
final LspServerConfiguration serverConfiguration,
|
||||
final LspProjectContext context,
|
||||
final CompilerLanguageServiceBridge compilerBridge) {
|
||||
this.request = Objects.requireNonNull(request, "request");
|
||||
this.serverConfiguration = Objects.requireNonNull(serverConfiguration, "serverConfiguration");
|
||||
this.context = Objects.requireNonNull(context, "context");
|
||||
this.compilerBridge = Objects.requireNonNull(compilerBridge, "compilerBridge");
|
||||
this.clientExecutor = Executors.newCachedThreadPool(runnable -> {
|
||||
final Thread thread = new Thread(runnable);
|
||||
@@ -38,20 +40,16 @@ public final class TcpLspServerHost implements ProjectScopedLspServerHost {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized LspServerEndpoint start() {
|
||||
if (endpoint != null) {
|
||||
return endpoint;
|
||||
public synchronized void start() {
|
||||
if (serverSocket != null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
final InetAddress bindAddress = InetAddress.getByName(request.configuration().host());
|
||||
final ServerSocket socket = new ServerSocket(request.configuration().port(), 50, bindAddress);
|
||||
serverSocket = socket;
|
||||
endpoint = new LspServerEndpoint(bindAddress.getHostAddress(), socket.getLocalPort());
|
||||
final InetAddress bindAddress = InetAddress.getByName(serverConfiguration.getEndpoint().host());
|
||||
serverSocket = new ServerSocket(serverConfiguration.getEndpoint().port(), 50, bindAddress);
|
||||
acceptThread = new Thread(this::acceptLoop, "prometeu-lsp-v1-accept");
|
||||
acceptThread.setDaemon(true);
|
||||
acceptThread.start();
|
||||
return endpoint;
|
||||
} catch (IOException exception) {
|
||||
throw new IllegalStateException("failed to start TCP LSP host", exception);
|
||||
}
|
||||
@@ -72,7 +70,7 @@ public final class TcpLspServerHost implements ProjectScopedLspServerHost {
|
||||
|
||||
private void handleClient(final Socket socket) {
|
||||
try (socket) {
|
||||
final var server = new PrometeuLanguageServer(request.project(), compilerBridge);
|
||||
final var server = new PrometeuLanguageServer(context, compilerBridge);
|
||||
final Launcher<LanguageClient> launcher = LSPLauncher.createServerLauncher(
|
||||
server,
|
||||
socket.getInputStream(),
|
||||
@@ -84,7 +82,6 @@ public final class TcpLspServerHost implements ProjectScopedLspServerHost {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void close() throws Exception {
|
||||
if (closed) {
|
||||
return;
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package p.studio.lsp.services;
|
||||
|
||||
import p.studio.lsp.messages.LspProjectContext;
|
||||
import p.studio.lsp.messages.LspServerConfiguration;
|
||||
import p.studio.lsp.services.compiler.CompilerLanguageServiceBridge;
|
||||
|
||||
public final class TcpLspServerHostFactory {
|
||||
public TcpLspServerHost create(
|
||||
final LspServerConfiguration serverConfiguration,
|
||||
final LspProjectContext context,
|
||||
final CompilerLanguageServiceBridge compilerBridge) {
|
||||
return new TcpLspServerHost(serverConfiguration, context, compilerBridge);
|
||||
}
|
||||
}
|
||||
+14
-17
@@ -4,10 +4,11 @@ import p.studio.compiler.workspaces.BuilderPipelineService;
|
||||
import p.studio.lsp.messages.*;
|
||||
import p.studio.lsp.services.LanguageServiceBridge;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class CompilerLanguageServiceBridge implements LanguageServiceBridge {
|
||||
private final BuilderPipelineService builderPipelineService;
|
||||
public record CompilerLanguageServiceBridge(
|
||||
BuilderPipelineService builderPipelineService) implements LanguageServiceBridge {
|
||||
|
||||
public CompilerLanguageServiceBridge() {
|
||||
this(BuilderPipelineService.INSTANCE);
|
||||
@@ -17,13 +18,9 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
|
||||
this.builderPipelineService = Objects.requireNonNull(builderPipelineService, "builderPipelineService");
|
||||
}
|
||||
|
||||
public BuilderPipelineService builderPipelineService() {
|
||||
return builderPipelineService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaselineServerDescription describeServer(final LspProjectContext project) {
|
||||
Objects.requireNonNull(project, "project");
|
||||
public BaselineServerDescription describeServer(final LspProjectContext context) {
|
||||
Objects.requireNonNull(context, "context");
|
||||
return new BaselineServerDescription(
|
||||
"Prometeu Studio LSP",
|
||||
"0.1.0",
|
||||
@@ -32,13 +29,13 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
|
||||
|
||||
@Override
|
||||
public BaselineDocumentAnalysis analyzeDocument(
|
||||
final LspProjectContext project,
|
||||
final LspProjectContext context,
|
||||
final String documentUri,
|
||||
final String text) {
|
||||
Objects.requireNonNull(project, "project");
|
||||
Objects.requireNonNull(context, "context");
|
||||
// Wave 1 keeps responses intentionally synthetic while hardening the bridge boundary.
|
||||
final String message = "Compiler-backed baseline via " + builderPipelineService.getClass().getSimpleName();
|
||||
return new BaselineDocumentAnalysis(java.util.List.of(new BaselineDocumentIssue(
|
||||
return new BaselineDocumentAnalysis(List.of(new BaselineDocumentIssue(
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
@@ -50,25 +47,25 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
|
||||
|
||||
@Override
|
||||
public BaselineHover hover(
|
||||
final LspProjectContext project,
|
||||
final LspProjectContext context,
|
||||
final String documentUri,
|
||||
final int line,
|
||||
final int character) {
|
||||
Objects.requireNonNull(project, "project");
|
||||
Objects.requireNonNull(context, "context");
|
||||
return new BaselineHover("""
|
||||
**Prometeu Studio LSP**
|
||||
|
||||
|
||||
Compiler-backed baseline.
|
||||
|
||||
|
||||
Semantic enrichment will be layered later.
|
||||
""");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String onSave(
|
||||
final LspProjectContext project,
|
||||
final LspProjectContext context,
|
||||
final String documentUri) {
|
||||
Objects.requireNonNull(project, "project");
|
||||
Objects.requireNonNull(context, "context");
|
||||
return "Prometeu Studio saw save: " + documentUri;
|
||||
}
|
||||
|
||||
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
package p.studio.lsp.services.host;
|
||||
|
||||
import p.studio.lsp.messages.LspServerEndpoint;
|
||||
|
||||
public interface ProjectScopedLspServerHost extends AutoCloseable {
|
||||
LspServerEndpoint start();
|
||||
|
||||
@Override
|
||||
void close() throws Exception;
|
||||
}
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
package p.studio.lsp.services.host;
|
||||
|
||||
import p.studio.lsp.messages.LspServerBootRequest;
|
||||
import p.studio.lsp.services.compiler.CompilerLanguageServiceBridge;
|
||||
|
||||
public interface ProjectScopedLspServerHostFactory {
|
||||
ProjectScopedLspServerHost create(
|
||||
LspServerBootRequest request,
|
||||
CompilerLanguageServiceBridge compilerBridge);
|
||||
}
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
package p.studio.lsp.services.host;
|
||||
|
||||
import p.studio.lsp.messages.LspServerBootRequest;
|
||||
import p.studio.lsp.services.compiler.CompilerLanguageServiceBridge;
|
||||
|
||||
public final class TcpLspServerHostFactory implements ProjectScopedLspServerHostFactory {
|
||||
@Override
|
||||
public ProjectScopedLspServerHost create(
|
||||
final LspServerBootRequest request,
|
||||
final CompilerLanguageServiceBridge compilerBridge) {
|
||||
return new TcpLspServerHost(request, compilerBridge);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user