summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorszarnekow2009-03-21 14:06:40 (EDT)
committer sefftinge2009-03-21 14:06:40 (EDT)
commit80838b914d3f4a9e4f6323c4c7ec961d70806ea3 (patch)
tree78da3f8ba7e0079459dfd121376afc085c136fa2
parent416a188dca5dc0cd66f2be64ad00c8cd19eccc5c (diff)
downloadorg.eclipse.xtext-80838b914d3f4a9e4f6323c4c7ec961d70806ea3.zip
org.eclipse.xtext-80838b914d3f4a9e4f6323c4c7ec961d70806ea3.tar.gz
org.eclipse.xtext-80838b914d3f4a9e4f6323c4c7ec961d70806ea3.tar.bz2
Some improvements for Content assist (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=269405)
-rw-r--r--plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultContentAssistProcessor.java44
-rw-r--r--tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parseerrorhandling/ParseErrorHandlingTest.java2
-rw-r--r--tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parsetree/ParseTreeUtilTest.java2
-rw-r--r--tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parsetree/reconstr/SerializationBug269362Test.java2
-rw-r--r--tests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/ContentAssistProcessorTestBuilder.java5
-rw-r--r--tests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultContentAssistProcessorTest.java42
6 files changed, 84 insertions, 13 deletions
diff --git a/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultContentAssistProcessor.java b/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultContentAssistProcessor.java
index 93bf1c5..d032b5b 100644
--- a/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultContentAssistProcessor.java
+++ b/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultContentAssistProcessor.java
@@ -12,6 +12,7 @@ import static org.eclipse.xtext.util.CollectionUtils.addIfNotNull;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
@@ -34,6 +35,7 @@ import org.eclipse.xtext.XtextPackage;
import org.eclipse.xtext.parser.IParseResult;
import org.eclipse.xtext.parsetree.AbstractNode;
import org.eclipse.xtext.parsetree.CompositeNode;
+import org.eclipse.xtext.parsetree.LeafNode;
import org.eclipse.xtext.parsetree.NodeUtil;
import org.eclipse.xtext.parsetree.ParseTreeUtil;
import org.eclipse.xtext.resource.XtextResource;
@@ -150,7 +152,7 @@ public class DefaultContentAssistProcessor implements IContentAssistProcessor {
break;
case XtextPackage.ASSIGNMENT:
// TODO: change interface
- // templateContextType =
+ // templateContextType =
// proposalProvider.getTemplateContextType((Assignment)computeProposalElement, contentAssistContext);
// addIfNotNull(templateContextTypes, templateContextType);
break;
@@ -194,9 +196,9 @@ public class DefaultContentAssistProcessor implements IContentAssistProcessor {
if (referenceNode.getOffset()+referenceNode.getLength() == offset) {
AbstractNode precedingReferenceNode = ParseTreeUtil.getLastCompleteNodeByOffset(rootNode,Math.max(0, referenceNode.getOffset()));
- String matchingString = referenceNode.serialize();
+ String matchingString = computeMatchString(referenceNode);
result.add(newCompletionProposal(matchingString, offset, rootNode, precedingReferenceNode));
- if (referenceNode.getGrammarElement() instanceof Keyword ||
+ if (referenceNode.getGrammarElement() instanceof Keyword ||
referenceNode.getGrammarElement() instanceof CrossReference ||
(referenceNode.getGrammarElement() instanceof RuleCall &&
((RuleCall)referenceNode.getGrammarElement()).getRule() instanceof TerminalRule)) {
@@ -209,6 +211,37 @@ public class DefaultContentAssistProcessor implements IContentAssistProcessor {
return result;
}
+
+ protected String computeMatchString(AbstractNode node) {
+ if (node instanceof LeafNode) {
+ return ((LeafNode) node).getText();
+ }
+ StringBuilder result = new StringBuilder(node.getTotalLength());
+ doComputeMatchString((CompositeNode) node, result);
+ return result.toString();
+ }
+
+ protected void doComputeMatchString(CompositeNode node, StringBuilder result) {
+ List<LeafNode> hiddens = new ArrayList<LeafNode>(2);
+ for (AbstractNode child: node.getChildren()) {
+ if (child instanceof CompositeNode) {
+ doComputeMatchString((CompositeNode) child, result);
+ } else {
+ if (((LeafNode) child).isHidden()) {
+ if (result.length() != 0)
+ hiddens.add((LeafNode)child);
+ } else {
+ Iterator<LeafNode> iter = hiddens.iterator();
+ while(iter.hasNext()) {
+ result.append(iter.next().getText());
+ iter.remove();
+ }
+ result.append(((LeafNode) child).getText());
+ }
+ }
+ }
+ }
+
/**
* Calculates the match string of the based on the specified location within the given text.
*
@@ -244,8 +277,9 @@ public class DefaultContentAssistProcessor implements IContentAssistProcessor {
public ICompletionProposal[] exec(XtextResource resource) throws Exception {
List<ICompletionProposal> completionProposalList = new ArrayList<ICompletionProposal>();
- for (IContentAssistContext contentAssistContext : createContextList(
- resource, viewer.getTextWidget().getText(), offset)) {
+ List<IContentAssistContext> contextList = createContextList(
+ resource, viewer.getTextWidget().getText(), offset);
+ for (IContentAssistContext contentAssistContext : contextList) {
List<AbstractElement> computedElements = contentAssistCalculator
.computeProposalElements(contentAssistContext);
completionProposalList.addAll(collectCompletionProposals(computedElements,contentAssistContext));
diff --git a/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parseerrorhandling/ParseErrorHandlingTest.java b/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parseerrorhandling/ParseErrorHandlingTest.java
index fe05bc4..0f87715 100644
--- a/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parseerrorhandling/ParseErrorHandlingTest.java
+++ b/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parseerrorhandling/ParseErrorHandlingTest.java
@@ -129,7 +129,7 @@ public class ParseErrorHandlingTest extends AbstractGeneratorTest {
assertTrue(lastWs.isHidden());
assertNull(lastWs.getSyntaxError());
LeafNode lastNode = leafs.get(leafs.size() - 2);
- assertTrue(lastNode.isHidden());
+ assertFalse(lastNode.isHidden());
assertNotNull(lastNode);
assertEquals("};", lastNode.getText());
assertNotNull(lastNode.getSyntaxError());
diff --git a/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parsetree/ParseTreeUtilTest.java b/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parsetree/ParseTreeUtilTest.java
index 0257467..176a26e 100644
--- a/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parsetree/ParseTreeUtilTest.java
+++ b/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parsetree/ParseTreeUtilTest.java
@@ -123,7 +123,7 @@ public class ParseTreeUtilTest extends AbstractGeneratorTest {
String text = "spielplatz 1 \"junit\" { kin ";
CompositeNode rootNode = getRootNode(text);
AbstractNode currentNodeByOffset = ParseTreeUtil.getCurrentOrPrecedingNodeByOffset(rootNode, 26);
- assertEquals("expect node with text '" + text + "'", currentNodeByOffset.serialize(), text);
+ assertEquals("expect node with text ''", "kin", currentNodeByOffset.serialize());
}
public void testGetCurrentOrPrecedingNodeByOffset_05() throws Exception {
diff --git a/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parsetree/reconstr/SerializationBug269362Test.java b/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parsetree/reconstr/SerializationBug269362Test.java
index e9c36db..7ab27ad 100644
--- a/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parsetree/reconstr/SerializationBug269362Test.java
+++ b/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parsetree/reconstr/SerializationBug269362Test.java
@@ -16,7 +16,7 @@ import org.eclipse.xtext.resource.XtextResourceSet;
/**
* @author Sebastian Zarnekow - Initial contribution and API
*/
-public class SerializationBug269362Test extends AbstractXtextTests {
+public abstract class SerializationBug269362Test extends AbstractXtextTests {
private XtextResource resource;
private XtextResourceSet resourceSet;
diff --git a/tests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/ContentAssistProcessorTestBuilder.java b/tests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/ContentAssistProcessorTestBuilder.java
index 39fc74c..861f7d6 100644
--- a/tests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/ContentAssistProcessorTestBuilder.java
+++ b/tests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/ContentAssistProcessorTestBuilder.java
@@ -109,6 +109,11 @@ public class ContentAssistProcessorTestBuilder {
return assertTextAtCursorPosition(this.cursorPosition, expectedText);
}
+ public ContentAssistProcessorTestBuilder assertTextAtCursorPosition(String cursorPosition, String... expectedText)
+ throws Exception {
+ return assertTextAtCursorPosition(getModel().indexOf(cursorPosition), expectedText);
+ }
+
public ContentAssistProcessorTestBuilder assertTextAtCursorPosition(int cursorPosition, String... expectedText)
throws Exception {
diff --git a/tests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultContentAssistProcessorTest.java b/tests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultContentAssistProcessorTest.java
index 644e3c5..7bd81a7 100644
--- a/tests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultContentAssistProcessorTest.java
+++ b/tests/org.eclipse.xtext.ui.common.tests/src/org/eclipse/xtext/ui/common/editor/contentassist/impl/DefaultContentAssistProcessorTest.java
@@ -210,7 +210,7 @@ public class DefaultContentAssistProcessorTest extends AbstractXtextTests {
"["
);
}
-
+
public void testCompleteAbstractRuleCall() throws Exception {
newBuilder(getContentAssistGrammarSetup())
.appendNl("abstract rules")
@@ -232,7 +232,7 @@ public class DefaultContentAssistProcessorTest extends AbstractXtextTests {
*
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=260825
*/
- public void testCompleteParserRule() throws Exception {
+ public void testCompleteParserRule_01() throws Exception {
newBuilder(getXtextGrammarSetup())
.appendNl("grammar foo")
.appendNl("generate foo \"foo\"")
@@ -240,6 +240,26 @@ public class DefaultContentAssistProcessorTest extends AbstractXtextTests {
"ParserRule_Name", "terminal"
);
}
+
+ public void testCompleteParserRule_02() throws Exception {
+ newBuilder(getXtextGrammarSetup())
+ .appendNl("grammar foo")
+ .appendNl("generate foo \"foo\"")
+ .appendNl("")
+ .appendNl("MyRule : 'foo' name=ID; ").
+ assertTextAtCursorPosition("MyRule",
+ "ParserRule_Name", "terminal");
+ }
+
+ public void testCompleteParserRule_03() throws Exception {
+ newBuilder(getXtextGrammarSetup())
+ .appendNl("grammar foo")
+ .appendNl("generate foo \"foo\"")
+ .appendNl("")
+ .appendNl(" MyRule : 'foo' name=ID; ").
+ assertTextAtCursorPosition(" MyRule",
+ "ParserRule_Name", "terminal");
+ }
/**
* regression test for:
@@ -272,7 +292,7 @@ public class DefaultContentAssistProcessorTest extends AbstractXtextTests {
newBuilder(getEnumsLangSetup()).append("exi").assertText("existing");
}
- public void _testEnumCompletion_03() throws Exception {
+ public void testEnumCompletion_03() throws Exception {
newBuilder(getEnumsLangSetup()).append(" ").assertText("existing", "generated");
}
@@ -280,14 +300,26 @@ public class DefaultContentAssistProcessorTest extends AbstractXtextTests {
newBuilder(getEnumsLangSetup()).append("existing").assertText("SameName", "DifferentLiteral", "overridden");
}
- public void _testEnumCompletion_05() throws Exception {
+ public void testEnumCompletion_05() throws Exception {
newBuilder(getEnumsLangSetup()).append("existing ").assertText("SameName", "DifferentLiteral", "overridden");
}
- public void _testEnumCompletion_06() throws Exception {
+ public void testEnumCompletion_06() throws Exception {
newBuilder(getEnumsLangSetup()).append("existing Same").assertText("SameName");
}
+ public void testEnumCompletion_07() throws Exception {
+ newBuilder(getEnumsLangSetup()).append("generated SameName").assertTextAtCursorPosition(0, "existing", "generated");
+ }
+
+ public void testEnumCompletion_08() throws Exception {
+ newBuilder(getEnumsLangSetup()).append(" generated SameName").assertTextAtCursorPosition(0, "existing", "generated");
+ }
+
+ public void testEnumCompletion_09() throws Exception {
+ newBuilder(getEnumsLangSetup()).append(" generated SameName").assertTextAtCursorPosition(2, "generated");
+ }
+
protected ContentAssistProcessorTestBuilder newBuilder(ISetup standAloneSetup) throws Exception {
with(standAloneSetup);
return new ContentAssistProcessorTestBuilder(standAloneSetup, this);