The Unified and Autonomous Development Platform
PostQuery implementation
robin shine7ae10fd36291
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 0dbef503a3..7217787a20 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@@ -497,32 +497,6 @@ public abstract class CodeAssist implements Serializable {return inputSuggestions;}- 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;- while (true) {- char contentChar = content.charAt(contentIndex);- if (!Character.isWhitespace(contentChar)) {- if (contentChar != mandatory.charAt(mandatoryIndex))- break;- mandatoryIndex++;- }- contentIndex++;- if (mandatoryIndex == mandatory.length() || contentIndex == content.length())- break;- }- if (mandatoryIndex == mandatory.length())- return contentIndex;- else- return 0;- } else {- return 0;- }- }-private List<ElementSuggestion> suggest(InputStatus inputStatus, List<TokenNode> matches) {List<ElementSuggestion> suggestions = new ArrayList<>();for (TokenNode match: matches) {@@ -557,6 +531,9 @@ public abstract class CodeAssist implements Serializable {tokens.add(stream.getToken(stream.size()-1));match = spec.match(new AssistStream(tokens), null, null, new HashMap<String, Integer>());elementSuggestions.addAll(suggest(inputStatus, match.getPaths()));+ } else {+ String matchWith = StringUtils.trimStart(inputStatus.getContentBeforeCaret());+ elementSuggestions.addAll(spec.suggestFirst(null, null, matchWith, new HashSet<String>()));}}}@@ -611,6 +588,32 @@ public abstract class CodeAssist implements Serializable {return elementReplacements;}+ 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;+ while (true) {+ char contentChar = content.charAt(contentIndex);+ if (!Character.isWhitespace(contentChar)) {+ if (contentChar != mandatory.charAt(mandatoryIndex))+ break;+ mandatoryIndex++;+ }+ contentIndex++;+ if (mandatoryIndex == mandatory.length() || contentIndex == content.length())+ break;+ }+ if (mandatoryIndex == mandatory.length())+ return contentIndex;+ else+ return 0;+ } else {+ return 0;+ }+ }+private List<String> getMandatoriesAfter(Node elementNode) {List<String> literals = new ArrayList<>();ElementSpec elementSpec = (ElementSpec) elementNode.getSpec();diff --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 c0d5fad139..7241fd81f9 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@@ -53,7 +53,7 @@ public class CodeAssistTest2 {@Testpublic void test() {List<InputSuggestion> suggestions;- /*+suggestions = codeAssist.suggest(new InputStatus("branch(master)"), "revisionCriteria");assertEquals(2, suggestions.size());assertEquals("branch(master)..:16", suggestions.get(0).toString());@@ -69,7 +69,7 @@ public class CodeAssistTest2 {suggestions = codeAssist.suggest(new InputStatus("br"), "query");assertEquals(1, suggestions.size());assertEquals("branch(:7", suggestions.get(0).toString());- */+suggestions = codeAssist.suggest(new InputStatus("branch("), "query");assertEquals(4, suggestions.size());assertEquals("branch(master):14", suggestions.get(0).toString());@@ -78,11 +78,12 @@ public class CodeAssistTest2 {assertEquals("branch(feature2):16", suggestions.get(3).toString());suggestions = codeAssist.suggest(new InputStatus("branch"), "query");- assertEquals(4, suggestions.size());+ assertEquals(5, suggestions.size());assertEquals("branch(master):14", suggestions.get(0).toString());assertEquals("branch(dev):11", suggestions.get(1).toString());assertEquals("branch(feature1):16", suggestions.get(2).toString());assertEquals("branch(feature2):16", suggestions.get(3).toString());+ assertEquals("branch(:7", suggestions.get(4).toString());suggestions = codeAssist.suggest(new InputStatus("branch( fea"), "query");assertEquals(3, suggestions.size());diff --git a/gitplex.product/src/test/java/gitplex/product/Test.java b/gitplex.product/src/test/java/gitplex/product/Test.javaindex 10752ee6cf..0ac5709d34 100644--- a/gitplex.product/src/test/java/gitplex/product/Test.java+++ b/gitplex.product/src/test/java/gitplex/product/Test.java@@ -3,13 +3,13 @@ package gitplex.product;import org.antlr.v4.runtime.ANTLRInputStream;import org.antlr.v4.runtime.Token;-import com.pmease.commons.antlr.codeassist.test.CodeAssistTest3Lexer;+import com.pmease.gitplex.web.page.test.PostQueryLexer;public class Test {@org.junit.Testpublic void test() {- CodeAssistTest3Lexer lexer = new CodeAssistTest3Lexer(new ANTLRInputStream("title: hello world just"));+ PostQueryLexer lexer = new PostQueryLexer(new ANTLRInputStream("hello world title:value"));Token token = lexer.nextToken();while (token.getType() != Token.EOF) {System.out.println(token);diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/PostQuery.g4 b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/PostQuery.g4index 86dad5d674..80f4dbf22f 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/PostQuery.g4+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/PostQuery.g4@@ -1,13 +1,16 @@grammar PostQuery;-query: criteria+ EOF;+query: (criteria|sentence)+ EOF;+criteria: Criteria|ExcludedCriteria;+sentence: Word+;-criteria: key=('title'|'author') ':' value;--value: QuotedValue|NQuotedValue;--QuotedValue: '"' (ESCAPE|~["\\])+? '"';-NQuotedValue: [a-zA-Z1-9_]+;+Criteria: TitleCriteria|AuthorCriteria|IsCriteria;+TitleCriteria: 'title:' (Word|QuotedValue);+AuthorCriteria: 'author:' (Word|QuotedValue);+IsCriteria: 'is:' ('open'|'close');+ExcludedCriteria: '-' Criteria;+QuotedValue: '"' (ESCAPE|~["\\])+ '"';+Word: ~[ \t\r\n]+;fragmentESCAPE: '\\'["\\];diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/TestPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/TestPage.javaindex e75305f3c2..62c6af04f1 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/TestPage.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/TestPage.java@@ -6,29 +6,25 @@ import java.util.List;import org.antlr.v4.runtime.ANTLRInputStream;import org.antlr.v4.runtime.BailErrorStrategy;import org.antlr.v4.runtime.CommonTokenStream;+import org.apache.commons.lang3.StringUtils;import org.apache.wicket.ajax.markup.html.form.AjaxButton;import org.apache.wicket.markup.html.form.Form;import org.apache.wicket.markup.html.form.TextField;import org.apache.wicket.model.IModel;-import com.google.common.collect.Lists;import com.pmease.commons.antlr.codeassist.InputSuggestion;-import com.pmease.commons.antlr.codeassist.LiteralElementSpec;import com.pmease.commons.antlr.codeassist.Node;import com.pmease.commons.antlr.codeassist.ParseTree;import com.pmease.commons.antlr.codeassist.RuleRefElementSpec;-import com.pmease.commons.antlr.codeassist.SurroundingAware;-import com.pmease.commons.antlr.codeassist.TokenNode;-import com.pmease.commons.antlr.codeassist.test.CodeAssistTest3Parser;import com.pmease.commons.wicket.behavior.inputassist.ANTLRAssistBehavior;import com.pmease.gitplex.web.page.base.BasePage;-import com.pmease.gitplex.web.page.test.PostQueryParser.CriteriaContext;-import com.pmease.gitplex.web.page.test.PostQueryParser.QueryContext;@SuppressWarnings("serial")public class TestPage extends BasePage {- private static final String[] AUTHORS = new String[]{"robin shen", "steve luo", "justin"};+ private static final String[] AUTHORS = new String[]{"\"robin shen\"", "\"steve luo\"", "justin"};++ private static final String[] STATUS = new String[]{"open", "close"};private String queryString;@@ -52,10 +48,7 @@ public class TestPage extends BasePage {PostQueryParser parser = new PostQueryParser(tokens);parser.removeErrorListeners();parser.setErrorHandler(new BailErrorStrategy());- QueryContext query = parser.query();- for (CriteriaContext criteria: query.criteria()) {- System.out.println(criteria.key.getText() + ":" + criteria.value().getText());- }+// QueryContext query = parser.query();} catch (Exception e) {System.out.println("query syntax error");}@@ -82,53 +75,24 @@ public class TestPage extends BasePage {}});- input.add(new ANTLRAssistBehavior(CodeAssistTest3Parser.class, "query") {+ input.add(new ANTLRAssistBehavior(PostQueryParser.class, "query") {@Overrideprotected List<InputSuggestion> suggest(final ParseTree parseTree, Node elementNode, String matchWith) {if (elementNode.getSpec() instanceof RuleRefElementSpec) {RuleRefElementSpec spec = (RuleRefElementSpec) elementNode.getSpec();- if (spec.getRuleName().equals("value")) {- return new SurroundingAware(codeAssist, "\"", "\"") {-- @Override- protected List<InputSuggestion> match(String matchWith) {- List<InputSuggestion> suggestions = new ArrayList<>();- Node criteria = parseTree.findParentNodeByRuleName(parseTree.getLastNode(), "criteria");- TokenNode tokenNode = parseTree.getFirstTokenNode(criteria);- if (tokenNode.getToken().getText().equals("author")) {- for (String value: AUTHORS) {- if (value.toLowerCase().contains(matchWith.toLowerCase()))- suggestions.add(new InputSuggestion(value));- }- }- return suggestions;- }-- @Override- protected String getSurroundingDescription() {- return "value containing space has to be quoted";- }-- }.suggest(elementNode, matchWith);- }- } else if (elementNode.getSpec() instanceof LiteralElementSpec) {- LiteralElementSpec spec = (LiteralElementSpec) elementNode.getSpec();- if (spec.getLiteral().toLowerCase().startsWith(matchWith.toLowerCase())) {- String description;- switch (spec.getLiteral()) {- case "title":- description = "filter by title";- break;- case "author":- description = "filter by author";- break;- default:- description = null;+ if (spec.getRuleName().equals("criteria")) {+ List<InputSuggestion> suggestions;+ if (matchWith.startsWith("author:") || matchWith.startsWith("-author:")) {+ suggestions = suggestAuthor(matchWith);+ } else if (matchWith.startsWith("is:") || matchWith.startsWith("-is:")) {+ suggestions = suggestStatus(matchWith);+ } else {+ suggestions = new ArrayList<>();}- return Lists.newArrayList(new InputSuggestion(spec.getLiteral(), description));+ return suggestions;}- }+ }return null;}@@ -138,4 +102,40 @@ public class TestPage extends BasePage {add(form);}+ private List<InputSuggestion> suggestAuthor(String matchWith) {+ String key;+ String query;+ if (matchWith.startsWith("author:")) {+ key = "author:";+ query = matchWith.substring("author:".length());+ } else {+ key = "-author:";+ query = matchWith.substring("-author:".length());+ }+ List<InputSuggestion> suggestions = new ArrayList<>();+ for (String author: AUTHORS) {+ if (StringUtils.deleteWhitespace(author).contains(query))+ suggestions.add(new InputSuggestion(key + author, author, null));+ }+ return suggestions;+ }++ private List<InputSuggestion> suggestStatus(String matchWith) {+ String key;+ String query;+ if (matchWith.startsWith("is:")) {+ key = "is:";+ query = matchWith.substring("is:".length());+ } else {+ key = "-is:";+ query = matchWith.substring("-is:".length());+ }+ List<InputSuggestion> suggestions = new ArrayList<>();+ for (String status: STATUS) {+ if (StringUtils.deleteWhitespace(status).contains(query))+ suggestions.add(new InputSuggestion(key + status, status, null));+ }+ return suggestions;+ }+}