add bare bones for a lsp
This commit is contained in:
@@ -0,0 +1,271 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user