Skip to content

Commit

Permalink
Fixed quick fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCockx committed Aug 31, 2023
1 parent 8469923 commit dd16d1e
Showing 1 changed file with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import jakarta.inject.Inject;

Expand All @@ -17,6 +16,7 @@
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.xtext.ide.editor.quickfix.DiagnosticResolution;
import org.eclipse.xtext.ide.editor.quickfix.IQuickFixProvider;
import org.eclipse.xtext.ide.server.ILanguageServerAccess;
import org.eclipse.xtext.ide.server.codeActions.ICodeActionService2;

/*
Expand All @@ -30,26 +30,35 @@ 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);

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

List<Either<Command, CodeAction>> result = new ArrayList<>();
for (Diagnostic diagnostic : options.getCodeActionParams().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)));
if (handlesDiagnostic(diagnostic)) {
result.addAll(options.getLanguageServerAccess()
.doSyncRead(options.getURI(), (ILanguageServerAccess.Context context) -> {
options.setDocument(context.getDocument());
options.setResource(context.getResource());
Options diagnosticOptions = createOptionsForSingleDiagnostic(options, diagnostic);
return getCodeActions(diagnosticOptions, diagnostic);
}));
}
}
return result;
}

protected boolean handlesDiagnostic(Diagnostic diagnostic) {
return quickfixes.handlesDiagnostic(diagnostic);
}

protected List<Either<Command, CodeAction>> getCodeActions(Options options, Diagnostic diagnostic) {
List<Either<Command, CodeAction>> codeActions = new ArrayList<>();

quickfixes.getResolutions(options, diagnostic).stream()
.sorted(Comparator
.nullsLast(Comparator.comparing(DiagnosticResolution::getLabel)))
.forEach(r -> codeActions.add(Either.forRight(createFix(r, diagnostic))));
return codeActions;
}

private CodeAction createFix(DiagnosticResolution resolution, Diagnostic diagnostic) {
CodeAction codeAction = new CodeAction();
Expand Down

0 comments on commit dd16d1e

Please sign in to comment.