implements PLN-0065
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
plugins {
|
||||
id("gradle.java-library-conventions")
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package p.studio.lsp.api;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
|
||||
public record LspProjectContext(
|
||||
String projectKey,
|
||||
String languageId,
|
||||
Path projectRoot) {
|
||||
|
||||
public LspProjectContext {
|
||||
projectKey = requireText(projectKey, "projectKey");
|
||||
languageId = requireText(languageId, "languageId");
|
||||
projectRoot = Objects.requireNonNull(projectRoot, "projectRoot").toAbsolutePath().normalize();
|
||||
}
|
||||
|
||||
private static String requireText(
|
||||
final String value,
|
||||
final String field) {
|
||||
final String candidate = Objects.requireNonNull(value, field).trim();
|
||||
if (candidate.isEmpty()) {
|
||||
throw new IllegalArgumentException(field + " must not be blank");
|
||||
}
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package p.studio.lsp.api;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record LspServerBootRequest(
|
||||
LspProjectContext project,
|
||||
LspServerConfiguration configuration) {
|
||||
|
||||
public LspServerBootRequest {
|
||||
project = Objects.requireNonNull(project, "project");
|
||||
configuration = Objects.requireNonNull(configuration, "configuration");
|
||||
}
|
||||
|
||||
public static LspServerBootRequest defaults(final LspProjectContext project) {
|
||||
return new LspServerBootRequest(project, LspServerConfiguration.defaults());
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package p.studio.lsp.api;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record LspServerConfiguration(
|
||||
String host,
|
||||
int port) {
|
||||
public static final String DEFAULT_HOST = "127.0.0.1";
|
||||
public static final int DEFAULT_PORT = 7777;
|
||||
|
||||
public LspServerConfiguration {
|
||||
host = requireHost(host);
|
||||
if (port < 0 || port > 65535) {
|
||||
throw new IllegalArgumentException("port must be between 0 and 65535");
|
||||
}
|
||||
}
|
||||
|
||||
public static LspServerConfiguration defaults() {
|
||||
return new LspServerConfiguration(DEFAULT_HOST, DEFAULT_PORT);
|
||||
}
|
||||
|
||||
private static String requireHost(final String value) {
|
||||
final String candidate = Objects.requireNonNull(value, "host").trim();
|
||||
if (candidate.isEmpty()) {
|
||||
throw new IllegalArgumentException("host must not be blank");
|
||||
}
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package p.studio.lsp.api;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record LspServerEndpoint(
|
||||
String host,
|
||||
int port) {
|
||||
|
||||
public LspServerEndpoint {
|
||||
final String candidate = Objects.requireNonNull(host, "host").trim();
|
||||
if (candidate.isEmpty()) {
|
||||
throw new IllegalArgumentException("host must not be blank");
|
||||
}
|
||||
if (port < 0 || port > 65535) {
|
||||
throw new IllegalArgumentException("port must be between 0 and 65535");
|
||||
}
|
||||
host = candidate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package p.studio.lsp.api;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record LspServerHandle(
|
||||
LspProjectContext project,
|
||||
LspServerEndpoint endpoint) {
|
||||
|
||||
public LspServerHandle {
|
||||
project = Objects.requireNonNull(project, "project");
|
||||
endpoint = Objects.requireNonNull(endpoint, "endpoint");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package p.studio.lsp.api;
|
||||
|
||||
public interface LspServerLifecycle {
|
||||
LspServerHandle bootServer(LspServerBootRequest request);
|
||||
|
||||
void shutdownServer(LspProjectContext project);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package p.studio.lsp.api;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class LspApiBoundaryTest {
|
||||
|
||||
@Test
|
||||
void apiSourceMustRemainFreeOfLsp4jAndProtocolPackages() throws IOException {
|
||||
final Path sourceRoot = Path.of("src/main/java");
|
||||
final List<Path> javaFiles;
|
||||
try (var stream = Files.walk(sourceRoot)) {
|
||||
javaFiles = stream
|
||||
.filter(path -> path.toString().endsWith(".java"))
|
||||
.toList();
|
||||
}
|
||||
|
||||
for (final Path javaFile : javaFiles) {
|
||||
final String source = Files.readString(javaFile);
|
||||
assertTrue(!source.contains("org.eclipse.lsp4j"),
|
||||
() -> "LSP4J leaked into lsp-api: " + javaFile);
|
||||
assertTrue(!source.contains(".protocol."),
|
||||
() -> "Protocol package leaked into lsp-api: " + javaFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package p.studio.lsp.api;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class LspServerBootRequestTest {
|
||||
|
||||
@Test
|
||||
void defaultsUseDeterministicLoopbackConfiguration() {
|
||||
final LspProjectContext project = new LspProjectContext("demo", "pbs", Path.of("."));
|
||||
|
||||
final LspServerBootRequest request = LspServerBootRequest.defaults(project);
|
||||
|
||||
assertEquals(project, request.project());
|
||||
assertEquals(LspServerConfiguration.DEFAULT_HOST, request.configuration().host());
|
||||
assertEquals(LspServerConfiguration.DEFAULT_PORT, request.configuration().port());
|
||||
}
|
||||
|
||||
@Test
|
||||
void configurationRejectsInvalidValues() {
|
||||
assertThrows(NullPointerException.class, () -> new LspServerConfiguration(null, 7777));
|
||||
assertThrows(IllegalArgumentException.class, () -> new LspServerConfiguration(" ", 7777));
|
||||
assertThrows(IllegalArgumentException.class, () -> new LspServerConfiguration("127.0.0.1", -1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void projectContextNormalizesAndRejectsBlankFields() {
|
||||
final LspProjectContext context = new LspProjectContext(" demo ", " pbs ", Path.of("."));
|
||||
|
||||
assertEquals("demo", context.projectKey());
|
||||
assertEquals("pbs", context.languageId());
|
||||
assertEquals(Path.of(".").toAbsolutePath().normalize(), context.projectRoot());
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> new LspProjectContext(" ", "pbs", Path.of(".")));
|
||||
assertThrows(IllegalArgumentException.class, () -> new LspProjectContext("demo", " ", Path.of(".")));
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ plugins {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(project(":prometeu-infra"))
|
||||
|
||||
implementation("org.eclipse.lsp4j:org.eclipse.lsp4j:1.0.0")
|
||||
}
|
||||
implementation(project(":prometeu-lsp:prometeu-lsp-api"))
|
||||
implementation(project(":prometeu-compiler:prometeu-build-pipeline"))
|
||||
implementation(libs.lsp4j)
|
||||
}
|
||||
|
||||
@@ -1,271 +0,0 @@
|
||||
package p.studio.lsp;
|
||||
|
||||
import org.eclipse.lsp4j.*;
|
||||
import org.eclipse.lsp4j.jsonrpc.Launcher;
|
||||
import org.eclipse.lsp4j.launch.LSPLauncher;
|
||||
import org.eclipse.lsp4j.services.*;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public final class PrometeuStudioLspMain {
|
||||
|
||||
private static final String HOST = "127.0.0.1";
|
||||
private static final int PORT = 7777;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
InetAddress bindAddress = InetAddress.getByName(HOST);
|
||||
|
||||
try (ServerSocket serverSocket = new ServerSocket(PORT, 50, bindAddress)) {
|
||||
System.out.println("Prometeu Studio LSP listening on " + HOST + ":" + PORT);
|
||||
|
||||
while (true) {
|
||||
Socket socket = serverSocket.accept();
|
||||
|
||||
Thread clientThread = new Thread(() -> handleClient(socket));
|
||||
clientThread.setName("prometeu-lsp-client");
|
||||
clientThread.setDaemon(true);
|
||||
clientThread.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void handleClient(Socket socket) {
|
||||
try {
|
||||
System.out.println("VS Code connected: " + socket.getRemoteSocketAddress());
|
||||
|
||||
InputStream input = socket.getInputStream();
|
||||
OutputStream output = socket.getOutputStream();
|
||||
|
||||
PrometeuLanguageServer server = new PrometeuLanguageServer();
|
||||
|
||||
Launcher<LanguageClient> launcher =
|
||||
LSPLauncher.createServerLauncher(server, input, output);
|
||||
|
||||
LanguageClient client = launcher.getRemoteProxy();
|
||||
server.connect(client);
|
||||
|
||||
launcher.startListening();
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("LSP client failed: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static final class PrometeuLanguageServer implements LanguageServer, LanguageClientAware {
|
||||
|
||||
private LanguageClient client;
|
||||
private final TextDocumentService textDocumentService = new PrometeuTextDocumentService(this);
|
||||
private final WorkspaceService workspaceService = new PrometeuWorkspaceService();
|
||||
|
||||
private boolean shutdownRequested = false;
|
||||
|
||||
@Override
|
||||
public void connect(LanguageClient client) {
|
||||
this.client = client;
|
||||
|
||||
if (textDocumentService instanceof PrometeuTextDocumentService service) {
|
||||
service.connect(client);
|
||||
}
|
||||
}
|
||||
|
||||
public LanguageClient client() {
|
||||
return client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
|
||||
ServerCapabilities capabilities = new ServerCapabilities();
|
||||
|
||||
TextDocumentSyncOptions syncOptions = new TextDocumentSyncOptions();
|
||||
syncOptions.setOpenClose(true);
|
||||
syncOptions.setChange(TextDocumentSyncKind.Full);
|
||||
|
||||
SaveOptions saveOptions = new SaveOptions();
|
||||
saveOptions.setIncludeText(true);
|
||||
syncOptions.setSave(saveOptions);
|
||||
|
||||
capabilities.setTextDocumentSync(syncOptions);
|
||||
capabilities.setHoverProvider(true);
|
||||
|
||||
InitializeResult result = new InitializeResult(capabilities);
|
||||
|
||||
ServerInfo serverInfo = new ServerInfo();
|
||||
serverInfo.setName("Prometeu Studio LSP");
|
||||
serverInfo.setVersion("0.1.0");
|
||||
result.setServerInfo(serverInfo);
|
||||
|
||||
return CompletableFuture.completedFuture(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialized(InitializedParams params) {
|
||||
if (client != null) {
|
||||
client.logMessage(new MessageParams(
|
||||
MessageType.Info,
|
||||
"Hello from Prometeu Studio LSP"
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Object> shutdown() {
|
||||
shutdownRequested = true;
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exit() {
|
||||
System.out.println("LSP client requested exit. shutdownRequested=" + shutdownRequested);
|
||||
|
||||
// Importantíssimo:
|
||||
// Como isso está dentro do Studio, NÃO use System.exit().
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextDocumentService getTextDocumentService() {
|
||||
return textDocumentService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WorkspaceService getWorkspaceService() {
|
||||
return workspaceService;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class PrometeuTextDocumentService implements TextDocumentService {
|
||||
|
||||
private final PrometeuLanguageServer server;
|
||||
private LanguageClient client;
|
||||
|
||||
private PrometeuTextDocumentService(PrometeuLanguageServer server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
private void connect(LanguageClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didOpen(DidOpenTextDocumentParams params) {
|
||||
String uri = params.getTextDocument().getUri();
|
||||
String text = params.getTextDocument().getText();
|
||||
|
||||
System.out.println("didOpen: " + uri);
|
||||
|
||||
publishHelloDiagnostic(uri, text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didChange(DidChangeTextDocumentParams params) {
|
||||
String uri = params.getTextDocument().getUri();
|
||||
|
||||
String text = "";
|
||||
if (!params.getContentChanges().isEmpty()) {
|
||||
text = params.getContentChanges().get(0).getText();
|
||||
}
|
||||
|
||||
System.out.println("didChange: " + uri);
|
||||
|
||||
publishHelloDiagnostic(uri, text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didClose(DidCloseTextDocumentParams params) {
|
||||
String uri = params.getTextDocument().getUri();
|
||||
|
||||
System.out.println("didClose: " + uri);
|
||||
|
||||
if (client != null) {
|
||||
client.publishDiagnostics(new PublishDiagnosticsParams(uri, List.of()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didSave(DidSaveTextDocumentParams params) {
|
||||
String uri = params.getTextDocument().getUri();
|
||||
|
||||
System.out.println("didSave: " + uri);
|
||||
|
||||
if (client != null) {
|
||||
client.logMessage(new MessageParams(
|
||||
MessageType.Info,
|
||||
"Prometeu Studio saw save: " + uri
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Hover> hover(HoverParams params) {
|
||||
MarkupContent content = new MarkupContent();
|
||||
content.setKind(MarkupKind.MARKDOWN);
|
||||
content.setValue("""
|
||||
**Prometeu Studio LSP**
|
||||
|
||||
Hello from Java + LSP4J.
|
||||
|
||||
This hover came from the Studio process.
|
||||
""");
|
||||
|
||||
Hover hover = new Hover();
|
||||
hover.setContents(content);
|
||||
|
||||
return CompletableFuture.completedFuture(hover);
|
||||
}
|
||||
|
||||
private void publishHelloDiagnostic(String uri, String text) {
|
||||
if (client == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Diagnostic diagnostic = new Diagnostic();
|
||||
|
||||
diagnostic.setRange(new Range(
|
||||
new Position(0, 0),
|
||||
new Position(0, Math.max(1, firstLineLength(text)))
|
||||
));
|
||||
|
||||
diagnostic.setSeverity(DiagnosticSeverity.Information);
|
||||
diagnostic.setSource("Prometeu Studio");
|
||||
diagnostic.setMessage("Hello from Prometeu Studio LSP");
|
||||
|
||||
client.publishDiagnostics(new PublishDiagnosticsParams(
|
||||
uri,
|
||||
List.of(diagnostic)
|
||||
));
|
||||
}
|
||||
|
||||
private int firstLineLength(String text) {
|
||||
if (text == null || text.isEmpty()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int newline = text.indexOf('\n');
|
||||
|
||||
if (newline < 0) {
|
||||
return text.length();
|
||||
}
|
||||
|
||||
return newline;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class PrometeuWorkspaceService implements WorkspaceService {
|
||||
|
||||
@Override
|
||||
public void didChangeConfiguration(DidChangeConfigurationParams params) {
|
||||
System.out.println("didChangeConfiguration");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didChangeWatchedFiles(DidChangeWatchedFilesParams params) {
|
||||
System.out.println("didChangeWatchedFiles: " + params.getChanges().size() + " changes");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package p.studio.lsp.v1;
|
||||
|
||||
import p.studio.lsp.api.LspProjectContext;
|
||||
import p.studio.lsp.api.LspServerBootRequest;
|
||||
import p.studio.lsp.v1.bootstrap.LspV1ServerLifecycle;
|
||||
import p.studio.lsp.v1.compiler.CompilerLanguageServiceBridge;
|
||||
import p.studio.lsp.v1.host.TcpLspServerHostFactory;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
public final class PrometeuStudioLspMain {
|
||||
private PrometeuStudioLspMain() {
|
||||
}
|
||||
|
||||
public static void main(final String[] args) throws Exception {
|
||||
final var lifecycle = new LspV1ServerLifecycle(
|
||||
new TcpLspServerHostFactory(),
|
||||
new CompilerLanguageServiceBridge());
|
||||
|
||||
final var request = LspServerBootRequest.defaults(new LspProjectContext(
|
||||
"standalone-mock",
|
||||
"pbs",
|
||||
Path.of(".")));
|
||||
|
||||
final var handle = lifecycle.bootServer(request);
|
||||
System.out.println("Prometeu Studio LSP listening on " + handle.endpoint().host() + ":" + handle.endpoint().port());
|
||||
Thread.currentThread().join();
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package p.studio.lsp.v1.bootstrap;
|
||||
|
||||
import p.studio.lsp.api.LspProjectContext;
|
||||
import p.studio.lsp.api.LspServerBootRequest;
|
||||
import p.studio.lsp.api.LspServerHandle;
|
||||
import p.studio.lsp.api.LspServerLifecycle;
|
||||
import p.studio.lsp.v1.compiler.CompilerLanguageServiceBridge;
|
||||
import p.studio.lsp.v1.host.ProjectScopedLspServerHost;
|
||||
import p.studio.lsp.v1.host.ProjectScopedLspServerHostFactory;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
public final class LspV1ServerLifecycle implements LspServerLifecycle {
|
||||
private final ProjectScopedLspServerHostFactory serverHostFactory;
|
||||
private final CompilerLanguageServiceBridge compilerBridge;
|
||||
private final ConcurrentMap<String, ProjectScopedLspServerHost> activeServers;
|
||||
|
||||
public LspV1ServerLifecycle(
|
||||
final ProjectScopedLspServerHostFactory serverHostFactory,
|
||||
final CompilerLanguageServiceBridge compilerBridge) {
|
||||
this.serverHostFactory = Objects.requireNonNull(serverHostFactory, "serverHostFactory");
|
||||
this.compilerBridge = Objects.requireNonNull(compilerBridge, "compilerBridge");
|
||||
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);
|
||||
if (previous != null) {
|
||||
throw new IllegalStateException("LSP server already booted for project " + safeRequest.project().projectKey());
|
||||
}
|
||||
|
||||
try {
|
||||
final var endpoint = host.start();
|
||||
return new LspServerHandle(safeRequest.project(), endpoint);
|
||||
} catch (RuntimeException runtimeException) {
|
||||
activeServers.remove(safeRequest.project().projectKey(), host);
|
||||
closeQuietly(host);
|
||||
throw runtimeException;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdownServer(final LspProjectContext project) {
|
||||
final LspProjectContext safeProject = Objects.requireNonNull(project, "project");
|
||||
final ProjectScopedLspServerHost host = activeServers.remove(safeProject.projectKey());
|
||||
if (host == null) {
|
||||
return;
|
||||
}
|
||||
closeQuietly(host);
|
||||
}
|
||||
|
||||
private static void closeQuietly(final ProjectScopedLspServerHost host) {
|
||||
try {
|
||||
host.close();
|
||||
} catch (Exception exception) {
|
||||
throw new IllegalStateException("failed to close LSP host", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package p.studio.lsp.v1.compiler;
|
||||
|
||||
import p.studio.compiler.workspaces.BuilderPipelineService;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public final class CompilerLanguageServiceBridge {
|
||||
private final BuilderPipelineService builderPipelineService;
|
||||
|
||||
public CompilerLanguageServiceBridge() {
|
||||
this(BuilderPipelineService.INSTANCE);
|
||||
}
|
||||
|
||||
public CompilerLanguageServiceBridge(final BuilderPipelineService builderPipelineService) {
|
||||
this.builderPipelineService = Objects.requireNonNull(builderPipelineService, "builderPipelineService");
|
||||
}
|
||||
|
||||
public BuilderPipelineService builderPipelineService() {
|
||||
return builderPipelineService;
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package p.studio.lsp.v1.host;
|
||||
|
||||
import p.studio.lsp.api.LspServerEndpoint;
|
||||
|
||||
public interface ProjectScopedLspServerHost extends AutoCloseable {
|
||||
LspServerEndpoint start();
|
||||
|
||||
@Override
|
||||
void close() throws Exception;
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package p.studio.lsp.v1.host;
|
||||
|
||||
import p.studio.lsp.api.LspServerBootRequest;
|
||||
import p.studio.lsp.v1.compiler.CompilerLanguageServiceBridge;
|
||||
|
||||
public interface ProjectScopedLspServerHostFactory {
|
||||
ProjectScopedLspServerHost create(
|
||||
LspServerBootRequest request,
|
||||
CompilerLanguageServiceBridge compilerBridge);
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package p.studio.lsp.v1.host;
|
||||
|
||||
import org.eclipse.lsp4j.jsonrpc.Launcher;
|
||||
import org.eclipse.lsp4j.launch.LSPLauncher;
|
||||
import org.eclipse.lsp4j.services.LanguageClient;
|
||||
import p.studio.lsp.api.LspServerBootRequest;
|
||||
import p.studio.lsp.api.LspServerEndpoint;
|
||||
import p.studio.lsp.v1.compiler.CompilerLanguageServiceBridge;
|
||||
import p.studio.lsp.v1.protocol.PrometeuLanguageServer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public final class TcpLspServerHost implements ProjectScopedLspServerHost {
|
||||
private final LspServerBootRequest request;
|
||||
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 CompilerLanguageServiceBridge compilerBridge) {
|
||||
this.request = Objects.requireNonNull(request, "request");
|
||||
this.compilerBridge = Objects.requireNonNull(compilerBridge, "compilerBridge");
|
||||
this.clientExecutor = Executors.newCachedThreadPool(runnable -> {
|
||||
final Thread thread = new Thread(runnable);
|
||||
thread.setName("prometeu-lsp-v1-client");
|
||||
thread.setDaemon(true);
|
||||
return thread;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized LspServerEndpoint start() {
|
||||
if (endpoint != null) {
|
||||
return endpoint;
|
||||
}
|
||||
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());
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private void acceptLoop() {
|
||||
while (!closed) {
|
||||
try {
|
||||
final Socket socket = serverSocket.accept();
|
||||
clientExecutor.submit(() -> handleClient(socket));
|
||||
} catch (IOException exception) {
|
||||
if (!closed) {
|
||||
throw new IllegalStateException("failed to accept LSP client", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void handleClient(final Socket socket) {
|
||||
try (socket) {
|
||||
final var server = new PrometeuLanguageServer(request.project(), compilerBridge);
|
||||
final Launcher<LanguageClient> launcher = LSPLauncher.createServerLauncher(
|
||||
server,
|
||||
socket.getInputStream(),
|
||||
socket.getOutputStream());
|
||||
server.connect(launcher.getRemoteProxy());
|
||||
launcher.startListening().get();
|
||||
} catch (Exception exception) {
|
||||
throw new IllegalStateException("LSP client failed", exception);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void close() throws Exception {
|
||||
if (closed) {
|
||||
return;
|
||||
}
|
||||
closed = true;
|
||||
if (serverSocket != null) {
|
||||
serverSocket.close();
|
||||
}
|
||||
if (acceptThread != null) {
|
||||
acceptThread.interrupt();
|
||||
}
|
||||
clientExecutor.shutdownNow();
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package p.studio.lsp.v1.host;
|
||||
|
||||
import p.studio.lsp.api.LspServerBootRequest;
|
||||
import p.studio.lsp.v1.compiler.CompilerLanguageServiceBridge;
|
||||
|
||||
public final class TcpLspServerHostFactory implements ProjectScopedLspServerHostFactory {
|
||||
@Override
|
||||
public ProjectScopedLspServerHost create(
|
||||
final LspServerBootRequest request,
|
||||
final CompilerLanguageServiceBridge compilerBridge) {
|
||||
return new TcpLspServerHost(request, compilerBridge);
|
||||
}
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package p.studio.lsp.v1.protocol;
|
||||
|
||||
import org.eclipse.lsp4j.*;
|
||||
import org.eclipse.lsp4j.services.LanguageClient;
|
||||
import org.eclipse.lsp4j.services.LanguageClientAware;
|
||||
import org.eclipse.lsp4j.services.LanguageServer;
|
||||
import org.eclipse.lsp4j.services.TextDocumentService;
|
||||
import org.eclipse.lsp4j.services.WorkspaceService;
|
||||
import p.studio.lsp.api.LspProjectContext;
|
||||
import p.studio.lsp.v1.compiler.CompilerLanguageServiceBridge;
|
||||
import p.studio.lsp.v1.protocol.mapping.ProtocolServerProfile;
|
||||
import p.studio.lsp.v1.protocol.mapping.ServerCapabilitiesMapper;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public final class PrometeuLanguageServer implements LanguageServer, LanguageClientAware {
|
||||
private final LspProjectContext project;
|
||||
private final CompilerLanguageServiceBridge compilerBridge;
|
||||
private final ServerCapabilitiesMapper capabilitiesMapper;
|
||||
private final TextDocumentService textDocumentService;
|
||||
private final WorkspaceService workspaceService;
|
||||
private volatile LanguageClient client;
|
||||
private volatile boolean shutdownRequested;
|
||||
|
||||
public PrometeuLanguageServer(
|
||||
final LspProjectContext project,
|
||||
final CompilerLanguageServiceBridge compilerBridge) {
|
||||
this(project,
|
||||
compilerBridge,
|
||||
new ServerCapabilitiesMapper(),
|
||||
new PrometeuTextDocumentService(),
|
||||
new PrometeuWorkspaceService());
|
||||
}
|
||||
|
||||
PrometeuLanguageServer(
|
||||
final LspProjectContext project,
|
||||
final CompilerLanguageServiceBridge compilerBridge,
|
||||
final ServerCapabilitiesMapper capabilitiesMapper,
|
||||
final TextDocumentService textDocumentService,
|
||||
final WorkspaceService workspaceService) {
|
||||
this.project = Objects.requireNonNull(project, "project");
|
||||
this.compilerBridge = Objects.requireNonNull(compilerBridge, "compilerBridge");
|
||||
this.capabilitiesMapper = Objects.requireNonNull(capabilitiesMapper, "capabilitiesMapper");
|
||||
this.textDocumentService = Objects.requireNonNull(textDocumentService, "textDocumentService");
|
||||
this.workspaceService = Objects.requireNonNull(workspaceService, "workspaceService");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect(final LanguageClient client) {
|
||||
this.client = client;
|
||||
if (textDocumentService instanceof PrometeuTextDocumentService service) {
|
||||
service.connect(client);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<InitializeResult> initialize(final InitializeParams params) {
|
||||
final ProtocolServerProfile profile = ProtocolServerProfile.defaults();
|
||||
return CompletableFuture.completedFuture(capabilitiesMapper.map(profile));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialized(final InitializedParams params) {
|
||||
final LanguageClient currentClient = client;
|
||||
if (currentClient == null) {
|
||||
return;
|
||||
}
|
||||
currentClient.logMessage(new MessageParams(
|
||||
MessageType.Info,
|
||||
"Prometeu Studio LSP connected for project " + project.projectKey()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Object> shutdown() {
|
||||
shutdownRequested = true;
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exit() {
|
||||
if (!shutdownRequested) {
|
||||
final LanguageClient currentClient = client;
|
||||
if (currentClient != null) {
|
||||
currentClient.logMessage(new MessageParams(
|
||||
MessageType.Warning,
|
||||
"Prometeu Studio LSP received exit before shutdown"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextDocumentService getTextDocumentService() {
|
||||
return textDocumentService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WorkspaceService getWorkspaceService() {
|
||||
return workspaceService;
|
||||
}
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
package p.studio.lsp.v1.protocol;
|
||||
|
||||
import org.eclipse.lsp4j.*;
|
||||
import org.eclipse.lsp4j.services.LanguageClient;
|
||||
import org.eclipse.lsp4j.services.TextDocumentService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public final class PrometeuTextDocumentService implements TextDocumentService {
|
||||
private volatile LanguageClient client;
|
||||
|
||||
void connect(final LanguageClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didOpen(final DidOpenTextDocumentParams params) {
|
||||
publishHelloDiagnostic(params.getTextDocument().getUri(), params.getTextDocument().getText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didChange(final DidChangeTextDocumentParams params) {
|
||||
final String text = params.getContentChanges().isEmpty()
|
||||
? ""
|
||||
: params.getContentChanges().getFirst().getText();
|
||||
publishHelloDiagnostic(params.getTextDocument().getUri(), text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didClose(final DidCloseTextDocumentParams params) {
|
||||
final LanguageClient currentClient = client;
|
||||
if (currentClient == null) {
|
||||
return;
|
||||
}
|
||||
currentClient.publishDiagnostics(new PublishDiagnosticsParams(params.getTextDocument().getUri(), List.of()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didSave(final DidSaveTextDocumentParams params) {
|
||||
final LanguageClient currentClient = client;
|
||||
if (currentClient == null) {
|
||||
return;
|
||||
}
|
||||
currentClient.logMessage(new MessageParams(
|
||||
MessageType.Info,
|
||||
"Prometeu Studio saw save: " + params.getTextDocument().getUri()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Hover> hover(final HoverParams params) {
|
||||
final MarkupContent content = new MarkupContent();
|
||||
content.setKind(MarkupKind.MARKDOWN);
|
||||
content.setValue("""
|
||||
**Prometeu Studio LSP**
|
||||
|
||||
Structured baseline server.
|
||||
|
||||
Semantic enrichment will be layered later.
|
||||
""");
|
||||
|
||||
final Hover hover = new Hover();
|
||||
hover.setContents(content);
|
||||
return CompletableFuture.completedFuture(hover);
|
||||
}
|
||||
|
||||
private void publishHelloDiagnostic(
|
||||
final String uri,
|
||||
final String text) {
|
||||
final LanguageClient currentClient = client;
|
||||
if (currentClient == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Diagnostic diagnostic = new Diagnostic();
|
||||
diagnostic.setRange(new Range(
|
||||
new Position(0, 0),
|
||||
new Position(0, Math.max(1, firstLineLength(text)))));
|
||||
diagnostic.setSeverity(DiagnosticSeverity.Information);
|
||||
diagnostic.setSource("Prometeu Studio");
|
||||
diagnostic.setMessage("Structured baseline LSP response");
|
||||
|
||||
currentClient.publishDiagnostics(new PublishDiagnosticsParams(uri, List.of(diagnostic)));
|
||||
}
|
||||
|
||||
private int firstLineLength(final String text) {
|
||||
if (text == null || text.isEmpty()) {
|
||||
return 1;
|
||||
}
|
||||
final int newline = text.indexOf('\n');
|
||||
return newline < 0 ? text.length() : newline;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package p.studio.lsp.v1.protocol;
|
||||
|
||||
import org.eclipse.lsp4j.DidChangeConfigurationParams;
|
||||
import org.eclipse.lsp4j.DidChangeWatchedFilesParams;
|
||||
import org.eclipse.lsp4j.services.WorkspaceService;
|
||||
|
||||
public final class PrometeuWorkspaceService implements WorkspaceService {
|
||||
@Override
|
||||
public void didChangeConfiguration(final DidChangeConfigurationParams params) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didChangeWatchedFiles(final DidChangeWatchedFilesParams params) {
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package p.studio.lsp.v1.protocol.mapping;
|
||||
|
||||
public record ProtocolServerProfile(
|
||||
String name,
|
||||
String version,
|
||||
boolean hoverSupported) {
|
||||
|
||||
public static ProtocolServerProfile defaults() {
|
||||
return new ProtocolServerProfile("Prometeu Studio LSP", "0.1.0", true);
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package p.studio.lsp.v1.protocol.mapping;
|
||||
|
||||
import org.eclipse.lsp4j.*;
|
||||
|
||||
public final class ServerCapabilitiesMapper {
|
||||
public InitializeResult map(final ProtocolServerProfile profile) {
|
||||
final ServerCapabilities capabilities = new ServerCapabilities();
|
||||
|
||||
final TextDocumentSyncOptions syncOptions = new TextDocumentSyncOptions();
|
||||
syncOptions.setOpenClose(true);
|
||||
syncOptions.setChange(TextDocumentSyncKind.Full);
|
||||
|
||||
final SaveOptions saveOptions = new SaveOptions();
|
||||
saveOptions.setIncludeText(true);
|
||||
syncOptions.setSave(saveOptions);
|
||||
|
||||
capabilities.setTextDocumentSync(syncOptions);
|
||||
capabilities.setHoverProvider(profile.hoverSupported());
|
||||
|
||||
final InitializeResult result = new InitializeResult(capabilities);
|
||||
final ServerInfo serverInfo = new ServerInfo();
|
||||
serverInfo.setName(profile.name());
|
||||
serverInfo.setVersion(profile.version());
|
||||
result.setServerInfo(serverInfo);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package p.studio.lsp.v1.bootstrap;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.lsp.api.LspProjectContext;
|
||||
import p.studio.lsp.api.LspServerBootRequest;
|
||||
import p.studio.lsp.api.LspServerEndpoint;
|
||||
import p.studio.lsp.v1.compiler.CompilerLanguageServiceBridge;
|
||||
import p.studio.lsp.v1.host.ProjectScopedLspServerHost;
|
||||
import p.studio.lsp.v1.host.ProjectScopedLspServerHostFactory;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class LspV1ServerLifecycleTest {
|
||||
|
||||
@Test
|
||||
void lifecycleBootsAndShutsDownProjectScopedHost() {
|
||||
final FakeHost host = new FakeHost();
|
||||
final ProjectScopedLspServerHostFactory factory = (request, compilerBridge) -> host;
|
||||
final var lifecycle = new LspV1ServerLifecycle(factory, new CompilerLanguageServiceBridge());
|
||||
final var project = new LspProjectContext("demo", "pbs", Path.of("."));
|
||||
|
||||
final var handle = lifecycle.bootServer(LspServerBootRequest.defaults(project));
|
||||
|
||||
assertEquals(project, handle.project());
|
||||
assertEquals("127.0.0.1", handle.endpoint().host());
|
||||
assertEquals(7777, handle.endpoint().port());
|
||||
assertTrue(host.started);
|
||||
|
||||
lifecycle.shutdownServer(project);
|
||||
assertTrue(host.closed);
|
||||
}
|
||||
|
||||
@Test
|
||||
void lifecycleRejectsDoubleBootForSameProject() {
|
||||
final ProjectScopedLspServerHostFactory factory = (request, compilerBridge) -> new FakeHost();
|
||||
final var lifecycle = new LspV1ServerLifecycle(factory, new CompilerLanguageServiceBridge());
|
||||
final var request = LspServerBootRequest.defaults(new LspProjectContext("demo", "pbs", Path.of(".")));
|
||||
|
||||
lifecycle.bootServer(request);
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> lifecycle.bootServer(request));
|
||||
}
|
||||
|
||||
private static final class FakeHost implements ProjectScopedLspServerHost {
|
||||
private boolean started;
|
||||
private boolean closed;
|
||||
|
||||
@Override
|
||||
public LspServerEndpoint start() {
|
||||
started = true;
|
||||
return new LspServerEndpoint("127.0.0.1", 7777);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
closed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package p.studio.lsp.v1.boundary;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class LspV1BoundaryTest {
|
||||
|
||||
@Test
|
||||
void lsp4jMustRemainContainedInsideLspV1Sources() throws IOException {
|
||||
final List<Path> forbiddenRoots = List.of(
|
||||
Path.of("../prometeu-lsp-api/src/main/java"),
|
||||
Path.of("../../../prometeu-studio/src/main/java"),
|
||||
Path.of("../../../prometeu-app/src/main/java"),
|
||||
Path.of("../../../prometeu-compiler"),
|
||||
Path.of("../../../prometeu-packer"),
|
||||
Path.of("../../../prometeu-infra"));
|
||||
|
||||
for (final Path root : forbiddenRoots) {
|
||||
assertNoLsp4jImports(root.normalize());
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertNoLsp4jImports(final Path root) throws IOException {
|
||||
if (!Files.exists(root)) {
|
||||
return;
|
||||
}
|
||||
final List<Path> javaFiles;
|
||||
try (var stream = Files.walk(root)) {
|
||||
javaFiles = stream
|
||||
.filter(path -> path.toString().endsWith(".java"))
|
||||
.toList();
|
||||
}
|
||||
|
||||
for (final Path javaFile : javaFiles) {
|
||||
final String source = Files.readString(javaFile);
|
||||
assertTrue(!source.contains("org.eclipse.lsp4j"),
|
||||
() -> "LSP4J leaked outside lsp-v1: " + javaFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user