Skip to content

The Unified and Autonomous Development Platform

midwork

robin shinefa90d70727e3
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/AssistStream.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/AssistStream.java
index cfe0ba61b6..61822f11e1 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/AssistStream.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/AssistStream.java
@@ -11,7 +11,14 @@ public class AssistStream {
private final List<Token> tokens;
private int index;
-
+
+ /**
+ * Construct assist stream with a list of tokens.
+ *
+ * @param tokens
+ * tokens to be used to construct the assist stream, the list should
+ * not be empty and the last token should be the EOF token
+ */
public AssistStream(List<Token> tokens) {
Preconditions.checkArgument(!tokens.isEmpty() && tokens.get(tokens.size()-1).getType() == Token.EOF);
this.tokens = tokens;
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 f75cba6edf..0dbef503a3 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
@@ -70,14 +70,22 @@ public abstract class CodeAssist implements Serializable {
private final Map<String, Integer> tokenTypesByRule = new HashMap<>();
+ /**
+ * Code assist constructor
+ * @param lexerClass
+ * lexer class to be used to lex code. Other required information such as
+ * grammar files and token file will be derived from the lexer class
+ */
public CodeAssist(Class<? extends Lexer> lexerClass) {
this(lexerClass, new String[]{AntlrUtils.getDefaultGrammarFile(lexerClass)},
AntlrUtils.getDefaultTokenFile(lexerClass));
}
/**
- * Construct object representation of ANTLR grammar file.
+ * Code assist constructor.
*
+ * @param lexerClass
+ * lexer class to be used to lex code
* @param grammarFiles
* grammar files in class path, relative to class path root
* @param tokenFile
@@ -388,8 +396,24 @@ public abstract class CodeAssist implements Serializable {
String inputContent = inputStatus.getContent();
List<InputSuggestion> inputSuggestions = 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<ElementReplacement>> grouped = new LinkedHashMap<>();
for (ElementReplacement replacement: suggest(rule, stream, inputStatus)) {
+ /*
+ * 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, replacement.begin)
+ replacement.content + inputContent.substring(replacement.end);
List<ElementReplacement> value = grouped.get(key);
@@ -406,6 +430,7 @@ public abstract class CodeAssist implements Serializable {
String label = replacement.label;
String description = replacement.description;
String content = entry.getKey();
+
if (replacement.end <= inputStatus.getCaret()) {
List<String> contents = new ArrayList<>();
for (ElementReplacement each: value) {
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementReplacement.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementReplacement.java
index b2c0c56a3f..e4838f86a7 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementReplacement.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementReplacement.java
@@ -2,18 +2,30 @@ package com.pmease.commons.antlr.codeassist;
class ElementReplacement {
- Node node;
+ // represent the node to be suggested at current caret
+ Node node;
- String content;
+ // represent content of the node to be suggested
+ String content;
- int begin;
+ // represent start position of node in current input
+ int begin;
- int end;
+ /*
+ * represent end position of node in current input, or take value of caret if
+ * current input does not match the node spec around caret. Text between
+ * "begin" and "end" in current input will be replaced by "content" (and
+ * append possible mandatories literals).
+ */
+ int end;
+ // caret position in suggested content
int caret;
+ // text to be displayed to user for this suggested content
String label;
+ // description to be displayed to user for this suggested content
String description;
}
\ No newline at end of file