Skip to content

The Unified and Autonomous Development Platform

midwork

robin shinee5a551944355
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/CodeAssist.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/CodeAssist.java
index 6ded7b3a77..c80abc813a 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/CodeAssist.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/CodeAssist.java
@@ -379,7 +379,7 @@ public abstract class CodeAssist {
matchWith = stream.getToken(stream.size()-2).getText();
List<Token> tokens = stream.getTokens();
- tokens.remove(tokens.size()-1);
+ tokens.remove(tokens.size()-2);
stream = new TokenStream(tokens);
mergeSuggestions(suggest(rule, stream, input, matchWith), texts);
}
@@ -406,19 +406,21 @@ public abstract class CodeAssist {
List<CaretAwareText> texts = new ArrayList<>();
List<ElementSuggestion> suggestions = new ArrayList<>();
- List<TokenNode> partialMatches = spec.getPartialMatches(stream, null);
- if (partialMatches != null && !partialMatches.isEmpty() && stream.isEof()) {
- for (TokenNode match: partialMatches) {
- ElementSpec matchSpec = (ElementSpec) match.getSpec();
- suggestions.addAll(matchSpec.suggestNext(match.getParent(), matchWith, stream));
+ List<TokenNode> matches = spec.getPartialMatches(stream, null);
+ if (matches != null && stream.isEof()) {
+ if (!matches.isEmpty()) {
+ for (TokenNode match: matches) {
+ ElementSpec matchSpec = (ElementSpec) match.getSpec();
+ suggestions.addAll(matchSpec.suggestNext(match.getParent(), matchWith, stream));
+ }
+ } else {
+ suggestions.addAll(spec.suggestFirst(null, matchWith, stream));
}
- } else {
- suggestions.addAll(spec.suggestFirst(null, matchWith, stream));
}
-
+
int replaceStart = input.getCaret() - matchWith.length();
for (ElementSuggestion suggestion: suggestions) {
- int replaceEnd = replaceStart;
+ int replaceEnd = input.getCaret();
String contentAfterReplaceStart = input.getContent().substring(replaceStart);
TokenStream streamAfterReplaceStart = lex(contentAfterReplaceStart);
List<String> mandatories;
@@ -426,13 +428,13 @@ public abstract class CodeAssist {
ElementSpec elementSpec = (ElementSpec) suggestion.getNode().getSpec();
if (elementSpec.matchOnce(streamAfterReplaceStart)) {
if (streamAfterReplaceStart.getIndex() != 0)
- replaceEnd += streamAfterReplaceStart.getPreviousToken().getStopIndex()+1;
+ replaceEnd = replaceStart + streamAfterReplaceStart.getPreviousToken().getStopIndex()+1;
} else if (elementSpec instanceof TokenElementSpec) {
List<String> tokenMandatories = elementSpec.getMandatories();
if (!tokenMandatories.isEmpty()) {
String toBeReplaced = tokenMandatories.get(0);
if (contentAfterReplaceStart.startsWith(toBeReplaced))
- replaceEnd += toBeReplaced.length();
+ replaceEnd = replaceStart + toBeReplaced.length();
}
}
mandatories = new ArrayList<>();
@@ -445,7 +447,7 @@ public abstract class CodeAssist {
for (CaretAwareText text: suggestion.getTexts()) {
String newContent;
- if (!stream.isEof()) {
+ if (stream.size() > 1) {
newContent = before + text.getContent();
TokenStream newStream = lex(newContent);
if (newStream.size() >= stream.size()) {
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementSpec.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementSpec.java
index 3d6777ff4f..ddfe78f178 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementSpec.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementSpec.java
@@ -33,34 +33,37 @@ public abstract class ElementSpec extends Spec {
if (multiplicity == Multiplicity.ONE) {
return getPartialMatchesOnce(stream, parent);
} else if (multiplicity == Multiplicity.ONE_OR_MORE) {
- List<TokenNode> partialMatches = getPartialMatchesOnce(stream, parent);
- if (partialMatches != null && !partialMatches.isEmpty()) {
+ List<TokenNode> matches = getPartialMatchesOnce(stream, parent);
+ if (matches != null && !matches.isEmpty()) {
while (!stream.isEof()) {
- List<TokenNode> prevPartialMatches = partialMatches;
- partialMatches = getPartialMatchesOnce(stream, parent);
- if (partialMatches == null || partialMatches.isEmpty())
- return prevPartialMatches;
+ List<TokenNode> nextMatches = getPartialMatchesOnce(stream, parent);
+ if (nextMatches != null && !nextMatches.isEmpty())
+ matches = nextMatches;
+ else
+ break;
}
}
- return partialMatches;
+ return matches;
} else if (multiplicity == Multiplicity.ZERO_OR_MORE) {
- List<TokenNode> partialMatches = new ArrayList<>();
+ List<TokenNode> matches = new ArrayList<>();
while (!stream.isEof()) {
- List<TokenNode> prevMatches = partialMatches;
- partialMatches = getPartialMatchesOnce(stream, parent);
- if (partialMatches == null || partialMatches.isEmpty())
- return prevMatches;
+ List<TokenNode> nextMatches = getPartialMatchesOnce(stream, parent);
+ if (nextMatches != null && !nextMatches.isEmpty())
+ matches = nextMatches;
+ else
+ break;
}
- return partialMatches;
+ return matches;
} else {
- List<TokenNode> partialMatches = getPartialMatchesOnce(stream, parent);
- if (partialMatches != null && !partialMatches.isEmpty() && !stream.isEof()) {
- List<TokenNode> prevMatches = partialMatches;
- partialMatches = getPartialMatchesOnce(stream, parent);
- if (partialMatches == null || partialMatches.isEmpty())
- return prevMatches;
+ List<TokenNode> matches = getPartialMatchesOnce(stream, parent);
+ if (matches == null) {
+ matches = new ArrayList<>();
+ } else if (!matches.isEmpty() && !stream.isEof()) {
+ List<TokenNode> nextMatches = getPartialMatchesOnce(stream, parent);
+ if (nextMatches != null && !nextMatches.isEmpty())
+ matches = nextMatches;
}
- return partialMatches;
+ return matches;
}
}
diff --git a/commons.antlr/src/test/java/com/pmease/commons/antlr/grammar/CodeAssistTest.java b/commons.antlr/src/test/java/com/pmease/commons/antlr/grammar/CodeAssistTest.java
index c61f6ce5ce..f9570c3e74 100644
--- a/commons.antlr/src/test/java/com/pmease/commons/antlr/grammar/CodeAssistTest.java
+++ b/commons.antlr/src/test/java/com/pmease/commons/antlr/grammar/CodeAssistTest.java
@@ -111,26 +111,26 @@ public class CodeAssistTest {
@Test
public void testSuggestGrammarSpec() {
- List<CaretAwareText> suggestions = codeAssist.suggest(new CaretAwareText(""), "grammarSpec");
- assertEquals("/**", suggestions.get(0).getContent());
- assertEquals("lexer", suggestions.get(1).getContent());
- assertEquals("parser", suggestions.get(2).getContent());
- assertEquals("grammar", suggestions.get(3).getContent());
+ List<CaretAwareText> suggestions;
- suggestions = codeAssist.suggest(new CaretAwareText("parser"), "grammarSpec");
- System.out.println(suggestions);
- }
-
- @Test
- public void testSuggestGrammarSpec() {
- List<CaretAwareText> suggestions = codeAssist.suggest(new CaretAwareText(""), "grammarSpec");
+ suggestions = codeAssist.suggest(new CaretAwareText(""), "grammarSpec");
+ assertEquals(4, suggestions.size());
assertEquals("/**", suggestions.get(0).getContent());
assertEquals("lexer", suggestions.get(1).getContent());
assertEquals("parser", suggestions.get(2).getContent());
assertEquals("grammar", suggestions.get(3).getContent());
suggestions = codeAssist.suggest(new CaretAwareText("parser"), "grammarSpec");
- System.out.println(suggestions);
+ assertEquals(1, suggestions.size());
+ assertEquals("parser grammar", suggestions.get(0).getContent());
+
+ suggestions = codeAssist.suggest(new CaretAwareText("gr"), "grammarSpec");
+ assertEquals(1, suggestions.size());
+ assertEquals("grammar", suggestions.get(0).getContent());
+
+ suggestions = codeAssist.suggest(new CaretAwareText("grammar", 4), "grammarSpec");
+ assertEquals(1, suggestions.size());
+ assertEquals("grammar:7", suggestions.get(0).toString());
}
}