use of org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor in project xtext-eclipse by eclipse.
the class DeclarativeQuickfixProviderTest method testGetResolutions.
@Test
public void testGetResolutions() throws Exception {
AbstractDeclarativeQuickfixProvider provider = new AbstractDeclarativeQuickfixProvider() {
@Fix(DeclarativeQuickfixProviderTest.DUMMY_CODE)
public void fixError1(Issue i, IssueResolutionAcceptor acceptor) {
acceptor.accept(i, "fixError1", "", "", IModification.NULL);
}
@Fix(DeclarativeQuickfixProviderTest.DUMMY_CODE)
public void fixError2(Issue i, IssueResolutionAcceptor acceptor) {
acceptor.accept(i, "fixError2", "", "", IModification.NULL);
}
};
provider.setIssueResolutionAcceptorProvider(() -> new IssueResolutionAcceptor(it -> null));
List<IssueResolution> resolutionsIterable = provider.getResolutions(createIssue(DeclarativeQuickfixProviderTest.DUMMY_CODE + 1));
assertNull(Iterables.getFirst(resolutionsIterable, null));
List<IssueResolution> resolutions = provider.getResolutions(createIssue(DeclarativeQuickfixProviderTest.DUMMY_CODE));
assertEquals(2, resolutions.size());
assertTrue(resolutions.stream().anyMatch(it -> "fixError1".equals(it.getLabel())));
assertTrue(resolutions.stream().anyMatch(it -> "fixError2".equals(it.getLabel())));
}
use of org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method removeUnusedPrivateMember.
@Fix(IssueCodes.UNUSED_PRIVATE_MEMBER)
public void removeUnusedPrivateMember(final Issue issue, IssueResolutionAcceptor acceptor) {
// use the same label, description and image
// to be able to use the quickfixes (issue resolution) in batch mode
String label = "Remove member.";
String description = "Remove the unused private member.";
String image = "delete_edit.png";
acceptor.accept(issue, label, description, image, new IModification() {
@Override
public void apply(IModificationContext context) throws Exception {
final IXtextDocument document = context.getXtextDocument();
document.modify(new IUnitOfWork.Void<XtextResource>() {
@Override
public void process(XtextResource state) throws Exception {
final EObject element = state.getEObject(issue.getUriToProblem().fragment());
final ICompositeNode elementNode = NodeModelUtils.findActualNodeFor(element);
final Position elementPosition = new Position(elementNode.getOffset(), elementNode.getLength());
document.addPosition(elementPosition);
if (element instanceof XtendField) {
final JvmField field = associations.getJvmField((XtendField) element);
final List<Position> assignments = EcoreUtil2.eAllContentsAsList(element.eResource()).stream().filter(XAssignment.class::isInstance).map(XAssignment.class::cast).filter(assignment -> field == assignment.getFeature()).map(assignment -> {
final ICompositeNode assignmentNode = NodeModelUtils.findActualNodeFor(assignment);
final int offset = assignmentNode.getOffset();
final XExpression assignmentValue = assignment.getValue();
int length = assignmentNode.getLength();
if (expressionHelper.hasSideEffects(assignmentValue)) {
length -= NodeModelUtils.findActualNodeFor(assignmentValue).getLength();
}
return new Position(offset, length);
}).collect(Collectors.toList());
for (final Position assignment : assignments) {
document.addPosition(assignment);
}
for (final Position assignment : assignments) {
document.replace(assignment.getOffset(), assignment.getLength(), "");
}
}
document.replace(elementPosition.getOffset(), elementPosition.getLength(), "");
}
});
organizeImportsHandler.doOrganizeImports(document);
}
});
}
Aggregations