Search in sources :

Example 1 with Rule

use of com.avaloq.tools.ddk.xtext.format.format.Rule in project dsl-devkit by dsldevkit.

the class FormatQuickfixProvider method removeOverride.

/**
 * Semantic quickfix removing the override flag for a rule.
 *
 * @param issue
 *          the issue
 * @param acceptor
 *          the acceptor
 */
@Fix(FormatJavaValidator.OVERRIDE_ILLEGAL_CODE)
public void removeOverride(final Issue issue, final IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Remove override", "Remove override.", null, new IModification() {

        @Override
        public void apply(final IModificationContext context) throws BadLocationException {
            context.getXtextDocument().modify(new IUnitOfWork<Void, XtextResource>() {

                @Override
                public java.lang.Void exec(final XtextResource state) {
                    Rule rule = (Rule) state.getEObject(issue.getUriToProblem().fragment());
                    rule.setOverride(false);
                    return null;
                }
            });
        }
    });
}
Also used : IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) XtextResource(org.eclipse.xtext.resource.XtextResource) Rule(com.avaloq.tools.ddk.xtext.format.format.Rule) BadLocationException(org.eclipse.jface.text.BadLocationException) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 2 with Rule

use of com.avaloq.tools.ddk.xtext.format.format.Rule in project dsl-devkit by dsldevkit.

the class FormatExtensions method getAllGrammarRules.

/**
 * Gets all GrammarRules in the inheritance hierarchy, including overridden rules.
 *
 * @param model
 *          the model
 * @return the all grammar rules
 */
private static List<GrammarRule> getAllGrammarRules(final FormatConfiguration model) {
    List<GrammarRule> allRules = Lists.newArrayList();
    if (model.getExtendedFormatConfiguration() != null && !model.getExtendedFormatConfiguration().eIsProxy()) {
        allRules.addAll(getAllGrammarRules(model.getExtendedFormatConfiguration()));
    }
    Predicate<Rule> resolvedGrammarRules = new Predicate<Rule>() {

        @Override
        public boolean apply(final Rule input) {
            return input instanceof GrammarRule && ((GrammarRule) input).getTargetRule() != null && !((GrammarRule) input).getTargetRule().eIsProxy();
        }
    };
    for (Rule rule : Iterables.filter(model.getRules(), resolvedGrammarRules)) {
        allRules.add((GrammarRule) rule);
    }
    return allRules;
}
Also used : GrammarRule(com.avaloq.tools.ddk.xtext.format.format.GrammarRule) EnumRule(org.eclipse.xtext.EnumRule) WildcardRule(com.avaloq.tools.ddk.xtext.format.format.WildcardRule) GrammarRule(com.avaloq.tools.ddk.xtext.format.format.GrammarRule) TerminalRule(org.eclipse.xtext.TerminalRule) ParserRule(org.eclipse.xtext.ParserRule) AbstractRule(org.eclipse.xtext.AbstractRule) Rule(com.avaloq.tools.ddk.xtext.format.format.Rule) Predicate(com.google.common.base.Predicate)

Example 3 with Rule

use of com.avaloq.tools.ddk.xtext.format.format.Rule in project dsl-devkit by dsldevkit.

the class FormatJavaValidator method checkOverrideMissing.

/**
 * Checks that rules declare overrides when there is a corresponding inherited rule.
 *
 * @param model
 *          the model
 */
@Check
public void checkOverrideMissing(final FormatConfiguration model) {
    FormatConfiguration extendedModel = model.getExtendedFormatConfiguration();
    if (extendedModel == null || extendedModel.eIsProxy()) {
        return;
    }
    Iterable<Rule> nonOverrideRules = Iterables.filter(model.getRules(), Predicates.not(IS_OVERRIDE));
    Iterable<Rule> overriddenRules = collectRules(extendedModel);
    Map<AbstractRule, GrammarRule> localAbstractRuleMap = Maps.newHashMap();
    for (GrammarRule rule : Iterables.filter(nonOverrideRules, GrammarRule.class)) {
        localAbstractRuleMap.put(TARGET_RULE.apply(rule), rule);
    }
    // Check GrammarRules
    for (GrammarRule overriddenRule : Iterables.filter(overriddenRules, GrammarRule.class)) {
        if (localAbstractRuleMap.containsKey(TARGET_RULE.apply(overriddenRule))) {
            GrammarRule localRule = localAbstractRuleMap.get(TARGET_RULE.apply(overriddenRule));
            error(OVERRIDE_MISSING_MESSAGE, localRule, FormatPackage.Literals.GRAMMAR_RULE__TARGET_RULE, OVERRIDE_MISSING_CODE);
        }
    }
    // Check WildcardRule
    if (!Iterables.isEmpty(Iterables.filter(nonOverrideRules, WildcardRule.class)) && !Iterables.isEmpty(Iterables.filter(overriddenRules, WildcardRule.class))) {
        error(OVERRIDE_MISSING_MESSAGE, Iterables.filter(nonOverrideRules, WildcardRule.class).iterator().next(), null, OVERRIDE_MISSING_CODE);
    }
}
Also used : FormatConfiguration(com.avaloq.tools.ddk.xtext.format.format.FormatConfiguration) WildcardRule(com.avaloq.tools.ddk.xtext.format.format.WildcardRule) GrammarRule(com.avaloq.tools.ddk.xtext.format.format.GrammarRule) Rule(com.avaloq.tools.ddk.xtext.format.format.Rule) EnumRule(org.eclipse.xtext.EnumRule) WildcardRule(com.avaloq.tools.ddk.xtext.format.format.WildcardRule) GrammarRule(com.avaloq.tools.ddk.xtext.format.format.GrammarRule) TerminalRule(org.eclipse.xtext.TerminalRule) ParserRule(org.eclipse.xtext.ParserRule) AbstractRule(org.eclipse.xtext.AbstractRule) AbstractRule(org.eclipse.xtext.AbstractRule) Check(org.eclipse.xtext.validation.Check)

Example 4 with Rule

use of com.avaloq.tools.ddk.xtext.format.format.Rule in project dsl-devkit by dsldevkit.

the class FormatJavaValidator method collectRules.

/**
 * Collect all rules contained by the given model (including any of its extended configurations).
 *
 * @param model
 *          the model
 * @return all rules
 */
private Iterable<Rule> collectRules(final FormatConfiguration model) {
    Iterable<Rule> result = model.getRules();
    FormatConfiguration extendedModel = model.getExtendedFormatConfiguration();
    if (extendedModel != null && !extendedModel.eIsProxy()) {
        result = Iterables.concat(result, collectRules(extendedModel));
    }
    return result;
}
Also used : FormatConfiguration(com.avaloq.tools.ddk.xtext.format.format.FormatConfiguration) Rule(com.avaloq.tools.ddk.xtext.format.format.Rule) EnumRule(org.eclipse.xtext.EnumRule) WildcardRule(com.avaloq.tools.ddk.xtext.format.format.WildcardRule) GrammarRule(com.avaloq.tools.ddk.xtext.format.format.GrammarRule) TerminalRule(org.eclipse.xtext.TerminalRule) ParserRule(org.eclipse.xtext.ParserRule) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 5 with Rule

use of com.avaloq.tools.ddk.xtext.format.format.Rule in project dsl-devkit by dsldevkit.

the class FormatJavaValidator method checkIllegalOverride.

/**
 * Checks that no rule declares override when there is no corresponding inherited rule.
 *
 * @param model
 *          the model
 */
@Check
public void checkIllegalOverride(final FormatConfiguration model) {
    Iterable<Rule> overrideRules = Iterables.filter(model.getRules(), IS_OVERRIDE);
    Iterable<Rule> overrideableRules = Lists.newArrayList();
    FormatConfiguration extendedModel = model.getExtendedFormatConfiguration();
    if (extendedModel != null && !extendedModel.eIsProxy()) {
        overrideableRules = collectRules(extendedModel);
    }
    Map<AbstractRule, GrammarRule> overrideableAbstractRuleMap = Maps.newHashMap();
    for (GrammarRule rule : Iterables.filter(overrideableRules, GrammarRule.class)) {
        overrideableAbstractRuleMap.put(TARGET_RULE.apply(rule), rule);
    }
    // Check GrammarRules
    for (GrammarRule overrideRule : Iterables.filter(overrideRules, GrammarRule.class)) {
        if (!overrideableAbstractRuleMap.containsKey(TARGET_RULE.apply(overrideRule))) {
            error(OVERRIDE_ILLEGAL_MESSAGE, overrideRule, FormatPackage.Literals.GRAMMAR_RULE__TARGET_RULE, OVERRIDE_ILLEGAL_CODE);
        }
    }
    // Check WildcardRule
    if (!Iterables.isEmpty(Iterables.filter(overrideRules, WildcardRule.class)) && Iterables.isEmpty(Iterables.filter(overrideableRules, WildcardRule.class))) {
        error(OVERRIDE_ILLEGAL_MESSAGE, Iterables.filter(overrideRules, WildcardRule.class).iterator().next(), null, OVERRIDE_ILLEGAL_CODE);
    }
}
Also used : FormatConfiguration(com.avaloq.tools.ddk.xtext.format.format.FormatConfiguration) WildcardRule(com.avaloq.tools.ddk.xtext.format.format.WildcardRule) GrammarRule(com.avaloq.tools.ddk.xtext.format.format.GrammarRule) Rule(com.avaloq.tools.ddk.xtext.format.format.Rule) EnumRule(org.eclipse.xtext.EnumRule) WildcardRule(com.avaloq.tools.ddk.xtext.format.format.WildcardRule) GrammarRule(com.avaloq.tools.ddk.xtext.format.format.GrammarRule) TerminalRule(org.eclipse.xtext.TerminalRule) ParserRule(org.eclipse.xtext.ParserRule) AbstractRule(org.eclipse.xtext.AbstractRule) AbstractRule(org.eclipse.xtext.AbstractRule) Check(org.eclipse.xtext.validation.Check)

Aggregations

Rule (com.avaloq.tools.ddk.xtext.format.format.Rule)9 GrammarRule (com.avaloq.tools.ddk.xtext.format.format.GrammarRule)7 WildcardRule (com.avaloq.tools.ddk.xtext.format.format.WildcardRule)7 AbstractRule (org.eclipse.xtext.AbstractRule)5 EnumRule (org.eclipse.xtext.EnumRule)5 ParserRule (org.eclipse.xtext.ParserRule)5 TerminalRule (org.eclipse.xtext.TerminalRule)5 FormatConfiguration (com.avaloq.tools.ddk.xtext.format.format.FormatConfiguration)4 Predicate (com.google.common.base.Predicate)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 XtextResource (org.eclipse.xtext.resource.XtextResource)2 IModification (org.eclipse.xtext.ui.editor.model.edit.IModification)2 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)2 Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)2 IUnitOfWork (org.eclipse.xtext.util.concurrent.IUnitOfWork)2 Check (org.eclipse.xtext.validation.Check)2 Region (org.eclipse.jface.text.Region)1 INode (org.eclipse.xtext.nodemodel.INode)1 IParseResult (org.eclipse.xtext.parser.IParseResult)1