Search in sources :

Example 1 with CrossReference

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

the class RefactoringCrossReferenceSerializerTest method testSerializer.

@Test
public void testSerializer() throws Exception {
    RefactoringCrossReferenceSerializer.RefTextEvaluator evaluator = new RefactoringCrossReferenceSerializer.RefTextEvaluator() {

        @Override
        public boolean isValid(IEObjectDescription target) {
            return true;
        }

        @Override
        public boolean isBetterThan(String newText, String currentText) {
            return newText.length() < currentText.length();
        }
    };
    String model = "bar { ref foo } foo";
    String wsRelativePath = "test/test." + getCurrentFileExtension();
    IFile file = createFile(wsRelativePath, model);
    Main main = (Main) getModel(file.getContents());
    XtextResource resource = (XtextResource) main.eResource();
    resource.setURI(URI.createPlatformResourceURI(wsRelativePath, true));
    Element bar = (Element) main.getElements().get(0);
    Element foo = bar.getReferenced().get(0);
    assertEquals("foo", foo.getName());
    CrossReference crossref = GrammarUtil.containedCrossReferences(grammarAccess.getElementRule()).get(0);
    TextRegion linkTextRegion = new TextRegion(model.lastIndexOf("foo"), 3);
    String linkText = facade.getCrossRefText(bar, crossref, foo, evaluator, linkTextRegion, status);
    assertEquals(linkText, "foo");
    assertTrue(status.getRefactoringStatus().isOK());
    foo.setName("fooBar");
    resource.getCache().clear(resource);
    String linkText1 = facade.getCrossRefText(bar, crossref, foo, evaluator, linkTextRegion, status);
    assertEquals(linkText1, "fooBar");
    assertTrue(status.getRefactoringStatus().isOK());
    assertEquals(foo, ((Main) resource.getContents().get(0)).getElements().get(1));
    foo.setName("bar");
    resource.getCache().clear(resource);
    assertNull(facade.getCrossRefText(bar, crossref, foo, evaluator, linkTextRegion, status));
}
Also used : RefactoringCrossReferenceSerializer(org.eclipse.xtext.ui.refactoring.impl.RefactoringCrossReferenceSerializer) IFile(org.eclipse.core.resources.IFile) TextRegion(org.eclipse.xtext.util.TextRegion) Element(org.eclipse.xtext.ui.tests.refactoring.refactoring.Element) CrossReference(org.eclipse.xtext.CrossReference) XtextResource(org.eclipse.xtext.resource.XtextResource) Main(org.eclipse.xtext.ui.tests.refactoring.refactoring.Main) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) Test(org.junit.Test)

Example 2 with CrossReference

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

the class XbaseProposalProvider method getConcreteSyntaxRuleName.

protected String getConcreteSyntaxRuleName(Assignment assignment) {
    AbstractElement terminal = assignment.getTerminal();
    if (terminal instanceof CrossReference) {
        return getConcreteSyntaxRuleName((CrossReference) terminal);
    }
    String ruleName = null;
    if (terminal instanceof RuleCall) {
        ruleName = getConcreteSyntaxRuleName((RuleCall) terminal);
    }
    return ruleName;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) CrossReference(org.eclipse.xtext.CrossReference) StyledString(org.eclipse.jface.viewers.StyledString) RuleCall(org.eclipse.xtext.RuleCall)

Example 3 with CrossReference

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

the class XbaseWithAnnotationsProposalProvider method completeXAnnotationElementValuePair_Element.

@Override
public void completeXAnnotationElementValuePair_Element(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
    XAnnotation annotationReference = null;
    if (model instanceof XAnnotationElementValuePair) {
        annotationReference = (XAnnotation) model.eContainer();
    } else if (model instanceof XAnnotation) {
        annotationReference = (XAnnotation) model;
    }
    if (annotationReference != null) {
        JvmType annotationType = annotationReference.getAnnotationType();
        if (annotationType != null && !annotationType.eIsProxy() && annotationType instanceof JvmAnnotationType) {
            // do not propose features like #toString, #hashCode etc
            JvmAnnotationType casted = (JvmAnnotationType) annotationType;
            final Set<JvmOperation> operations = Sets.newHashSet(casted.getDeclaredOperations());
            Predicate<IEObjectDescription> predicate = Predicates.and(new Predicate<IEObjectDescription>() {

                @Override
                public boolean apply(IEObjectDescription in) {
                    return operations.contains(in.getEObjectOrProxy());
                }
            }, getFeatureDescriptionPredicate(context));
            lookupCrossReference(((CrossReference) assignment.getTerminal()), context, acceptor, predicate);
        }
    }
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) XAnnotationElementValuePair(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotationElementValuePair) XAnnotation(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation) CrossReference(org.eclipse.xtext.CrossReference) JvmType(org.eclipse.xtext.common.types.JvmType) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 4 with CrossReference

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

the class XtextLabelProvider method text.

String text(Assignment object) {
    StringBuffer label = new StringBuffer();
    label.append(object.getFeature()).append(" ").append(object.getOperator()).append(" ");
    AbstractElement terminal = object.getTerminal();
    if (terminal instanceof RuleCall) {
        RuleCall ruleCall = (RuleCall) terminal;
        String string = NodeModelUtils.getNode(ruleCall).getText();
        label.append(string);
    } else if (terminal instanceof Keyword) {
        Keyword keyword = (Keyword) terminal;
        String value = "'" + keyword.getValue() + "'";
        label.append(value);
    } else if (terminal instanceof CrossReference) {
        CrossReference crossReference = (CrossReference) terminal;
        label.append(getLabel(crossReference));
    } else {
        label.append("(..)");
    }
    String cardinality = object.getCardinality();
    label.append(cardinality != null ? cardinality : "");
    return label.toString();
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) Keyword(org.eclipse.xtext.Keyword) CrossReference(org.eclipse.xtext.CrossReference) StyledString(org.eclipse.jface.viewers.StyledString) RuleCall(org.eclipse.xtext.RuleCall)

Example 5 with CrossReference

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

the class XtextProposalProvider method completeRuleCall.

private void completeRuleCall(EObject model, Assignment assignment, final ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
    final Assignment containingAssignment = GrammarUtil.containingAssignment(model);
    CrossReference crossReference = (CrossReference) assignment.getTerminal();
    lookupCrossReference(crossReference, context, acceptor, new Predicate<IEObjectDescription>() {

        @Override
        public boolean apply(IEObjectDescription input) {
            if (input.getEClass() == XtextPackage.Literals.TERMINAL_RULE) {
                EObject object = resolve(input, context);
                if (object instanceof TerminalRule) {
                    return !((TerminalRule) object).isFragment();
                }
            }
            if (containingAssignment != null && input.getEClass() == XtextPackage.Literals.PARSER_RULE) {
                EObject object = resolve(input, context);
                if (object instanceof ParserRule) {
                    return !((ParserRule) object).isFragment();
                }
            }
            return true;
        }
    });
}
Also used : Assignment(org.eclipse.xtext.Assignment) ParserRule(org.eclipse.xtext.ParserRule) EObject(org.eclipse.emf.ecore.EObject) CrossReference(org.eclipse.xtext.CrossReference) TerminalRule(org.eclipse.xtext.TerminalRule) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Aggregations

CrossReference (org.eclipse.xtext.CrossReference)47 EObject (org.eclipse.emf.ecore.EObject)21 RuleCall (org.eclipse.xtext.RuleCall)20 AbstractRule (org.eclipse.xtext.AbstractRule)12 ParserRule (org.eclipse.xtext.ParserRule)11 INode (org.eclipse.xtext.nodemodel.INode)11 EReference (org.eclipse.emf.ecore.EReference)10 AbstractElement (org.eclipse.xtext.AbstractElement)10 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)10 Assignment (org.eclipse.xtext.Assignment)9 Test (org.junit.Test)8 Keyword (org.eclipse.xtext.Keyword)7 TypeRef (org.eclipse.xtext.TypeRef)7 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)7 TerminalRule (org.eclipse.xtext.TerminalRule)6 Action (org.eclipse.xtext.Action)5 EnumLiteralDeclaration (org.eclipse.xtext.EnumLiteralDeclaration)5 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)5 ArrayList (java.util.ArrayList)4 EClass (org.eclipse.emf.ecore.EClass)4