use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-eclipse by eclipse.
the class XbaseQuickfixProvider method fixObsoletCast.
@Fix(IssueCodes.OBSOLETE_CAST)
public void fixObsoletCast(final Issue issue, IssueResolutionAcceptor acceptor) {
String fixup = "Remove unnecessary cast";
acceptor.accept(issue, fixup, fixup, null, new IModification() {
@Override
public void apply(IModificationContext context) throws Exception {
final IXtextDocument document = context.getXtextDocument();
ReplaceRegion replacement = document.tryReadOnly(new IUnitOfWork<ReplaceRegion, XtextResource>() {
@Override
public ReplaceRegion exec(XtextResource state) throws Exception {
EObject type = state.getEObject(issue.getUriToProblem().fragment());
XCastedExpression cast = EcoreUtil2.getContainerOfType(type, XCastedExpression.class);
INode castNode = NodeModelUtils.findActualNodeFor(cast);
INode targetNode = NodeModelUtils.findActualNodeFor(cast.getTarget());
return new ReplaceRegion(castNode.getTotalTextRegion(), targetNode.getText());
}
});
if (replacement != null) {
document.replace(replacement.getOffset(), replacement.getLength(), replacement.getText());
}
}
});
}
use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-eclipse by eclipse.
the class XbaseQuickfixProvider method fixRedundantCase.
@Fix(IssueCodes.REDUNDANT_CASE)
public void fixRedundantCase(final Issue issue, IssueResolutionAcceptor acceptor) {
acceptor.accept(issue, "Remove redundant case.", "Remove redundant case.", null, new ReplaceModification(issue, ""));
acceptor.accept(issue, "Assign empty expression.", "Assign empty expression.", null, new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
XSwitchExpression switchExpression = EcoreUtil2.getContainerOfType(element, XSwitchExpression.class);
if (switchExpression == null) {
return;
}
XCasePart casePart = IterableExtensions.last(switchExpression.getCases());
if (casePart == null || !(casePart.isFallThrough() && casePart.getThen() == null)) {
return;
}
List<INode> nodes = NodeModelUtils.findNodesForFeature(casePart, XbasePackage.Literals.XCASE_PART__FALL_THROUGH);
if (nodes.isEmpty()) {
return;
}
INode firstNode = IterableExtensions.head(nodes);
INode lastNode = IterableExtensions.last(nodes);
int offset = firstNode.getOffset();
int length = lastNode.getEndOffset() - offset;
ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) element.eResource(), offset, length);
appendable.append(": {");
appendable.increaseIndentation().newLine();
appendable.decreaseIndentation().newLine().append("}");
appendable.commitChanges();
}
});
}
use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-eclipse by eclipse.
the class DomainmodelQuickfixProvider method fixTypeName.
@Fix(IssueCodes.INVALID_TYPE_NAME)
public void fixTypeName(Issue issue, IssueResolutionAcceptor acceptor) {
// exemplary textual modification
acceptor.accept(// exemplary textual modification
issue, // exemplary textual modification
"Capitalize name", // exemplary textual modification
"Capitalize name of '" + issue.getData()[0] + "'", // exemplary textual modification
"upcase.png", (IModificationContext context) -> {
IXtextDocument xtextDocument = context.getXtextDocument();
String firstLetter = xtextDocument.get(issue.getOffset(), 1);
xtextDocument.replace(issue.getOffset(), 1, Strings.toFirstUpper(firstLetter));
});
}
Aggregations