Skip to content

The Unified and Autonomous Development Platform

midwork

robin shine4ed65054becb
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 f523496eb2..c930775329 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,13 +2,10 @@ package com.pmease.commons.antlr.codeassist;
import java.io.Serializable;
import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
-import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
import java.util.List;
-import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
@@ -17,7 +14,6 @@ import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.Token;
import com.google.common.base.Preconditions;
-import com.google.common.collect.Sets;
import com.pmease.commons.antlr.grammar.AlternativeSpec;
import com.pmease.commons.antlr.grammar.ElementSpec;
import com.pmease.commons.antlr.grammar.Grammar;
@@ -69,7 +65,7 @@ public abstract class CodeAssist implements Serializable {
List<Token> tokens = grammar.lex(inputStatus.getContentBeforeCaret());
EarleyParser parser = new EarleyParser(rule, tokens);
- List<String> hints = new ArrayList<>();
+ Set<String> hints = new LinkedHashSet<>();
for (Chart chart: getTerminatingCharts(parser)) {
String matchWith = getMatchWith(chart, inputStatus);
for (List<ParentedElement> expectChain: getExpectChains(chart)) {
@@ -77,7 +73,7 @@ public abstract class CodeAssist implements Serializable {
hints.addAll(getHints(element, matchWith));
}
}
- return hints;
+ return new ArrayList<>(hints);
}
public List<InputCompletion> suggest(InputStatus inputStatus, String ruleName, int count) {
@@ -85,56 +81,22 @@ public abstract class CodeAssist implements Serializable {
String inputContent = inputStatus.getContent();
List<InputCompletion> inputCompletions = new ArrayList<>();
-
- /*
- * Mandatory literals may come after a suggestion (for instance if a method name is suggested,
- * the character '(' can be a mandatory literal), so for every suggestion, we need to
- * append mandatory literals to make user typing less, however if a suggested text has two or
- * more different mandatories (this is possible if the suggested text comes from different
- * element specs, and each spec calculates a different set of mandatories after the text), we
- * should display the suggested text as a single entry in the final suggestion list without
- * appending mandatories they are no longer mandatories from aspect of whole rule. Below code
- * mainly handles this logic.
- */
- Map<String, List<ElementCompletion>> grouped = new LinkedHashMap<>();
- for (ElementCompletion elementCompletion: suggest(rule, inputStatus, count)) {
- /*
- * key will be the new input (without considering mandatories of course), and we use
- * this to group suggestions to facilitate mandatories calculation and exclusion
- * logic
- */
- String key = inputContent.substring(0, elementCompletion.getReplaceBegin())
- + elementCompletion.getReplaceContent()
- + inputContent.substring(elementCompletion.getReplaceEnd());
- List<ElementCompletion> value = grouped.get(key);
- if (value == null) {
- value = new ArrayList<>();
- grouped.put(key, value);
- }
- value.add(elementCompletion);
- }
-
- for (Map.Entry<String, List<ElementCompletion>> entry: grouped.entrySet()) {
- List<ElementCompletion> value = entry.getValue();
- ElementCompletion completion = value.get(0);
- String description = completion.getDescription();
- String content = entry.getKey();
-
- /*
- * only append mandatory literals if suggested content is complete and it does not
- * replace any part of current input. We do not append mandatories in the latter case
- * as most probably they already exist in the current input
- */
- if (completion.isComplete() && completion.getReplaceEnd() == inputStatus.getCaret()) {
- List<String> contents = new ArrayList<>();
- for (ElementCompletion each: value) {
- content = inputContent.substring(0, each.getReplaceBegin()) + each.getReplaceContent();
- ParentedElement parentElement = each.getExpectedElement().getParent();
- ElementSpec elementSpec = each.getExpectedElement().getSpec();
+
+ Set<String> labels = new HashSet<>();
+ for (ElementCompletion completion: suggest(rule, inputStatus, count)) {
+ if (!labels.contains(completion.getLabel())) {
+ labels.add(completion.getLabel());
+ String content = inputContent.substring(0, completion.getReplaceBegin())
+ + completion.getReplaceContent();
+ boolean mandatoriesAdded = false;
+ if (completion.isComplete() && completion.getReplaceEnd() == inputStatus.getCaret()) {
+ ParentedElement parentElement = completion.getExpectedElement().getParent();
+ ElementSpec elementSpec = completion.getExpectedElement().getSpec();
for (String mandatory: getMandatoriesAfter(parentElement, elementSpec)) {
String prevContent = content;
content += mandatory;
-
+ mandatoriesAdded = true;
+
if (grammar.getTokenTypeByLiteral(mandatory) != null) {
/*
* if mandatory can be appended without space, the last token
@@ -152,56 +114,46 @@ public abstract class CodeAssist implements Serializable {
}
}
}
- content += inputContent.substring(each.getReplaceEnd());
-
- if (contents.isEmpty()) {
- contents.add(content);
- } else if (!contents.get(contents.size()-1).equals(content)) {
- content = entry.getKey();
- break;
- }
}
- }
+ content += inputContent.substring(completion.getReplaceEnd());
- /*
- * Adjust caret to move it to next place expecting user input if suggestion is
- * complete, 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 = completion.getCaret();
- if (completion.isComplete() && caret == -1) {
- // caret is not at the middle of replacement, so we need to move it to
- // be after mandatory tokens if provided suggestion is complete
- caret = content.length() - inputContent.length() + completion.getReplaceEnd();
- 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);
- ParentedElement parentElement = completion.getExpectedElement().getParent();
- ElementSpec elementSpec = completion.getExpectedElement().getSpec();
- caret += skipMandatories(contentAfterCaret, getMandatoriesAfter(parentElement, elementSpec));
+ /*
+ * Adjust caret to move it to next place expecting user input if suggestion is
+ * complete, 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 = completion.getCaret();
+ if (completion.isComplete() && caret == -1) {
+ // caret is not at the middle of replacement, so we need to move it to
+ // be after mandatory tokens if provided suggestion is complete
+ caret = content.length() - inputContent.length() + completion.getReplaceEnd();
+ if (!mandatoriesAdded) {
+ // in case mandatory tokens are not added after replacement,
+ // we skip existing mandatory tokens in current input
+ String contentAfterCaret = content.substring(caret);
+ ParentedElement parentElement = completion.getExpectedElement().getParent();
+ ElementSpec elementSpec = completion.getExpectedElement().getSpec();
+ caret += skipMandatories(contentAfterCaret, getMandatoriesAfter(parentElement, elementSpec));
+ }
+ } else {
+ if (caret == -1)
+ caret = completion.getReplaceContent().length();
+ caret += completion.getReplaceBegin();
}
- } else {
- if (caret == -1)
- caret = completion.getReplaceContent().length();
- caret += completion.getReplaceBegin();
+
+ String replaceContent = content.substring(completion.getReplaceBegin(),
+ content.length()-inputContent.length()+completion.getReplaceEnd());
+ inputCompletions.add(new InputCompletion(completion.getReplaceBegin(),
+ completion.getReplaceEnd(), replaceContent, caret, completion.getLabel(),
+ completion.getDescription(), completion.getMatchRange()));
}
-
- String replaceContent = content.substring(completion.getReplaceBegin(),
- content.length()-inputContent.length()+completion.getReplaceEnd());
- String label = completion.getLabel();
- if (label == null)
- label = completion.getReplaceContent();
- inputCompletions.add(new InputCompletion(completion.getReplaceBegin(),
- completion.getReplaceEnd(), replaceContent, caret, label,
- description, completion.getMatchRange()));
}
/*
- * remove duplicate suggestions and suggestions the same as current input
+ * remove duplicate suggestions
*/
- Set<String> suggestedContents = Sets.newHashSet(inputStatus.getContent());
+ Set<String> suggestedContents = new HashSet<>();
for (Iterator<InputCompletion> it = inputCompletions.iterator(); it.hasNext();) {
String content = it.next().complete(inputStatus).getContent();
if (suggestedContents.contains(content))
@@ -314,19 +266,17 @@ public abstract class CodeAssist implements Serializable {
List<ElementSuggestion> suggestions = new ArrayList<>();
String matchWith = getMatchWith(chart, inputStatus);
+ int numSuggested = 0;
for (List<ParentedElement> expectChain: getExpectChains(chart)) {
// goes through the expect chain to check if there are suggestions for expected element
List<InputSuggestion> inputSuggestions = null;
for (ParentedElement element: expectChain) {
- inputSuggestions = suggest(element, matchWith, count);
+ inputSuggestions = suggest(element, matchWith, count - numSuggested);
if (inputSuggestions != null) {
- for (Iterator<InputSuggestion> it = inputSuggestions.iterator(); it.hasNext();) {
- InputSuggestion suggestion = it.next();
- if (suggestion.getContent().length() != 0 && suggestion.getContent().equals(matchWith))
- it.remove();
- }
- if (!inputSuggestions.isEmpty())
+ if (!inputSuggestions.isEmpty()) {
suggestions.add(new ElementSuggestion(element, matchWith, inputSuggestions));
+ numSuggested += inputSuggestions.size();
+ }
break;
}
}
@@ -341,56 +291,30 @@ public abstract class CodeAssist implements Serializable {
List<Token> tokens = grammar.lex(leadingLiteral);
boolean complete = tokens.size() == 1 && tokens.get(0).getType() == spec.getTokenType();
InputSuggestion suggestion = wrapAsSuggestion(elementExpectingTerminal, leadingLiteral, complete);
- if (suggestion != null)
+ if (suggestion != null) {
inputSuggestions.add(suggestion);
+ if (++numSuggested >= count)
+ break;
+ }
}
}
if (!inputSuggestions.isEmpty())
suggestions.add(new ElementSuggestion(elementExpectingTerminal, matchWith, inputSuggestions));
}
+
+ if (numSuggested >= count)
+ break;
}
- // sort suggestions to make it the same order as appearing in grammar definition
- Collections.sort(suggestions, new Comparator<ElementSuggestion>() {
-
- @Override
- public int compare(ElementSuggestion suggestion1, ElementSuggestion suggestion2) {
- ParentedElement root1 = suggestion1.getExpectedElement().getRoot();
- ParentedElement root2 = suggestion2.getExpectedElement().getRoot();
- return compare(root1, root2);
- }
-
- private int compare(Element element1, Element element2) {
- Preconditions.checkState(element1.getState() != null && element2.getState() != null);
- int alternativeSpecIndex1 = element1.getState().getAlternativeSpecIndex();
- int alternativeSpecIndex2 = element2.getState().getAlternativeSpecIndex();
- if (alternativeSpecIndex1 != alternativeSpecIndex2)
- return alternativeSpecIndex1 - alternativeSpecIndex2;
- int expectedElementSpecIndex1 = element1.getState().getExpectedElementSpecIndex();
- int expectedElementSpecIndex2 = element2.getState().getExpectedElementSpecIndex();
- if (expectedElementSpecIndex1 != expectedElementSpecIndex2)
- return expectedElementSpecIndex1 - expectedElementSpecIndex2;
- element1 = element1.getState().getElements().get(element1.getState().getElements().size()-1);
- element2 = element2.getState().getElements().get(element2.getState().getElements().size()-1);
- if (element1.getState() == null || element2.getState() == null)
- return 0;
- else
- return compare(element1, element2);
- }
-
- });
return suggestions;
}
private List<Chart> getTerminatingCharts(EarleyParser parser) {
List<Chart> terminatingCharts = new ArrayList<>();
- if (parser.getCharts().size() >= 1)
- terminatingCharts.add(parser.getCharts().get(parser.getCharts().size()-1));
-
/*
- * do another match by not considering the last matched token. This is useful
- * for below cases:
+ * Use the second last chart in order to not considering the last matched token.
+ * This is useful for below cases:
* 1. when the last token matches either a keyword or part of an identifier.
* For instance, the last token can be keyword 'for', but can also match
* identifier 'forme' if the spec allows.
@@ -403,6 +327,9 @@ public abstract class CodeAssist implements Serializable {
if (parser.getCharts().size() >= 2)
terminatingCharts.add(parser.getCharts().get(parser.getCharts().size()-2));
+ if (parser.getCharts().size() >= 1)
+ terminatingCharts.add(parser.getCharts().get(parser.getCharts().size()-1));
+
return terminatingCharts;
}
@@ -414,8 +341,15 @@ public abstract class CodeAssist implements Serializable {
List<Token> tokens = grammar.lex(inputStatus.getContentBeforeCaret());
EarleyParser parser = new EarleyParser(spec, tokens);
- for (Chart chart: getTerminatingCharts(parser))
- suggestions.addAll(suggest(chart, inputStatus, count));
+ int numSuggested = 0;
+ for (Chart chart: getTerminatingCharts(parser)) {
+ List<ElementSuggestion> chartSuggestions = suggest(chart, inputStatus, count-numSuggested);
+ suggestions.addAll(chartSuggestions);
+ for (ElementSuggestion each: chartSuggestions)
+ numSuggested += each.getInputSuggestions().size();
+ if (numSuggested >= count)
+ break;
+ }
String inputContent = inputStatus.getContent();
for (ElementSuggestion suggestion: suggestions) {
@@ -465,8 +399,8 @@ public abstract class CodeAssist implements Serializable {
// 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(),
- inputSuggestion.getLabel(), inputSuggestion.getMatchRange()));
+ inputSuggestion.isComplete(), inputSuggestion.getLabel(),
+ inputSuggestion.getDescription(), inputSuggestion.getMatchRange()));
}
}
return completions;
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/InputCompletion.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/InputCompletion.java
index 9a60597628..d19f040ea2 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/InputCompletion.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/InputCompletion.java
@@ -30,7 +30,10 @@ public class InputCompletion implements Serializable {
this.replaceEnd = replaceEnd;
this.replaceContent = replaceContent;
this.caret = caret;
- this.label = label;
+ if (label != null)
+ this.label = label;
+ else
+ this.label = replaceContent;
this.description = description;
this.matchRange = matchRange;
}
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest4.g4 b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest4.g4
index be02f7f3b5..18c1aada5b 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest4.g4
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest4.g4
@@ -1,7 +1,7 @@
-grammar CodeAssistTest4;
-
-stat: expr ';' | ID '=' expr ';' | ';';
-expr: INT | ID | expr ('+'|'-'|'*'|'/') expr | '(' expr ')';
-ID: [a-zA-Z]+;
-INT: [0-9]+;
-WS: [ \t\n]+ -> skip;
+grammar CodeAssistTest4;
+
+stat: expr ';' | ID '=' expr ';' | ';';
+expr: INT | ID | expr ('+'|'-'|'*'|'/') expr | '(' expr ')';
+ID: [a-zA-Z]+;
+INT: [0-9]+;
+WS: [ \t\n]+ -> skip;
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 866429bdd4..43ff0d067d 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
@@ -43,7 +43,7 @@ public class CodeAssistTest1 {
suggestions = suggest(new InputStatus("ab "), "selfReference");
assertEquals(1, suggestions.size());
- assertEquals("ab cd:5", suggestions.get(0).toString());
+ assertEquals("ab cd cd:8", suggestions.get(0).toString());
suggestions = suggest(new InputStatus(""), "selfReference");
assertEquals(1, suggestions.size());
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 686e971e10..e1ca344d61 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
@@ -80,21 +80,21 @@ public class CodeAssistTest2 {
assertEquals("branch(feature)tag(:19", suggestions.get(1).toString());
assertEquals("branch(feature)id(:18", suggestions.get(2).toString());
assertEquals("branch(feature)^:16", suggestions.get(3).toString());
- assertEquals("branch(feature)..:17", suggestions.get(4).toString());
- assertEquals("branch(feature)...:18", suggestions.get(5).toString());
- assertEquals("branch(feature)before(:22", suggestions.get(6).toString());
- assertEquals("branch(feature)after(:21", suggestions.get(7).toString());
- assertEquals("branch(feature)committer(:25", suggestions.get(8).toString());
- assertEquals("branch(feature)author(:22", suggestions.get(9).toString());
- assertEquals("branch(feature)path(:20", suggestions.get(10).toString());
- assertEquals("branch(feature)message(:23", suggestions.get(11).toString());
+ assertEquals("branch(feature)before(:22", suggestions.get(4).toString());
+ assertEquals("branch(feature)after(:21", suggestions.get(5).toString());
+ assertEquals("branch(feature)committer(:25", suggestions.get(6).toString());
+ assertEquals("branch(feature)author(:22", suggestions.get(7).toString());
+ assertEquals("branch(feature)path(:20", suggestions.get(8).toString());
+ assertEquals("branch(feature)message(:23", suggestions.get(9).toString());
+ assertEquals("branch(feature)..:17", suggestions.get(10).toString());
+ assertEquals("branch(feature)...:18", suggestions.get(11).toString());
suggestions = 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());
+ assertEquals("^:1", suggestions.get(0).toString());
+ assertEquals("branch(:7", suggestions.get(1).toString());
+ assertEquals("tag(:4", suggestions.get(2).toString());
+ assertEquals("id(:3", suggestions.get(3).toString());
suggestions = suggest(new InputStatus("branch(master)"), "revisionCriteria");
assertEquals(2, suggestions.size());
diff --git a/commons.antlr/src/test/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest4.java b/commons.antlr/src/test/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest4.java
index 0f2558033b..dc44956d11 100644
--- a/commons.antlr/src/test/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest4.java
+++ b/commons.antlr/src/test/java/com/pmease/commons/antlr/codeassist/test/CodeAssistTest4.java
@@ -39,17 +39,17 @@ public class CodeAssistTest4 {
suggestions = suggest(new InputStatus("a"), "stat");
assertEquals(6, suggestions.size());
- assertEquals("a+:2", suggestions.get(0).toString());
- assertEquals("a-:2", suggestions.get(1).toString());
- assertEquals("a*:2", suggestions.get(2).toString());
- assertEquals("a/:2", suggestions.get(3).toString());
- assertEquals("a;:2", suggestions.get(4).toString());
- assertEquals("a=:2", suggestions.get(5).toString());
+ assertEquals("a=:2", suggestions.get(0).toString());
+ assertEquals("a;:2", suggestions.get(1).toString());
+ assertEquals("a+:2", suggestions.get(2).toString());
+ assertEquals("a-:2", suggestions.get(3).toString());
+ assertEquals("a*:2", suggestions.get(4).toString());
+ assertEquals("a/:2", suggestions.get(5).toString());
suggestions = suggest(new InputStatus(""), "stat");
assertEquals(2, suggestions.size());
- assertEquals("(:1", suggestions.get(0).toString());
- assertEquals(";:1", suggestions.get(1).toString());
+ assertEquals(";:1", suggestions.get(0).toString());
+ assertEquals("(:1", suggestions.get(1).toString());
suggestions = suggest(new InputStatus("5*"), "stat");
assertEquals(1, suggestions.size());
@@ -57,11 +57,11 @@ public class CodeAssistTest4 {
suggestions = suggest(new InputStatus("5"), "stat");
assertEquals(5, suggestions.size());
- assertEquals("5+:2", suggestions.get(0).toString());
- assertEquals("5-:2", suggestions.get(1).toString());
- assertEquals("5*:2", suggestions.get(2).toString());
- assertEquals("5/:2", suggestions.get(3).toString());
- assertEquals("5;:2", suggestions.get(4).toString());
+ assertEquals("5;:2", suggestions.get(0).toString());
+ assertEquals("5+:2", suggestions.get(1).toString());
+ assertEquals("5-:2", suggestions.get(2).toString());
+ assertEquals("5*:2", suggestions.get(3).toString());
+ assertEquals("5/:2", suggestions.get(4).toString());
}
}
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/scrollintoview/jquery.scrollintoview.js b/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/scrollintoview/jquery.scrollintoview.js
index 721ff3d0ae..307a6392ad 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/scrollintoview/jquery.scrollintoview.js
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/scrollintoview/jquery.scrollintoview.js
@@ -1,32 +1,32 @@
-/*
- * 2015, Robin Shen
- */
-(function ( $ ) {
-
- var defaultTopMargin = defaultBottomMargin = 8;
-
- $.fn.scrollIntoView = function(selector, topMargin, bottomMargin) {
- if (topMargin == undefined)
- topMargin = defaultTopMargin;
- if (bottomMargin == undefined)
- bottomMargin = defaultBottomMargin;
-
- var $container = jQuery(this);
- var $active = $container.find(selector);
-
- if ($active.length != 0) {
- var containerTop = $container.offset().top;
- var activeTop = $active.offset().top;
- if (activeTop-topMargin<containerTop)
- $container.scrollTop($container.scrollTop()-(containerTop-activeTop+topMargin));
-
- var containerBottom = containerTop + $container.height();
- var activeBottom = activeTop + $active.height();
- if (activeBottom+bottomMargin>containerBottom)
- $container.scrollTop($container.scrollTop()+(activeBottom+bottomMargin-containerBottom));
- }
-
- return this;
- };
-
-}( jQuery ));
+/*
+ * 2015, Robin Shen
+ */
+(function ( $ ) {
+
+ var defaultTopMargin = defaultBottomMargin = 8;
+
+ $.fn.scrollIntoView = function(selector, topMargin, bottomMargin) {
+ if (topMargin == undefined)
+ topMargin = defaultTopMargin;
+ if (bottomMargin == undefined)
+ bottomMargin = defaultBottomMargin;
+
+ var $container = jQuery(this);
+ var $active = $container.find(selector);
+
+ if ($active.length != 0) {
+ var containerTop = $container.offset().top;
+ var activeTop = $active.offset().top;
+ if (activeTop-topMargin<containerTop)
+ $container.scrollTop($container.scrollTop()-(containerTop-activeTop+topMargin));
+
+ var containerBottom = containerTop + $container.height();
+ var activeBottom = activeTop + $active.height();
+ if (activeBottom+bottomMargin>containerBottom)
+ $container.scrollTop($container.scrollTop()+(activeBottom+bottomMargin-containerBottom));
+ }
+
+ return this;
+ };
+
+}( jQuery ));
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 61e98b42a9..23bebbbf9c 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
@@ -96,8 +96,6 @@ class AssistPanel extends Panel {
WebMarkupContainer link = new WebMarkupContainer("link");
Range matchRange = suggestion.getMatchRange();
String label = suggestion.getLabel();
- if (label == null)
- label = suggestion.getReplaceContent();
if (matchRange != null) {
String prefix = StringEscapeUtils.escapeHtml4(label.substring(0, matchRange.getFrom()));
String suffix = StringEscapeUtils.escapeHtml4(label.substring(matchRange.getTo()));
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/input-assist.js b/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/input-assist.js
index c60e636982..e5a152f374 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/input-assist.js
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/inputassist/input-assist.js
@@ -169,8 +169,9 @@ pmease.commons.inputassist = {
initInfiniteScroll: function(assistId, callback) {
$("#" + assistId + " .suggestions").scroll(function() {
- if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight)
+ if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight - 25) {
callback();
+ }
});
},
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/util/fullbranchmatch/FullBranchMatch.g4 b/gitplex.core/src/main/java/com/pmease/gitplex/core/util/fullbranchmatch/FullBranchMatch.g4
index 756bac1f8c..2e46f1be0c 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/util/fullbranchmatch/FullBranchMatch.g4
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/util/fullbranchmatch/FullBranchMatch.g4
@@ -7,19 +7,17 @@ criteria: includeMatch | excludeMatch;
includeMatch: INCLUDE '(' fullBranchMatch ')';
excludeMatch: EXCLUDE '(' fullBranchMatch ')';
-fullBranchMatch: depotMatch ':' branchMatch | branchMatch;
+fullBranchMatch: branchMatch | fullDepotMatch ':' branchMatch;
-depotMatch: DEPOT depotFQN=Value;
-branchMatch: BRANCH Value;
+fullDepotMatch: Value;
+branchMatch: Value;
INCLUDE: 'include';
EXCLUDE: 'exclude';
-DEPOT: 'repository';
-BRANCH: 'branch';
-Value: '(' (ESCAPE|~[()\\])+? ')';
+Value: (ESCAPE|~[:()\\])+;
fragment
-ESCAPE: '\\'[()\\];
+ESCAPE: '\\'[:()\\];
WS: [ \t\r\n]+ -> skip;
\ No newline at end of file
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/util/fullbranchmatch/FullBranchMatchUtils.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/util/fullbranchmatch/FullBranchMatchUtils.java
index 24a3bd9812..9a54763719 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/util/fullbranchmatch/FullBranchMatchUtils.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/util/fullbranchmatch/FullBranchMatchUtils.java
@@ -6,7 +6,6 @@ import java.util.List;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.CommonTokenStream;
-import org.antlr.v4.runtime.tree.TerminalNode;
import org.apache.commons.lang3.StringUtils;
import com.pmease.commons.util.match.IncludeExclude;
@@ -31,11 +30,6 @@ public class FullBranchMatchUtils {
return parser.match();
}
- private static String getValue(TerminalNode terminal) {
- String value = terminal.getText().substring(1);
- return value.substring(0, value.length()-1).trim();
- }
-
public static boolean matches(String match, final Depot currentDepot, DepotAndBranch depotAndBranch) {
MatchContext matchContext = parse(match);
List<FullBranchMatchContext> includes = new ArrayList<>();
@@ -52,12 +46,12 @@ public class FullBranchMatchUtils {
@Override
public boolean matches(FullBranchMatchContext rule, DepotAndBranch value) {
- if (rule.depotMatch() != null) {
- String fullDepotPattern = getValue(rule.depotMatch().Value());
- String branchPattern = getValue(rule.branchMatch().Value());
+ if (rule.fullDepotMatch() != null) {
+ String fullDepotPattern = rule.fullDepotMatch().Value().getText().trim();
+ String branchPattern = rule.branchMatch().Value().getText().trim();
if (fullDepotPattern.indexOf("/") != -1) {
- String accountPattern = StringUtils.substringBefore(fullDepotPattern, "/");
- String depotPattern = StringUtils.substringAfter(fullDepotPattern, "/");
+ String accountPattern = StringUtils.substringBeforeLast(fullDepotPattern, "/");
+ String depotPattern = StringUtils.substringAfterLast(fullDepotPattern, "/");
return WildcardUtils.matchString(accountPattern, value.getDepot().getUser().getName())
&& WildcardUtils.matchString(depotPattern, value.getDepot().getName())
&& WildcardUtils.matchString(branchPattern, value.getBranch());
@@ -67,7 +61,7 @@ public class FullBranchMatchUtils {
&& WildcardUtils.matchString(branchPattern, value.getBranch());
}
} else {
- String branchPattern = getValue(rule.branchMatch().Value());
+ String branchPattern = rule.branchMatch().Value().getText().trim();
return value.getDepot().equals(currentDepot)
&& WildcardUtils.matchString(branchPattern, value.getBranch());
}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/branchmatch/BranchMatchBehavior.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/branchmatch/BranchMatchBehavior.java
index c26802d15e..c55c4e74a0 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/branchmatch/BranchMatchBehavior.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/branchmatch/BranchMatchBehavior.java
@@ -45,7 +45,8 @@ public class BranchMatchBehavior extends ANTLRAssistBehavior {
@Override
protected List<InputSuggestion> match(String unfencedMatchWith) {
- return SuggestionUtils.suggestBranch(depotModel.getObject(), unfencedMatchWith, count);
+ return SuggestionUtils.suggestBranch(depotModel.getObject(), unfencedMatchWith, count,
+ null, "any branch");
}
@Override
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fullbranchmatch/FullBranchMatchBehavior.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fullbranchmatch/FullBranchMatchBehavior.java
index 7d6aaa80ba..3f1e674fdd 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fullbranchmatch/FullBranchMatchBehavior.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fullbranchmatch/FullBranchMatchBehavior.java
@@ -6,10 +6,8 @@ import java.util.List;
import org.apache.wicket.Component;
import org.apache.wicket.model.IModel;
-import com.pmease.commons.antlr.codeassist.FenceAware;
import com.pmease.commons.antlr.codeassist.InputSuggestion;
import com.pmease.commons.antlr.codeassist.ParentedElement;
-import com.pmease.commons.antlr.grammar.ElementSpec;
import com.pmease.commons.antlr.grammar.LexerRuleRefElementSpec;
import com.pmease.commons.antlr.parser.Element;
import com.pmease.commons.wicket.behavior.inputassist.ANTLRAssistBehavior;
@@ -25,10 +23,6 @@ public class FullBranchMatchBehavior extends ANTLRAssistBehavior {
private final IModel<Depot> depotModel;
- private static final String VALUE_OPEN = "(";
-
- private static final String VALUE_CLOSE = ")";
-
public FullBranchMatchBehavior(IModel<Depot> depotModel) {
super(FullBranchMatchParser.class, "match");
this.depotModel = depotModel;
@@ -42,88 +36,54 @@ public class FullBranchMatchBehavior extends ANTLRAssistBehavior {
@Override
protected List<InputSuggestion> suggest(final ParentedElement expectedElement, String matchWith, final int count) {
- if (expectedElement.getSpec() instanceof LexerRuleRefElementSpec) {
- LexerRuleRefElementSpec spec = (LexerRuleRefElementSpec) expectedElement.getSpec();
- if (spec.getRuleName().equals("Value")) {
- return new FenceAware(codeAssist.getGrammar(), VALUE_OPEN, VALUE_CLOSE) {
-
- @Override
- protected List<InputSuggestion> match(String unfencedMatchWith) {
- int tokenType = expectedElement.getRoot().getLastMatchedToken().getType();
- DepotPage page = (DepotPage) getComponent().getPage();
- Depot depot = page.getDepot();
- if (tokenType == FullBranchMatchParser.DEPOT) {
- return SuggestionUtils.suggestAffinals(depot, unfencedMatchWith, count);
- } else {
- ParentedElement criteriaElement = expectedElement.findParentByRule("criteria");
- List<Element> depotFQNElements = criteriaElement.findChildrenByLabel("depotFQN", true);
- if (!depotFQNElements.isEmpty()) {
- Element depotFQNElement = depotFQNElements.get(0);
- String branchDepotFQN = depotFQNElement.getMatchedText().trim().substring(1);
- branchDepotFQN = branchDepotFQN.substring(0, branchDepotFQN.length()-1);
- Depot branchDepot = GitPlex.getInstance(DepotManager.class).findBy(branchDepotFQN);
- if (branchDepot != null) {
- return SuggestionUtils.suggestBranch(branchDepot, unfencedMatchWith, count);
- } else {
- return new ArrayList<>();
- }
- } else {
- return SuggestionUtils.suggestBranch(depot, unfencedMatchWith, count);
- }
- }
+ if (expectedElement.getState() != null) {
+ String ruleName = expectedElement.getState().getRuleSpec().getName();
+ if (ruleName.equals("branchMatch")) {
+ DepotPage page = (DepotPage) getComponent().getPage();
+ Depot depot = page.getDepot();
+ ParentedElement criteriaElement = expectedElement.findParentByRule("criteria");
+ List<Element> children = criteriaElement.findChildrenByRule("fullDepotMatch", true);
+ if (children.isEmpty()) {
+ return SuggestionUtils.suggestBranch(depot, matchWith, count,
+ "branch in current repository", "any branch in current repository");
+ } else {
+ String depotFQN = children.get(0).getMatchedText().trim();
+ Depot branchDepot;
+ DepotManager depotManager = GitPlex.getInstance(DepotManager.class);
+ if (depotFQN.indexOf("/") == -1) {
+ branchDepot = depotManager.findBy(depot.getUser(), depotFQN);
+ } else {
+ branchDepot = depotManager.findBy(depotFQN);
}
-
- @Override
- protected String getFencingDescription() {
- return "value needs to be enclosed in brackets";
+ if (branchDepot != null) {
+ return SuggestionUtils.suggestBranch(branchDepot, matchWith, count,
+ "branch in selected repository", "any branch in selected repository");
+ } else {
+ return new ArrayList<>();
}
-
- }.suggest(expectedElement.getSpec(), matchWith);
- }
+ }
+ } else if (ruleName.equals("fullDepotMatch")) {
+ List<InputSuggestion> suggestions = new ArrayList<>();
+ DepotPage page = (DepotPage) getComponent().getPage();
+ Depot depot = page.getDepot();
+ suggestions.addAll(SuggestionUtils.suggestAffinals(depot, matchWith, count,
+ "select branch in this repository", "any repository"));
+ return suggestions;
+ }
}
return null;
}
- @Override
- protected InputSuggestion wrapAsSuggestion(ParentedElement expectedElement,
- String suggestedLiteral, boolean complete) {
- String description;
- switch (suggestedLiteral) {
- case "repository":
- description = "Choose repository (branch will be selected later)";
- break;
- case "branch":
- ParentedElement criteriaElement = expectedElement.findParentByRule("criteria");
- if (!criteriaElement.findChildrenByRule("depotMatch", true).isEmpty()) {
- description = "Choose branch from specified repository";
- } else {
- description = "Choose branch from current repository";
- }
- break;
- default:
- description = null;
- }
- return new InputSuggestion(suggestedLiteral, description, null);
- }
-
@Override
protected List<String> getHints(ParentedElement expectedElement, String matchWith) {
List<String> hints = new ArrayList<>();
if (expectedElement.getSpec() instanceof LexerRuleRefElementSpec) {
LexerRuleRefElementSpec spec = (LexerRuleRefElementSpec) expectedElement.getSpec();
- if (spec.getRuleName().equals("Value") && !matchWith.contains(VALUE_CLOSE)) {
+ if (spec.getRuleName().equals("Value")) {
hints.add("Use * to do wildcard match");
}
}
return hints;
}
- @Override
- protected int getEndOfMatch(ElementSpec spec, String content) {
- if (content.startsWith(VALUE_OPEN+VALUE_CLOSE))
- return 2;
- else
- return super.getEndOfMatch(spec, content);
- }
-
}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/commit/QueryAssistBehavior.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/commit/QueryAssistBehavior.java
index 4b7bfa530b..706d8db7e8 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/commit/QueryAssistBehavior.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/commit/QueryAssistBehavior.java
@@ -65,10 +65,10 @@ public class QueryAssistBehavior extends ANTLRAssistBehavior {
Depot depot = depotModel.getObject();
switch (tokenType) {
case CommitQueryParser.BRANCH:
- suggestions.addAll(SuggestionUtils.suggestBranch(depot, unfencedMatchWith, count));
+ suggestions.addAll(SuggestionUtils.suggestBranch(depot, unfencedMatchWith, count, null, null));
break;
case CommitQueryParser.TAG:
- suggestions.addAll(SuggestionUtils.suggestBranch(depot, unfencedMatchWith, count));
+ suggestions.addAll(SuggestionUtils.suggestBranch(depot, unfencedMatchWith, count, null, null));
break;
case CommitQueryParser.AUTHOR:
case CommitQueryParser.COMMITTER:
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/util/SuggestionUtils.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/util/SuggestionUtils.java
index 7332f680fd..581c0a6283 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/util/SuggestionUtils.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/util/SuggestionUtils.java
@@ -7,6 +7,8 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import javax.annotation.Nullable;
+
import org.eclipse.jgit.lib.Ref;
import com.pmease.commons.antlr.codeassist.InputSuggestion;
@@ -20,31 +22,49 @@ import com.pmease.gitplex.core.model.Depot;
public class SuggestionUtils {
- public static List<InputSuggestion> suggestBranch(Depot depot, String matchWith, int count) {
+ public static List<InputSuggestion> suggestBranch(Depot depot, String matchWith,
+ int count, @Nullable String description, @Nullable String wildcardDescription) {
String lowerCaseMatchWith = matchWith.toLowerCase();
int numSuggestions = 0;
List<InputSuggestion> suggestions = new ArrayList<>();
+ if (wildcardDescription != null) {
+ String wildcard = "*";
+ int index = wildcard.indexOf(lowerCaseMatchWith);
+ if (index != -1) {
+ Range matchRange = new Range(index, index+lowerCaseMatchWith.length());
+ suggestions.add(new InputSuggestion(wildcard, wildcardDescription, matchRange));
+ }
+ }
for (Ref ref: depot.getBranchRefs()) {
String branch = GitUtils.ref2branch(ref.getName());
int index = branch.toLowerCase().indexOf(lowerCaseMatchWith);
if (index != -1 && numSuggestions++<count) {
Range matchRange = new Range(index, index+lowerCaseMatchWith.length());
- suggestions.add(new InputSuggestion(branch, matchRange));
+ suggestions.add(new InputSuggestion(branch, description, matchRange));
}
}
return suggestions;
}
- public static List<InputSuggestion> suggestAffinals(Depot depot, String matchWith, int count) {
+ public static List<InputSuggestion> suggestAffinals(Depot depot, String matchWith,
+ int count, @Nullable String description, @Nullable String wildcardDescription) {
String lowerCaseMatchWith = matchWith.toLowerCase();
int numSuggestions = 0;
List<InputSuggestion> suggestions = new ArrayList<>();
+ if (wildcardDescription != null) {
+ String wildcard = "*/*";
+ int index = wildcard.indexOf(lowerCaseMatchWith);
+ if (index != -1) {
+ Range matchRange = new Range(index, index+lowerCaseMatchWith.length());
+ suggestions.add(new InputSuggestion(wildcard, wildcardDescription, matchRange));
+ }
+ }
for (Depot affinal: depot.findAffinals()) {
String FQN = affinal.getFQN();
int index = FQN.toLowerCase().indexOf(lowerCaseMatchWith);
if (index != -1 && numSuggestions++<count) {
Range matchRange = new Range(index, index+lowerCaseMatchWith.length());
- suggestions.add(new InputSuggestion(FQN, matchRange));
+ suggestions.add(new InputSuggestion(FQN, description, matchRange));
}
}
return suggestions;