The Unified and Autonomous Development Platform
midwork
robin shine98c20f6056e2
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.javaindex e53eae713f..69055da6f7 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@@ -2,7 +2,6 @@ package com.pmease.commons.antlr.codeassist;import java.io.Serializable;import java.util.ArrayList;-import java.util.Collections;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.List;@@ -109,8 +108,8 @@ public abstract class CodeAssist implements Serializable {List<String> contents = new ArrayList<>();for (ElementCompletion each: value) {content = inputContent.substring(0, each.getReplaceBegin()) + each.getReplaceContent();- ParentedElement parentElement = each.getExpectingElement().getParent();- ElementSpec elementSpec = each.getExpectingElement().getSpec();+ ParentedElement parentElement = each.getExpectedElement().getParent();+ ElementSpec elementSpec = each.getExpectedElement().getSpec();for (String mandatory: getMandatoriesAfter(parentElement, elementSpec)) {String prevContent = content;content += mandatory;@@ -158,8 +157,8 @@ public abstract class CodeAssist implements Serializable {// in case mandatory tokens are not added after replacement,// we skip existing mandatory tokens in current inputString contentAfterCaret = content.substring(caret);- ParentedElement parentElement = completion.getExpectingElement().getParent();- ElementSpec elementSpec = completion.getExpectingElement().getSpec();+ ParentedElement parentElement = completion.getExpectedElement().getParent();+ ElementSpec elementSpec = completion.getExpectedElement().getSpec();caret += skipMandatories(contentAfterCaret, getMandatoriesAfter(parentElement, elementSpec));}} else {@@ -228,33 +227,39 @@ public abstract class CodeAssist implements Serializable {private List<ElementSuggestion> suggest(EarleyParser parser, State state, InputStatus inputStatus) {List<ElementSuggestion> suggestions = new ArrayList<>();for (Node node: state.getNodesExpectingTerminal()) {- List<Element> rootElements = assumeCompleted(parser, node, state.getEndTokenIndex());+ int endTokenIndex = state.getEndTokenIndex();+ List<Element> elements = new ArrayList<>(node.getElements());+ elements.add(new Element(parser, node.getExpectedElementSpec(), 0, null));+ node = new Node(node.getBeginTokenIndex(), node.getRuleSpec(),+ node.getAlternativeSpecIndex(), node.getExpectedElementSpecIndex(),+ node.isExpectedElementSpecMatchedOnce(), elements);++ List<Element> rootElements = assumeCompleted(parser, node, endTokenIndex);String matchWith;- if (state.getEndTokenIndex() != 0) {- int stopIndex = parser.getTokens().get(state.getEndTokenIndex()-1).getStopIndex()+1;- matchWith = inputStatus.getContent().substring(stopIndex, inputStatus.getCaret());+ Token token = state.getInitiatingToken();+ if (token != null) {+ int endCharIndex = token.getStopIndex()+1;+ matchWith = inputStatus.getContent().substring(endCharIndex, inputStatus.getCaret());} else {matchWith = inputStatus.getContentBeforeCaret();}matchWith = StringUtils.trimStart(matchWith);for (Element rootElement: rootElements) {- List<ParentedElement> expectingElements = new ArrayList<>();+ List<ParentedElement> expectedElements = new ArrayList<>();ParentedElement elementExpectingTerminal = getElementExpectingTerminal(null, rootElement);- expectingElements.add(elementExpectingTerminal);- ParentedElement expectingElement = elementExpectingTerminal.getParent();- while (expectingElement != null) {- if (expectingElement.getNode().getElements().size() == 1) {- expectingElements.add(expectingElement);- expectingElement = expectingElement.getParent();+ expectedElements.add(elementExpectingTerminal);+ ParentedElement expectedElement = elementExpectingTerminal.getParent();+ while (expectedElement != null) {+ if (expectedElement.getNode().getElements().size() == 1) {+ expectedElements.add(expectedElement);+ expectedElement = expectedElement.getParent();} else {break;}}- Collections.reverse(expectingElements);-List<InputSuggestion> inputSuggestions = null;- for (ParentedElement element: expectingElements) {+ for (ParentedElement element: expectedElements) {inputSuggestions = suggest(element, matchWith);if (inputSuggestions != null) {if (!inputSuggestions.isEmpty())@@ -310,41 +315,41 @@ public abstract class CodeAssist implements Serializable {String inputContent = inputStatus.getContent();for (ElementSuggestion suggestion: suggestions) {- int replaceStart = inputStatus.getCaret() - suggestion.getMatchWith().length();+ int replaceBegin = inputStatus.getCaret() - suggestion.getMatchWith().length();int replaceEnd = inputStatus.getCaret();- String contentAfterReplaceStart = inputContent.substring(replaceStart);- tokens = grammar.lex(contentAfterReplaceStart);-+/** if input around the caret matches spec of the suggestion, we then replace* the matched text with suggestion, instead of simply inserting the* suggested content*/- if (!tokens.isEmpty() && tokens.get(0).getStartIndex() == 0) {- ElementSpec elementSpec = (ElementSpec) suggestion.getExpectingElement().getSpec();- int matchDistance = elementSpec.getEndOfMatch(tokens);- if (matchDistance > 0) {- int charIndex = tokens.get(matchDistance-1).getStopIndex()+1;- if (replaceStart + charIndex > replaceEnd)- replaceEnd = replaceStart + charIndex;+ tokens = grammar.lex(inputContent.substring(replaceBegin));+ if (!tokens.isEmpty() && tokens.get(0).getStartIndex() == 0) {+ ElementSpec elementSpec = (ElementSpec) suggestion.getExpectedElement().getSpec();+ int endOfMatch = elementSpec.getEndOfMatch(tokens);+ if (endOfMatch > 0) { // there exist an element match+ int charIndex = tokens.get(endOfMatch-1).getStopIndex()+1;+ if (replaceBegin + charIndex > replaceEnd)+ replaceEnd = replaceBegin + charIndex;}}- String before = inputContent.substring(0, replaceStart);+ String before = inputContent.substring(0, replaceBegin);for (InputSuggestion inputSuggestion: suggestion.getInputSuggestions()) {- int nextTokenIndex = suggestion.getExpectingElement().getEndTokenIndex();- if (nextTokenIndex != 0) {- Token lastToken = parser.getTokens().get(nextTokenIndex-1);+ int endTokenIndex = suggestion.getExpectedElement().getEndTokenIndex();+ if (endTokenIndex != 0) {+ Token lastToken = parser.getTokens().get(endTokenIndex-1);tokens = grammar.lex(before + inputSuggestion.getContent());+/** ignore the suggestion if we can not append the suggested content directly.* This normally indicates that a space is required before the suggestion,* and we will show the suggestion when user presses the space to make the* suggestion list less confusing*/- if (tokens.size() > nextTokenIndex) {- Token newToken = tokens.get(nextTokenIndex-1);+ if (tokens.size() > endTokenIndex) {+ Token newToken = tokens.get(endTokenIndex-1);if (lastToken.getStartIndex() != newToken.getStartIndex()|| lastToken.getStopIndex() != newToken.getStopIndex()) {continue;@@ -354,9 +359,11 @@ public abstract class CodeAssist implements Serializable {}}- completions.add(new ElementCompletion(suggestion.getExpectingElement(), replaceStart,- replaceEnd, inputSuggestion.getContent(), inputSuggestion.getCaret(),- inputSuggestion.getDescription()));+ completions.add(new ElementCompletion(suggestion.getExpectedElement(), replaceBegin,+ // replace only if provided suggestion is a complete representation of the element+ inputSuggestion.isComplete()?replaceEnd:inputStatus.getCaret(),+ inputSuggestion.getContent(), inputSuggestion.getCaret(),+ inputSuggestion.isComplete(), inputSuggestion.getDescription()));}}return completions;@@ -426,6 +433,6 @@ public abstract class CodeAssist implements Serializable {return grammar;}- protected abstract List<InputSuggestion> suggest(ParentedElement element, String matchWith);+ protected abstract List<InputSuggestion> suggest(ParentedElement expectedElement, String matchWith);}diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementCompletion.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementCompletion.javaindex 6442ad9397..b935a38762 100644--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementCompletion.java+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementCompletion.java@@ -4,18 +4,25 @@ public class ElementCompletion extends InputCompletion {private static final long serialVersionUID = 1L;- private final ParentedElement expectingElement;+ private final boolean complete;- public ElementCompletion(ParentedElement expectingElement, int replaceBegin,+ private final ParentedElement expectedElement;++ public ElementCompletion(ParentedElement expectedElement, int replaceBegin,int replaceEnd, String replaceContent, int caret, boolean complete,String description) {- super(replaceBegin, replaceEnd, replaceContent, caret, complete, description);+ super(replaceBegin, replaceEnd, replaceContent, caret, description);- this.expectingElement = expectingElement;+ this.complete = complete;+ this.expectedElement = expectedElement;+ }++ public ParentedElement getExpectedElement() {+ return expectedElement;}- public ParentedElement getExpectingElement() {- return expectingElement;+ public boolean isComplete() {+ return complete;}}\ No newline at end of filediff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementSuggestion.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementSuggestion.javaindex f30c110d09..238cb277b4 100644--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementSuggestion.java+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementSuggestion.java@@ -4,21 +4,21 @@ import java.util.List;public class ElementSuggestion {- private final ParentedElement expectingElement;+ private final ParentedElement expectedElement;private final String matchWith;private final List<InputSuggestion> inputSuggestions;- public ElementSuggestion(ParentedElement expectingElement,+ public ElementSuggestion(ParentedElement expectedElement,String matchWith, List<InputSuggestion> inputSuggestions) {- this.expectingElement = expectingElement;+ this.expectedElement = expectedElement;this.matchWith = matchWith;this.inputSuggestions = inputSuggestions;}- public ParentedElement getExpectingElement() {- return expectingElement;+ public ParentedElement getExpectedElement() {+ return expectedElement;}public String getMatchWith() {diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/InputSuggestion.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/InputSuggestion.javaindex 83afc3e506..c7487aa78e 100644--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/InputSuggestion.java+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/InputSuggestion.java@@ -9,7 +9,20 @@ public class InputSuggestion extends InputStatus {private final boolean complete;private final String description;-++ /**+ * Construct the input suggestion.+ *+ * @param content+ * content of the suggestion+ * @param caret+ * caret of the suggestion+ * @param complete+ * whether or not the suggested content is a complete representation of+ * corresponding element spec+ * @param description+ * description of the suggestion+ */public InputSuggestion(String content, int caret, boolean complete, @Nullable String description) {super(content, caret);this.complete = complete;diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/ElementSpec.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/ElementSpec.javaindex 9aa02ed7aa..486e25021c 100644--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/ElementSpec.java+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/ElementSpec.java@@ -63,6 +63,18 @@ public abstract class ElementSpec implements Serializable {return toStringOnce() + "?";}+ /**+ * Get the next token index after match of current spec.+ *+ * @param tokens+ * tokens to match against+ * @return+ * next token index after the match.+ * <ul><li>greater than 0 if the element matches part of the tokens+ * <li>0 if the element does not match any tokens, and the element allows to be empty+ * <li>-1 if the element does not match any tokens, and the element does not allow+ * to be empty+ */public abstract int getEndOfMatch(List<Token> tokens);protected abstract String toStringOnce();diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/parser/EarleyParser.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/parser/EarleyParser.javaindex 337b9daa07..26c7089b35 100644--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/parser/EarleyParser.java+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/parser/EarleyParser.java@@ -35,7 +35,7 @@ public class EarleyParser {nodes.add(new Node(tokenIndex, rule, i, 0, false, new ArrayList<Element>()));while (!nodes.isEmpty()) {- State state = new State(tokenIndex, nodes);+ State state = new State(this, tokenIndex, nodes);states.add(state);for (Node node: Lists.newArrayList(state.getNodes()))process(node, state);@@ -52,9 +52,9 @@ public class EarleyParser {private void process(Node node, State state) {if (!node.isCompleted()) { // predict- ElementSpec nextElement = node.getElementSpecs().get(node.getExpectedElementSpecIndex());- if (nextElement instanceof RuleRefElementSpec) {- RuleRefElementSpec ruleRefElement = (RuleRefElementSpec) nextElement;+ ElementSpec expectedElementSpec = node.getExpectedElementSpec();+ if (expectedElementSpec instanceof RuleRefElementSpec) {+ RuleRefElementSpec ruleRefElement = (RuleRefElementSpec) expectedElementSpec;RuleSpec elementRule = ruleRefElement.getRule();for (int i=0; i<elementRule.getAlternatives().size(); i++) {Node predictedNode = new Node(tokenIndex,@@ -63,9 +63,7 @@ public class EarleyParser {process(predictedNode, state);}}- if (nextElement.getMultiplicity() == Multiplicity.ZERO_OR_MORE- || nextElement.getMultiplicity() == Multiplicity.ZERO_OR_ONE- || node.isExpectedElementSpecMatchedOnce()) {+ if (expectedElementSpec.isOptional() || node.isExpectedElementSpecMatchedOnce()) {Node advancedNode = new Node(node.getBeginTokenIndex(), node.getRuleSpec(),node.getAlternativeSpecIndex(), node.getExpectedElementSpecIndex()+1, false,new ArrayList<>(node.getElements()));@@ -76,16 +74,15 @@ public class EarleyParser {State startState = states.get(node.getBeginTokenIndex());for (Node startNode: startState.getNodes()) {if (!startNode.isCompleted()) {- ElementSpec nextElement = startNode.getExpectedElementSpec();- if (nextElement instanceof RuleRefElementSpec) {- RuleRefElementSpec ruleRefElement = (RuleRefElementSpec) nextElement;+ ElementSpec expectedElementSpec = startNode.getExpectedElementSpec();+ if (expectedElementSpec instanceof RuleRefElementSpec) {+ RuleRefElementSpec ruleRefElement = (RuleRefElementSpec) expectedElementSpec;if (ruleRefElement.getRuleName().equals(node.getRuleSpec().getName())) {Node advancedNode;List<Element> elements = new ArrayList<>(startNode.getElements());if (!node.getElements().isEmpty())elements.add(new Element(this, ruleRefElement, state.getEndTokenIndex(), node));- if (nextElement.getMultiplicity() == Multiplicity.ONE- || nextElement.getMultiplicity() == Multiplicity.ZERO_OR_ONE) {+ if (!expectedElementSpec.isMultiple()) {advancedNode = new Node(startNode.getBeginTokenIndex(),startNode.getRuleSpec(), startNode.getAlternativeSpecIndex(),startNode.getExpectedElementSpecIndex()+1, false, elements);@@ -167,6 +164,18 @@ public class EarleyParser {return new ArrayList<>();}+ /**+ * Get the next token index after the match.+ *+ * @param tokens+ * tokens to match against+ * @return+ * next token index after the match.+ * <ul><li>greater than 0 if the rule matches part of the tokens+ * <li>0 if the rule does not match any tokens, and the rule allows to be empty+ * <li>-1 if the rule does not match any tokens, and the rule does not allow+ * to be empty+ */public int getEndOfMatch() {for (int i=states.size()-1; i>=0; i--) {State state = states.get(i);diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/parser/Element.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/parser/Element.javaindex 38388e6fbb..b126aa68a4 100644--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/parser/Element.java+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/parser/Element.java@@ -44,6 +44,14 @@ public class Element {return spec!=null?spec.getLabel():null;}+ /**+ * Get index of the next token after the match+ *+ * @return+ * token index after match of this element, this is NOT index of the+ * last token matching the element, it is index of the next token+ * after the match+ */public int getEndTokenIndex() {return endTokenIndex;}@@ -53,13 +61,29 @@ public class Element {return node;}+ /**+ * Get tokens matched by this element so far.+ *+ * @return+ * tokens matched by this element so far, or empty if this element does not+ * match any tokens yet+ */public List<Token> getMatchedTokens() {if (node != null)return parser.getTokens().subList(node.getBeginTokenIndex(), endTokenIndex);- else+ else if (endTokenIndex > 0)return parser.getTokens().subList(endTokenIndex-1, endTokenIndex);+ else+ return new ArrayList<>();}+ /**+ * Get the first token matched by this element.+ *+ * @return+ * first token matched by this element, or <tt>null</tt> if this element+ * does not match any tokens yet+ */@Nullablepublic Token getFirstMatchedToken() {if (node != null) {@@ -67,11 +91,20 @@ public class Element {return parser.getTokens().get(node.getBeginTokenIndex());elsereturn null;- } else {+ } else if (endTokenIndex > 0) {return parser.getTokens().get(endTokenIndex-1);+ } else {+ return null;}}+ /**+ * Get the last token matched by this element.+ *+ * @return+ * last token matched by this element, or <tt>null</tt> if this element does not+ * match any tokens yet+ */@Nullablepublic Token getLastMatchedToken() {if (node != null) {@@ -79,8 +112,10 @@ public class Element {return parser.getTokens().get(endTokenIndex-1);elsereturn null;- } else {+ } else if (endTokenIndex > 0) {return parser.getTokens().get(endTokenIndex-1);+ } else {+ return null;}}diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/parser/State.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/parser/State.javaindex 11a98b9462..61a57b6699 100644--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/parser/State.java+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/parser/State.java@@ -5,11 +5,15 @@ import java.util.Set;import javax.annotation.Nullable;+import org.antlr.v4.runtime.Token;+import com.pmease.commons.antlr.grammar.ElementSpec;import com.pmease.commons.antlr.grammar.TerminalElementSpec;public class State {+ private final EarleyParser parser;+private final int endTokenIndex;private final Set<Node> nodes;@@ -20,7 +24,8 @@ public class State {return nodes;}- public State(int tokenIndex, Set<Node> nodes) {+ public State(EarleyParser parser, int tokenIndex, Set<Node> nodes) {+ this.parser = parser;this.endTokenIndex = tokenIndex;this.nodes = nodes;for (Node node: nodes) {@@ -59,6 +64,20 @@ public class State {return endTokenIndex;}+ /**+ * Get the token being scanned to create this state.+ *+ * @return+ * the token being scanned to create this state, or <tt>null</tt> for initial state+ */+ @Nullable+ public Token getInitiatingToken() {+ if (endTokenIndex > 0)+ return parser.getTokens().get(endTokenIndex-1);+ else+ return null;+ }+public Set<Node> getMatches(String ruleName) {Set<Node> matches = new HashSet<>();for (Node node: nodes) {