summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhbehrens2008-11-27 04:01:09 (EST)
committer sefftinge2008-11-27 04:01:09 (EST)
commitbeb6c024225ddf87c4d9cc93f4da8d1847569ca0 (patch)
treeea3d5fb500991a60cacfb8c98688b47cf5c3c09c
parentb33290642a96878b1c21b4eb1fad38a53b1a17b1 (diff)
downloadorg.eclipse.xtext-beb6c024225ddf87c4d9cc93f4da8d1847569ca0.zip
org.eclipse.xtext-beb6c024225ddf87c4d9cc93f4da8d1847569ca0.tar.gz
org.eclipse.xtext-beb6c024225ddf87c4d9cc93f4da8d1847569ca0.tar.bz2
Tests and minor changes to linking candidates (from Sebastian's patch)
-rw-r--r--plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/codecompletion/AbstractProposalProvider.java12
-rw-r--r--plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/codecompletion/impl/DefaultLinkingCandidatesService.java6
-rw-r--r--tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/GrammarUtilTests.java17
-rw-r--r--tests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/codecompletion/impl/DefaultLinkingCandidatesServiceTest.java112
4 files changed, 142 insertions, 5 deletions
diff --git a/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/codecompletion/AbstractProposalProvider.java b/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/codecompletion/AbstractProposalProvider.java
index 08be1e7..0319dbd 100644
--- a/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/codecompletion/AbstractProposalProvider.java
+++ b/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/codecompletion/AbstractProposalProvider.java
@@ -188,15 +188,23 @@ public abstract class AbstractProposalProvider implements IProposalProvider {
if (linkingCandidatesService != null) {
final EReference ref = GrammarUtil.getReference(crossReference, model.eClass());
final List<EObject> candidates = linkingCandidatesService.getLinkingCandidates(model, ref);
+ final String trimmedPrefix = prefix.trim();
for (EObject candidate : candidates) {
- completionProposalList.add(createCompletionProposal(crossReference, model, getLabel(
- candidate, ref, model), offset));
+ if (isCandidateMatchingPrefix(model, ref, candidate, trimmedPrefix)) {
+ completionProposalList.add(createCompletionProposal(crossReference, model, getLabel(
+ candidate, ref, model), offset));
+ }
}
}
return completionProposalList;
}
+ protected boolean isCandidateMatchingPrefix(EObject model, EReference ref, EObject candidate, String prefix) {
+ final String label = getLabel(candidate, ref, model);
+ return label != null && label.regionMatches(true, 0, prefix, 0, prefix.length());
+ }
+
protected String getLabel(EObject candidate, EReference ref, EObject context) {
return nameResolver.getValue(candidate);
}
diff --git a/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/codecompletion/impl/DefaultLinkingCandidatesService.java b/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/codecompletion/impl/DefaultLinkingCandidatesService.java
index 68dc521..4b67bd1 100644
--- a/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/codecompletion/impl/DefaultLinkingCandidatesService.java
+++ b/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/codecompletion/impl/DefaultLinkingCandidatesService.java
@@ -28,9 +28,9 @@ public class DefaultLinkingCandidatesService extends AbstractLinkingCandidatesSe
private ILinkingScopeService scopeService;
public List<EObject> getLinkingCandidates(EObject context, EReference reference) {
- List<EObject> candidates =scopeService.getObjectsInScope(context, reference);
- List<EObject> result = new ArrayList<EObject>();
- Iterator<EObject> iter = candidates.iterator();
+ final List<EObject> candidates = scopeService.getObjectsInScope(context, reference);
+ final List<EObject> result = new ArrayList<EObject>();
+ final Iterator<EObject> iter = candidates.iterator();
final EClass requiredType = reference.getEReferenceType();
while (iter.hasNext()) {
final EObject candidate = iter.next();
diff --git a/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/GrammarUtilTests.java b/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/GrammarUtilTests.java
index ca8981b..fa2147f 100644
--- a/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/GrammarUtilTests.java
+++ b/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/GrammarUtilTests.java
@@ -10,6 +10,7 @@ package org.eclipse.xtext;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
+import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtext.crossrefs.LangAStandaloneSetup;
import org.eclipse.xtext.crossrefs.services.LangAGrammarAccess;
@@ -71,5 +72,21 @@ public class GrammarUtilTests extends AbstractGeneratorTest {
EObject typeA = (EObject) invokeWithXtend("types.first()", model);
assertEquals(typeA.eClass(), referencedEClass);
}
+
+ public void testGetReference() throws Exception {
+ with(LangAStandaloneSetup.class);
+ XtextResource resource = getResourceFromString("type A extends B");
+
+ ParserRule prType = (new LangAGrammarAccess()).pr_Type();
+ Assignment asExtends = (Assignment)((Group)prType.getAlternatives()).getAbstractTokens().get(1);
+ CrossReference xref = (CrossReference) asExtends.getTerminal();
+ EObject model = getModel(resource);
+ EObject typeA = (EObject) invokeWithXtend("types.first()", model);
+ EReference ref = GrammarUtil.getReference(xref, typeA.eClass());
+ assertNotNull(ref);
+ assertEquals("extends", ref.getName());
+ assertFalse(ref.isMany());
+ assertEquals(typeA.eClass(), ref.getEReferenceType());
+ }
}
diff --git a/tests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/codecompletion/impl/DefaultLinkingCandidatesServiceTest.java b/tests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/codecompletion/impl/DefaultLinkingCandidatesServiceTest.java
new file mode 100644
index 0000000..6a4c2a1
--- /dev/null
+++ b/tests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/codecompletion/impl/DefaultLinkingCandidatesServiceTest.java
@@ -0,0 +1,112 @@
+/*******************************************************************************
+ * Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+package org.eclipse.xtext.ui.common.editor.codecompletion.impl;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EFactory;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EPackage;
+import org.eclipse.emf.ecore.EReference;
+import org.eclipse.xtext.crossref.ILinkingScopeService;
+import org.eclipse.xtext.testlanguages.ReferenceGrammarStandaloneSetup;
+import org.eclipse.xtext.ui.common.editor.codecompletion.ILinkingCandidatesService;
+
+/**
+ * @author Sebastian Zarnekow - Initial contribution and API
+ */
+public class DefaultLinkingCandidatesServiceTest extends TestCase implements ILinkingScopeService {
+
+ private List<EObject> scope;
+
+ private EObject familie;
+
+ private EReference reference;
+
+ private ILinkingCandidatesService service;
+
+ private EFactory factory;
+
+ private EPackage pack;
+
+ private EClass familieClass;
+
+ private EClass erwachsenerClass;
+
+ private EClass kindClass;
+
+ private EReference kindRef;
+
+ private EReference vaterRef;
+
+ private EReference mutterRef;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ service = new DefaultLinkingCandidatesService();
+ ((DefaultLinkingCandidatesService) service).setScopeService(this);
+ ReferenceGrammarStandaloneSetup.doSetup();
+ factory = EPackage.Registry.INSTANCE.getEFactory("http://eclipse.org/xtext/reference/ReferenceGrammar");
+ scope = new ArrayList<EObject>();
+ pack = factory.getEPackage();
+ familieClass = (EClass) pack.getEClassifier("Familie");
+ kindClass = (EClass) pack.getEClassifier("Kind");
+ erwachsenerClass = (EClass) pack.getEClassifier("Erwachsener");
+ kindRef = (EReference) familieClass.getEStructuralFeature("kinder");
+ vaterRef = (EReference) familieClass.getEStructuralFeature("vater");
+ mutterRef = (EReference) familieClass.getEStructuralFeature("mutter");
+ familie = factory.create(familieClass);
+ }
+
+ public List<EObject> getObjectsInScope(EObject context, EReference reference) {
+ assertEquals(this.familie, context);
+ assertEquals(this.reference, reference);
+ return scope;
+ }
+
+ public void testGetLinkingCandidates_01() {
+ reference = kindRef;
+ assertTrue(service.getLinkingCandidates(familie, kindRef).isEmpty());
+ }
+
+ public void testGetLinkingCandidates_02() {
+ scope.add(factory.create(erwachsenerClass));
+ reference = kindRef;
+ assertTrue(service.getLinkingCandidates(familie, kindRef).isEmpty());
+ }
+
+ public void testGetLinkingCandidates_03() {
+ scope.add(factory.create(kindClass));
+ reference = kindRef;
+ assertEquals(scope, service.getLinkingCandidates(familie, kindRef));
+ }
+
+ public void testGetLinkingCandidates_04() {
+ scope.add(factory.create(kindClass));
+ scope.add(factory.create(erwachsenerClass));
+ reference = kindRef;
+ final List<EObject> result = service.getLinkingCandidates(familie, kindRef);
+ assertEquals(scope.get(0), result.get(0));
+ assertEquals(1, result.size());
+ }
+
+ public void testGetLinkingCandidates_05() {
+ scope.add(factory.create(kindClass));
+ scope.add(factory.create(erwachsenerClass));
+ scope.add(factory.create(erwachsenerClass));
+ reference = vaterRef;
+ final List<EObject> vaterCandidates = service.getLinkingCandidates(familie, vaterRef);
+ reference = mutterRef;
+ final List<EObject> mutterCandidates = service.getLinkingCandidates(familie, mutterRef);
+ assertEquals(vaterCandidates, mutterCandidates);
+ }
+}