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:
+40
@@ -13,9 +13,13 @@ import p.studio.compiler.pbs.semantics.PbsEditorialSignatureHelp;
|
||||
import p.studio.compiler.pbs.semantics.PbsEditorialSymbolKind;
|
||||
import p.studio.compiler.pbs.semantics.PbsEditorialSupportService;
|
||||
import p.studio.compiler.pbs.semantics.PbsEditorialSupportService.EditorialDocument;
|
||||
import p.studio.compiler.models.SourceKind;
|
||||
import p.studio.compiler.pbs.semantics.PbsEditorialRename;
|
||||
import p.studio.compiler.pbs.semantics.PbsEditorialWorkspaceSymbol;
|
||||
import p.studio.compiler.pbs.semantics.PbsQuickFix;
|
||||
import p.studio.compiler.pbs.semantics.PbsQuickFixCollector;
|
||||
import p.studio.compiler.services.PBSFrontendPhaseService.PbsSemanticReadSurface;
|
||||
import p.studio.compiler.services.FrontendCodeAction;
|
||||
import p.studio.compiler.services.FrontendCompletionCandidate;
|
||||
import p.studio.compiler.services.FrontendDefinitionLocation;
|
||||
import p.studio.compiler.services.FrontendDocumentRequest;
|
||||
@@ -397,6 +401,42 @@ public final class PBSFrontendLanguageService implements FrontendLanguageService
|
||||
return FrontendRenameResult.applied(List.copyOf(edits.values()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean codeActionsSupported() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FrontendCodeAction> codeActions(
|
||||
final FrontendDocumentRequest request,
|
||||
final FrontendEditorialContext editorialContext) {
|
||||
if (request == null
|
||||
|| editorialContext == null
|
||||
|| !(editorialContext.syntaxTree() instanceof PbsAst.File ast)
|
||||
|| !(editorialContext.sourceFiles() instanceof FileTableReader fileTable)) {
|
||||
return List.of();
|
||||
}
|
||||
final FileId fileId = ast.span() == null ? null : ast.span().getFileId();
|
||||
final Path path = pathForRegularSource(fileId, fileTable);
|
||||
if (path == null || !path.equals(request.documentPath().toAbsolutePath().normalize())) {
|
||||
return List.of();
|
||||
}
|
||||
final boolean interfaceModule = editorialContext.projectSurface() instanceof PbsSemanticReadSurface surface
|
||||
&& surface.sourceKindByFile() != null
|
||||
&& surface.sourceKindByFile().get(fileId) == SourceKind.SDK_INTERFACE;
|
||||
final ArrayList<FrontendCodeAction> actions = new ArrayList<>();
|
||||
for (final PbsQuickFix repair : PbsQuickFixCollector.collect(ast, interfaceModule)) {
|
||||
actions.add(new FrontendCodeAction(
|
||||
repair.title(),
|
||||
repair.diagnosticCode(),
|
||||
path,
|
||||
repair.startOffset(),
|
||||
repair.endOffset(),
|
||||
repair.newText()));
|
||||
}
|
||||
return List.copyOf(actions);
|
||||
}
|
||||
|
||||
private PbsEditorialRename editorialRename(
|
||||
final FrontendDocumentRequest request,
|
||||
final FrontendEditorialContext editorialContext,
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package p.studio.compiler.pbs.semantics;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record PbsQuickFix(
|
||||
String diagnosticCode,
|
||||
int startOffset,
|
||||
int endOffset,
|
||||
String newText,
|
||||
String title) {
|
||||
|
||||
public PbsQuickFix {
|
||||
diagnosticCode = requireText(diagnosticCode, "diagnosticCode");
|
||||
title = requireText(title, "title");
|
||||
if (startOffset < 0) {
|
||||
throw new IllegalArgumentException("startOffset must not be negative");
|
||||
}
|
||||
if (endOffset < startOffset) {
|
||||
throw new IllegalArgumentException("endOffset must not be before startOffset");
|
||||
}
|
||||
newText = Objects.requireNonNull(newText, "newText");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
package p.studio.compiler.pbs.semantics;
|
||||
|
||||
import p.studio.compiler.pbs.ast.PbsAst;
|
||||
import p.studio.compiler.source.Span;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public final class PbsQuickFixCollector {
|
||||
static final String REMOVE_DUPLICATE_TITLE = "Remove duplicate reserved attribute";
|
||||
static final String REMOVE_INIT_ALLOWED_ARGUMENTS_TITLE = "Remove InitAllowed arguments";
|
||||
private static final String INIT_ALLOWED_REPLACEMENT = "[InitAllowed]";
|
||||
|
||||
private PbsQuickFixCollector() {
|
||||
}
|
||||
|
||||
public static List<PbsQuickFix> collect(
|
||||
final PbsAst.File ast,
|
||||
final boolean interfaceModule) {
|
||||
if (ast == null) {
|
||||
return List.of();
|
||||
}
|
||||
final ArrayList<PbsQuickFix> repairs = new ArrayList<>();
|
||||
for (final PbsAst.TopDecl topDecl : ast.topDecls()) {
|
||||
switch (topDecl) {
|
||||
case PbsAst.FunctionDecl functionDecl -> duplicateDoc(functionDecl.attributes(), repairs);
|
||||
case PbsAst.StructDecl structDecl -> {
|
||||
duplicateDoc(structDecl.attributes(), repairs);
|
||||
for (final PbsAst.FunctionDecl method : structDecl.methods()) {
|
||||
duplicateDoc(method.attributes(), repairs);
|
||||
}
|
||||
}
|
||||
case PbsAst.ServiceDecl serviceDecl -> {
|
||||
duplicateDoc(serviceDecl.attributes(), repairs);
|
||||
for (final PbsAst.FunctionDecl method : serviceDecl.methods()) {
|
||||
duplicateDoc(method.attributes(), repairs);
|
||||
}
|
||||
}
|
||||
case PbsAst.ContractDecl contractDecl -> {
|
||||
duplicateDoc(contractDecl.attributes(), repairs);
|
||||
if (interfaceModule) {
|
||||
for (final PbsAst.FunctionSignature signature : contractDecl.signatures()) {
|
||||
duplicateDoc(signature.attributes(), repairs);
|
||||
}
|
||||
}
|
||||
}
|
||||
case PbsAst.HostDecl hostDecl -> {
|
||||
duplicateDoc(hostDecl.attributes(), repairs);
|
||||
if (interfaceModule) {
|
||||
for (final PbsAst.FunctionSignature signature : hostDecl.signatures()) {
|
||||
collectHostSignature(signature, repairs);
|
||||
}
|
||||
}
|
||||
}
|
||||
case PbsAst.BuiltinTypeDecl builtinTypeDecl -> {
|
||||
if (interfaceModule) {
|
||||
duplicateDoc(builtinTypeDecl.attributes(), repairs);
|
||||
duplicateNamed(builtinTypeDecl.attributes(), "BuiltinType", repairs);
|
||||
for (final PbsAst.FunctionSignature signature : builtinTypeDecl.signatures()) {
|
||||
duplicateDoc(signature.attributes(), repairs);
|
||||
duplicateNamed(signature.attributes(), "IntrinsicCall", repairs);
|
||||
}
|
||||
}
|
||||
}
|
||||
case PbsAst.ErrorDecl errorDecl -> duplicateDoc(errorDecl.attributes(), repairs);
|
||||
case PbsAst.EnumDecl enumDecl -> duplicateDoc(enumDecl.attributes(), repairs);
|
||||
case PbsAst.CallbackDecl callbackDecl -> duplicateDoc(callbackDecl.attributes(), repairs);
|
||||
case PbsAst.ConstDecl constDecl -> {
|
||||
duplicateDoc(constDecl.attributes(), repairs);
|
||||
duplicateNamed(constDecl.attributes(), "BuiltinConst", repairs);
|
||||
}
|
||||
default -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
repairs.sort(Comparator
|
||||
.comparingInt(PbsQuickFix::startOffset)
|
||||
.thenComparingInt(PbsQuickFix::endOffset)
|
||||
.thenComparing(PbsQuickFix::diagnosticCode));
|
||||
return List.copyOf(repairs);
|
||||
}
|
||||
|
||||
private static void collectHostSignature(
|
||||
final PbsAst.FunctionSignature signature,
|
||||
final List<PbsQuickFix> repairs) {
|
||||
duplicateDoc(signature.attributes(), repairs);
|
||||
duplicateNamed(signature.attributes(), "Host", repairs);
|
||||
duplicateNamed(signature.attributes(), "InitAllowed", repairs);
|
||||
duplicateNamed(signature.attributes(), "AssetLowering", repairs);
|
||||
final List<PbsAst.Attribute> initAllowed = named(signature.attributes(), "InitAllowed");
|
||||
if (!initAllowed.isEmpty() && !initAllowed.getFirst().arguments().isEmpty()) {
|
||||
add(repairs, initAllowed.getFirst(), PbsSemanticsErrors.E_SEM_MALFORMED_RESERVED_ATTRIBUTE.name(),
|
||||
INIT_ALLOWED_REPLACEMENT, REMOVE_INIT_ALLOWED_ARGUMENTS_TITLE);
|
||||
}
|
||||
}
|
||||
|
||||
private static void duplicateDoc(
|
||||
final ReadOnlyList<PbsAst.Attribute> attributes,
|
||||
final List<PbsQuickFix> repairs) {
|
||||
duplicateNamed(attributes, "Doc", repairs);
|
||||
}
|
||||
|
||||
private static void duplicateNamed(
|
||||
final ReadOnlyList<PbsAst.Attribute> attributes,
|
||||
final String name,
|
||||
final List<PbsQuickFix> repairs) {
|
||||
final List<PbsAst.Attribute> matches = named(attributes, name);
|
||||
for (int index = 1; index < matches.size(); index++) {
|
||||
add(repairs, matches.get(index), PbsSemanticsErrors.E_SEM_DUPLICATE_RESERVED_ATTRIBUTE.name(),
|
||||
"", REMOVE_DUPLICATE_TITLE);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<PbsAst.Attribute> named(
|
||||
final ReadOnlyList<PbsAst.Attribute> attributes,
|
||||
final String name) {
|
||||
final ArrayList<PbsAst.Attribute> matches = new ArrayList<>();
|
||||
if (attributes == null) {
|
||||
return matches;
|
||||
}
|
||||
for (final PbsAst.Attribute attribute : attributes) {
|
||||
if (name.equals(attribute.name())) {
|
||||
matches.add(attribute);
|
||||
}
|
||||
}
|
||||
return matches;
|
||||
}
|
||||
|
||||
private static void add(
|
||||
final List<PbsQuickFix> repairs,
|
||||
final PbsAst.Attribute attribute,
|
||||
final String code,
|
||||
final String newText,
|
||||
final String title) {
|
||||
if (attribute == null || attribute.span() == null || attribute.span().isNone()) {
|
||||
return;
|
||||
}
|
||||
final Span span = attribute.span();
|
||||
final long start = span.getStart();
|
||||
final long end = span.getEnd();
|
||||
if (start < 0 || end < start || end > Integer.MAX_VALUE) {
|
||||
return;
|
||||
}
|
||||
repairs.add(new PbsQuickFix(code, (int) start, (int) end, newText, title));
|
||||
}
|
||||
}
|
||||
+9
-2
@@ -5,6 +5,7 @@ import p.studio.compiler.messages.BuildingIssueSink;
|
||||
import p.studio.compiler.messages.FESurfaceContext;
|
||||
import p.studio.compiler.messages.FrontendPhaseContext;
|
||||
import p.studio.compiler.models.IRBackend;
|
||||
import p.studio.compiler.models.SourceKind;
|
||||
import p.studio.compiler.pbs.PbsFrontendCompiler;
|
||||
import p.studio.compiler.pbs.PbsReservedMetadataExtractor;
|
||||
import p.studio.compiler.pbs.semantics.PbsFlowSemanticsValidator;
|
||||
@@ -127,11 +128,15 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
|
||||
final Map<FileId, p.studio.compiler.pbs.ast.PbsAst.File> astByFile = new LinkedHashMap<>();
|
||||
final Map<FileId, ReadOnlyList<p.studio.compiler.pbs.ast.PbsAst.TopDecl>> supplementalTopDeclsByFile = new LinkedHashMap<>();
|
||||
final Map<FileId, ReadOnlyList<PbsInlineHintSurface>> inlineHintsByFile = new LinkedHashMap<>();
|
||||
final Map<FileId, SourceKind> sourceKindByFile = new LinkedHashMap<>();
|
||||
for (final var entry : importedSemanticContexts.entrySet()) {
|
||||
supplementalTopDeclsByFile.put(entry.getKey(), entry.getValue().supplementalTopDecls());
|
||||
}
|
||||
for (final var parsedSourceFile : assembly.parsedSourceFiles()) {
|
||||
astByFile.put(parsedSourceFile.fileId(), parsedSourceFile.ast());
|
||||
sourceKindByFile.put(
|
||||
parsedSourceFile.fileId(),
|
||||
parsedSourceFile.sourceKind() == null ? SourceKind.PROJECT : parsedSourceFile.sourceKind());
|
||||
final var importedSemanticContext = importedSemanticContexts.getOrDefault(
|
||||
parsedSourceFile.fileId(),
|
||||
PbsImportedSemanticContext.empty());
|
||||
@@ -147,13 +152,15 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
|
||||
return new PbsSemanticReadSurface(
|
||||
Map.copyOf(astByFile),
|
||||
Map.copyOf(supplementalTopDeclsByFile),
|
||||
Map.copyOf(inlineHintsByFile));
|
||||
Map.copyOf(inlineHintsByFile),
|
||||
Map.copyOf(sourceKindByFile));
|
||||
}
|
||||
|
||||
public record PbsSemanticReadSurface(
|
||||
Map<FileId, p.studio.compiler.pbs.ast.PbsAst.File> astByFile,
|
||||
Map<FileId, ReadOnlyList<p.studio.compiler.pbs.ast.PbsAst.TopDecl>> supplementalTopDeclsByFile,
|
||||
Map<FileId, ReadOnlyList<PbsInlineHintSurface>> inlineHintsByFile) {
|
||||
Map<FileId, ReadOnlyList<PbsInlineHintSurface>> inlineHintsByFile,
|
||||
Map<FileId, SourceKind> sourceKindByFile) {
|
||||
}
|
||||
|
||||
private IRBackend mergeCompiledSources(
|
||||
|
||||
+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) {
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package p.studio.compiler.services;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
|
||||
public record FrontendCodeAction(
|
||||
String title,
|
||||
String diagnosticCode,
|
||||
Path path,
|
||||
int startOffset,
|
||||
int endOffset,
|
||||
String newText) {
|
||||
|
||||
public FrontendCodeAction {
|
||||
title = requireText(title, "title");
|
||||
diagnosticCode = requireText(diagnosticCode, "diagnosticCode");
|
||||
path = Objects.requireNonNull(path, "path").toAbsolutePath().normalize();
|
||||
if (startOffset < 0) {
|
||||
throw new IllegalArgumentException("startOffset must not be negative");
|
||||
}
|
||||
if (endOffset < startOffset) {
|
||||
throw new IllegalArgumentException("endOffset must not be before startOffset");
|
||||
}
|
||||
newText = Objects.requireNonNull(newText, "newText");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
+14
@@ -138,4 +138,18 @@ public interface FrontendLanguageService {
|
||||
final String newName) {
|
||||
return rename(request, offset, newName);
|
||||
}
|
||||
|
||||
default boolean codeActionsSupported() {
|
||||
return false;
|
||||
}
|
||||
|
||||
default List<FrontendCodeAction> codeActions(final FrontendDocumentRequest request) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
default List<FrontendCodeAction> codeActions(
|
||||
final FrontendDocumentRequest request,
|
||||
final FrontendEditorialContext editorialContext) {
|
||||
return codeActions(request);
|
||||
}
|
||||
}
|
||||
|
||||
+32
@@ -30,6 +30,38 @@ class FrontendLanguageServiceTest {
|
||||
assertTrue(service.prepareRename(request, 0).isEmpty());
|
||||
assertEquals(FrontendRenameResult.Status.UNSUPPORTED, service.rename(request, 0, "renamed").status());
|
||||
assertTrue(service.rename(request, 0, "renamed").edits().isEmpty());
|
||||
assertFalse(service.codeActionsSupported());
|
||||
assertTrue(service.codeActions(request).isEmpty());
|
||||
assertTrue(service.codeActions(request, null).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void codeActionAllowsEmptyReplacementAndRejectsBlankIdentity() {
|
||||
final Path path = Path.of("src/main.pbs").toAbsolutePath();
|
||||
final var action = new FrontendCodeAction(
|
||||
" Remove duplicate reserved attribute ",
|
||||
" E_SEM_DUPLICATE_RESERVED_ATTRIBUTE ",
|
||||
path,
|
||||
4,
|
||||
12,
|
||||
"");
|
||||
assertEquals("Remove duplicate reserved attribute", action.title());
|
||||
assertEquals("E_SEM_DUPLICATE_RESERVED_ATTRIBUTE", action.diagnosticCode());
|
||||
assertEquals("", action.newText());
|
||||
assertThrows(IllegalArgumentException.class, () -> new FrontendCodeAction(
|
||||
" ",
|
||||
"E_SEM_DUPLICATE_RESERVED_ATTRIBUTE",
|
||||
path,
|
||||
0,
|
||||
1,
|
||||
""));
|
||||
assertThrows(NullPointerException.class, () -> new FrontendCodeAction(
|
||||
"Remove duplicate reserved attribute",
|
||||
"E_SEM_DUPLICATE_RESERVED_ATTRIBUTE",
|
||||
path,
|
||||
0,
|
||||
1,
|
||||
null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user