implements PLN-0136
Intrepid/Prometeu/Studio/pipeline/pr-master Build started...
JaCoCo Coverage #### Project Overview
No changes detected, that affect the code coverage.
* Line Coverage: 63.75% (19407/30442)
* Branch Coverage: 54.17% (7740/14289)
* Lines of Code: 30442
* Cyclomatic Complexity: 12365
#### Quality Gates Summary
Output truncated.
Test / Build skipped: 15, passed: 685
Intrepid/Prometeu/Studio/pipeline/head This commit looks good
Intrepid/Prometeu/Studio/pipeline/pr-master Build started...
JaCoCo Coverage #### Project Overview
No changes detected, that affect the code coverage.
* Line Coverage: 63.75% (19407/30442)
* Branch Coverage: 54.17% (7740/14289)
* Lines of Code: 30442
* Cyclomatic Complexity: 12365
#### Quality Gates Summary
Output truncated.
Test / Build skipped: 15, passed: 685
Intrepid/Prometeu/Studio/pipeline/head This commit looks good
Add PBS quick fixes as frontend-owned repairs transported by the LSP. Duplicate reserved attributes are deleted at the extra span. Interface InitAllowed attributes with arguments become [InitAllowed]. The server matches Diagnostic.code plus range, does not parse messages, and returns an empty list when the capability or the fix is absent. Housekeep DSC-0044 with LSN-0073.
This commit is contained in:
+234
@@ -0,0 +1,234 @@
|
||||
package p.studio.compiler.pbs.semantics;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.models.SourceKind;
|
||||
import p.studio.compiler.pbs.ast.PbsAst;
|
||||
import p.studio.compiler.pbs.lexer.PbsLexer;
|
||||
import p.studio.compiler.pbs.parser.PbsParser;
|
||||
import p.studio.compiler.source.diagnostics.Diagnostic;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import p.studio.compiler.source.tables.NameTable;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PbsQuickFixCollectorTest {
|
||||
|
||||
@Test
|
||||
void duplicateDocRepairsMatchValidatorSpansAndKeepTheFirstCopy() {
|
||||
final var parsed = parse("""
|
||||
[Doc(markdown = \"""
|
||||
First.
|
||||
\""")]
|
||||
[Doc(markdown = \"""
|
||||
Second.
|
||||
\""")]
|
||||
[Doc(markdown = \"""
|
||||
Third.
|
||||
\""")]
|
||||
fn duplicated() -> void { return; }
|
||||
|
||||
declare struct Player(x: int) {
|
||||
[Doc(markdown = \"""
|
||||
Kept.
|
||||
\""")]
|
||||
[Doc(markdown = \"""
|
||||
Extra method.
|
||||
\""")]
|
||||
fn update() -> void { return; }
|
||||
}
|
||||
""", SourceKind.PROJECT);
|
||||
|
||||
final var duplicates = diagnostics(parsed, PbsSemanticsErrors.E_SEM_DUPLICATE_RESERVED_ATTRIBUTE.name());
|
||||
assertEquals(3, duplicates.size(), parsed.diagnostics().stream().map(Diagnostic::getMessage).toList().toString());
|
||||
assertEquals(3, parsed.repairs().size());
|
||||
assertTrue(parsed.repairs().stream().allMatch(repair ->
|
||||
repair.newText().isEmpty()
|
||||
&& repair.title().equals(PbsQuickFixCollector.REMOVE_DUPLICATE_TITLE)
|
||||
&& repair.diagnosticCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_RESERVED_ATTRIBUTE.name())));
|
||||
assertRepairsMatchDiagnostics(parsed);
|
||||
|
||||
final String edited = apply(parsed.source(), parsed.repairs());
|
||||
assertTrue(edited.contains("First."));
|
||||
assertTrue(edited.contains("Kept."));
|
||||
assertFalse(edited.contains("Second."));
|
||||
assertFalse(edited.contains("Third."));
|
||||
assertFalse(edited.contains("Extra method."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedAndEmptyDocProduceNoRepair() {
|
||||
final var parsed = parse("""
|
||||
[Doc(text = \"""
|
||||
Wrong argument.
|
||||
\""")]
|
||||
fn malformed() -> void { return; }
|
||||
|
||||
[Doc(markdown = \"""
|
||||
|
||||
\""")]
|
||||
fn empty() -> void { return; }
|
||||
|
||||
[InitAllowed(flag = 1)]
|
||||
fn frame() -> void { return; }
|
||||
""", SourceKind.PROJECT);
|
||||
|
||||
assertTrue(diagnostics(parsed, PbsSemanticsErrors.E_SEM_MALFORMED_RESERVED_ATTRIBUTE.name()).size() >= 2);
|
||||
assertTrue(parsed.repairs().isEmpty(), parsed.repairs().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void initAllowedArgumentsAreRewrittenOnlyForInterfaceHostSignatures() {
|
||||
final var parsed = parse("""
|
||||
declare host Gfx {
|
||||
[Host(module = "gfx2d", name = "clear", version = 1)]
|
||||
[InitAllowed(flag = 1)]
|
||||
fn clear() -> void;
|
||||
|
||||
[Host(module = "gfx2d", name = "fill", version = 1)]
|
||||
[InitAllowed()]
|
||||
fn fill() -> void;
|
||||
|
||||
[Host(module = "gfx2d", name = "blit", version = 1)]
|
||||
[InitAllowed]
|
||||
fn blit() -> void;
|
||||
|
||||
[Host(module = "gfx2d")]
|
||||
fn broken() -> void;
|
||||
|
||||
[Host(module = "gfx2d", name = "copy", version = 1)]
|
||||
[Host(module = "gfx2d", name = "copy", version = 1)]
|
||||
fn copy() -> void;
|
||||
}
|
||||
""", SourceKind.SDK_INTERFACE);
|
||||
|
||||
final var argumentRepairs = parsed.repairs().stream()
|
||||
.filter(repair -> repair.newText().equals("[InitAllowed]"))
|
||||
.toList();
|
||||
assertEquals(1, argumentRepairs.size(), parsed.repairs().toString());
|
||||
assertEquals(PbsSemanticsErrors.E_SEM_MALFORMED_RESERVED_ATTRIBUTE.name(), argumentRepairs.getFirst().diagnosticCode());
|
||||
assertEquals(PbsQuickFixCollector.REMOVE_INIT_ALLOWED_ARGUMENTS_TITLE, argumentRepairs.getFirst().title());
|
||||
assertRepairsMatchDiagnostics(parsed);
|
||||
|
||||
final var duplicateRepairs = parsed.repairs().stream()
|
||||
.filter(repair -> repair.diagnosticCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_RESERVED_ATTRIBUTE.name()))
|
||||
.toList();
|
||||
assertEquals(1, duplicateRepairs.size());
|
||||
assertTrue(duplicateRepairs.getFirst().newText().isEmpty());
|
||||
|
||||
final String edited = apply(parsed.source(), parsed.repairs());
|
||||
assertTrue(edited.contains("[InitAllowed]\n"));
|
||||
assertFalse(edited.contains("flag = 1"));
|
||||
assertTrue(edited.contains("[InitAllowed()]"));
|
||||
assertEquals(1, count(edited, "[Host(module = \"gfx2d\", name = \"copy\", version = 1)]"));
|
||||
|
||||
final var projectHost = parse("""
|
||||
declare host Gfx {
|
||||
[Host(module = "gfx2d", name = "clear", version = 1)]
|
||||
[InitAllowed(flag = 1)]
|
||||
fn clear() -> void;
|
||||
}
|
||||
""", SourceKind.PROJECT);
|
||||
assertTrue(projectHost.repairs().stream().noneMatch(repair -> repair.newText().equals("[InitAllowed]")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void builtinConstTypeIntrinsicAndAssetLoweringDuplicatesAreDeleted() {
|
||||
final var parsed = parse("""
|
||||
[BuiltinType(name = "Color", version = 1)]
|
||||
[BuiltinType(name = "Color", version = 1)]
|
||||
declare builtin type Color(
|
||||
pub raw: int
|
||||
) {
|
||||
[IntrinsicCall(name = "width", version = 1)]
|
||||
[IntrinsicCall(name = "width", version = 1)]
|
||||
fn width() -> int;
|
||||
}
|
||||
[BuiltinConst(target = "Color", name = "white", version = 1)]
|
||||
[BuiltinConst(target = "Color", name = "white", version = 1)]
|
||||
declare const WHITE: Color;
|
||||
declare host LowAssets {
|
||||
[Host(module = "asset", name = "load", version = 1)]
|
||||
[AssetLowering(param = 0)]
|
||||
[AssetLowering(param = 0)]
|
||||
fn load(addressable: int) -> void;
|
||||
}
|
||||
""", SourceKind.SDK_INTERFACE);
|
||||
|
||||
assertEquals(4, parsed.repairs().size(), parsed.repairs().toString());
|
||||
assertTrue(parsed.repairs().stream().allMatch(repair ->
|
||||
repair.newText().isEmpty()
|
||||
&& repair.diagnosticCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_RESERVED_ATTRIBUTE.name())));
|
||||
assertRepairsMatchDiagnostics(parsed);
|
||||
final String edited = apply(parsed.source(), parsed.repairs());
|
||||
assertEquals(1, count(edited, "[BuiltinType(name = \"Color\", version = 1)]"));
|
||||
assertEquals(1, count(edited, "[IntrinsicCall(name = \"width\", version = 1)]"));
|
||||
assertEquals(1, count(edited, "[BuiltinConst(target = \"Color\", name = \"white\", version = 1)]"));
|
||||
assertEquals(1, count(edited, "[AssetLowering(param = 0)]"));
|
||||
}
|
||||
|
||||
private static Parsed parse(final String source, final SourceKind sourceKind) {
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
final var fileId = new FileId(0);
|
||||
final var tokens = PbsLexer.lex(source, fileId, diagnostics);
|
||||
final var parseMode = sourceKind == SourceKind.SDK_INTERFACE
|
||||
? PbsParser.ParseMode.INTERFACE_MODULE
|
||||
: PbsParser.ParseMode.ORDINARY;
|
||||
final PbsAst.File ast = PbsParser.parse(tokens, fileId, diagnostics, parseMode);
|
||||
new PbsDeclarationSemanticsValidator(new NameTable()).validate(ast, sourceKind, diagnostics);
|
||||
return new Parsed(
|
||||
source,
|
||||
diagnostics,
|
||||
PbsQuickFixCollector.collect(ast, sourceKind == SourceKind.SDK_INTERFACE));
|
||||
}
|
||||
|
||||
private static List<Diagnostic> diagnostics(final Parsed parsed, final String code) {
|
||||
return parsed.diagnostics().stream().filter(diagnostic -> diagnostic.getCode().equals(code)).toList();
|
||||
}
|
||||
|
||||
private static void assertRepairsMatchDiagnostics(final Parsed parsed) {
|
||||
for (final PbsQuickFix repair : parsed.repairs()) {
|
||||
assertTrue(parsed.diagnostics().stream().anyMatch(diagnostic ->
|
||||
diagnostic.getCode().equals(repair.diagnosticCode())
|
||||
&& diagnostic.getSpan().getStart() == repair.startOffset()
|
||||
&& diagnostic.getSpan().getEnd() == repair.endOffset()),
|
||||
repair + " missing from " + parsed.diagnostics());
|
||||
}
|
||||
}
|
||||
|
||||
private static String apply(final String source, final List<PbsQuickFix> repairs) {
|
||||
final var ordered = repairs.stream()
|
||||
.sorted(Comparator.comparingInt(PbsQuickFix::startOffset).reversed())
|
||||
.toList();
|
||||
String current = source;
|
||||
for (final PbsQuickFix repair : ordered) {
|
||||
current = current.substring(0, repair.startOffset()) + repair.newText() + current.substring(repair.endOffset());
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
private static int count(final String source, final String needle) {
|
||||
int count = 0;
|
||||
int from = 0;
|
||||
while (from <= source.length()) {
|
||||
final int found = source.indexOf(needle, from);
|
||||
if (found < 0) {
|
||||
return count;
|
||||
}
|
||||
count += 1;
|
||||
from = found + needle.length();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private record Parsed(
|
||||
String source,
|
||||
DiagnosticSink diagnostics,
|
||||
List<PbsQuickFix> repairs) {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user