implements PLN-0025
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
||||
package p.lsp.dtos;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record PrometeuLspHighlightSpanDTO(
|
||||
PrometeuLspRangeDTO range,
|
||||
String semanticKey) {
|
||||
|
||||
public PrometeuLspHighlightSpanDTO {
|
||||
range = Objects.requireNonNull(range, "range");
|
||||
semanticKey = Objects.requireNonNull(semanticKey, "semanticKey");
|
||||
}
|
||||
}
|
||||
+3
@@ -1,6 +1,7 @@
|
||||
package p.lsp.messages;
|
||||
|
||||
import p.lsp.dtos.PrometeuLspDiagnosticDTO;
|
||||
import p.lsp.dtos.PrometeuLspHighlightSpanDTO;
|
||||
import p.lsp.dtos.PrometeuLspSessionStateDTO;
|
||||
import p.lsp.dtos.PrometeuLspSymbolDTO;
|
||||
|
||||
@@ -10,12 +11,14 @@ import java.util.Objects;
|
||||
public record PrometeuLspAnalyzeDocumentResult(
|
||||
PrometeuLspSessionStateDTO sessionState,
|
||||
List<PrometeuLspDiagnosticDTO> diagnostics,
|
||||
List<PrometeuLspHighlightSpanDTO> semanticHighlights,
|
||||
List<PrometeuLspSymbolDTO> documentSymbols,
|
||||
List<PrometeuLspSymbolDTO> workspaceSymbols) {
|
||||
|
||||
public PrometeuLspAnalyzeDocumentResult {
|
||||
Objects.requireNonNull(sessionState, "sessionState");
|
||||
diagnostics = List.copyOf(Objects.requireNonNull(diagnostics, "diagnostics"));
|
||||
semanticHighlights = List.copyOf(Objects.requireNonNull(semanticHighlights, "semanticHighlights"));
|
||||
documentSymbols = List.copyOf(Objects.requireNonNull(documentSymbols, "documentSymbols"));
|
||||
workspaceSymbols = List.copyOf(Objects.requireNonNull(workspaceSymbols, "workspaceSymbols"));
|
||||
}
|
||||
|
||||
+70
@@ -50,6 +50,7 @@ final class PrometeuLspSemanticReadPhase {
|
||||
return new PrometeuLspAnalyzeDocumentResult(
|
||||
new PrometeuLspSessionStateDTO(true, List.of("diagnostics", "symbols", "definition", "highlight")),
|
||||
session.diagnosticsByDocument().getOrDefault(normalizedRequestedDocument, List.of()),
|
||||
session.semanticHighlightsByDocument().getOrDefault(normalizedRequestedDocument, List.of()),
|
||||
session.documentSymbolsByDocument().getOrDefault(normalizedRequestedDocument, List.of()),
|
||||
session.workspaceSymbols());
|
||||
}
|
||||
@@ -142,6 +143,7 @@ final class PrometeuLspSemanticReadPhase {
|
||||
normalize(requestedDocumentPath),
|
||||
diagnosticsByDocument,
|
||||
Map.of(),
|
||||
Map.of(),
|
||||
List.of(),
|
||||
Map.of(),
|
||||
Map.of());
|
||||
@@ -160,6 +162,7 @@ final class PrometeuLspSemanticReadPhase {
|
||||
return new SemanticSession(
|
||||
normalize(requestedDocumentPath),
|
||||
diagnosticsByDocument,
|
||||
semanticIndex.semanticHighlightsByDocument(),
|
||||
semanticIndex.documentSymbolsByDocument(),
|
||||
semanticIndex.workspaceSymbols(),
|
||||
semanticIndex.symbolsByName(),
|
||||
@@ -281,6 +284,7 @@ final class PrometeuLspSemanticReadPhase {
|
||||
private record SemanticSession(
|
||||
Path requestedDocumentPath,
|
||||
Map<Path, List<PrometeuLspDiagnosticDTO>> diagnosticsByDocument,
|
||||
Map<Path, List<PrometeuLspHighlightSpanDTO>> semanticHighlightsByDocument,
|
||||
Map<Path, List<PrometeuLspSymbolDTO>> documentSymbolsByDocument,
|
||||
List<PrometeuLspSymbolDTO> workspaceSymbols,
|
||||
Map<String, List<PrometeuLspSymbolDTO>> symbolsByName,
|
||||
@@ -293,6 +297,7 @@ final class PrometeuLspSemanticReadPhase {
|
||||
}
|
||||
|
||||
private static final class SemanticIndex {
|
||||
private final Map<Path, List<PrometeuLspHighlightSpanDTO>> semanticHighlightsByDocument = new LinkedHashMap<>();
|
||||
private final Map<Path, List<PrometeuLspSymbolDTO>> documentSymbolsByDocument = new LinkedHashMap<>();
|
||||
private final List<PrometeuLspSymbolDTO> workspaceSymbols = new ArrayList<>();
|
||||
private final Map<String, List<PrometeuLspSymbolDTO>> symbolsByName = new LinkedHashMap<>();
|
||||
@@ -317,9 +322,70 @@ final class PrometeuLspSemanticReadPhase {
|
||||
symbolsByName.computeIfAbsent(child.name(), ignored -> new ArrayList<>()).add(child);
|
||||
}
|
||||
}
|
||||
semanticHighlightsByDocument.put(
|
||||
normalizedDocumentPath,
|
||||
buildSemanticHighlights(tokens, symbolsByName));
|
||||
documentSymbolsByDocument.put(normalizedDocumentPath, List.copyOf(documentSymbols));
|
||||
}
|
||||
|
||||
private List<PrometeuLspHighlightSpanDTO> buildSemanticHighlights(
|
||||
final List<PbsToken> tokens,
|
||||
final Map<String, List<PrometeuLspSymbolDTO>> indexedSymbolsByName) {
|
||||
final List<PrometeuLspHighlightSpanDTO> highlights = new ArrayList<>();
|
||||
for (final PbsToken token : tokens) {
|
||||
if (token.kind() == PbsTokenKind.EOF) {
|
||||
continue;
|
||||
}
|
||||
final String semanticKey = semanticKey(token, indexedSymbolsByName);
|
||||
if (semanticKey == null || semanticKey.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
highlights.add(new PrometeuLspHighlightSpanDTO(
|
||||
new PrometeuLspRangeDTO(token.start(), token.end()),
|
||||
semanticKey));
|
||||
}
|
||||
return List.copyOf(highlights);
|
||||
}
|
||||
|
||||
private String semanticKey(
|
||||
final PbsToken token,
|
||||
final Map<String, List<PrometeuLspSymbolDTO>> indexedSymbolsByName) {
|
||||
return switch (token.kind()) {
|
||||
case COMMENT -> "fe-comment";
|
||||
case STRING_LITERAL -> "fe-string";
|
||||
case INT_LITERAL, FLOAT_LITERAL, BOUNDED_LITERAL -> "fe-number";
|
||||
case TRUE, FALSE, NONE -> "fe-literal";
|
||||
case IDENTIFIER -> semanticKeyForIdentifier(token.lexeme(), indexedSymbolsByName);
|
||||
case IMPORT, FROM, AS, SERVICE, HOST, FN, APPLY, BIND, NEW, IMPLEMENTS, USING, CTOR,
|
||||
DECLARE, LET, CONST, GLOBAL, STRUCT, CONTRACT, ERROR, ENUM, CALLBACK, BUILTIN,
|
||||
SELF, THIS, PUB, MUT, MOD, TYPE, IF, ELSE, SWITCH, DEFAULT, FOR, UNTIL, STEP,
|
||||
WHILE, BREAK, CONTINUE, RETURN, VOID, OPTIONAL, RESULT, SOME, OK, ERR, HANDLE,
|
||||
AND, OR, NOT -> "fe-keyword";
|
||||
case PLUS, MINUS, STAR, SLASH, PERCENT, BANG, PLUS_EQUAL, MINUS_EQUAL, STAR_EQUAL,
|
||||
SLASH_EQUAL, PERCENT_EQUAL, BANG_EQUAL, EQUAL, EQUAL_EQUAL, LESS, LESS_EQUAL,
|
||||
GREATER, GREATER_EQUAL, AND_AND, OR_OR, ARROW, QUESTION -> "fe-operator";
|
||||
case LEFT_PAREN, RIGHT_PAREN, LEFT_BRACE, RIGHT_BRACE, LEFT_BRACKET, RIGHT_BRACKET,
|
||||
COMMA, COLON, SEMICOLON, AT, DOT, DOT_DOT -> "fe-punctuation";
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
private String semanticKeyForIdentifier(
|
||||
final String lexeme,
|
||||
final Map<String, List<PrometeuLspSymbolDTO>> indexedSymbolsByName) {
|
||||
final List<PrometeuLspSymbolDTO> symbols = indexedSymbolsByName.getOrDefault(lexeme, List.of());
|
||||
if (symbols.isEmpty()) {
|
||||
return "fe-identifier";
|
||||
}
|
||||
final PrometeuLspSymbolKindDTO kind = symbols.get(0).kind();
|
||||
return switch (kind) {
|
||||
case FUNCTION, METHOD, CALLBACK -> "fe-callable";
|
||||
case STRUCT, CONTRACT, HOST, BUILTIN_TYPE, SERVICE, ERROR, ENUM -> "fe-type";
|
||||
case GLOBAL, CONST -> "fe-binding";
|
||||
default -> "fe-identifier";
|
||||
};
|
||||
}
|
||||
|
||||
private PrometeuLspSymbolDTO symbolForTopDecl(
|
||||
final Path documentPath,
|
||||
final PbsAst.TopDecl topDecl) {
|
||||
@@ -407,6 +473,10 @@ final class PrometeuLspSemanticReadPhase {
|
||||
children);
|
||||
}
|
||||
|
||||
Map<Path, List<PrometeuLspHighlightSpanDTO>> semanticHighlightsByDocument() {
|
||||
return Map.copyOf(semanticHighlightsByDocument);
|
||||
}
|
||||
|
||||
Map<Path, List<PrometeuLspSymbolDTO>> documentSymbolsByDocument() {
|
||||
return Map.copyOf(documentSymbolsByDocument);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user