use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method refactorTernaryOperatorIntoNormalIf.
@Fix(IssueCodes.TERNARY_EXPRESSION_NOT_ALLOWED)
public void refactorTernaryOperatorIntoNormalIf(final Issue issue, IssueResolutionAcceptor acceptor) {
String label = "Refactor into inline if-expression.";
String description = "Refactor ternary expression (cond? a : b) \ninto an inline if-expression (if cond a else b).";
String image = "delete_edit.png";
// stop before NullpointerExceptions are thrown
if (issue == null)
return;
// Fix
acceptor.accept(issue, label, description, image, new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
// Collect basic elements
IXtextDocument xtextDocument = context.getXtextDocument();
if (xtextDocument == null || element == null || !(element instanceof XExpression))
return;
XIfExpression exp = (XIfExpression) element;
// Collect original ternary expression information
ICompositeNode expNode = NodeModelUtils.findActualNodeFor(exp);
if (expNode == null)
throw new IllegalStateException("Node to refactor may not be null");
int expOffset = expNode.getTextRegion().getOffset();
int expLength = expNode.getTextRegion().getLength();
String ifExpr = this.buildIfExpression(exp, xtextDocument);
xtextDocument.replace(expOffset, expLength, ifExpr);
}
/**
* Build an inline if expression from the given ternary expression
* Also apply this fix to nested elements in then or else part
*/
private String buildIfExpression(XIfExpression expression, IXtextDocument document) throws Exception {
// Basic elements
XExpression thenPart = expression.getThen();
XExpression elsePart = expression.getElse();
boolean outerBracketsSet = false;
// Collect String representations
String ternExpStr = this.getString(NodeModelUtils.findActualNodeFor(expression), document);
if (ternExpStr.endsWith(")"))
outerBracketsSet = true;
String condStr = this.getString(NodeModelUtils.findActualNodeFor(expression.getIf()), document);
if (!condStr.contains("(")) {
condStr = "(" + condStr + ")";
}
// Check if then or else part is a ternary expression, too,
// and apply the fix to the inner elements
String thenStr = "", elseStr = "";
if (thenPart instanceof XIfExpression) {
if (((XIfExpression) thenPart).isConditionalExpression()) {
thenStr = buildIfExpression((XIfExpression) thenPart, document);
}
}
if (elsePart != null && elsePart instanceof XIfExpression) {
if (((XIfExpression) elsePart).isConditionalExpression()) {
elseStr = buildIfExpression((XIfExpression) elsePart, document);
}
}
// if then/else part does not contain nested ternary expressions
if (thenStr.isEmpty()) {
thenStr = this.getString(NodeModelUtils.findActualNodeFor(thenPart), document);
}
if (elsePart != null && elseStr.isEmpty()) {
elseStr = this.getString(NodeModelUtils.findActualNodeFor(elsePart), document);
}
// Combine
String ifExpStr = this.combineIfExpParts(condStr, thenStr, elseStr, outerBracketsSet);
return ifExpStr;
}
/**
* Stitch together the inline if statement
*/
private String combineIfExpParts(String condStr, String thenStr, String elseStr, boolean outerBracketsSet) {
String ifExpr = "if " + condStr + " " + thenStr;
if (!elseStr.isEmpty())
ifExpr += " else " + elseStr;
if (outerBracketsSet)
ifExpr = "(" + ifExpr + ")";
return ifExpr;
}
/**
* Get the String from a document Node
* Attention! The Document will not be updated in the meantime, so offsets and thus
* the outputString might get messed up, if the document is changed and the String
* is fetched after that.
*/
private String getString(ICompositeNode partNode, IXtextDocument doc) throws Exception {
if (partNode == null)
throw new IllegalStateException("Node to refactor may not be null");
ITextRegion partRegion = partNode.getTextRegion();
int partOffset = partRegion.getOffset();
int partLength = partRegion.getLength();
String partString = doc.get(partOffset, partLength);
return partString;
}
});
}
use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method removeUnnecessaryModifier.
@Fix(IssueCodes.UNNECESSARY_MODIFIER)
public void removeUnnecessaryModifier(final Issue issue, IssueResolutionAcceptor acceptor) {
String[] issueData = issue.getData();
if (issueData == null || issueData.length == 0) {
return;
}
// use the same label, description and image
// to be able to use the quickfixes (issue resolution) in batch mode
String label = "Remove the unnecessary modifier.";
String description = "The modifier is unnecessary and could be removed.";
String image = "fix_indent.gif";
acceptor.accept(issue, label, description, image, new ITextualMultiModification() {
@Override
public void apply(IModificationContext context) throws Exception {
if (context instanceof IssueModificationContext) {
Issue theIssue = ((IssueModificationContext) context).getIssue();
Integer offset = theIssue.getOffset();
IXtextDocument document = context.getXtextDocument();
document.replace(offset, theIssue.getLength(), "");
while (Character.isWhitespace(document.getChar(offset))) {
document.replace(offset, 1, "");
}
}
}
});
}
use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method implementAbstractMethods.
@Fix(IssueCodes.CLASS_MUST_BE_ABSTRACT)
public void implementAbstractMethods(final Issue issue, IssueResolutionAcceptor acceptor) {
doOverrideMethods(issue, acceptor, "Add unimplemented methods");
acceptor.accept(issue, "Make class abstract", "Make class abstract", "fix_indent.gif", new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
internalDoAddAbstractKeyword(element, context);
}
});
}
use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-eclipse by eclipse.
the class DomainmodelQuickfixProvider method fixFeatureName.
@Fix(IssueCodes.INVALID_FEATURE_NAME)
public void fixFeatureName(final Issue issue, final IssueResolutionAcceptor acceptor) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("Uncapitalize name of \'");
String _get = issue.getData()[0];
_builder.append(_get);
_builder.append("\'");
final ISemanticModification _function = (EObject element, IModificationContext context) -> {
((Feature) element).setName(Strings.toFirstLower(issue.getData()[0]));
};
acceptor.accept(issue, "Uncapitalize name", _builder.toString(), "upcase.png", _function);
}
use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-eclipse by eclipse.
the class XbaseQuickfixProvider method fixEqualsWithNull.
@Fix(IssueCodes.EQUALS_WITH_NULL)
public void fixEqualsWithNull(final Issue issue, IssueResolutionAcceptor acceptor) {
String[] data = issue.getData();
if (data == null || data.length == 0) {
return;
}
String operator = data[0];
String message = "Replace '" + operator + "' with '" + operator + "='";
acceptor.accept(issue, message, message, null, new IModification() {
@Override
public void apply(IModificationContext context) throws Exception {
IXtextDocument document = context.getXtextDocument();
document.replace(issue.getOffset(), issue.getLength(), operator + "=");
}
});
}
Aggregations