implements PLN-0067
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
package p.studio.lsp.v1.compiler;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public record BaselineDocumentAnalysis(
|
||||
List<BaselineDocumentIssue> issues) {
|
||||
|
||||
public BaselineDocumentAnalysis {
|
||||
issues = List.copyOf(Objects.requireNonNull(issues, "issues"));
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package p.studio.lsp.v1.compiler;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record BaselineDocumentIssue(
|
||||
int startLine,
|
||||
int startCharacter,
|
||||
int endLine,
|
||||
int endCharacter,
|
||||
BaselineIssueSeverity severity,
|
||||
String source,
|
||||
String message) {
|
||||
|
||||
public BaselineDocumentIssue {
|
||||
severity = Objects.requireNonNull(severity, "severity");
|
||||
source = requireText(source, "source");
|
||||
message = requireText(message, "message");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package p.studio.lsp.v1.compiler;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record BaselineHover(
|
||||
String markdown) {
|
||||
|
||||
public BaselineHover {
|
||||
final String candidate = Objects.requireNonNull(markdown, "markdown").trim();
|
||||
if (candidate.isEmpty()) {
|
||||
throw new IllegalArgumentException("markdown must not be blank");
|
||||
}
|
||||
markdown = candidate;
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package p.studio.lsp.v1.compiler;
|
||||
|
||||
public enum BaselineIssueSeverity {
|
||||
INFORMATION,
|
||||
WARNING,
|
||||
ERROR
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package p.studio.lsp.v1.compiler;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record BaselineServerDescription(
|
||||
String name,
|
||||
String version,
|
||||
boolean hoverSupported) {
|
||||
|
||||
public BaselineServerDescription {
|
||||
name = requireText(name, "name");
|
||||
version = requireText(version, "version");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
+61
-1
@@ -1,10 +1,11 @@
|
||||
package p.studio.lsp.v1.compiler;
|
||||
|
||||
import p.studio.compiler.workspaces.BuilderPipelineService;
|
||||
import p.studio.lsp.api.LspProjectContext;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public final class CompilerLanguageServiceBridge {
|
||||
public final class CompilerLanguageServiceBridge implements LanguageServiceBridge {
|
||||
private final BuilderPipelineService builderPipelineService;
|
||||
|
||||
public CompilerLanguageServiceBridge() {
|
||||
@@ -18,4 +19,63 @@ public final class CompilerLanguageServiceBridge {
|
||||
public BuilderPipelineService builderPipelineService() {
|
||||
return builderPipelineService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaselineServerDescription describeServer(final LspProjectContext project) {
|
||||
Objects.requireNonNull(project, "project");
|
||||
return new BaselineServerDescription(
|
||||
"Prometeu Studio LSP",
|
||||
"0.1.0",
|
||||
true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaselineDocumentAnalysis analyzeDocument(
|
||||
final LspProjectContext project,
|
||||
final String documentUri,
|
||||
final String text) {
|
||||
Objects.requireNonNull(project, "project");
|
||||
// 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(
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
Math.max(1, firstLineLength(text)),
|
||||
BaselineIssueSeverity.INFORMATION,
|
||||
"Prometeu Studio",
|
||||
message)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaselineHover hover(
|
||||
final LspProjectContext project,
|
||||
final String documentUri,
|
||||
final int line,
|
||||
final int character) {
|
||||
Objects.requireNonNull(project, "project");
|
||||
return new BaselineHover("""
|
||||
**Prometeu Studio LSP**
|
||||
|
||||
Compiler-backed baseline.
|
||||
|
||||
Semantic enrichment will be layered later.
|
||||
""");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String onSave(
|
||||
final LspProjectContext project,
|
||||
final String documentUri) {
|
||||
Objects.requireNonNull(project, "project");
|
||||
return "Prometeu Studio saw save: " + documentUri;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package p.studio.lsp.v1.compiler;
|
||||
|
||||
import p.studio.lsp.api.LspProjectContext;
|
||||
|
||||
public interface LanguageServiceBridge {
|
||||
BaselineServerDescription describeServer(LspProjectContext project);
|
||||
|
||||
BaselineDocumentAnalysis analyzeDocument(LspProjectContext project, String documentUri, String text);
|
||||
|
||||
BaselineHover hover(LspProjectContext project, String documentUri, int line, int character);
|
||||
|
||||
String onSave(LspProjectContext project, String documentUri);
|
||||
}
|
||||
+18
-17
@@ -8,16 +8,17 @@ 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 p.studio.lsp.v1.compiler.LanguageServiceBridge;
|
||||
import p.studio.lsp.v1.protocol.mapping.Lsp4jProtocolMessageMapper;
|
||||
import p.studio.lsp.v1.protocol.mapping.ProtocolMessageMapper;
|
||||
|
||||
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 LanguageServiceBridge languageServiceBridge;
|
||||
private final ProtocolMessageMapper protocolMessageMapper;
|
||||
private final TextDocumentService textDocumentService;
|
||||
private final WorkspaceService workspaceService;
|
||||
private volatile LanguageClient client;
|
||||
@@ -28,21 +29,23 @@ public final class PrometeuLanguageServer implements LanguageServer, LanguageCli
|
||||
final CompilerLanguageServiceBridge compilerBridge) {
|
||||
this(project,
|
||||
compilerBridge,
|
||||
new ServerCapabilitiesMapper(),
|
||||
new PrometeuTextDocumentService(),
|
||||
new Lsp4jProtocolMessageMapper(),
|
||||
null,
|
||||
new PrometeuWorkspaceService());
|
||||
}
|
||||
|
||||
PrometeuLanguageServer(
|
||||
final LspProjectContext project,
|
||||
final CompilerLanguageServiceBridge compilerBridge,
|
||||
final ServerCapabilitiesMapper capabilitiesMapper,
|
||||
final LanguageServiceBridge languageServiceBridge,
|
||||
final ProtocolMessageMapper protocolMessageMapper,
|
||||
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.languageServiceBridge = Objects.requireNonNull(languageServiceBridge, "languageServiceBridge");
|
||||
this.protocolMessageMapper = Objects.requireNonNull(protocolMessageMapper, "protocolMessageMapper");
|
||||
this.textDocumentService = textDocumentService != null
|
||||
? textDocumentService
|
||||
: new PrometeuTextDocumentService(project, languageServiceBridge, protocolMessageMapper);
|
||||
this.workspaceService = Objects.requireNonNull(workspaceService, "workspaceService");
|
||||
}
|
||||
|
||||
@@ -56,8 +59,8 @@ public final class PrometeuLanguageServer implements LanguageServer, LanguageCli
|
||||
|
||||
@Override
|
||||
public CompletableFuture<InitializeResult> initialize(final InitializeParams params) {
|
||||
final ProtocolServerProfile profile = ProtocolServerProfile.defaults();
|
||||
return CompletableFuture.completedFuture(capabilitiesMapper.map(profile));
|
||||
return CompletableFuture.completedFuture(protocolMessageMapper.mapInitializeResult(
|
||||
languageServiceBridge.describeServer(project)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -66,8 +69,7 @@ public final class PrometeuLanguageServer implements LanguageServer, LanguageCli
|
||||
if (currentClient == null) {
|
||||
return;
|
||||
}
|
||||
currentClient.logMessage(new MessageParams(
|
||||
MessageType.Info,
|
||||
currentClient.logMessage(protocolMessageMapper.mapInfoMessage(
|
||||
"Prometeu Studio LSP connected for project " + project.projectKey()));
|
||||
}
|
||||
|
||||
@@ -82,8 +84,7 @@ public final class PrometeuLanguageServer implements LanguageServer, LanguageCli
|
||||
if (!shutdownRequested) {
|
||||
final LanguageClient currentClient = client;
|
||||
if (currentClient != null) {
|
||||
currentClient.logMessage(new MessageParams(
|
||||
MessageType.Warning,
|
||||
currentClient.logMessage(new MessageParams(MessageType.Warning,
|
||||
"Prometeu Studio LSP received exit before shutdown"));
|
||||
}
|
||||
}
|
||||
|
||||
+30
-39
@@ -3,20 +3,35 @@ package p.studio.lsp.v1.protocol;
|
||||
import org.eclipse.lsp4j.*;
|
||||
import org.eclipse.lsp4j.services.LanguageClient;
|
||||
import org.eclipse.lsp4j.services.TextDocumentService;
|
||||
import p.studio.lsp.api.LspProjectContext;
|
||||
import p.studio.lsp.v1.compiler.LanguageServiceBridge;
|
||||
import p.studio.lsp.v1.protocol.mapping.ProtocolMessageMapper;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public final class PrometeuTextDocumentService implements TextDocumentService {
|
||||
private final LspProjectContext project;
|
||||
private final LanguageServiceBridge languageServiceBridge;
|
||||
private final ProtocolMessageMapper protocolMessageMapper;
|
||||
private volatile LanguageClient client;
|
||||
|
||||
public PrometeuTextDocumentService(
|
||||
final LspProjectContext project,
|
||||
final LanguageServiceBridge languageServiceBridge,
|
||||
final ProtocolMessageMapper protocolMessageMapper) {
|
||||
this.project = Objects.requireNonNull(project, "project");
|
||||
this.languageServiceBridge = Objects.requireNonNull(languageServiceBridge, "languageServiceBridge");
|
||||
this.protocolMessageMapper = Objects.requireNonNull(protocolMessageMapper, "protocolMessageMapper");
|
||||
}
|
||||
|
||||
void connect(final LanguageClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void didOpen(final DidOpenTextDocumentParams params) {
|
||||
publishHelloDiagnostic(params.getTextDocument().getUri(), params.getTextDocument().getText());
|
||||
publishAnalysis(params.getTextDocument().getUri(), params.getTextDocument().getText());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -24,7 +39,7 @@ public final class PrometeuTextDocumentService implements TextDocumentService {
|
||||
final String text = params.getContentChanges().isEmpty()
|
||||
? ""
|
||||
: params.getContentChanges().getFirst().getText();
|
||||
publishHelloDiagnostic(params.getTextDocument().getUri(), text);
|
||||
publishAnalysis(params.getTextDocument().getUri(), text);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -33,7 +48,7 @@ public final class PrometeuTextDocumentService implements TextDocumentService {
|
||||
if (currentClient == null) {
|
||||
return;
|
||||
}
|
||||
currentClient.publishDiagnostics(new PublishDiagnosticsParams(params.getTextDocument().getUri(), List.of()));
|
||||
currentClient.publishDiagnostics(protocolMessageMapper.emptyDiagnostics(params.getTextDocument().getUri()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -42,52 +57,28 @@ public final class PrometeuTextDocumentService implements TextDocumentService {
|
||||
if (currentClient == null) {
|
||||
return;
|
||||
}
|
||||
currentClient.logMessage(new MessageParams(
|
||||
MessageType.Info,
|
||||
"Prometeu Studio saw save: " + params.getTextDocument().getUri()));
|
||||
currentClient.logMessage(protocolMessageMapper.mapInfoMessage(
|
||||
languageServiceBridge.onSave(project, 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);
|
||||
return CompletableFuture.completedFuture(protocolMessageMapper.mapHover(languageServiceBridge.hover(
|
||||
project,
|
||||
params.getTextDocument().getUri(),
|
||||
params.getPosition().getLine(),
|
||||
params.getPosition().getCharacter())));
|
||||
}
|
||||
|
||||
private void publishHelloDiagnostic(
|
||||
private void publishAnalysis(
|
||||
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;
|
||||
currentClient.publishDiagnostics(protocolMessageMapper.mapDiagnostics(
|
||||
uri,
|
||||
languageServiceBridge.analyzeDocument(project, uri, text)));
|
||||
}
|
||||
}
|
||||
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package p.studio.lsp.v1.protocol.mapping;
|
||||
|
||||
import org.eclipse.lsp4j.*;
|
||||
import p.studio.lsp.v1.compiler.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public final class Lsp4jProtocolMessageMapper implements ProtocolMessageMapper {
|
||||
@Override
|
||||
public InitializeResult mapInitializeResult(final BaselineServerDescription description) {
|
||||
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(description.hoverSupported());
|
||||
|
||||
final InitializeResult result = new InitializeResult(capabilities);
|
||||
final ServerInfo serverInfo = new ServerInfo();
|
||||
serverInfo.setName(description.name());
|
||||
serverInfo.setVersion(description.version());
|
||||
result.setServerInfo(serverInfo);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PublishDiagnosticsParams mapDiagnostics(
|
||||
final String uri,
|
||||
final BaselineDocumentAnalysis analysis) {
|
||||
return new PublishDiagnosticsParams(uri, analysis.issues().stream().map(this::mapDiagnostic).toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public PublishDiagnosticsParams emptyDiagnostics(final String uri) {
|
||||
return new PublishDiagnosticsParams(uri, List.of());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Hover mapHover(final BaselineHover hover) {
|
||||
final MarkupContent content = new MarkupContent();
|
||||
content.setKind(MarkupKind.MARKDOWN);
|
||||
content.setValue(hover.markdown());
|
||||
|
||||
final Hover result = new Hover();
|
||||
result.setContents(content);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageParams mapInfoMessage(final String message) {
|
||||
return new MessageParams(MessageType.Info, message);
|
||||
}
|
||||
|
||||
private Diagnostic mapDiagnostic(final BaselineDocumentIssue issue) {
|
||||
final Diagnostic diagnostic = new Diagnostic();
|
||||
diagnostic.setRange(new Range(
|
||||
new Position(issue.startLine(), issue.startCharacter()),
|
||||
new Position(issue.endLine(), issue.endCharacter())));
|
||||
diagnostic.setSeverity(mapSeverity(issue.severity()));
|
||||
diagnostic.setSource(issue.source());
|
||||
diagnostic.setMessage(issue.message());
|
||||
return diagnostic;
|
||||
}
|
||||
|
||||
private DiagnosticSeverity mapSeverity(final BaselineIssueSeverity severity) {
|
||||
return switch (severity) {
|
||||
case INFORMATION -> DiagnosticSeverity.Information;
|
||||
case WARNING -> DiagnosticSeverity.Warning;
|
||||
case ERROR -> DiagnosticSeverity.Error;
|
||||
};
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package p.studio.lsp.v1.protocol.mapping;
|
||||
|
||||
import org.eclipse.lsp4j.Hover;
|
||||
import org.eclipse.lsp4j.InitializeResult;
|
||||
import org.eclipse.lsp4j.MessageParams;
|
||||
import org.eclipse.lsp4j.PublishDiagnosticsParams;
|
||||
import p.studio.lsp.v1.compiler.BaselineDocumentAnalysis;
|
||||
import p.studio.lsp.v1.compiler.BaselineHover;
|
||||
import p.studio.lsp.v1.compiler.BaselineServerDescription;
|
||||
|
||||
public interface ProtocolMessageMapper {
|
||||
InitializeResult mapInitializeResult(BaselineServerDescription description);
|
||||
|
||||
PublishDiagnosticsParams mapDiagnostics(String uri, BaselineDocumentAnalysis analysis);
|
||||
|
||||
PublishDiagnosticsParams emptyDiagnostics(String uri);
|
||||
|
||||
Hover mapHover(BaselineHover hover);
|
||||
|
||||
MessageParams mapInfoMessage(String message);
|
||||
}
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
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
@@ -1,27 +0,0 @@
|
||||
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