Skip to content

The Unified and Autonomous Development Platform

midwork

robin shine68c3745cdb65
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 e682784d62..5ccdbb1c97 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
@@ -602,38 +602,4 @@ public abstract class CodeAssist implements Serializable {
protected abstract List<InputSuggestion> suggest(@Nullable ParseTree parseTree,
Node elementNode, String matchWith);
- public List<InputSuggestion> decorate(Suggester suggester,
- @Nullable ParseTree parseTree, Node elementNode, String matchWith,
- String prefix, String suffix) {
- if (matchWith.startsWith(prefix))
- matchWith = matchWith.substring(prefix.length());
- List<InputSuggestion> suggestions = suggester.suggest(matchWith);
- List<InputSuggestion> surroundedSuggestions = new ArrayList<>();
-
- boolean matchFound = false;
- for (InputSuggestion suggestion: suggestions) {
- String content = suggestion.getContent();
- int caret = suggestion.getCaret();
- if (!elementNode.getSpec().matches(lex(content))) {
- content = prefix + content + suffix;
- if (caret == suggestion.getContent().length())
- caret = content.length();
- else
- caret += prefix.length();
- surroundedSuggestions.add(new InputSuggestion(content, caret, suggestion.getDescription()));
- } else {
- surroundedSuggestions.add(suggestion);
- }
- if (suggestion.getContent().equals(matchWith))
- matchFound = true;
- }
- if (!matchFound && !elementNode.getSpec().matches(lex(matchWith))) {
- String surroundedMatchWith = prefix + matchWith + suffix;
- if (elementNode.getSpec().matches(lex(surroundedMatchWith))) {
- surroundedSuggestions.add(new InputSuggestion(surroundedMatchWith,
- surroundedMatchWith.length(), surroundedMatchWith));
- }
- }
- return surroundedSuggestions;
- }
}
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/SurroundingAware.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/SurroundingAware.java
new file mode 100644
index 0000000000..b59c025320
--- /dev/null
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/SurroundingAware.java
@@ -0,0 +1,56 @@
+package com.pmease.commons.antlr.codeassist;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public abstract class SurroundingAware {
+
+ private final CodeAssist codeAssist;
+
+ private final String prefix;
+
+ private final String suffix;
+
+ public SurroundingAware(CodeAssist codeAssist, String prefix, String suffix) {
+ this.codeAssist = codeAssist;
+ this.prefix = prefix;
+ this.suffix = suffix;
+ }
+
+ public List<InputSuggestion> suggest(Node elementNode, String matchWith) {
+ if (matchWith.startsWith(prefix))
+ matchWith = matchWith.substring(prefix.length());
+ matchWith = matchWith.trim();
+
+ List<InputSuggestion> suggestions = match(matchWith);
+ List<InputSuggestion> checkedSuggestions = new ArrayList<>();
+
+ boolean matchFound = false;
+ for (InputSuggestion suggestion: suggestions) {
+ String content = suggestion.getContent();
+ int caret = suggestion.getCaret();
+ if (!elementNode.getSpec().matches(codeAssist.lex(content))) {
+ content = prefix + content + suffix;
+ if (caret == suggestion.getContent().length())
+ caret = content.length();
+ else
+ caret += prefix.length();
+ checkedSuggestions.add(new InputSuggestion(content, caret, suggestion.getDescription()));
+ } else {
+ checkedSuggestions.add(suggestion);
+ }
+ if (suggestion.getContent().equals(matchWith))
+ matchFound = true;
+ }
+ if (!matchFound && !elementNode.getSpec().matches(codeAssist.lex(matchWith))) {
+ String surroundedMatchWith = prefix + matchWith + suffix;
+ if (elementNode.getSpec().matches(codeAssist.lex(surroundedMatchWith))) {
+ checkedSuggestions.add(new InputSuggestion(surroundedMatchWith,
+ surroundedMatchWith.length(), surroundedMatchWith));
+ }
+ }
+ return checkedSuggestions;
+ }
+
+ protected abstract List<InputSuggestion> match(String matchWith);
+}
diff --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.g4
index dd21284e3b..0b9b6da526 100644
--- 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.g4
@@ -4,10 +4,10 @@ query: criteria+ EOF;
criteria: titleCriteria|authorCriteria;
-titleCriteria: 'title' ':' Value;
-authorCriteria: 'author' ':' Value;
+titleCriteria: 'title' ':' value;
+authorCriteria: 'author' ':' value;
-Value: QuotedValue|NQuotedValue;
+value: QuotedValue|NQuotedValue;
QuotedValue: '"' (ESCAPE|~["\\])+? '"';
NQuotedValue: [a-zA-Z1-9]+;
diff --git a/commons.antlr/src/test/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest1.java b/commons.antlr/src/test/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest1.java
index 8bf5e65495..38e0798f51 100644
--- a/commons.antlr/src/test/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest1.java
+++ b/commons.antlr/src/test/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest1.java
@@ -4,7 +4,6 @@ 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;
@@ -15,44 +14,29 @@ import com.pmease.commons.antlr.codeassist.ParseTree;
public class CodeAssistTest1 {
- private Suggester suggester;
-
private CodeAssist codeAssist = new CodeAssist(CodeAssistTest1Lexer.class) {
private static final long serialVersionUID = 1L;
@Override
protected List<InputSuggestion> suggest(ParseTree parseTree, Node elementNode, String matchWith) {
- return suggester.suggest(parseTree, elementNode, matchWith);
+ return null;
}
};
- @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(""), "selfReference");
assertEquals(1, suggestions.size());
assertEquals("ab:2", suggestions.get(0).toString());
- */
+
suggestions = codeAssist.suggest(new InputStatus("ab"), "selfReference");
assertEquals(1, suggestions.size());
assertEquals("ab cd:5", suggestions.get(0).toString());
- /*
+
suggestions = codeAssist.suggest(new InputStatus(""), "mandatories");
assertEquals(2, suggestions.size());
assertEquals("ab c:4", suggestions.get(0).toString());
@@ -61,10 +45,6 @@ public class CodeAssistTest1 {
suggestions = codeAssist.suggest(new InputStatus("cd"), "mandatories");
assertEquals(1, suggestions.size());
assertEquals("cd ef g h:9", suggestions.get(0).toString());
- */
- }
-
- interface Suggester {
- List<InputSuggestion> suggest(ParseTree parseTree, Node elementNode, String matchWith);
}
+
}
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.java
index 676bd55de4..30be148bad 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
@@ -2,44 +2,54 @@ package com.pmease.commons.antlr.codeassist.test;
import static org.junit.Assert.assertEquals;
+import java.util.ArrayList;
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.LexerRuleRefElementSpec;
import com.pmease.commons.antlr.codeassist.Node;
import com.pmease.commons.antlr.codeassist.ParseTree;
+import com.pmease.commons.antlr.codeassist.SurroundingAware;
public class CodeAssistTest2 {
- private Suggester suggester;
-
+ private static final String[] BRANCHS = new String[]{"master", "dev", "feature1", "feature2"};
+
private CodeAssist codeAssist = new CodeAssist(CodeAssistTest2Lexer.class) {
private static final long serialVersionUID = 1L;
@Override
- protected List<InputSuggestion> suggest(ParseTree parseTree, Node elementNode, String matchWith) {
- return suggester.suggest(parseTree, elementNode, matchWith);
+ protected List<InputSuggestion> suggest(final ParseTree parseTree, Node elementNode, String matchWith) {
+ if (elementNode.getSpec() instanceof LexerRuleRefElementSpec) {
+ LexerRuleRefElementSpec spec = (LexerRuleRefElementSpec) elementNode.getSpec();
+ if (spec.getRuleName().equals("Value")) {
+ return new SurroundingAware(codeAssist, "(", ")") {
+
+ @Override
+ protected List<InputSuggestion> match(String matchWith) {
+ List<InputSuggestion> suggestions = new ArrayList<>();
+ if (parseTree.getLastNode().getToken().getType() == CodeAssistTest2Lexer.BRANCH) {
+ for (String value: BRANCHS) {
+ if (value.toLowerCase().contains(matchWith.toLowerCase()))
+ suggestions.add(new InputSuggestion(value, value));
+ }
+ }
+ return suggestions;
+ }
+
+ }.suggest(elementNode, matchWith);
+ }
+ }
+ return null;
}
};
- @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;
@@ -54,9 +64,23 @@ 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());
+ assertEquals("branch(dev):11", suggestions.get(1).toString());
+ assertEquals("branch(feature1):16", suggestions.get(2).toString());
+ assertEquals("branch(feature2):16", suggestions.get(3).toString());
+
+ suggestions = codeAssist.suggest(new InputStatus("branch( fea"), "query");
+ assertEquals(3, suggestions.size());
+ assertEquals("branch(feature1):16", suggestions.get(0).toString());
+ assertEquals("branch(feature2):16", suggestions.get(1).toString());
+ assertEquals("branch(fea):11", suggestions.get(2).toString());
+
+ suggestions = codeAssist.suggest(new InputStatus("tag"), "query");
+ assertEquals(1, suggestions.size());
+ assertEquals("tag(:4", suggestions.get(0).toString());
}
- interface Suggester {
- List<InputSuggestion> suggest(ParseTree parseTree, Node elementNode, String matchWith);
- }
}
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.java
index 35185ffb99..2bf8adb4df 100644
--- 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.java
@@ -1,8 +1,8 @@
package com.pmease.commons.antlr.codeassist.test;
+import java.util.ArrayList;
import java.util.List;
-import org.junit.Before;
import org.junit.Test;
import com.pmease.commons.antlr.codeassist.CodeAssist;
@@ -10,43 +10,57 @@ 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;
+import com.pmease.commons.antlr.codeassist.RuleRefElementSpec;
+import com.pmease.commons.antlr.codeassist.SurroundingAware;
public class CodeAssistTest3 {
- private Suggester suggester;
-
+ private static final String[] AUTHORS = new String[]{"robin shen", "steve luo"};
+
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);
+ protected 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<>();
+// if (parseTree.getLastNode().getToken().getType() == CodeAssistTest2Lexer.BRANCH) {
+ for (String value: AUTHORS) {
+ if (value.toLowerCase().contains(matchWith.toLowerCase()))
+ suggestions.add(new InputSuggestion(value, value));
+ }
+ }
+ return suggestions;
+ }
+
+ }.suggest(elementNode, matchWith);
+ }
+ }
+ return null;
}
};
- @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(""), "query");
+ assertEquals(2, suggestions.size());
+ assertEquals("title::6", suggestions.get(0).toString());
+ assertEquals("author::7", suggestions.get(1).toString());
+ */
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/ANTLRAssistBehavior.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/ANTLRAssistBehavior.java
index 602cc23755..11e85ce9fb 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/ANTLRAssistBehavior.java
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/ANTLRAssistBehavior.java
@@ -25,15 +25,14 @@ 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;
-import com.pmease.commons.antlr.codeassist.Suggester;
import com.pmease.commons.util.StringUtils;
@SuppressWarnings("serial")
public abstract class ANTLRAssistBehavior extends InputAssistBehavior {
- private final Class<? extends Lexer> lexerClass;
+ protected final Class<? extends Lexer> lexerClass;
- private final Class<? extends Parser> parserClass;
+ protected final Class<? extends Parser> parserClass;
private transient Constructor<? extends Lexer> lexerCtor;
@@ -41,9 +40,9 @@ public abstract class ANTLRAssistBehavior extends InputAssistBehavior {
private transient Method ruleMethod;
- private final CodeAssist codeAssist;
+ protected final CodeAssist codeAssist;
- private final String ruleName;
+ protected final String ruleName;
public ANTLRAssistBehavior(Class<? extends Parser> parserClass, String ruleName) {
this(parserClass, AntlrUtils.getLexerClass(parserClass), ruleName);
@@ -188,10 +187,4 @@ public abstract class ANTLRAssistBehavior extends InputAssistBehavior {
protected abstract List<InputSuggestion> suggest(ParseTree parseTree,
Node elementNode, String matchWith);
- protected List<InputSuggestion> decorate(Suggester suggester, ParseTree parseTree,
- Node elementNode, String matchWith, String prefix, String suffix) {
- return codeAssist.decorate(suggester, parseTree, elementNode,
- matchWith, prefix, suffix);
- }
-
}
diff --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.java
index 44e9b21d45..7a16850638 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
@@ -7,7 +7,7 @@ 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.antlr.codeassist.Suggester;
+import com.pmease.commons.antlr.codeassist.SurroundingAware;
import com.pmease.commons.wicket.behavior.inputassist.ANTLRAssistBehavior;
@SuppressWarnings("serial")
@@ -24,10 +24,10 @@ public class QueryAssistBehavior extends ANTLRAssistBehavior {
if (elementNode.getSpec() instanceof LexerRuleRefElementSpec) {
LexerRuleRefElementSpec spec = (LexerRuleRefElementSpec) elementNode.getSpec();
if (spec.getRuleName().equals("Value")) {
- return decorate(new Suggester() {
+ return new SurroundingAware(codeAssist, "(", ")") {
@Override
- public List<InputSuggestion> suggest(String matchWith) {
+ protected List<InputSuggestion> match(String matchWith) {
List<InputSuggestion> suggestions = new ArrayList<>();
if (parseTree.getLastNode().getToken().getType() == CommitQueryParser.BRANCH) {
for (String value: BRANCHS) {
@@ -38,7 +38,7 @@ public class QueryAssistBehavior extends ANTLRAssistBehavior {
return suggestions;
}
- }, parseTree, elementNode, matchWith, "(", ")");
+ }.suggest(elementNode, matchWith);
}
}
return null;