Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

constructor quick fix #886

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added lazy code assist
minesh-s-patel committed Jan 7, 2025
commit d95ce85aecb4c59bdfcabe67e2d61e42e313bfa0
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
import com.regnosys.rosetta.types.RosettaTypeProvider;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@@ -23,7 +24,20 @@ public class ConstructorQuickFix {
private RosettaTypeProvider types;
@Inject
private RosettaEcoreUtil extensions;


public void modifyConstructorWithAllAttributes(RosettaConstructorExpression constructor) {
RosettaFeatureGroup rosettaFeatureGroup = groupConstructorFeatures(constructor);
List<RosettaFeature> allAttributes = rosettaFeatureGroup.allAttributes();

allAttributes.forEach(attr -> {
ConstructorKeyValuePair constructorKeyValuePair = ExpressionFactory.eINSTANCE.createConstructorKeyValuePair();
constructorKeyValuePair.setKey(attr);
constructorKeyValuePair.setValue(ExpressionFactory.eINSTANCE.createListLiteral());
constructor.getValues().add(constructorKeyValuePair);
});
}


public void modifyConstructorWithMandatoryAttributes(RosettaConstructorExpression constructor) {
RosettaFeatureGroup rosettaFeatureGroup = groupConstructorFeatures(constructor);
List<RosettaFeature> requiredAbsentAttributes = rosettaFeatureGroup.requiredAbsentAttributes();
@@ -72,6 +86,10 @@ private RosettaFeatureGroup(List<RosettaFeature> populated, List<RosettaFeature>
this.all = all;
}

private List<RosettaFeature> allAttributes() {
return all.stream().filter(x -> !populated.contains(x)).collect(Collectors.toList());
}

private List<RosettaFeature> requiredAbsentAttributes() {
return all.stream()
.filter(x -> !populated.contains(x))
Original file line number Diff line number Diff line change
@@ -46,38 +46,47 @@ public class RosettaQuickFixCodeActionService implements ICodeActionService2 {

@Override
public List<Either<Command, CodeAction>> getCodeActions(Options options) {
boolean handleQuickfixes = options.getCodeActionParams().getContext().getOnly() == null
|| options.getCodeActionParams().getContext().getOnly().isEmpty()
|| options.getCodeActionParams().getContext().getOnly().contains(CodeActionKind.QuickFix);
CodeActionParams codeActionParams = options.getCodeActionParams();
boolean handleQuickfixes = codeActionParams.getContext().getOnly() == null
|| codeActionParams.getContext().getOnly().isEmpty()
|| codeActionParams.getContext().getOnly().contains(CodeActionKind.QuickFix);

if (!handleQuickfixes) {
return Collections.emptyList();
}

List<Either<Command, CodeAction>> result = new ArrayList<>();
for (Diagnostic diagnostic : options.getCodeActionParams().getContext().getDiagnostics()) {
for (Diagnostic diagnostic : codeActionParams.getContext().getDiagnostics()) {
Options diagnosticOptions = createOptionsForSingleDiagnostic(options, diagnostic);

List<DiagnosticResolution> resolutions = quickfixes.getResolutions(diagnosticOptions, diagnostic).stream()
.sorted(Comparator.nullsLast(Comparator.comparing(DiagnosticResolution::getLabel)))
.collect(Collectors.toList());
for (DiagnosticResolution resolution : resolutions) {


result.add(Either.forRight(createFix(resolution, diagnostic)));
// result.add(Either.forRight(createFix(resolution, diagnostic)));
result.add(Either.forRight(createUnresolvedFix(resolution.getLabel(), codeActionParams, diagnostic)));
}
}
return result;
}

private CodeAction createUnresolvedFix(String label, CodeActionParams codeActionParams, Diagnostic diagnostic) {
CodeAction codeAction = new CodeAction();
codeAction.setDiagnostics(Collections.singletonList(diagnostic));
codeAction.setTitle(label);
codeAction.setData(codeActionParams);
codeAction.setKind(CodeActionKind.QuickFix);
return codeAction;
}

private CodeAction createFix(DiagnosticResolution resolution, Diagnostic diagnostic) {
CodeAction codeAction = new CodeAction();
codeAction.setDiagnostics(Collections.singletonList(diagnostic));
codeAction.setTitle(resolution.getLabel());
// This causes very slow perf as the fix is applied in memory before needed.
// There needs to be another mechanism to do this.
// There needs to be another mechanism to do this.
codeAction.setEdit(resolution.apply());
codeAction.setKind(CodeActionKind.QuickFix);

return codeAction;
}

@@ -98,5 +107,5 @@ private Options createOptionsForSingleDiagnostic(Options base, Diagnostic diagno

return options;
}

}
Original file line number Diff line number Diff line change
@@ -79,4 +79,12 @@ public void missingAttributes(DiagnosticResolutionAcceptor acceptor) {
acceptor.accept("Auto add mandatory attributes.", semanticModification);
}

@QuickFix(RosettaIssueCodes.MISSING_MANDATORY_CONSTRUCTOR_ARGUMENT)
public void addAllMissingAttributes(DiagnosticResolutionAcceptor acceptor) {
ISemanticModification semanticModification = (Diagnostic diagnostic, EObject object) ->
context -> constructorQuickFix.modifyConstructorWithAllAttributes(getContainerOfType(object, RosettaConstructorExpression.class));
acceptor.accept("Auto add all attributes.", semanticModification);
}


}
Loading