use of org.eclipse.xtext.ide.editor.quickfix.DiagnosticResolution in project xtext-core by eclipse.
the class QuickFixCodeActionService method getCodeActions.
@Override
public List<Either<Command, CodeAction>> getCodeActions(Options options) {
List<Either<Command, CodeAction>> result = new ArrayList<>();
boolean handleQuickfixes = options.getCodeActionParams().getContext().getOnly() == null || options.getCodeActionParams().getContext().getOnly().isEmpty() || options.getCodeActionParams().getContext().getOnly().contains(CodeActionKind.QuickFix);
if (handleQuickfixes) {
for (Diagnostic error : options.getCodeActionParams().getContext().getDiagnostics()) {
List<DiagnosticResolution> resolutions = quickfixes.getResolutions(options, error);
for (DiagnosticResolution resolution : resolutions) {
result.add(Either.forRight(createFix(resolution, error)));
}
}
}
return result;
}
use of org.eclipse.xtext.ide.editor.quickfix.DiagnosticResolution in project xtext-core by eclipse.
the class AbstractIdeQuickfixTest method quickfixesAreOffered.
private void quickfixesAreOffered(EObject target, String issueCode, String originalText, QuickfixExpectation... expected) {
List<QuickfixExpectation> expectedSorted = IterableExtensions.sortBy(Arrays.asList(expected), it -> it.label);
ICompositeNode elementNode = NodeModelUtils.getNode(target);
LineAndColumn elementStartPosition = NodeModelUtils.getLineAndColumn(elementNode, elementNode.getOffset());
LineAndColumn elementEndPosition = NodeModelUtils.getLineAndColumn(elementNode, elementNode.getEndOffset());
Position startPos = new Position(elementStartPosition.getLine() - 1, elementStartPosition.getColumn() - 1);
Position endPos = new Position(elementEndPosition.getLine() - 1, elementEndPosition.getColumn() - 1);
Diagnostic issue = new Diagnostic();
issue.setCode(issueCode);
issue.setMessage("error");
issue.setSeverity(DiagnosticSeverity.Error);
issue.setSource("source");
issue.setRange(new Range(startPos, endPos));
ICodeActionService2.Options options = new ICodeActionService2.Options();
options.setCancelIndicator(CancelIndicator.NullImpl);
options.setDocument(new Document(Integer.valueOf(0), originalText));
options.setResource((XtextResource) target.eResource());
options.setLanguageServerAccess(new ILanguageServerAccess() {
@Override
public void addBuildListener(ILanguageServerAccess.IBuildListener listener) {
throw new UnsupportedOperationException();
}
@Override
public <T extends Object> CompletableFuture<T> doRead(String uri, Function<ILanguageServerAccess.Context, T> function) {
ILanguageServerAccess.Context ctx = new ILanguageServerAccess.Context(options.getResource(), options.getDocument(), true, CancelIndicator.NullImpl);
return CompletableFuture.completedFuture(function.apply(ctx));
}
@Override
public <T extends Object> CompletableFuture<T> doReadIndex(Function<? super ILanguageServerAccess.IndexContext, ? extends T> function) {
return null;
}
@Override
public InitializeParams getInitializeParams() {
return null;
}
@Override
public InitializeResult getInitializeResult() {
return null;
}
@Override
public LanguageClient getLanguageClient() {
return null;
}
@Override
public ResourceSet newLiveScopeResourceSet(URI uri) {
// re-using the existing ResourceSet because it contains the URI protocol mapping for "inmemory" resources.
ResourceSet resourceSet = options.getResource().getResourceSet();
return resourceSet;
}
});
CodeActionParams codeActionParams = new CodeActionParams();
codeActionParams.setRange(new Range(startPos, endPos));
codeActionParams.setTextDocument(new TextDocumentIdentifier(target.eResource().getURI().toString()));
CodeActionContext codeActionContext = new CodeActionContext();
codeActionContext.setDiagnostics(Collections.singletonList(issue));
codeActionParams.setContext(codeActionContext);
options.setCodeActionParams(codeActionParams);
List<DiagnosticResolution> actualIssueResolutions = IterableExtensions.sortBy(quickFixProvider.getResolutions(options, issue), DiagnosticResolution::getLabel);
assertEquals("The number of quickfixes does not match!", expectedSorted.size(), actualIssueResolutions.size());
for (int i = 0; i < actualIssueResolutions.size(); i++) {
DiagnosticResolution actualIssueResolution = actualIssueResolutions.get(i);
QuickfixExpectation expectedIssueResolution = expectedSorted.get(i);
assertEquals(expectedIssueResolution.label, actualIssueResolution.getLabel());
assertEquals(expectedIssueResolution.description, actualIssueResolution.getLabel());
assertIssueResolutionResult(toUnixLineSeparator(expectedIssueResolution.getExpectedResult()), actualIssueResolution, originalText, options.getDocument());
}
}
Aggregations