The Unified and Autonomous Development Platform
midwork
robin shine82afb340030d
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/AssistStream.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/AssistStream.javaindex 5a78c01348..cfe0ba61b6 100644--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/AssistStream.java+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/AssistStream.java@@ -56,6 +56,10 @@ public class AssistStream {return tokens.get(index);}+ public int getTokenIndex(Token token) {+ return tokens.indexOf(token);+ }+public Token getPreviousToken() {return getToken(index-1);}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 4f7ff80a13..2cfa5001b4 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@@ -382,35 +382,12 @@ public abstract class CodeAssist implements Serializable {public List<InputSuggestion> suggest(InputStatus inputStatus, String ruleName) {RuleSpec rule = Preconditions.checkNotNull(getRule(ruleName));- String contentBeforeCaret = inputStatus.getContentBeforeCaret();- AssistStream stream = lex(contentBeforeCaret);-- List<ElementReplacement> replacements = new ArrayList<>();- if (!stream.isEof()) {- Token lastToken = stream.getToken(stream.size()-2);- String matchWith;- int beyondLastToken = inputStatus.getCaret() - lastToken.getStopIndex() - 1;- if (beyondLastToken > 0) {- matchWith = contentBeforeCaret.substring(inputStatus.getCaret()-beyondLastToken, inputStatus.getCaret());- matchWith = StringUtils.trimStart(matchWith);- replacements.addAll(suggest(rule, stream, inputStatus, matchWith));- } else {- replacements.addAll(suggest(rule, stream, inputStatus, ""));-- matchWith = stream.getToken(stream.size()-2).getText();- List<Token> tokens = stream.getTokens();- tokens.remove(tokens.size()-2);- stream = new AssistStream(tokens);- replacements.addAll(suggest(rule, stream, inputStatus, matchWith));- }- } else {- replacements.addAll(suggest(rule, stream, inputStatus, contentBeforeCaret));- }+ AssistStream stream = lex(inputStatus.getContentBeforeCaret());String inputContent = inputStatus.getContent();List<InputSuggestion> inputSuggestions = new ArrayList<>();Map<String, List<ElementReplacement>> grouped = new LinkedHashMap<>();- for (ElementReplacement replacement: replacements) {+ for (ElementReplacement replacement: suggest(rule, stream, inputStatus)) {String key = inputContent.substring(0, replacement.begin)+ replacement.content + inputContent.substring(replacement.end);List<ElementReplacement> value = grouped.get(key);@@ -425,7 +402,6 @@ public abstract class CodeAssist implements Serializable {List<ElementReplacement> value = entry.getValue();ElementReplacement replacement = value.get(0);String description = replacement.description;- int caret = replacement.begin + replacement.caret;String content = entry.getKey();if (replacement.end <= inputStatus.getCaret()) {List<String> contents = new ArrayList<>();@@ -447,7 +423,6 @@ public abstract class CodeAssist implements Serializable {}}}-content += inputContent.substring(each.end);if (contents.isEmpty()) {@@ -458,10 +433,28 @@ public abstract class CodeAssist implements Serializable {}}}- if (replacement.caret == replacement.content.length() && caret < content.length()) {- String contentAfterCaret = content.substring(caret);- caret += skipMandatories(contentAfterCaret, getMandatoriesAfter(replacement.node));++ /*+ * Adjust caret to move it to next place expecting user input, that is, we move+ * it after all mandatory tokens after the replacement, unless user puts caret+ * explicitly in the middle of replacement via suggestion, indicating the+ * replacement has some place holders expecting user input.+ */+ int caret;+ if (replacement.caret == replacement.content.length()) {+ // caret is not at the middle of replacement, so we need to move it to+ // be after mandatory tokens+ caret = content.length() - inputContent.length() + replacement.end;+ if (content.equals(entry.getKey())) {+ // in case mandatory tokens are not added after replacement,+ // we skip existing mandatory tokens in current input+ String contentAfterCaret = content.substring(caret);+ caret += skipMandatories(contentAfterCaret, getMandatoriesAfter(replacement.node));+ }+ } else {+ caret = replacement.begin + replacement.content.length();}+inputSuggestions.add(new InputSuggestion(content, caret, description));}return inputSuggestions;@@ -469,6 +462,7 @@ public abstract class CodeAssist implements Serializable {private int skipMandatories(String content, List<String> mandatories) {String mandatory = StringUtils.join(mandatories, "");+ mandatory = StringUtils.deleteWhitespace(mandatory);if (mandatory.length() != 0 && content.length() != 0) {int mandatoryIndex = 0;int contentIndex = 0;@@ -492,26 +486,47 @@ public abstract class CodeAssist implements Serializable {}}- private List<ElementReplacement> suggest(RuleSpec spec, AssistStream stream,- InputStatus inputStatus, String matchWith) {+ private List<ElementSuggestion> suggest(InputStatus inputStatus, List<TokenNode> matches) {+ List<ElementSuggestion> suggestions = new ArrayList<>();+ for (TokenNode match: matches) {+ String matchWith = inputStatus.getContent().substring(+ match.getToken().getStopIndex()+1, inputStatus.getCaret());+ matchWith = StringUtils.trimStart(matchWith);+ ElementSpec matchSpec = (ElementSpec) match.getSpec();+ suggestions.addAll(matchSpec.suggestNext(new ParseTree(match), match.getParent(), matchWith));+ }+ return suggestions;+ }++ private List<ElementReplacement> suggest(RuleSpec spec, AssistStream stream, InputStatus inputStatus) {List<ElementReplacement> elementReplacements = new ArrayList<>();List<ElementSuggestion> elementSuggestions = new ArrayList<>();if (stream.isEof()) {+ String matchWith = StringUtils.trimStart(inputStatus.getContentBeforeCaret());elementSuggestions.addAll(spec.suggestFirst(null, null, matchWith, new HashSet<String>()));} else {List<TokenNode> matches = spec.getPartialMatches(stream, null, null, new HashMap<String, Integer>());- if (!matches.isEmpty() && stream.isEof()) {- for (TokenNode match: matches) {- ElementSpec matchSpec = (ElementSpec) match.getSpec();- elementSuggestions.addAll(matchSpec.suggestNext(new ParseTree(match), match.getParent(), matchWith));+ if (matches.isEmpty()) {+ String matchWith = StringUtils.trimStart(inputStatus.getContentBeforeCaret());+ elementSuggestions.addAll(spec.suggestFirst(null, null, matchWith, new HashSet<String>()));+ } else {+ elementSuggestions.addAll(suggest(inputStatus, matches));+ int index = stream.getTokenIndex(matches.get(0).getToken());+ if (index>0) {+ List<Token> tokens = new ArrayList<>();+ for (int i=0; i<index; i++)+ tokens.add(stream.getToken(i));+ tokens.add(stream.getToken(stream.size()-1));+ matches = spec.getPartialMatches(new AssistStream(tokens), null, null, new HashMap<String, Integer>());+ elementSuggestions.addAll(suggest(inputStatus, matches));}}}String inputContent = inputStatus.getContent();- int replaceStart = inputStatus.getCaret() - matchWith.length();for (ElementSuggestion elementSuggestion: elementSuggestions) {+ int replaceStart = inputStatus.getCaret() - elementSuggestion.getMatchWith().length();int replaceEnd = inputStatus.getCaret();String contentAfterReplaceStart = inputContent.substring(replaceStart);AssistStream streamAfterReplaceStart = lex(contentAfterReplaceStart);@@ -534,11 +549,12 @@ public abstract class CodeAssist implements Serializable {elementReplacement.content = inputSuggestion.getContent();elementReplacement.caret = inputSuggestion.getCaret();- if (stream.size() > 1) {+ if (elementSuggestion.getParseTree() != null) {AssistStream newStream = lex(before + elementReplacement.content);- if (newStream.size() >= stream.size()) {- Token lastToken = stream.getToken(stream.size()-2);- Token newToken = newStream.getToken(stream.size()-2);+ Token lastToken = elementSuggestion.getParseTree().getLastNode().getToken();+ int index = stream.getTokenIndex(lastToken);+ if (newStream.size() > index+1) {+ Token newToken = newStream.getToken(index);if (lastToken.getStartIndex() != newToken.getStartIndex()|| lastToken.getStopIndex() != newToken.getStopIndex()) {elementReplacement.content = " " + inputSuggestion.getContent();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.javaindex 4c53525161..e9f3ebc730 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@@ -7,6 +7,8 @@ import java.util.List;import java.util.Map;import java.util.Set;+import javax.annotation.Nullable;+import com.google.common.base.Preconditions;import com.google.common.collect.Lists;@@ -92,18 +94,18 @@ public abstract class ElementSpec extends Spec {}@Override- public List<ElementSuggestion> suggestFirst(ParseTree parseTree, Node parent,+ public List<ElementSuggestion> suggestFirst(@Nullable ParseTree parseTree, Node parent,String matchWith, Set<String> checkedRules) {Node elementNode = new Node(this, parent, null);List<InputSuggestion> texts = codeAssist.suggest(parseTree, elementNode, matchWith);if (texts != null)- return Lists.newArrayList(new ElementSuggestion(elementNode, texts));+ return Lists.newArrayList(new ElementSuggestion(parseTree, elementNode, matchWith, texts));elsereturn doSuggestFirst(parent, parseTree, matchWith, checkedRules);}protected abstract List<ElementSuggestion> doSuggestFirst(Node parent,- ParseTree parseTree, String matchWith, Set<String> checkedRules);+ @Nullable ParseTree parseTree, String matchWith, Set<String> checkedRules);@Overridepublic boolean match(AssistStream stream, Map<String, Integer> checkedIndexes) {diff --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 92ef8d429e..fba742381b 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@@ -2,21 +2,39 @@ package com.pmease.commons.antlr.codeassist;import java.util.List;+import javax.annotation.Nullable;+public class ElementSuggestion {+ private final ParseTree parseTree;++ private final String matchWith;+private final Node node;private final List<InputSuggestion> inputSuggestions;- public ElementSuggestion(Node node, List<InputSuggestion> inputSuggestions) {+ public ElementSuggestion(@Nullable ParseTree parseTree, Node node, String matchWith,+ List<InputSuggestion> inputSuggestions) {+ this.parseTree = parseTree;this.node = node;+ this.matchWith = matchWith;this.inputSuggestions = inputSuggestions;}+ @Nullable+ public ParseTree getParseTree() {+ return parseTree;+ }+public Node getNode() {return node;}+ public String getMatchWith() {+ return matchWith;+ }+public List<InputSuggestion> getInputSuggestions() {return inputSuggestions;}diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/LiteralElementSpec.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/LiteralElementSpec.javaindex c3498c5737..d466b1434e 100644--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/LiteralElementSpec.java+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/LiteralElementSpec.java@@ -33,7 +33,8 @@ public class LiteralElementSpec extends TokenElementSpec {if (!matchWith.equals(literal)&& literal.toLowerCase().startsWith(matchWith.toLowerCase())) {InputSuggestion text = new InputSuggestion(literal, literal.length(), literal);- return Lists.newArrayList(new ElementSuggestion(new Node(this, parent, null), Lists.newArrayList(text)));+ return Lists.newArrayList(new ElementSuggestion(parseTree, new Node(this, parent, null),+ matchWith, Lists.newArrayList(text)));} else {return new ArrayList<>();}diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ParseTree.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ParseTree.javaindex 46bf80a36e..be32d0cafd 100644--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ParseTree.java+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ParseTree.java@@ -3,6 +3,8 @@ package com.pmease.commons.antlr.codeassist;import java.util.ArrayList;import java.util.List;+import javax.annotation.Nullable;+public class ParseTree {private final TokenNode lastNode;@@ -31,13 +33,52 @@ public class ParseTree {return allNodes;}- public List<Node> getChildNodes(Node parent) {+ public List<Node> getChildNodes(Node parentNode) {List<Node> childNodes = new ArrayList<>();for (Node node: getAllNodes()) {- if (node.getParent().equals(parent))+ if (node.getParent().equals(parentNode))childNodes.add(node);}return childNodes;}++ public Node getRootNode() {+ Node node = lastNode;+ while (node.getParent() != null)+ node = node.getParent();++ return node;+ }++ public TokenNode getFirstTokenNode() {+ TokenNode tokenNode = lastNode;+ Node node = lastNode;+ while (node.getPrevious() != null) {+ node = node.getPrevious();+ if (node instanceof TokenNode)+ tokenNode = (TokenNode)node;+ }+ return tokenNode;+ }++ @Nullable+ public TokenNode getPreviousTokenNode(TokenNode tokenNode, int...tokenTypes) {+ Node node = tokenNode;+ while (node.getPrevious() != null) {+ node = node.getPrevious();+ if (node instanceof TokenNode) {+ tokenNode = (TokenNode) node;+ if (tokenTypes.length == 0) {+ return tokenNode;+ } else {+ for (int tokenType: tokenTypes) {+ if (tokenType == tokenNode.getToken().getType())+ return tokenNode;+ }+ }+ }+ }+ return null;+ }}\ No newline at end of filediff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest3.g4 b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest3.g4new file mode 100644index 0000000000..dd21284e3b--- /dev/null+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest3.g4@@ -0,0 +1,18 @@+grammar CodeAssistTest3;++query: criteria+ EOF;++criteria: titleCriteria|authorCriteria;++titleCriteria: 'title' ':' Value;+authorCriteria: 'author' ':' Value;++Value: QuotedValue|NQuotedValue;++QuotedValue: '"' (ESCAPE|~["\\])+? '"';+NQuotedValue: [a-zA-Z1-9]+;++fragment+ESCAPE: '\\'["\\];++WS: [ \t\r\n]+ -> skip;\ No newline at end of filediff --git a/commons.antlr/src/test/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest2.java b/commons.antlr/src/test/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest2.javaindex a5a024894c..676bd55de4 100644--- a/commons.antlr/src/test/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest2.java+++ b/commons.antlr/src/test/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest2.java@@ -44,14 +44,12 @@ public class CodeAssistTest2 {public void test() {List<InputSuggestion> suggestions;- /*suggestions = codeAssist.suggest(new InputStatus(""), "revisionCriteria");assertEquals(4, suggestions.size());assertEquals("branch(:7", suggestions.get(0).toString());assertEquals("tag(:4", suggestions.get(1).toString());assertEquals("id(:3", suggestions.get(2).toString());assertEquals("^:1", suggestions.get(3).toString());- */suggestions = codeAssist.suggest(new InputStatus("br"), "query");assertEquals(1, suggestions.size());diff --git a/commons.antlr/src/test/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest3.java b/commons.antlr/src/test/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest3.javanew file mode 100644index 0000000000..722d7dd506--- /dev/null+++ b/commons.antlr/src/test/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest3.java@@ -0,0 +1,54 @@+package com.pmease.commons.antlr.codeassist.test;++import static org.junit.Assert.assertEquals;++import java.util.List;++import org.junit.Before;+import org.junit.Test;++import com.pmease.commons.antlr.codeassist.CodeAssist;+import com.pmease.commons.antlr.codeassist.InputStatus;+import com.pmease.commons.antlr.codeassist.InputSuggestion;+import com.pmease.commons.antlr.codeassist.Node;+import com.pmease.commons.antlr.codeassist.ParseTree;++public class CodeAssistTest3 {++ private Suggester suggester;++ private CodeAssist codeAssist = new CodeAssist(CodeAssistTest3Lexer.class) {++ private static final long serialVersionUID = 1L;++ @Override+ protected List<InputSuggestion> suggest(ParseTree parseTree, Node elementNode, String matchWith) {+ return suggester.suggest(parseTree, elementNode, matchWith);+ }++ };++ @Before+ public void setup() {+ suggester = new Suggester() {++ @Override+ public List<InputSuggestion> suggest(ParseTree parseTree, Node elementNode, String matchWith) {+ return null;+ }++ };+ }++ @Test+ public void test() {+ List<InputSuggestion> suggestions;++ suggestions = codeAssist.suggest(new InputStatus("title: hello world"), "query");+ System.out.println(suggestions);+ }++ interface Suggester {+ List<InputSuggestion> suggest(ParseTree parseTree, Node elementNode, String matchWith);+ }+}diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/InputAssistBehavior.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/InputAssistBehavior.javaindex 33539115c6..e6c7f23a36 100644--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/InputAssistBehavior.java+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/InputAssistBehavior.java@@ -150,10 +150,13 @@ public abstract class InputAssistBehavior extends AbstractDefaultAjaxBehavior {final List<InputSuggestion> suggestions;+ long time = System.currentTimeMillis();if (inputCaret != null)suggestions = getSuggestions(new InputStatus(inputContent, inputCaret));elsesuggestions = new ArrayList<>();++ System.out.println(System.currentTimeMillis()-time);if (!suggestions.isEmpty()) {int anchor = getAnchor(inputContent.substring(0, inputCaret));diff --git a/gitplex.product/src/test/java/gitplex/product/Test.java b/gitplex.product/src/test/java/gitplex/product/Test.javaindex d57a44ba72..10752ee6cf 100644--- a/gitplex.product/src/test/java/gitplex/product/Test.java+++ b/gitplex.product/src/test/java/gitplex/product/Test.java@@ -1,12 +1,20 @@package gitplex.product;-import com.google.common.base.Splitter;+import org.antlr.v4.runtime.ANTLRInputStream;+import org.antlr.v4.runtime.Token;++import com.pmease.commons.antlr.codeassist.test.CodeAssistTest3Lexer;public class Test {@org.junit.Testpublic void test() {- System.out.println(Splitter.on('\n').splitToList("\n").size());+ CodeAssistTest3Lexer lexer = new CodeAssistTest3Lexer(new ANTLRInputStream("title: hello world just"));+ Token token = lexer.nextToken();+ while (token.getType() != Token.EOF) {+ System.out.println(token);+ token = lexer.nextToken();+ }}}\ No newline at end of filediff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/commit/QueryAssistBehavior.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/commit/QueryAssistBehavior.javaindex 4f1154c449..111b3199cd 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/commit/QueryAssistBehavior.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/commit/QueryAssistBehavior.java@@ -1,8 +1,10 @@package com.pmease.gitplex.web.page.repository.commit;+import java.util.ArrayList;import java.util.List;import com.pmease.commons.antlr.codeassist.InputSuggestion;+import com.pmease.commons.antlr.codeassist.LexerRuleRefElementSpec;import com.pmease.commons.antlr.codeassist.Node;import com.pmease.commons.antlr.codeassist.ParseTree;import com.pmease.commons.wicket.behavior.inputassist.ANTLRAssistBehavior;@@ -10,12 +12,27 @@ import com.pmease.commons.wicket.behavior.inputassist.ANTLRAssistBehavior;@SuppressWarnings("serial")public class QueryAssistBehavior extends ANTLRAssistBehavior {+ private static final String[] BRANCHS = new String[]{"master", "dev", "feature1", "feature2"};+public QueryAssistBehavior() {super(CommitQueryParser.class, "query");}@Overrideprotected List<InputSuggestion> suggest(ParseTree parseTree, Node elementNode, String matchWith) {+ if (elementNode.getSpec() instanceof LexerRuleRefElementSpec) {+ LexerRuleRefElementSpec spec = (LexerRuleRefElementSpec) elementNode.getSpec();+ if (spec.getRuleName().equals("Value")+ && parseTree.getLastNode().getToken().getType() == CommitQueryParser.BRANCH) {+ List<InputSuggestion> suggestions = new ArrayList<>();+ for (String value: BRANCHS) {+ String bracedValue = "(" + value + ")";+ if (bracedValue.toLowerCase().contains(matchWith.toLowerCase()))+ suggestions.add(new InputSuggestion(bracedValue, bracedValue));+ }+ return suggestions;+ }+ }return null;}