implements PLN-0065
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user