aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorszarnekow2009-04-24 05:26:00 (EDT)
committersefftinge2009-04-24 05:26:00 (EDT)
commit4d5dd5ffa140ab048caf91e34dd5fb77d0003144 (patch)
tree68a573b8e622a2e1c7d89a0366c9374445b9411d
parentf5fbb1afcf727c852402eb33254e1f712bdb7866 (diff)
downloadorg.eclipse.xtext-4d5dd5ffa140ab048caf91e34dd5fb77d0003144.zip
org.eclipse.xtext-4d5dd5ffa140ab048caf91e34dd5fb77d0003144.tar.gz
org.eclipse.xtext-4d5dd5ffa140ab048caf91e34dd5fb77d0003144.tar.bz2
Fix: https://bugs.eclipse.org/bugs/show_bug.cgi?id=269680
-rwxr-xr-xplugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultContentAssistContextFactory.java34
-rwxr-xr-xtests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/AbstractContentAssistProcessorTest.java22
2 files changed, 48 insertions, 8 deletions
diff --git a/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultContentAssistContextFactory.java b/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultContentAssistContextFactory.java
index 298e1ab..b7b9dd9 100755
--- a/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultContentAssistContextFactory.java
+++ b/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultContentAssistContextFactory.java
@@ -17,7 +17,9 @@ import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.xtext.CrossReference;
+import org.eclipse.xtext.GrammarUtil;
import org.eclipse.xtext.Keyword;
+import org.eclipse.xtext.ParserRule;
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.TerminalRule;
import org.eclipse.xtext.parser.IParseResult;
@@ -62,9 +64,10 @@ public class DefaultContentAssistContextFactory implements ContentAssistContext.
CompositeNode rootNode = parseResult.getRootNode();
AbstractNode lastCompleteNode = ParseTreeUtil.getLastCompleteNodeByOffset(rootNode, fixedOffset);
+ lastCompleteNode = getContainingDatatypeRuleNode(lastCompleteNode);
EObject currentModel = NodeUtil.getNearestSemanticObject(lastCompleteNode);
AbstractNode currentNode = ParseTreeUtil.getCurrentOrFollowingNodeByOffset(rootNode, fixedOffset);
-
+ currentNode = getContainingDatatypeRuleNode(currentNode);
if (lastCompleteNode.getOffset()+lastCompleteNode.getLength() == fixedOffset) {
AbstractNode precedingLastCompleteNode = ParseTreeUtil.getLastCompleteNodeByOffset(rootNode, Math.max(0, lastCompleteNode.getOffset()));
String prefix = getPrefix(lastCompleteNode, fixedOffset);
@@ -88,6 +91,20 @@ public class DefaultContentAssistContextFactory implements ContentAssistContext.
return result.toArray(new ContentAssistContext[result.size()]);
}
+ public AbstractNode getContainingDatatypeRuleNode(AbstractNode node) {
+ AbstractNode result = node;
+ EObject grammarElement = result.getGrammarElement();
+ if (grammarElement != null) {
+ ParserRule parserRule = GrammarUtil.containingParserRule(grammarElement);
+ while(parserRule != null && GrammarUtil.isDatatypeRule(parserRule)) {
+ result = result.getParent();
+ grammarElement = result.getGrammarElement();
+ parserRule = GrammarUtil.containingParserRule(grammarElement);
+ }
+ }
+ return result;
+ }
+
public ContentAssistContext createContext(ITextViewer viewer, int offset, IParseResult parseResult, CompositeNode rootNode,
AbstractNode lastCompleteNode, EObject currentModel, AbstractNode currentNode, String prefix) {
ITextSelection selection= (ITextSelection) viewer.getSelectionProvider().getSelection();
@@ -113,21 +130,22 @@ public class DefaultContentAssistContextFactory implements ContentAssistContext.
return context;
}
- public String getPrefix(AbstractNode currentNode, int offset) {
- if (currentNode instanceof LeafNode) {
- if (((LeafNode) currentNode).isHidden())
+ public String getPrefix(AbstractNode prefixNode, int offset) {
+ if (prefixNode instanceof LeafNode) {
+ if (((LeafNode) prefixNode).isHidden())
return "";
- return getNodeText(currentNode, offset);
+ return getNodeText(prefixNode, offset);
}
- StringBuilder result = new StringBuilder(currentNode.getTotalLength());
- doComputePrefix((CompositeNode) currentNode, result, offset);
+ StringBuilder result = new StringBuilder(prefixNode.getTotalLength());
+ doComputePrefix((CompositeNode) prefixNode, result, offset);
return result.toString();
}
public String getNodeText(AbstractNode currentNode, int offset) {
int startOffset = currentNode.getOffset();
String text = ((LeafNode) currentNode).getText();
- String result = text.substring(0, offset - startOffset);
+ int length = offset - startOffset;
+ String result = length > text.length() ? text : text.substring(0, length);
return result;
}
diff --git a/tests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/AbstractContentAssistProcessorTest.java b/tests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/AbstractContentAssistProcessorTest.java
index 83beeb7..0c2cc4d 100755
--- a/tests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/AbstractContentAssistProcessorTest.java
+++ b/tests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/AbstractContentAssistProcessorTest.java
@@ -291,6 +291,28 @@ public abstract class AbstractContentAssistProcessorTest extends AbstractXtextTe
.appendNl("Rule: name=ID;")
.assertTextAtCursorPosition("generate", 3,"generate");
}
+
+ /**
+ * https://bugs.eclipse.org/bugs/show_bug.cgi?id=269680
+ */
+ public void testCompletionOnDatatypeReference_01() throws Exception {
+ newBuilder(setup.getXtextSetup())
+ .appendNl("grammar foo with org.eclipse.xtext.common.Terminals")
+ .appendNl("generate meta \"url\"")
+ .appendNl("Rule: name=ID;")
+ .assertTextAtCursorPosition("org.eclipse.xtext", 2, "org.eclipse.xtext.common.Terminals");
+ }
+
+ /**
+ * https://bugs.eclipse.org/bugs/show_bug.cgi?id=269680
+ */
+ public void testCompletionOnDatatypeReference_02() throws Exception {
+ newBuilder(setup.getXtextSetup())
+ .appendNl("grammar foo with org.eclipse.xtext.common.Terminals")
+ .appendNl("generate meta \"url\"")
+ .appendNl("Rule: name=ID;")
+ .assertTextAtCursorPosition("org.eclipse.xtext", 5, "org.eclipse.xtext.common.Terminals");
+ }
/**
* regression test for: