better semantic style for PBS
This commit is contained in:
@@ -25,7 +25,47 @@ final class LspServiceImplTest {
|
||||
private static final String OVERLAY_SOURCE = """
|
||||
fn helper_call() -> void
|
||||
{
|
||||
helper();
|
||||
if true {
|
||||
helper();
|
||||
}
|
||||
while false {
|
||||
helper();
|
||||
}
|
||||
let value = switch true {
|
||||
true: { 1 },
|
||||
default: { 2 }
|
||||
};
|
||||
let recovered = handle helper_result() {
|
||||
_: {
|
||||
ok(0)
|
||||
}
|
||||
};
|
||||
}
|
||||
""";
|
||||
|
||||
private static final String SERVICE_SOURCE = """
|
||||
declare service Game {
|
||||
fn tick(x: int) -> int { return x; }
|
||||
}
|
||||
|
||||
fn main() -> void {
|
||||
Game.tick(1);
|
||||
}
|
||||
""";
|
||||
|
||||
private static final String SDK_IMPORT_SOURCE = """
|
||||
import { Gfx } from @sdk:gfx;
|
||||
|
||||
fn main() -> void {
|
||||
Gfx.clear();
|
||||
}
|
||||
""";
|
||||
|
||||
private static final String PROJECT_IMPORT_SOURCE = """
|
||||
import { Color } from @app:graphics;
|
||||
|
||||
fn main(color: Color) -> Color {
|
||||
return color;
|
||||
}
|
||||
""";
|
||||
|
||||
@@ -52,6 +92,18 @@ final class LspServiceImplTest {
|
||||
assertTrue(
|
||||
analysis.documentSymbols().stream().anyMatch(symbol -> symbol.name().equals("helper_call")),
|
||||
analysis.toString());
|
||||
assertTrue(
|
||||
flatten(analysis.documentSymbols()).stream().anyMatch(symbol -> symbol.kind() == p.studio.lsp.messages.LspSymbolKind.IF),
|
||||
analysis.toString());
|
||||
assertTrue(
|
||||
flatten(analysis.documentSymbols()).stream().anyMatch(symbol -> symbol.kind() == p.studio.lsp.messages.LspSymbolKind.WHILE),
|
||||
analysis.toString());
|
||||
assertTrue(
|
||||
flatten(analysis.documentSymbols()).stream().anyMatch(symbol -> symbol.kind() == p.studio.lsp.messages.LspSymbolKind.SWITCH),
|
||||
analysis.toString());
|
||||
assertTrue(
|
||||
flatten(analysis.documentSymbols()).stream().anyMatch(symbol -> symbol.kind() == p.studio.lsp.messages.LspSymbolKind.HANDLE),
|
||||
analysis.toString());
|
||||
assertTrue(
|
||||
analysis.workspaceSymbols().stream().anyMatch(symbol ->
|
||||
symbol.name().equals("helper") && symbol.documentPath().equals(normalize(helperFile))),
|
||||
@@ -89,6 +141,62 @@ final class LspServiceImplTest {
|
||||
diagnostic.documentPath().equals(normalize(mainFile))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void analyzeDocumentHighlightsServiceIdentifiersWithServiceSemanticKey() throws Exception {
|
||||
final Path projectRoot = createProject();
|
||||
final Path mainFile = projectRoot.resolve("src/main.pbs");
|
||||
Files.writeString(mainFile, SERVICE_SOURCE);
|
||||
|
||||
final VfsProjectDocument vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext(projectRoot));
|
||||
final LspService service = new LspServiceImpl(
|
||||
new LspProjectContext("Example", "pbs", projectRoot),
|
||||
vfs);
|
||||
|
||||
final var analysis = service.analyzeDocument(new LspAnalyzeDocumentRequest(mainFile));
|
||||
|
||||
assertEquals("pbs-service", semanticKeyForLexeme(analysis, SERVICE_SOURCE, "Game"));
|
||||
assertTrue(flatten(analysis.documentSymbols()).stream().anyMatch(symbol ->
|
||||
symbol.kind() == p.studio.lsp.messages.LspSymbolKind.SERVICE && symbol.name().equals("Game")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void analyzeDocumentHighlightsSdkImportedHostIdentifiersWithHostSemanticKey() throws Exception {
|
||||
final Path projectRoot = createProject();
|
||||
final Path mainFile = projectRoot.resolve("src/main.pbs");
|
||||
Files.writeString(mainFile, SDK_IMPORT_SOURCE);
|
||||
|
||||
final VfsProjectDocument vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext(projectRoot));
|
||||
final LspService service = new LspServiceImpl(
|
||||
new LspProjectContext("Example", "pbs", projectRoot),
|
||||
vfs);
|
||||
|
||||
final var analysis = service.analyzeDocument(new LspAnalyzeDocumentRequest(mainFile));
|
||||
|
||||
assertEquals(List.of("pbs-service", "pbs-service"), semanticKeysForLexeme(analysis, SDK_IMPORT_SOURCE, "Gfx"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void analyzeDocumentHighlightsProjectImportedStructIdentifiersWithStructSemanticKey() throws Exception {
|
||||
final Path projectRoot = createProject();
|
||||
final Path mainFile = projectRoot.resolve("src/main.pbs");
|
||||
final Path graphicsDir = projectRoot.resolve("src/graphics");
|
||||
final Path graphicsFile = graphicsDir.resolve("types.pbs");
|
||||
final Path graphicsBarrel = graphicsDir.resolve("mod.barrel");
|
||||
Files.createDirectories(graphicsDir);
|
||||
Files.writeString(mainFile, PROJECT_IMPORT_SOURCE);
|
||||
Files.writeString(graphicsFile, "declare struct Color(raw: int);\n");
|
||||
Files.writeString(graphicsBarrel, "pub struct Color;\n");
|
||||
|
||||
final VfsProjectDocument vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext(projectRoot));
|
||||
final LspService service = new LspServiceImpl(
|
||||
new LspProjectContext("Example", "pbs", projectRoot),
|
||||
vfs);
|
||||
|
||||
final var analysis = service.analyzeDocument(new LspAnalyzeDocumentRequest(mainFile));
|
||||
|
||||
assertEquals(List.of("pbs-struct", "pbs-struct", "pbs-struct"), semanticKeysForLexeme(analysis, PROJECT_IMPORT_SOURCE, "Color"));
|
||||
}
|
||||
|
||||
private Path createProject() throws Exception {
|
||||
final Path src = Files.createDirectories(tempDir.resolve("src"));
|
||||
Files.writeString(tempDir.resolve("prometeu.json"), """
|
||||
@@ -100,10 +208,34 @@ final class LspServiceImplTest {
|
||||
"dependencies": []
|
||||
}
|
||||
""");
|
||||
Files.writeString(src.resolve("mod.barrel"), "pub fn helper() -> void;\n");
|
||||
Files.writeString(src.resolve("mod.barrel"), """
|
||||
pub fn helper() -> void;
|
||||
pub fn helper_result() -> result<MyError> int;
|
||||
pub error MyError;
|
||||
""");
|
||||
Files.writeString(src.resolve("helper.pbs"), "fn helper() -> void {}\n");
|
||||
Files.writeString(src.resolve("helper_result.pbs"), """
|
||||
declare error MyError {
|
||||
Failed
|
||||
}
|
||||
|
||||
fn helper_result() -> result<MyError> int {
|
||||
return ok(1);
|
||||
}
|
||||
""");
|
||||
return tempDir;
|
||||
}
|
||||
|
||||
private static List<p.studio.lsp.dtos.LspSymbolDTO> flatten(
|
||||
final List<p.studio.lsp.dtos.LspSymbolDTO> symbols) {
|
||||
final List<p.studio.lsp.dtos.LspSymbolDTO> flattened = new java.util.ArrayList<>();
|
||||
for (final var symbol : symbols) {
|
||||
flattened.add(symbol);
|
||||
flattened.addAll(flatten(symbol.children()));
|
||||
}
|
||||
return flattened;
|
||||
}
|
||||
|
||||
private VfsProjectContext projectContext(final Path projectRoot) {
|
||||
return new VfsProjectContext("Example", "pbs", projectRoot);
|
||||
}
|
||||
@@ -120,11 +252,17 @@ final class LspServiceImplTest {
|
||||
final p.studio.lsp.messages.LspAnalyzeDocumentResult analysis,
|
||||
final String source,
|
||||
final String lexeme) {
|
||||
return semanticKeysForLexeme(analysis, source, lexeme).stream().findFirst().orElseThrow();
|
||||
}
|
||||
|
||||
private static List<String> semanticKeysForLexeme(
|
||||
final p.studio.lsp.messages.LspAnalyzeDocumentResult analysis,
|
||||
final String source,
|
||||
final String lexeme) {
|
||||
return analysis.semanticHighlights().stream()
|
||||
.filter(highlight -> lexeme.equals(spanContent(source, highlight.range().startOffset(), highlight.range().endOffset())))
|
||||
.map(p.studio.lsp.dtos.LspHighlightSpanDTO::semanticKey)
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
.toList();
|
||||
}
|
||||
|
||||
private static String spanContent(
|
||||
|
||||
Reference in New Issue
Block a user