Search in sources :

Example 1 with EnumLiteralDeclaration

use of org.eclipse.xtext.EnumLiteralDeclaration in project xtext-eclipse by eclipse.

the class XtextGrammarQuickfixProvider method fixEmptyEnumLiteral.

@Fix(EMPTY_ENUM_LITERAL)
public void fixEmptyEnumLiteral(final Issue issue, IssueResolutionAcceptor acceptor) {
    acceptor.acceptMulti(issue, "Fix empty enum literal", "Fix empty enum literal", NULL_QUICKFIX_IMAGE, (EObject element) -> {
        EnumLiteralDeclaration enumLiteralDeclaration = (EnumLiteralDeclaration) element;
        Keyword keyword = XtextFactory.eINSTANCE.createKeyword();
        keyword.setValue(enumLiteralDeclaration.getEnumLiteral().getName().toLowerCase());
        enumLiteralDeclaration.setLiteral(keyword);
    });
}
Also used : EnumLiteralDeclaration(org.eclipse.xtext.EnumLiteralDeclaration) Keyword(org.eclipse.xtext.Keyword) EObject(org.eclipse.emf.ecore.EObject) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 2 with EnumLiteralDeclaration

use of org.eclipse.xtext.EnumLiteralDeclaration in project xtext-core by eclipse.

the class XtextTransientValueService2 method isValueTransient.

@Override
public ValueTransient isValueTransient(EObject owner, EStructuralFeature feature) {
    if (feature == XtextPackage.eINSTANCE.getTypeRef_Metamodel()) {
        final TypeRef typeRef = (TypeRef) owner;
        final AbstractMetamodelDeclaration m = typeRef.getMetamodel();
        if (m == null || Strings.isEmpty(m.getAlias()))
            return YES;
    } else if (feature == XtextPackage.eINSTANCE.getAbstractRule_Type()) {
        final AbstractRule rule = (AbstractRule) owner;
        if (!GrammarUtil.isDatatypeRule(rule) && (rule instanceof ParserRule || rule instanceof EnumRule)) {
            final TypeRef returnType = rule.getType();
            if (returnType == null || returnType.getClassifier() == null)
                return YES;
            else if (rule.getName().equals(returnType.getClassifier().getName())) {
                if (isValueTransient(returnType, XtextPackage.eINSTANCE.getTypeRef_Metamodel()) == YES)
                    return PREFERABLY;
                else
                    return NO;
            } else if (GrammarUtil.isDatatypeRule(rule)) {
                return NodeModelUtils.getNode(returnType) == null ? YES : NO;
            }
        } else if (rule instanceof TerminalRule || GrammarUtil.isDatatypeRule(rule)) {
            final TypeRef returnType = rule.getType();
            if (returnType == null)
                return YES;
            if (rule instanceof TerminalRule) {
                if (((TerminalRule) rule).isFragment()) {
                    return YES;
                }
            }
            if (GrammarUtil.findEString(GrammarUtil.getGrammar(owner)).equals(rule.getType().getClassifier()))
                return PREFERABLY;
            return NO;
        }
    } else if (feature == XtextPackage.eINSTANCE.getCrossReference_Terminal()) {
        final CrossReference ref = (CrossReference) owner;
        if (ref.getTerminal() instanceof RuleCall && ((RuleCall) ref.getTerminal()).getRule() != null) {
            if ("ID".equals(((RuleCall) ref.getTerminal()).getRule().getName()))
                return PREFERABLY;
            else
                return NO;
        }
        return YES;
    } else if (feature == XtextPackage.eINSTANCE.getEnumLiteralDeclaration_Literal()) {
        final EnumLiteralDeclaration decl = (EnumLiteralDeclaration) owner;
        if (decl.getEnumLiteral() != null && decl.getLiteral() != null) {
            if (Strings.equal(decl.getLiteral().getValue(), decl.getEnumLiteral().getName()))
                return PREFERABLY;
            return NO;
        }
        return YES;
    } else if (feature == XtextPackage.eINSTANCE.getRuleCall_ExplicitlyCalled()) {
        return YES;
    } else if (feature == XtextPackage.eINSTANCE.getNamedArgument_Parameter()) {
        if (((NamedArgument) owner).isCalledByName()) {
            return NO;
        }
        return YES;
    }
    return super.isValueTransient(owner, feature);
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) EnumRule(org.eclipse.xtext.EnumRule) EnumLiteralDeclaration(org.eclipse.xtext.EnumLiteralDeclaration) TypeRef(org.eclipse.xtext.TypeRef) CrossReference(org.eclipse.xtext.CrossReference) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 3 with EnumLiteralDeclaration

use of org.eclipse.xtext.EnumLiteralDeclaration in project xtext-core by eclipse.

the class XtextValidator method checkGeneratedEnumIsValid.

@Check
public void checkGeneratedEnumIsValid(EnumLiteralDeclaration decl) {
    EnumRule rule = GrammarUtil.containingEnumRule(decl);
    if (!(rule.getType().getMetamodel() instanceof GeneratedMetamodel))
        return;
    if (!(rule.getType().getClassifier() instanceof EEnum))
        return;
    EEnum eEnum = (EEnum) rule.getType().getClassifier();
    List<EnumLiteralDeclaration> declarations = EcoreUtil2.getAllContentsOfType(rule, EnumLiteralDeclaration.class);
    if (declarations.size() == eEnum.getELiterals().size())
        return;
    for (EnumLiteralDeclaration otherDecl : declarations) {
        if (decl == otherDecl) {
            return;
        }
        if (otherDecl.getEnumLiteral() == decl.getEnumLiteral()) {
            if (!decl.getEnumLiteral().getLiteral().equals(decl.getLiteral().getValue()))
                addIssue("Enum literal '" + decl.getEnumLiteral().getName() + "' has already been defined with literal '" + decl.getEnumLiteral().getLiteral() + "'.", decl, XtextPackage.Literals.ENUM_LITERAL_DECLARATION__ENUM_LITERAL, DUPLICATE_ENUM_LITERAL);
            return;
        }
    }
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) GeneratedMetamodel(org.eclipse.xtext.GeneratedMetamodel) EnumLiteralDeclaration(org.eclipse.xtext.EnumLiteralDeclaration) EEnum(org.eclipse.emf.ecore.EEnum) Check(org.eclipse.xtext.validation.Check)

Example 4 with EnumLiteralDeclaration

use of org.eclipse.xtext.EnumLiteralDeclaration in project xtext-core by eclipse.

the class XtextValidator method checkEnumLiteralIsUnique.

@Check
public void checkEnumLiteralIsUnique(EnumLiteralDeclaration decl) {
    EnumRule rule = GrammarUtil.containingEnumRule(decl);
    List<EnumLiteralDeclaration> declarations = EcoreUtil2.getAllContentsOfType(rule, EnumLiteralDeclaration.class);
    String literal = decl.getLiteral().getValue();
    if (literal != null) {
        for (EnumLiteralDeclaration otherDecl : declarations) {
            if (otherDecl != decl && literal.equals(otherDecl.getLiteral().getValue())) {
                error("Enum literal '" + literal + "' is used multiple times in enum rule '" + rule.getName() + "'.", XtextPackage.Literals.ENUM_LITERAL_DECLARATION__LITERAL);
            }
        }
    }
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) EnumLiteralDeclaration(org.eclipse.xtext.EnumLiteralDeclaration) Check(org.eclipse.xtext.validation.Check)

Example 5 with EnumLiteralDeclaration

use of org.eclipse.xtext.EnumLiteralDeclaration in project xtext-core by eclipse.

the class GrammarAccessExtensions method getSingleElementDescription.

private List<String> getSingleElementDescription(final AbstractElement ele) {
    final ArrayList<String> r = new ArrayList<String>(2);
    boolean _matched = false;
    if (ele instanceof Keyword) {
        _matched = true;
        String _value = ((Keyword) ele).getValue();
        r.add(_value);
    }
    if (!_matched) {
        if (ele instanceof Assignment) {
            _matched = true;
            String _feature = ((Assignment) ele).getFeature();
            r.add(_feature);
        }
    }
    if (!_matched) {
        if (ele instanceof RuleCall) {
            _matched = true;
            String _name = ((RuleCall) ele).getRule().getName();
            r.add(_name);
        }
    }
    if (!_matched) {
        if (ele instanceof Action) {
            _matched = true;
            TypeRef _type = ((Action) ele).getType();
            EClassifier _classifier = null;
            if (_type != null) {
                _classifier = _type.getClassifier();
            }
            boolean _tripleNotEquals = (_classifier != null);
            if (_tripleNotEquals) {
                String _name = ((Action) ele).getType().getClassifier().getName();
                r.add(_name);
            }
            boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(((Action) ele).getFeature());
            boolean _not = (!_isNullOrEmpty);
            if (_not) {
                String _feature = ((Action) ele).getFeature();
                r.add(_feature);
            }
        }
    }
    if (!_matched) {
        if (ele instanceof CrossReference) {
            _matched = true;
            TypeRef _type = ((CrossReference) ele).getType();
            EClassifier _classifier = null;
            if (_type != null) {
                _classifier = _type.getClassifier();
            }
            boolean _tripleNotEquals = (_classifier != null);
            if (_tripleNotEquals) {
                String _name = ((CrossReference) ele).getType().getClassifier().getName();
                r.add(_name);
            }
        }
    }
    if (!_matched) {
        if (ele instanceof EnumLiteralDeclaration) {
            _matched = true;
            String _name = ((EnumLiteralDeclaration) ele).getEnumLiteral().getName();
            r.add(_name);
        }
    }
    return r;
}
Also used : Assignment(org.eclipse.xtext.Assignment) Action(org.eclipse.xtext.Action) Keyword(org.eclipse.xtext.Keyword) EnumLiteralDeclaration(org.eclipse.xtext.EnumLiteralDeclaration) TypeRef(org.eclipse.xtext.TypeRef) ArrayList(java.util.ArrayList) EClassifier(org.eclipse.emf.ecore.EClassifier) CrossReference(org.eclipse.xtext.CrossReference) RuleCall(org.eclipse.xtext.RuleCall)

Aggregations

EnumLiteralDeclaration (org.eclipse.xtext.EnumLiteralDeclaration)15 EnumRule (org.eclipse.xtext.EnumRule)9 Keyword (org.eclipse.xtext.Keyword)6 EEnum (org.eclipse.emf.ecore.EEnum)5 CrossReference (org.eclipse.xtext.CrossReference)5 Grammar (org.eclipse.xtext.Grammar)5 RuleCall (org.eclipse.xtext.RuleCall)5 EEnumLiteral (org.eclipse.emf.ecore.EEnumLiteral)4 EPackage (org.eclipse.emf.ecore.EPackage)4 TypeRef (org.eclipse.xtext.TypeRef)4 Test (org.junit.Test)4 AbstractRule (org.eclipse.xtext.AbstractRule)3 ParserRule (org.eclipse.xtext.ParserRule)3 TerminalRule (org.eclipse.xtext.TerminalRule)3 EObject (org.eclipse.emf.ecore.EObject)2 AbstractMetamodelDeclaration (org.eclipse.xtext.AbstractMetamodelDeclaration)2 Action (org.eclipse.xtext.Action)2 Alternatives (org.eclipse.xtext.Alternatives)2 Assignment (org.eclipse.xtext.Assignment)2 GeneratedMetamodel (org.eclipse.xtext.GeneratedMetamodel)2