use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-eclipse by eclipse.
the class XtextGrammarQuickfixProvider method fixInvalidMetaModelName.
@Fix(INVALID_METAMODEL_NAME)
public void fixInvalidMetaModelName(final Issue issue, IssueResolutionAcceptor acceptor) {
final String metaModelName = issue.getData()[0];
acceptor.accept(issue, "Fix metamodel name '" + metaModelName + "'", "Fix metamodel name '" + metaModelName + "'", NULL_QUICKFIX_IMAGE, new IModification() {
@Override
public void apply(IModificationContext context) throws Exception {
context.getXtextDocument().replace(issue.getOffset(), issue.getLength(), Strings.toFirstLower(metaModelName));
}
});
}
use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-eclipse by eclipse.
the class XtextGrammarQuickfixProvider method fixTerminalRuleName.
@Fix(INVALID_TERMINALRULE_NAME)
public void fixTerminalRuleName(final Issue issue, IssueResolutionAcceptor acceptor) {
if (issue.getData().length == 1) {
final String upperCase = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, issue.getData()[0]).toString();
acceptor.accept(issue, "Change name to " + upperCase, "Change name to " + upperCase, "upcase.png", new IModification() {
@Override
public void apply(IModificationContext context) throws Exception {
final IXtextDocument xtextDocument = context.getXtextDocument();
xtextDocument.replace(issue.getOffset(), issue.getLength(), upperCase);
xtextDocument.modify(new IUnitOfWork.Void<XtextResource>() {
@Override
public void process(XtextResource state) throws Exception {
final EObject terminalRule = state.getEObject(issue.getUriToProblem().fragment());
Iterable<RuleCall> candidates = Iterables.filter(Iterables.filter(Lists.newArrayList(state.getAllContents()), RuleCall.class), new Predicate<RuleCall>() {
@Override
public boolean apply(RuleCall ruleCall) {
return ruleCall.getRule() == terminalRule;
}
});
for (RuleCall ruleCall : candidates) {
List<INode> nodes = NodeModelUtils.findNodesForFeature(ruleCall, XtextPackage.eINSTANCE.getRuleCall_Rule());
for (INode node : nodes) {
ITextRegion textRegion = node.getTextRegion();
xtextDocument.replace(textRegion.getOffset(), textRegion.getLength(), upperCase);
}
}
}
});
}
});
}
}
use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-eclipse by eclipse.
the class XtextGrammarQuickfixProvider method fixConvertTerminalFragmentToTerminalRule.
@Fix(INVALID_HIDDEN_TOKEN_FRAGMENT)
@Fix(INVALID_TERMINAL_FRAGMENT_RULE_REFERENCE)
public void fixConvertTerminalFragmentToTerminalRule(Issue issue, IssueResolutionAcceptor acceptor) {
//
acceptor.accept(//
issue, //
"Convert terminal fragment to terminal rule", //
"Convert terminal fragment to terminal rule", //
NULL_QUICKFIX_IMAGE, (context) -> {
IXtextDocument xtextDocument = context.getXtextDocument();
xtextDocument.modify(new IUnitOfWork.Void<XtextResource>() {
@Override
public void process(XtextResource state) throws Exception {
if (state == null) {
return;
}
EObject eObject = state.getEObject(issue.getUriToProblem().fragment());
AbstractRule rule = null;
if (eObject instanceof Grammar) {
rule = ((Grammar) eObject).getHiddenTokens().get(Integer.valueOf(issue.getData()[0]).intValue());
} else if (eObject instanceof ParserRule) {
rule = ((ParserRule) eObject).getHiddenTokens().get(Integer.valueOf(issue.getData()[0]).intValue());
} else if (eObject instanceof RuleCall) {
RuleCall ruleCall = (RuleCall) state.getEObject(issue.getUriToProblem().fragment());
rule = ruleCall.getRule();
}
if (rule != null) {
ICompositeNode ruleNode = NodeModelUtils.findActualNodeFor(rule);
for (INode node : ruleNode.getAsTreeIterable()) {
if (node.getGrammarElement() != null && node.getGrammarElement() == grammarAccess.getTerminalRuleAccess().getFragmentFragmentKeyword_2_0_0_0()) {
ITextRegion fragmentRegion = node.getTextRegion();
ICompositeNode compositeNode = (ICompositeNode) node.getNextSibling();
if (compositeNode.getFirstChild().getGrammarElement() == grammarAccess.getWSRule()) {
xtextDocument.replace(fragmentRegion.getOffset(), fragmentRegion.getLength() + 1, "");
} else {
xtextDocument.replace(fragmentRegion.getOffset(), fragmentRegion.getLength(), "");
}
break;
}
}
}
}
});
});
}
use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-eclipse by eclipse.
the class XbaseQuickfixProvider method fixTypeArguments.
@Fix(IssueCodes.INVALID_TYPE_ARGUMENTS_ON_TYPE_LITERAL)
public void fixTypeArguments(final Issue issue, IssueResolutionAcceptor acceptor) {
String message = issue.getMessage();
String fixup = "Remove invalid type arguments";
if (message.contains("argument.")) {
fixup = "Remove invalid type argument";
}
acceptor.accept(issue, fixup, fixup, null, new IModification() {
@Override
public void apply(IModificationContext context) throws Exception {
IXtextDocument document = context.getXtextDocument();
document.replace(issue.getOffset(), issue.getLength(), "");
}
});
}
use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-eclipse by eclipse.
the class XbaseQuickfixProvider method fixUnreachableCase.
@Fix(IssueCodes.UNREACHABLE_CASE)
public void fixUnreachableCase(final Issue issue, IssueResolutionAcceptor acceptor) {
acceptor.accept(issue, "Remove case", "Remove case", null, new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
remove(element, XCasePart.class, context);
}
});
acceptor.accept(issue, "Move case up", "Move case up", null, new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
XCasePart casePart = EcoreUtil2.getContainerOfType(element, XCasePart.class);
if (casePart == null) {
return;
}
ICompositeNode caseNode = NodeModelUtils.findActualNodeFor(casePart);
if (caseNode == null) {
return;
}
XSwitchExpression switchExpression = EcoreUtil2.getContainerOfType(casePart, XSwitchExpression.class);
if (switchExpression == null) {
return;
}
ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, switchExpression);
LightweightTypeReference actualTypeReference = owner.toLightweightTypeReference(casePart.getTypeGuard());
for (XCasePart previousCasePart : switchExpression.getCases()) {
if (previousCasePart == casePart) {
return;
}
JvmTypeReference typeGuard = previousCasePart.getTypeGuard();
if (typeGuard == null || previousCasePart.getCase() != null) {
continue;
}
LightweightTypeReference previousTypeReference = owner.toLightweightTypeReference(typeGuard);
if (typesOrderUtil.isHandled(actualTypeReference, previousTypeReference)) {
ICompositeNode previousCaseNode = NodeModelUtils.findActualNodeFor(previousCasePart);
if (previousCaseNode == null) {
return;
}
moveUp(caseNode, previousCaseNode, context);
return;
}
}
}
});
}
Aggregations