Skip to content

The Unified and Autonomous Development Platform

midwork

robin shinefeb4a7b370b0
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 d6b941cc1b..d0501c3585 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
@@ -350,10 +350,10 @@ public abstract class CodeAssist {
return rules.get(ruleName);
}
- public final TokenStream lex(String input) {
+ public final TokenStream lex(String content) {
try {
List<Token> tokens = new ArrayList<>();
- Lexer lexer = lexerConstructor.newInstance(new ANTLRInputStream(input));
+ Lexer lexer = lexerConstructor.newInstance(new ANTLRInputStream(content));
lexer.removeErrorListeners();
Token token;
do {
@@ -369,71 +369,72 @@ public abstract class CodeAssist {
}
}
- public List<CaretAwareText> suggest(CaretAwareText input, String ruleName) {
- List<CaretAwareText> texts = new ArrayList<>();
+ public List<InputSuggestion> suggest(InputStatus inputStatus, String ruleName) {
+ List<InputSuggestion> inputSuggestions = new ArrayList<>();
RuleSpec rule = Preconditions.checkNotNull(getRule(ruleName));
- String contentBeforeCaret = input.getContentBeforeCaret();
+
+ String contentBeforeCaret = inputStatus.getContentBeforeCaret();
TokenStream stream = lex(contentBeforeCaret);
if (!stream.isEof()) {
Token lastToken = stream.getToken(stream.size()-2);
String matchWith;
- int beyondLastToken = input.getCaret() - lastToken.getStopIndex() -1;
+ int beyondLastToken = inputStatus.getCaret() - lastToken.getStopIndex() -1;
if (beyondLastToken > 0) {
- matchWith = contentBeforeCaret.substring(input.getCaret()-beyondLastToken, input.getCaret());
+ matchWith = contentBeforeCaret.substring(inputStatus.getCaret()-beyondLastToken, inputStatus.getCaret());
matchWith = StringUtils.trimStart(matchWith);
- mergeSuggestions(suggest(rule, stream, input, matchWith), texts);
+ mergeSuggestions(suggest(rule, stream, inputStatus, matchWith), inputSuggestions);
} else {
- mergeSuggestions(suggest(rule, stream, input, ""), texts);
+ mergeSuggestions(suggest(rule, stream, inputStatus, ""), inputSuggestions);
matchWith = stream.getToken(stream.size()-2).getText();
List<Token> tokens = stream.getTokens();
tokens.remove(tokens.size()-2);
stream = new TokenStream(tokens);
- mergeSuggestions(suggest(rule, stream, input, matchWith), texts);
+ mergeSuggestions(suggest(rule, stream, inputStatus, matchWith), inputSuggestions);
}
} else {
- mergeSuggestions(suggest(rule, stream, input, ""), texts);
+ mergeSuggestions(suggest(rule, stream, inputStatus, ""), inputSuggestions);
}
- return texts;
+ return inputSuggestions;
}
- private void mergeSuggestions(List<CaretAwareText> from, List<CaretAwareText> to) {
+ private void mergeSuggestions(List<InputSuggestion> from, List<InputSuggestion> to) {
Set<String> includedContents = new HashSet<>();
- for (CaretAwareText text: to)
- includedContents.add(text.getContent());
- for (CaretAwareText text: from) {
- if (!includedContents.contains(text.getContent())) {
- includedContents.add(text.getContent());
- to.add(text);
+ for (InputSuggestion suggestion: to)
+ includedContents.add(suggestion.getContent());
+ for (InputSuggestion suggestion: from) {
+ if (!includedContents.contains(suggestion.getContent())) {
+ includedContents.add(suggestion.getContent());
+ to.add(suggestion);
}
}
}
- private List<CaretAwareText> suggest(RuleSpec spec, TokenStream stream,
- CaretAwareText input, String matchWith) {
- List<CaretAwareText> texts = new ArrayList<>();
+ private List<InputSuggestion> suggest(RuleSpec spec, TokenStream stream,
+ InputStatus inputStatus, String matchWith) {
+ List<InputSuggestion> inputSuggestions = new ArrayList<>();
- List<ElementSuggestion> suggestions = new ArrayList<>();
+ List<ElementSuggestion> elementSuggestions = new ArrayList<>();
if (stream.isEof()) {
- suggestions.addAll(spec.suggestFirst(null, matchWith, stream));
+ elementSuggestions.addAll(spec.suggestFirst(null, matchWith, stream));
} else {
List<TokenNode> matches = spec.getPartialMatches(stream, null);
if (!matches.isEmpty() && stream.isEof()) {
for (TokenNode match: matches) {
ElementSpec matchSpec = (ElementSpec) match.getSpec();
- suggestions.addAll(matchSpec.suggestNext(match.getParent(), matchWith, stream));
+ elementSuggestions.addAll(matchSpec.suggestNext(match.getParent(), matchWith, stream));
}
}
}
- int replaceStart = input.getCaret() - matchWith.length();
- for (ElementSuggestion suggestion: suggestions) {
- int replaceEnd = input.getCaret();
- String contentAfterReplaceStart = input.getContent().substring(replaceStart);
+ int replaceStart = inputStatus.getCaret() - matchWith.length();
+ for (ElementSuggestion elementSuggestion: elementSuggestions) {
+ int replaceEnd = inputStatus.getCaret();
+ String contentAfterReplaceStart = inputStatus.getContent().substring(replaceStart);
TokenStream streamAfterReplaceStart = lex(contentAfterReplaceStart);
if (!streamAfterReplaceStart.isEof() && streamAfterReplaceStart.getToken(0).getStartIndex() == 0) {
- ElementSpec elementSpec = (ElementSpec) suggestion.getNode().getSpec();
+ ElementSpec elementSpec = (ElementSpec) elementSuggestion.getNode().getSpec();
if (elementSpec.matchOnce(streamAfterReplaceStart)) {
if (streamAfterReplaceStart.getIndex() != 0)
replaceEnd = replaceStart + streamAfterReplaceStart.getPreviousToken().getStopIndex()+1;
@@ -448,36 +449,36 @@ public abstract class CodeAssist {
}
List<String> mandatories;
- if (input.getCaret() == input.getContent().length()
- || Character.isWhitespace(input.getContent().charAt(input.getCaret()))) {
- mandatories = getMandatoriesAfter(suggestion.getNode());
+ if (inputStatus.getCaret() == inputStatus.getContent().length()
+ || Character.isWhitespace(inputStatus.getContent().charAt(inputStatus.getCaret()))) {
+ mandatories = getMandatoriesAfter(elementSuggestion.getNode());
} else {
mandatories = new ArrayList<>();
}
- String before = input.getContent().substring(0, replaceStart);
- String after = input.getContent().substring(replaceEnd);
+ String before = inputStatus.getContent().substring(0, replaceStart);
+ String after = inputStatus.getContent().substring(replaceEnd);
- for (CaretAwareText text: suggestion.getTexts()) {
+ for (InputSuggestion inputSuggestion: elementSuggestion.getInputSuggestions()) {
String newContent;
if (stream.size() > 1) {
- newContent = before + text.getContent();
+ newContent = before + inputSuggestion.getContent();
TokenStream newStream = lex(newContent);
if (newStream.size() >= stream.size()) {
Token lastToken = stream.getToken(stream.size()-2);
Token newToken = newStream.getToken(stream.size()-2);
if (lastToken.getStartIndex() != newToken.getStartIndex()
|| lastToken.getStopIndex() != newToken.getStopIndex()) {
- newContent = before + " " + text.getContent();
+ newContent = before + " " + inputSuggestion.getContent();
}
} else {
- newContent = before + " " + text.getContent();
+ newContent = before + " " + inputSuggestion.getContent();
}
} else {
- newContent = before + text.getContent();
+ newContent = before + inputSuggestion.getContent();
}
- int caret = text.getCaret();
- if (caret == text.getContent().length())
+ int caret = inputSuggestion.getCaret();
+ if (caret == inputSuggestion.getContent().length())
caret = newContent.length();
else
caret += before.length() + 1;
@@ -501,16 +502,16 @@ public abstract class CodeAssist {
newContent += after;
- if (text.getCaret() == text.getContent().length()
+ if (inputSuggestion.getCaret() == inputSuggestion.getContent().length()
&& caret < newContent.length()
&& !Character.isWhitespace(newContent.charAt(caret))) {
- caret += skipMandatoriesAfter(suggestion.getNode(), newContent.substring(caret), 0);
+ caret += skipMandatoriesAfter(elementSuggestion.getNode(), newContent.substring(caret), 0);
}
- texts.add(new CaretAwareText(newContent, caret));
+ inputSuggestions.add(new InputSuggestion(newContent, caret));
}
}
- return texts;
+ return inputSuggestions;
}
private List<String> getMandatoriesAfter(Node elementNode) {
@@ -562,7 +563,7 @@ public abstract class CodeAssist {
return offset;
}
- protected abstract List<CaretAwareText> suggest(ElementSpec spec, Node parent,
+ protected abstract List<InputSuggestion> suggest(ElementSpec spec, Node parent,
String matchWith, TokenStream stream);
}
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 216201c0d2..d573b26ec4 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
@@ -85,7 +85,7 @@ public abstract class ElementSpec extends Spec {
@Override
public List<ElementSuggestion> suggestFirst(Node parent, String matchWith, TokenStream stream) {
- List<CaretAwareText> texts = codeAssist.suggest(this, parent, matchWith, stream);
+ List<InputSuggestion> texts = codeAssist.suggest(this, parent, matchWith, stream);
if (texts != null)
return Lists.newArrayList(new ElementSuggestion(new Node(this, parent), texts));
else
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.java
index 834729b25d..92ef8d429e 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
@@ -6,19 +6,19 @@ public class ElementSuggestion {
private final Node node;
- private final List<CaretAwareText> texts;
+ private final List<InputSuggestion> inputSuggestions;
- public ElementSuggestion(Node node, List<CaretAwareText> texts) {
+ public ElementSuggestion(Node node, List<InputSuggestion> inputSuggestions) {
this.node = node;
- this.texts = texts;
+ this.inputSuggestions = inputSuggestions;
}
public Node getNode() {
return node;
}
- public List<CaretAwareText> getTexts() {
- return texts;
+ public List<InputSuggestion> getInputSuggestions() {
+ return inputSuggestions;
}
}
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/InputAssist.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/InputAssist.java
deleted file mode 100644
index 1ea685919e..0000000000
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/InputAssist.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package com.pmease.commons.antlr.codeassist;
-
-import java.io.Serializable;
-
-public class InputAssist implements Serializable {
-
- private static final long serialVersionUID = 1L;
-
- private final String content;
-
- private final int caret;
-
- public InputAssist(String content, int caret) {
- this.content = content;
- this.caret = caret;
- }
-
- public String getContent() {
- return content;
- }
-
- public int getCaret() {
- return caret;
- }
-
-}
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/CaretAwareText.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/InputStatus.java
similarity index 81%
rename from commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/CaretAwareText.java
rename to commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/InputStatus.java
index aa9ebcc8c7..43bfb86677 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/CaretAwareText.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/InputStatus.java
@@ -1,20 +1,20 @@
package com.pmease.commons.antlr.codeassist;
-public class CaretAwareText {
+public class InputStatus {
private final String content;
private final int caret;
-
- public CaretAwareText(String content, int caret) {
+
+ public InputStatus(String content, int caret) {
this.content = content;
this.caret = caret;
}
- public CaretAwareText(String content) {
+ public InputStatus(String content) {
this(content, content.length());
}
-
+
public String getContent() {
return content;
}
@@ -35,4 +35,5 @@ public class CaretAwareText {
public String toString() {
return content + ":" + caret;
}
+
}
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.java
new file mode 100644
index 0000000000..c7d8c7926f
--- /dev/null
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/InputSuggestion.java
@@ -0,0 +1,27 @@
+package com.pmease.commons.antlr.codeassist;
+
+import javax.annotation.Nullable;
+
+public class InputSuggestion extends InputStatus {
+
+ private final String description;
+
+ public InputSuggestion(String content, int caret, @Nullable String description) {
+ super(content, caret);
+ this.description = description;
+ }
+
+ public InputSuggestion(String content, int caret) {
+ this(content, caret, null);
+ }
+
+ public InputSuggestion(String content) {
+ this(content, content.length());
+ }
+
+ @Nullable
+ public String getDescription() {
+ return description;
+ }
+
+}
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.java
index 07f196f044..4d0c9939d9 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
@@ -27,7 +27,7 @@ public class LiteralElementSpec extends TokenElementSpec {
@Override
public List<ElementSuggestion> doSuggestFirst(Node parent, String matchWith, TokenStream stream) {
if (!matchWith.equals(literal) && literal.toLowerCase().startsWith(matchWith.toLowerCase())) {
- CaretAwareText text = new CaretAwareText(literal, literal.length());
+ InputSuggestion text = new InputSuggestion(literal, literal.length());
return Lists.newArrayList(new ElementSuggestion(new Node(this, parent), Lists.newArrayList(text)));
} else {
return new ArrayList<>();
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 1fccc6eba5..c979f9e325 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
@@ -12,11 +12,12 @@ import org.junit.Test;
import com.pmease.commons.antlr.ANTLRv4Lexer;
import com.pmease.commons.antlr.codeassist.AlternativeSpec;
import com.pmease.commons.antlr.codeassist.AnyTokenElementSpec;
-import com.pmease.commons.antlr.codeassist.CaretAwareText;
import com.pmease.commons.antlr.codeassist.CodeAssist;
import com.pmease.commons.antlr.codeassist.ElementSpec;
import com.pmease.commons.antlr.codeassist.ElementSpec.Multiplicity;
import com.pmease.commons.antlr.codeassist.EofElementSpec;
+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.LiteralElementSpec;
import com.pmease.commons.antlr.codeassist.Node;
@@ -37,7 +38,7 @@ public class CodeAssistTest {
}, "ANTLRv4Parser.tokens") {
@Override
- protected List<CaretAwareText> suggest(ElementSpec spec, Node parent,
+ protected List<InputSuggestion> suggest(ElementSpec spec, Node parent,
String matchWith, TokenStream stream) {
return suggester.suggest(spec, parent, matchWith, stream);
}
@@ -49,7 +50,7 @@ public class CodeAssistTest {
suggester = new Suggester() {
@Override
- public List<CaretAwareText> suggest(ElementSpec spec, Node parent, String matchWith, TokenStream stream) {
+ public List<InputSuggestion> suggest(ElementSpec spec, Node parent, String matchWith, TokenStream stream) {
return null;
}
@@ -126,28 +127,28 @@ public class CodeAssistTest {
@Test
public void testSuggestGrammarSpec() {
- List<CaretAwareText> suggestions;
+ List<InputSuggestion> suggestions;
- suggestions = codeAssist.suggest(new CaretAwareText(""), "grammarSpec");
+ suggestions = codeAssist.suggest(new InputStatus(""), "grammarSpec");
assertEquals(4, suggestions.size());
assertEquals("/**;", suggestions.get(0).getContent());
assertEquals("lexer grammar;", suggestions.get(1).getContent());
assertEquals("parser grammar;", suggestions.get(2).getContent());
assertEquals("grammar;", suggestions.get(3).getContent());
- suggestions = codeAssist.suggest(new CaretAwareText("parser"), "grammarSpec");
+ suggestions = codeAssist.suggest(new InputStatus("parser"), "grammarSpec");
assertEquals(1, suggestions.size());
assertEquals("parser grammar", suggestions.get(0).getContent());
- suggestions = codeAssist.suggest(new CaretAwareText("gr"), "grammarSpec");
+ suggestions = codeAssist.suggest(new InputStatus("gr"), "grammarSpec");
assertEquals(1, suggestions.size());
assertEquals("grammar;", suggestions.get(0).getContent());
- suggestions = codeAssist.suggest(new CaretAwareText("grammar", 4), "grammarSpec");
+ suggestions = codeAssist.suggest(new InputStatus("grammar", 4), "grammarSpec");
assertEquals(1, suggestions.size());
assertEquals("grammar:7", suggestions.get(0).toString());
- suggestions = codeAssist.suggest(new CaretAwareText("grammar grammar1;rule1:TOKEN1;"), "grammarSpec");
+ suggestions = codeAssist.suggest(new InputStatus("grammar grammar1;rule1:TOKEN1;"), "grammarSpec");
assertEquals(8, suggestions.size());
assertEquals("grammar grammar1;rule1:TOKEN1;catch[]{}:36", suggestions.get(0).toString());
assertEquals("grammar grammar1;rule1:TOKEN1;finally{}:38", suggestions.get(1).toString());
@@ -161,37 +162,37 @@ public class CodeAssistTest {
@Test
public void testSuggestOptionsSpec() {
- List<CaretAwareText> suggestions;
+ List<InputSuggestion> suggestions;
- suggestions = codeAssist.suggest(new CaretAwareText(""), "optionsSpec");
+ suggestions = codeAssist.suggest(new InputStatus(""), "optionsSpec");
assertEquals(1, suggestions.size());
assertEquals("options{}:8", suggestions.get(0).toString());
- suggestions = codeAssist.suggest(new CaretAwareText("options{100}", 2), "optionsSpec");
+ suggestions = codeAssist.suggest(new InputStatus("options{100}", 2), "optionsSpec");
assertEquals(1, suggestions.size());
assertEquals("options{100}:8", suggestions.get(0).toString());
- suggestions = codeAssist.suggest(new CaretAwareText("options{a"), "optionsSpec");
+ suggestions = codeAssist.suggest(new InputStatus("options{a"), "optionsSpec");
assertEquals(1, suggestions.size());
assertEquals("options{a=:10", suggestions.get(0).toString());
- suggestions = codeAssist.suggest(new CaretAwareText("options{"), "optionsSpec");
+ suggestions = codeAssist.suggest(new InputStatus("options{"), "optionsSpec");
assertEquals(1, suggestions.size());
assertEquals("options{}:9", suggestions.get(0).toString());
suggester = new Suggester() {
@Override
- public List<CaretAwareText> suggest(ElementSpec spec, Node parent, String matchWith, TokenStream stream) {
+ public List<InputSuggestion> suggest(ElementSpec spec, Node parent, String matchWith, TokenStream stream) {
if (spec instanceof RuleRefElementSpec) {
RuleRefElementSpec ruleRefElementSpec = (RuleRefElementSpec) spec;
if (ruleRefElementSpec.getRuleName().equals("id")) {
- List<CaretAwareText> texts = new ArrayList<>();
+ List<InputSuggestion> suggestions = new ArrayList<>();
if ("hello".startsWith(matchWith))
- texts.add(new CaretAwareText("hello"));
+ suggestions.add(new InputSuggestion("hello"));
if ("world".startsWith(matchWith))
- texts.add(new CaretAwareText("world"));
- return texts;
+ suggestions.add(new InputSuggestion("world"));
+ return suggestions;
}
}
return null;
@@ -199,18 +200,18 @@ public class CodeAssistTest {
};
- suggestions = codeAssist.suggest(new CaretAwareText("options{he"), "optionsSpec");
+ suggestions = codeAssist.suggest(new InputStatus("options{he"), "optionsSpec");
assertEquals(2, suggestions.size());
assertEquals("options{he=:11", suggestions.get(0).toString());
assertEquals("options{hello=;:14", suggestions.get(1).toString());
- suggestions = codeAssist.suggest(new CaretAwareText("options{"), "optionsSpec");
+ suggestions = codeAssist.suggest(new InputStatus("options{"), "optionsSpec");
assertEquals(3, suggestions.size());
assertEquals("options{hello=;:14", suggestions.get(0).toString());
assertEquals("options{world=;:14", suggestions.get(1).toString());
assertEquals("options{}:9", suggestions.get(2).toString());
- suggestions = codeAssist.suggest(new CaretAwareText("options{hello=100}", 8), "optionsSpec");
+ suggestions = codeAssist.suggest(new InputStatus("options{hello=100}", 8), "optionsSpec");
assertEquals(3, suggestions.size());
assertEquals("options{hello=100}:14", suggestions.get(0).toString());
assertEquals("options{world=100}:14", suggestions.get(1).toString());
@@ -219,9 +220,9 @@ public class CodeAssistTest {
@Test
public void testSuggestRuleSpec() {
- List<CaretAwareText> suggestions;
+ List<InputSuggestion> suggestions;
- suggestions = codeAssist.suggest(new CaretAwareText("query"), "ruleSpec");
+ suggestions = codeAssist.suggest(new InputStatus("query"), "ruleSpec");
assertEquals(7, suggestions.size());
assertEquals("query[]:6", suggestions.get(0).toString());
assertEquals("query returns[]:14", suggestions.get(1).toString());
@@ -233,7 +234,7 @@ public class CodeAssistTest {
}
interface Suggester {
- List<CaretAwareText> suggest(ElementSpec spec, Node parent,
+ List<InputSuggestion> suggest(ElementSpec spec, Node parent,
String matchWith, TokenStream stream);
}
}
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 9de34c71be..aabe5e75c1 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
@@ -17,11 +17,13 @@ import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.Token;
import com.pmease.commons.antlr.AntlrUtils;
-import com.pmease.commons.antlr.codeassist.CaretAwareText;
+import com.pmease.commons.antlr.codeassist.InputSuggestion;
import com.pmease.commons.antlr.codeassist.CodeAssist;
import com.pmease.commons.antlr.codeassist.ElementSpec;
+import com.pmease.commons.antlr.codeassist.InputStatus;
import com.pmease.commons.antlr.codeassist.Node;
import com.pmease.commons.antlr.codeassist.TokenStream;
+import com.pmease.commons.util.StringUtils;
@SuppressWarnings("serial")
public abstract class ANTLRAssistBehavior extends InputAssistBehavior {
@@ -57,7 +59,7 @@ public abstract class ANTLRAssistBehavior extends InputAssistBehavior {
codeAssist = new CodeAssist(lexerClass, grammarFiles, tokenFile) {
@Override
- protected List<CaretAwareText> suggest(ElementSpec spec, Node parent, String matchWith,
+ protected List<InputSuggestion> suggest(ElementSpec spec, Node parent, String matchWith,
TokenStream stream) {
return ANTLRAssistBehavior.this.suggest(spec, parent, matchWith, stream);
}
@@ -67,17 +69,17 @@ public abstract class ANTLRAssistBehavior extends InputAssistBehavior {
}
@Override
- protected List<InputAssist> getAssists(String input, final int caret) {
-
+ protected List<InputSuggestion> getSuggestions(InputStatus inputStatus) {
+ return codeAssist.suggest(inputStatus, ruleName);
}
@Override
- protected List<InputError> getErrors(String input) {
+ protected List<InputError> getErrors(String inputContent) {
final List<InputError> errors = new ArrayList<>();
Lexer lexer;
try {
- lexer = lexerCtor.newInstance(new ANTLRInputStream(input));
+ lexer = lexerCtor.newInstance(new ANTLRInputStream(inputContent));
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
throw new RuntimeException(e);
@@ -120,6 +122,19 @@ public abstract class ANTLRAssistBehavior extends InputAssistBehavior {
return errors;
}
- protected abstract List<CaretAwareText> suggest(ElementSpec spec, Node parent,
+ @Override
+ protected int getAnchor(String content) {
+ TokenStream stream = codeAssist.lex(content);
+ if (stream.isEof()) {
+ return content.length();
+ } else {
+ Token lastToken = stream.getToken(stream.size()-2);
+ String contentAfterLastToken = content.substring(lastToken.getStopIndex()+1);
+ contentAfterLastToken = StringUtils.trimStart(contentAfterLastToken);
+ return content.length() - contentAfterLastToken.length();
+ }
+ }
+
+ protected abstract List<InputSuggestion> suggest(ElementSpec spec, Node parent,
String matchWith, TokenStream stream);
}
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/AssistPanel.html b/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/AssistPanel.html
index ee624df6f4..b73df6986b 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/AssistPanel.html
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/AssistPanel.html
@@ -1,6 +1,6 @@
<wicket:panel>
- <ul class="input-assists list-unstyled">
- <li wicket:id="assists" class="selectable" tabindex="-1">
+ <ul class="input-assist list-unstyled">
+ <li wicket:id="suggestions" class="selectable" tabindex="-1">
<a wicket:id="link"><span wicket:id="label"></span></a>
</li>
</ul>
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/AssistPanel.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/AssistPanel.java
index 39eea55c9e..4cccab9a04 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/AssistPanel.java
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/AssistPanel.java
@@ -9,31 +9,32 @@ import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
+import com.pmease.commons.antlr.codeassist.InputSuggestion;
+
@SuppressWarnings("serial")
class AssistPanel extends Panel {
- private final List<InputAssist> assists;
+ private final List<InputSuggestion> suggestions;
- public AssistPanel(String id, List<InputAssist> assists) {
+ public AssistPanel(String id, List<InputSuggestion> suggestions) {
super(id);
-
- this.assists = assists;
+ this.suggestions = suggestions;
}
@Override
protected void onInitialize() {
super.onInitialize();
- add(new ListView<InputAssist>("assists", assists) {
+ add(new ListView<InputSuggestion>("suggestions", suggestions) {
@Override
- protected void populateItem(ListItem<InputAssist> item) {
- InputAssist assist = item.getModelObject();
+ protected void populateItem(ListItem<InputSuggestion> item) {
+ InputSuggestion suggestion = item.getModelObject();
WebMarkupContainer link = new WebMarkupContainer("link");
- link.add(new Label("label", assist.getInput()));
+ link.add(new Label("label", suggestion.getContent()));
item.add(link);
- item.add(AttributeAppender.append("data-input", assist.getInput()));
- item.add(AttributeAppender.append("data-caret", assist.getCaret()));
+ item.add(AttributeAppender.append("data-input", suggestion.getContent()));
+ item.add(AttributeAppender.append("data-caret", suggestion.getCaret()));
}
});
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/InputAssist.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/InputAssist.java
deleted file mode 100644
index 0a314e56ce..0000000000
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/InputAssist.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package com.pmease.commons.wicket.behavior.inputassist;
-
-import java.io.Serializable;
-
-public class InputAssist implements Serializable {
-
- private static final long serialVersionUID = 1L;
-
- private final String input;
-
- private final int caret;
-
- public InputAssist(String input, int caret) {
- this.input = input;
- this.caret = caret;
- }
-
- public String getInput() {
- return input;
- }
-
- public int getCaret() {
- return caret;
- }
-
-}
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.java
index 294e9d823f..82da680b27 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
@@ -23,11 +23,14 @@ import org.apache.wicket.request.resource.JavaScriptResourceReference;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.pmease.commons.antlr.codeassist.InputStatus;
+import com.pmease.commons.antlr.codeassist.InputSuggestion;
import com.pmease.commons.loader.AppLoader;
import com.pmease.commons.wicket.assets.caret.CaretResourceReference;
import com.pmease.commons.wicket.assets.doneevents.DoneEventsResourceReference;
import com.pmease.commons.wicket.assets.hotkeys.HotkeysResourceReference;
import com.pmease.commons.wicket.assets.textareacaretposition.TextareaCaretPositionResourceReference;
+import com.pmease.commons.wicket.component.floating.AlignWith;
import com.pmease.commons.wicket.component.floating.AlignWithComponent;
import com.pmease.commons.wicket.component.floating.Alignment;
import com.pmease.commons.wicket.component.floating.FloatingPanel;
@@ -95,20 +98,21 @@ public abstract class InputAssistBehavior extends AbstractDefaultAjaxBehavior {
getComponent().getMarkupId(), json);
target.appendJavaScript(script);
- final List<InputAssist> assists;
+ final List<InputSuggestion> suggestions;
if (caret != null)
- assists = getAssists(input, caret);
+ suggestions = getSuggestions(new InputStatus(input, caret));
else
- assists = new ArrayList<>();
+ suggestions = new ArrayList<>();
- if (!assists.isEmpty()) {
+ if (!suggestions.isEmpty()) {
+ int anchor = getAnchor(input.substring(0, caret));
if (dropdown == null) {
- dropdown = new FloatingPanel(target, new AlignWithComponent(getComponent()), Alignment.bottom(0)) {
+ dropdown = new FloatingPanel(target, new AlignWithComponent(getComponent(), anchor), Alignment.bottom(0)) {
@Override
protected Component newContent(String id) {
- return new AssistPanel(id, assists);
+ return new AssistPanel(id, suggestions);
}
@Override
@@ -123,13 +127,17 @@ public abstract class InputAssistBehavior extends AbstractDefaultAjaxBehavior {
target.appendJavaScript(script);
} else {
Component content = dropdown.getContent();
- Component newContent = new AssistPanel(content.getId(), assists);
+ Component newContent = new AssistPanel(content.getId(), suggestions);
content.replaceWith(newContent);
target.add(newContent);
script = String.format("pmease.commons.inputassist.assistUpdated('%s', '%s');",
getComponent().getMarkupId(), dropdown.getMarkupId());
target.appendJavaScript(script);
+
+ AlignWith alignWith = new AlignWithComponent(getComponent(), anchor);
+ script = String.format("$('#%s').align(%s);", dropdown.getMarkupId(), alignWith.toJSON());
+ target.appendJavaScript(script);
}
} else if (dropdown != null) {
dropdown.close(target);
@@ -157,7 +165,10 @@ public abstract class InputAssistBehavior extends AbstractDefaultAjaxBehavior {
response.render(OnDomReadyHeaderItem.forScript(script));
}
- protected abstract List<InputAssist> getAssists(String input, int caret);
+ protected abstract List<InputSuggestion> getSuggestions(InputStatus inputStatus);
+
+ protected abstract List<InputError> getErrors(String inputContent);
+
+ protected abstract int getAnchor(String content);
- protected abstract List<InputError> getErrors(String input);
}
\ No newline at end of file
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/input-assist.css b/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/input-assist.css
index 7d1909fdb4..5a5c1b43cc 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/input-assist.css
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/input-assist.css
@@ -1,4 +1,4 @@
-.input-assists {
+.input-assist {
padding: 8px;
}
.input-error-mark {
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/component/floating/AlignWithComponent.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/component/floating/AlignWithComponent.java
index bf52ca706a..7fe72da262 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/component/floating/AlignWithComponent.java
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/component/floating/AlignWithComponent.java
@@ -8,14 +8,22 @@ public class AlignWithComponent implements AlignWith {
private final Component component;
- public AlignWithComponent(Component component) {
+ private final int index;
+
+ public AlignWithComponent(Component component, int index) {
this.component = component;
+ this.index = index;
+
component.setOutputMarkupId(true);
}
+ public AlignWithComponent(Component component) {
+ this(component, -1);
+ }
+
@Override
public String toJSON() {
- return "'" + component.getMarkupId() + "'";
+ return String.format("{element: '%s', index: %d}", component.getMarkupId(), index);
}
}
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/component/floating/floating.js b/commons.wicket/src/main/java/com/pmease/commons/wicket/component/floating/floating.js
index 716935fc80..f19894eb87 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/component/floating/floating.js
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/component/floating/floating.js
@@ -26,8 +26,9 @@ pmease.commons.floating = {
// use keydown as keypress does not work in chrome/safari
$(document).on("keydown", $floating.data("keydown"));
- if (typeof alignWith === "string") {
- alignment.target = $("#" + alignWith);
+ if (alignWith.element) {
+ alignment.target = $("#" + alignWith.element);
+ alignment.index = alignWith.index;
alignment.target.addClass("floating-aligned");
} else {
alignment.target = alignWith;
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 54bd36494a..318e4eb9c8 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,65 +1,24 @@
package com.pmease.gitplex.web.page.repository.commit;
-import java.util.ArrayList;
import java.util.List;
-import org.antlr.v4.runtime.ANTLRErrorListener;
-import org.antlr.v4.runtime.ANTLRInputStream;
-import org.antlr.v4.runtime.CommonTokenStream;
-import org.antlr.v4.runtime.ParserRuleContext;
-import org.antlr.v4.runtime.Token;
-
+import com.pmease.commons.antlr.codeassist.ElementSpec;
+import com.pmease.commons.antlr.codeassist.InputSuggestion;
+import com.pmease.commons.antlr.codeassist.Node;
+import com.pmease.commons.antlr.codeassist.TokenStream;
import com.pmease.commons.wicket.behavior.inputassist.ANTLRAssistBehavior;
-import com.pmease.commons.wicket.behavior.inputassist.InputAssist;
@SuppressWarnings("serial")
public class QueryAssistBehavior extends ANTLRAssistBehavior {
- protected ParserRuleContext parse(String input, ANTLRErrorListener lexerErrorListener,
- ANTLRErrorListener parserErrorListener) {
- CommitQueryLexer lexer = new CommitQueryLexer(new ANTLRInputStream(input));
- CommonTokenStream tokens = new CommonTokenStream(lexer);
- CommitQueryParser parser = new CommitQueryParser(tokens);
- lexer.removeErrorListeners();
- parser.removeErrorListeners();
- lexer.addErrorListener(lexerErrorListener);
- parser.addErrorListener(parserErrorListener);
- return parser.query();
+ public QueryAssistBehavior() {
+ super(CommitQueryParser.class, "query");
}
@Override
- protected List<InputAssist> getInsertions(ParserRuleContext ruleBeforeCaret,
- List<Token> tokensBeforeCaret, String textToMatch) {
- List<InputAssist> insertions = new ArrayList<>();
-
- if (ruleBeforeCaret.getRuleIndex() == CommitQueryParser.RULE_query && ruleBeforeCaret.getStop() != null) {
- // previous input is a valid query
- suggestCriterias(insertions, textToMatch);
- }
-
- return insertions;
- }
-
- private void suggestCriteria(List<InputAssist> insertions, String criteriaName, String textToMatch) {
- if (criteriaName.startsWith(textToMatch))
- insertions.add(new InputAssist(criteriaName + "()", criteriaName.length()+1));
+ protected List<InputSuggestion> suggest(ElementSpec spec, Node parent,
+ String matchWith, TokenStream stream) {
+ return null;
}
- private void suggestRevisionCriterias(List<InputAssist> insertions, String textToMatch) {
- suggestCriteria(insertions, "branch", textToMatch);
- suggestCriteria(insertions, "tag", textToMatch);
- suggestCriteria(insertions, "id", textToMatch);
- }
-
- private void suggestCriterias(List<InputAssist> insertions, String textToMatch) {
- suggestRevisionCriterias(insertions, textToMatch);
- suggestCriteria(insertions, "path", textToMatch);
- suggestCriteria(insertions, "before", textToMatch);
- suggestCriteria(insertions, "after", textToMatch);
- suggestCriteria(insertions, "author", textToMatch);
- suggestCriteria(insertions, "committer", textToMatch);
- suggestCriteria(insertions, "message", textToMatch);
-
- }
-
}