Search in sources :

Example 36 with EReference

use of org.eclipse.emf.ecore.EReference in project xtext-eclipse by eclipse.

the class XtextGrammarRefactoringIntegrationTest method testRefactorXtextGrammarWithGeneratedClassifierAndModelWithRefToClassifier.

@Test
public void testRefactorXtextGrammarWithGeneratedClassifierAndModelWithRefToClassifier() throws Exception {
    ResourceSet rs = resourceSetProvider.get();
    EcoreFactory eInstance = EcoreFactory.eINSTANCE;
    Resource ecoreModelResource = createEcoreModel(rs, ecoreURI, initialModelRoot);
    EClass greetingClass = getGreetingClass(ecoreModelResource);
    EReference greetingRefLocal = getReferenceoGreeting(ecoreModelResource, greetingClass);
    String greetingClassFragment = EcoreUtil.getURI(greetingClass).fragment();
    String greetingRefFragment = EcoreUtil.getURI(greetingRefLocal).fragment();
    EPackage refPackage = eInstance.createEPackage();
    refPackage.setName("myDsl2");
    refPackage.setNsPrefix("myDsl2");
    refPackage.setNsURI("http://testrefactoring2");
    EClass modelRefClass = eInstance.createEClass();
    refPackage.getEClassifiers().add(modelRefClass);
    modelRefClass.setName("ModelRef");
    EReference reference = eInstance.createEReference();
    reference.setName("ref");
    reference.setLowerBound(0);
    reference.setUpperBound(-1);
    reference.setEType(greetingClass);
    modelRefClass.getEStructuralFeatures().add(reference);
    Resource refToGreetingResource = createEcoreModel(rs, URI.createPlatformResourceURI(TEST_PROJECT + "/src/org/xtext/example/mydsl/" + "MyDsl2.ecore", true), refPackage);
    refToGreetingResource.unload();
    ecoreModelResource.unload();
    waitForBuild();
    waitForDisplay();
    XtextEditor editor = openEditor(grammarFile);
    doRefactoring(editor);
    waitForBuild();
    checkConsistenceOfGrammar(editor);
    ecoreModelResource.load(null);
    String renamedGreetingClassFragment = greetingClassFragment.replaceFirst(CLASSIFIERNAME, REFACTOREDCLASSIFIERNAME);
    EObject renamedGreetingClass = ecoreModelResource.getEObject(renamedGreetingClassFragment);
    assertNotNull(renamedGreetingClass);
    assertEquals(REFACTOREDCLASSIFIERNAME, SimpleAttributeResolver.NAME_RESOLVER.apply(renamedGreetingClass));
    EReference greetingReference = (EReference) ecoreModelResource.getEObject(greetingRefFragment);
    EClassifier eType = greetingReference.getEType();
    assertFalse(eType.eIsProxy());
    assertEquals(REFACTOREDCLASSIFIERNAME, eType.getName());
    refToGreetingResource.load(null);
    EReference externalReferenceToGreeting = getReferenceoGreeting(refToGreetingResource, eType);
    assertFalse(externalReferenceToGreeting.getEType().eIsProxy());
    assertEquals(REFACTOREDCLASSIFIERNAME, externalReferenceToGreeting.getEType().getName());
}
Also used : EClass(org.eclipse.emf.ecore.EClass) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) EObject(org.eclipse.emf.ecore.EObject) EcoreFactory(org.eclipse.emf.ecore.EcoreFactory) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) EClassifier(org.eclipse.emf.ecore.EClassifier) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) EReference(org.eclipse.emf.ecore.EReference) EPackage(org.eclipse.emf.ecore.EPackage) AbstractLinkedEditingIntegrationTest(org.eclipse.xtext.ui.testing.AbstractLinkedEditingIntegrationTest) Test(org.junit.Test)

Example 37 with EReference

use of org.eclipse.emf.ecore.EReference in project xtext-eclipse by eclipse.

the class XtextGrammarRefactoringIntegrationTest method setUp.

@SuppressWarnings("static-access")
@Override
public void setUp() throws Exception {
    super.setUp();
    EcoreRefactoringParticipant.setDisableWarning(true);
    project = createProject(TEST_PROJECT);
    IJavaProject javaProject = makeJavaProject(project);
    addNature(javaProject.getProject(), XtextProjectHelper.NATURE_ID);
    Injector injector = Activator.getInstance().getInjector(getEditorId());
    injector.injectMembers(this);
    grammar = "grammar org.xtext.example.mydsl.MyDsl\n hidden(WS) generate myDsl\n 'http://testrefactoring'\n import 'http://www.eclipse.org/emf/2002/Ecore'\n as ecore \nModel: greetings+=" + CLASSIFIERNAME + "*; \n" + CLASSIFIERNAME + ": 'Hello' name=ID '!';\n terminal ID : '^'?('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;\n terminal WS: (' '|'\t'|'\r'|'\n')+;";
    grammarFile = IResourcesSetupUtil.createFile(GRAMMAR_FILE, grammar);
    grammarUri = URI.createPlatformResourceURI(GRAMMAR_FILE, true);
    EcoreFactory eInstance = EcoreFactory.eINSTANCE;
    initialModelRoot = eInstance.createEPackage();
    initialModelRoot.setName("myDsl");
    initialModelRoot.setNsPrefix("myDsl");
    initialModelRoot.setNsURI("http://testrefactoring");
    EClass modelClass = eInstance.createEClass();
    modelClass.setName("Model");
    EClass greetingClass = eInstance.createEClass();
    EAttribute nameAttribute = eInstance.createEAttribute();
    nameAttribute.setEType(EcorePackage.Literals.ESTRING);
    nameAttribute.setName("name");
    greetingClass.getEStructuralFeatures().add(nameAttribute);
    greetingClass.setName(CLASSIFIERNAME);
    EReference reference = eInstance.createEReference();
    reference.setName("greetings");
    reference.setLowerBound(0);
    reference.setUpperBound(-1);
    reference.setEType(greetingClass);
    modelClass.getEStructuralFeatures().add(reference);
    initialModelRoot.getEClassifiers().add(modelClass);
    initialModelRoot.getEClassifiers().add(greetingClass);
    ecoreURI = URI.createPlatformResourceURI(ECORE_FILE, true);
    Resource grammarResource = resourceSetProvider.get().getResource(grammarUri, true);
    ParserRule greetingsParserRule = Iterables.filter(EcoreUtil2.getAllContentsOfType(grammarResource.getContents().get(0), ParserRule.class), new Predicate<ParserRule>() {

        @Override
        public boolean apply(ParserRule input) {
            return input.getName().equals(CLASSIFIERNAME);
        }
    }).iterator().next();
    ParserRule modelParserRule = Iterables.filter(EcoreUtil2.getAllContentsOfType(grammarResource.getContents().get(0), ParserRule.class), new Predicate<ParserRule>() {

        @Override
        public boolean apply(ParserRule input) {
            return input.getName().equals("Model");
        }
    }).iterator().next();
    greetingParserRuleUri = EcoreUtil.getURI(greetingsParserRule);
    modelParserRuleUri = EcoreUtil.getURI(modelParserRule);
    grammarResource.unload();
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) EClass(org.eclipse.emf.ecore.EClass) EAttribute(org.eclipse.emf.ecore.EAttribute) IJavaProject(org.eclipse.jdt.core.IJavaProject) Injector(com.google.inject.Injector) EcoreFactory(org.eclipse.emf.ecore.EcoreFactory) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) EReference(org.eclipse.emf.ecore.EReference)

Example 38 with EReference

use of org.eclipse.emf.ecore.EReference in project xtext-eclipse by eclipse.

the class CrossReferenceTemplateVariableResolver method resolveValues.

@Override
public List<String> resolveValues(TemplateVariable variable, XtextTemplateContext castedContext) {
    String abbreviatedCrossReference = (String) variable.getVariableType().getParams().iterator().next();
    int dotIndex = abbreviatedCrossReference.lastIndexOf('.');
    if (dotIndex <= 0) {
        // $NON-NLS-1$ //$NON-NLS-2$
        log.error("CrossReference '" + abbreviatedCrossReference + "' could not be resolved.");
        return Collections.emptyList();
    }
    String[] classReferencePair = new String[] { abbreviatedCrossReference.substring(0, dotIndex), abbreviatedCrossReference.substring(dotIndex + 1) };
    Grammar grammar = getGrammar(castedContext);
    if (grammar == null) {
        return Collections.emptyList();
    }
    EReference reference = getReference(classReferencePair[0], classReferencePair[1], grammar);
    if (reference == null) {
        log.debug(// $NON-NLS-1$ //$NON-NLS-2$
        "CrossReference to class '" + classReferencePair[0] + "' and reference '" + classReferencePair[1] + // $NON-NLS-1$
        "' could not be resolved.");
        return Collections.emptyList();
    }
    IScope scope = null;
    EObject currentModel = castedContext.getContentAssistContext().getCurrentModel();
    if (currentModel == null) {
        scope = globalScopeProvider.getScope(castedContext.getContentAssistContext().getResource(), reference, null);
    } else {
        scope = castedContext.getScopeProvider().getScope(currentModel, reference);
    }
    Iterable<IEObjectDescription> linkingCandidates = queryScope(scope);
    List<String> names = new ArrayList<String>();
    for (IEObjectDescription eObjectDescription : linkingCandidates) {
        names.add(qualifiedNameConverter.toString(eObjectDescription.getName()));
    }
    return names;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) ArrayList(java.util.ArrayList) IScope(org.eclipse.xtext.scoping.IScope) Grammar(org.eclipse.xtext.Grammar) EReference(org.eclipse.emf.ecore.EReference) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 39 with EReference

use of org.eclipse.emf.ecore.EReference in project xtext-eclipse by eclipse.

the class DefaultQuickfixProvider method createLinkingIssueResolutions.

public void createLinkingIssueResolutions(final Issue issue, final IssueResolutionAcceptor issueResolutionAcceptor) {
    final IModificationContext modificationContext = modificationContextFactory.createModificationContext(issue);
    final IXtextDocument xtextDocument = modificationContext.getXtextDocument();
    if (xtextDocument == null)
        return;
    xtextDocument.readOnly(new CancelableUnitOfWork<Void, XtextResource>() {

        IssueResolutionAcceptor myAcceptor = null;

        @Override
        public java.lang.Void exec(XtextResource state, CancelIndicator cancelIndicator) throws Exception {
            myAcceptor = getCancelableAcceptor(issueResolutionAcceptor, cancelIndicator);
            EObject target = state.getEObject(issue.getUriToProblem().fragment());
            EReference reference = getUnresolvedEReference(issue, target);
            if (reference == null)
                return null;
            fixUnresolvedReference(issue, xtextDocument, target, reference);
            return null;
        }

        protected void fixUnresolvedReference(final Issue issue, final IXtextDocument xtextDocument, EObject target, EReference reference) throws BadLocationException {
            boolean caseInsensitive = caseInsensitivityHelper.isIgnoreCase(reference);
            EObject crossReferenceTerminal = getCrossReference(issue, target);
            String ruleName = null;
            Keyword keyword = null;
            if (crossReferenceTerminal instanceof RuleCall) {
                RuleCall ruleCall = (RuleCall) crossReferenceTerminal;
                ruleName = ruleCall.getRule().getName();
            } else if (crossReferenceTerminal instanceof Keyword) {
                keyword = (Keyword) crossReferenceTerminal;
            }
            String issueString = xtextDocument.get(issue.getOffset(), issue.getLength());
            IScope scope = scopeProvider.getScope(target, reference);
            List<IEObjectDescription> discardedDescriptions = Lists.newArrayList();
            Set<String> qualifiedNames = Sets.newHashSet();
            int addedDescriptions = 0;
            int checkedDescriptions = 0;
            for (IEObjectDescription referableElement : queryScope(scope)) {
                String referableElementQualifiedName = qualifiedNameConverter.toString(referableElement.getQualifiedName());
                if (similarityMatcher.isSimilar(issueString, qualifiedNameConverter.toString(referableElement.getName()))) {
                    addedDescriptions++;
                    createResolution(issueString, referableElement, ruleName, keyword, caseInsensitive);
                    qualifiedNames.add(referableElementQualifiedName);
                } else {
                    if (qualifiedNames.add(referableElementQualifiedName))
                        discardedDescriptions.add(referableElement);
                }
                checkedDescriptions++;
                if (checkedDescriptions > 100)
                    break;
            }
            if (discardedDescriptions.size() + addedDescriptions <= 5) {
                for (IEObjectDescription referableElement : discardedDescriptions) {
                    createResolution(issueString, referableElement, ruleName, keyword, caseInsensitive);
                }
            }
        }

        protected AbstractElement getCrossReference(final Issue issue, EObject target) {
            final ICompositeNode node = NodeModelUtils.getNode(target);
            if (node == null)
                throw new IllegalStateException("Cannot happen since we found a reference");
            ICompositeNode rootNode = node.getRootNode();
            ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(rootNode, issue.getOffset());
            CrossReference crossReference = findCrossReference(target, leaf);
            return crossReference.getTerminal();
        }

        public void createResolution(String issueString, IEObjectDescription solution, String ruleName, Keyword keyword, boolean caseInsensitive) {
            String replacement = qualifiedNameConverter.toString(solution.getName());
            String replaceLabel = fixCrossReferenceLabel(issueString, replacement);
            if (keyword != null) {
                if (caseInsensitive && !replacement.equalsIgnoreCase(keyword.getValue()))
                    return;
                if (!caseInsensitive && !replacement.equals(keyword.getValue()))
                    return;
            } else if (ruleName != null) {
                replacement = converter.convertToString(replacement, ruleName);
                if (replacement == null) {
                    return;
                }
            } else {
                logger.error("either keyword or ruleName have to present", new IllegalStateException());
            }
            myAcceptor.accept(issue, replaceLabel, replaceLabel, fixCrossReferenceImage(issueString, replacement), new ReplaceModification(issue, replacement));
        }
    });
}
Also used : Issue(org.eclipse.xtext.validation.Issue) Set(java.util.Set) XtextResource(org.eclipse.xtext.resource.XtextResource) RuleCall(org.eclipse.xtext.RuleCall) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) EObject(org.eclipse.emf.ecore.EObject) IScope(org.eclipse.xtext.scoping.IScope) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) ArrayList(java.util.ArrayList) List(java.util.List) EReference(org.eclipse.emf.ecore.EReference) Keyword(org.eclipse.xtext.Keyword) AbstractElement(org.eclipse.xtext.AbstractElement) BadLocationException(org.eclipse.jface.text.BadLocationException) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) CrossReference(org.eclipse.xtext.CrossReference) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) BadLocationException(org.eclipse.jface.text.BadLocationException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 40 with EReference

use of org.eclipse.emf.ecore.EReference in project xtext-eclipse by eclipse.

the class XbaseQuickfixProvider method createLinkingIssueResolutions.

/**
 * Filter quickfixes for types and constructors.
 */
@Override
public void createLinkingIssueResolutions(final Issue issue, final IssueResolutionAcceptor issueResolutionAcceptor) {
    final IModificationContext modificationContext = getModificationContextFactory().createModificationContext(issue);
    final IXtextDocument xtextDocument = modificationContext.getXtextDocument();
    if (xtextDocument != null) {
        xtextDocument.readOnly(new CancelableUnitOfWork<Void, XtextResource>() {

            @Override
            public java.lang.Void exec(XtextResource state, CancelIndicator cancelIndicator) throws Exception {
                try {
                    EObject target = state.getEObject(issue.getUriToProblem().fragment());
                    EReference reference = getUnresolvedEReference(issue, target);
                    if (reference != null && reference.getEReferenceType() != null) {
                        createLinkingIssueQuickfixes(issue, getCancelableAcceptor(issueResolutionAcceptor, cancelIndicator), xtextDocument, state, target, reference);
                    }
                } catch (WrappedException e) {
                // issue information seems to be out of sync, e.g. there is no
                // EObject with the given fragment
                }
                return null;
            }
        });
    }
}
Also used : WrappedException(org.eclipse.emf.common.util.WrappedException) EObject(org.eclipse.emf.ecore.EObject) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) XtextResource(org.eclipse.xtext.resource.XtextResource) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) WrappedException(org.eclipse.emf.common.util.WrappedException) BadLocationException(org.eclipse.jface.text.BadLocationException) EReference(org.eclipse.emf.ecore.EReference) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Aggregations

EReference (org.eclipse.emf.ecore.EReference)229 EObject (org.eclipse.emf.ecore.EObject)118 EClass (org.eclipse.emf.ecore.EClass)58 List (java.util.List)52 ArrayList (java.util.ArrayList)48 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)37 Test (org.junit.Test)31 EAttribute (org.eclipse.emf.ecore.EAttribute)29 PalladioSelectEObjectDialog (org.palladiosimulator.editors.commons.dialogs.selection.PalladioSelectEObjectDialog)28 EClassifier (org.eclipse.emf.ecore.EClassifier)21 Resource (org.eclipse.emf.ecore.resource.Resource)21 IdEObject (org.bimserver.emf.IdEObject)18 EList (org.eclipse.emf.common.util.EList)17 IScope (org.eclipse.xtext.scoping.IScope)15 HashMapVirtualObject (org.bimserver.shared.HashMapVirtualObject)14 InternalEObject (org.eclipse.emf.ecore.InternalEObject)14 URI (org.eclipse.emf.common.util.URI)13 EPackage (org.eclipse.emf.ecore.EPackage)12 AbstractEList (org.eclipse.emf.common.util.AbstractEList)10 CrossReference (org.eclipse.xtext.CrossReference)10