提交 71f69140 authored 作者: Thomas Mueller's avatar Thomas Mueller

H2 Console: improved autocomplete feature (also simplified the source code for this feature).

上级 efc3bd71
......@@ -280,7 +280,9 @@ public class Bnf {
continue;
}
sentence.start();
head.getRule().addNextTokenList(sentence);
if (head.getRule().autoComplete(sentence)) {
break;
}
}
return sentence.getNext();
}
......
......@@ -28,23 +28,13 @@ public interface Rule {
void setLinks(HashMap<String, RuleHead> ruleMap);
/**
* Add the next possible token for a query.
* Used for autocomplete support.
* Add the next possible token(s). If there was a match, the query in the
* sentence is updated (the matched token is removed).
*
* @param sentence the sentence context
* @return true if a full match
*/
void addNextTokenList(Sentence sentence);
/**
* Remove a token from a sentence. Used for autocomplete support.
* If there was a match, the query in the sentence is updated
* (the matched token is removed).
*
* @param sentence
* the sentence context
* @return false if not a match or a partial match, true if a full match
*/
boolean matchRemove(Sentence sentence);
boolean autoComplete(Sentence sentence);
/**
* Call the visit method in the given visitor.
......
......@@ -29,10 +29,6 @@ public class RuleElement implements Rule {
this.type = topic.startsWith("function") ? Sentence.FUNCTION : Sentence.KEYWORD;
}
public String toString() {
return name;
}
public void accept(BnfVisitor visitor) {
visitor.visitRuleElement(keyword, name, link);
}
......@@ -60,16 +56,14 @@ public class RuleElement implements Rule {
throw new AssertionError("Unknown " + name + "/" + test);
}
public boolean matchRemove(Sentence sentence) {
public boolean autoComplete(Sentence sentence) {
if (sentence.shouldStop()) {
return false;
}
String query = sentence.getQuery();
if (query.length() == 0) {
return false;
}
if (keyword) {
String up = sentence.getQueryUpper();
String query = sentence.getQuery();
String q = query.trim();
String up = sentence.getQueryUpper().trim();
if (up.startsWith(name)) {
query = query.substring(name.length());
while (!"_".equals(name) && query.length() > 0 && Character.isWhitespace(query.charAt(0))) {
......@@ -77,38 +71,18 @@ public class RuleElement implements Rule {
}
sentence.setQuery(query);
return true;
}
return false;
}
if (!link.matchRemove(sentence)) {
return false;
}
if (name != null && !name.startsWith("@") && (link.name() == null || !link.name().startsWith("@"))) {
query = sentence.getQuery();
while (query.length() > 0 && Character.isWhitespace(query.charAt(0))) {
query = query.substring(1);
}
sentence.setQuery(query);
}
return true;
}
public void addNextTokenList(Sentence sentence) {
if (sentence.shouldStop()) {
return;
}
if (keyword) {
String query = sentence.getQuery();
String q = query.trim();
String up = sentence.getQueryUpper().trim();
if (q.length() == 0 || name.startsWith(up)) {
} else if (q.length() == 0 || name.startsWith(up)) {
if (q.length() < name.length()) {
sentence.add(name, name.substring(q.length()), type);
}
}
return;
return false;
}
link.addNextTokenList(sentence);
return link.autoComplete(sentence);
}
public String toString() {
return name;
}
}
......@@ -9,7 +9,6 @@ package org.h2.bnf;
import java.util.ArrayList;
import java.util.HashMap;
import org.h2.util.New;
import org.h2.util.StatementBuilder;
/**
* Represents a sequence of BNF rules, or a list of alternative rules.
......@@ -35,24 +34,6 @@ public class RuleList implements Rule {
this.or = or;
}
public String toString() {
StatementBuilder buff = new StatementBuilder();
if (or) {
buff.append('{');
for (Rule r : list) {
buff.appendExceptFirst("|");
buff.append(r.toString());
}
buff.append('}');
} else {
for (Rule r : list) {
buff.appendExceptFirst(" ");
buff.append(r.toString());
}
}
return buff.toString();
}
public void accept(BnfVisitor visitor) {
visitor.visitRuleList(or, list);
}
......@@ -70,43 +51,32 @@ public class RuleList implements Rule {
}
}
public boolean matchRemove(Sentence sentence) {
String query = sentence.getQuery();
if (query.length() == 0) {
public boolean autoComplete(Sentence sentence) {
if (sentence.shouldStop()) {
return false;
}
String old = sentence.getQuery();
if (or) {
for (Rule r : list) {
if (r.matchRemove(sentence)) {
sentence.setQuery(old);
if (r.autoComplete(sentence)) {
return true;
}
}
return false;
}
for (Rule r : list) {
if (!r.matchRemove(sentence)) {
return false;
}
}
return true;
}
public void addNextTokenList(Sentence sentence) {
String old = sentence.getQuery();
if (or) {
for (Rule r : list) {
sentence.setQuery(old);
r.addNextTokenList(sentence);
}
} else {
for (Rule r : list) {
r.addNextTokenList(sentence);
if (!r.matchRemove(sentence)) {
break;
if (!r.autoComplete(sentence)) {
sentence.setQuery(old);
return false;
}
}
return true;
}
sentence.setQuery(old);
}
public String toString() {
return or ? "or: " : "" + list.toString();
}
}
......@@ -19,10 +19,6 @@ public class RuleOptional implements Rule {
this.rule = rule;
}
public String toString() {
return "[" + rule.toString() + "]";
}
public void accept(BnfVisitor visitor) {
visitor.visitRuleOptional(rule);
}
......@@ -37,26 +33,16 @@ public class RuleOptional implements Rule {
mapSet = true;
}
}
public boolean matchRemove(Sentence sentence) {
public boolean autoComplete(Sentence sentence) {
if (sentence.shouldStop()) {
return false;
}
String query = sentence.getQuery();
if (query.length() == 0) {
return true;
}
if (!rule.matchRemove(sentence)) {
return true;
}
rule.autoComplete(sentence);
return true;
}
public void addNextTokenList(Sentence sentence) {
if (sentence.shouldStop()) {
return;
}
rule.addNextTokenList(sentence);
public String toString() {
return rule.toString();
}
}
......@@ -21,10 +21,6 @@ public class RuleRepeat implements Rule {
this.comma = comma;
}
public String toString() {
return "...";
}
public void accept(BnfVisitor visitor) {
visitor.visitRuleRepeat(comma, rule);
}
......@@ -37,36 +33,18 @@ public class RuleRepeat implements Rule {
// rule.setLinks(ruleMap);
}
public boolean matchRemove(Sentence sentence) {
public boolean autoComplete(Sentence sentence) {
if (sentence.shouldStop()) {
return false;
}
String query = sentence.getQuery();
if (query.length() == 0) {
return false;
}
while (true) {
if (!rule.matchRemove(sentence)) {
return true;
}
if (sentence.getQuery().length() == 0) {
return true;
}
while (rule.autoComplete(sentence)) {
// nothing to do
}
return true;
}
public void addNextTokenList(Sentence sentence) {
if (sentence.shouldStop()) {
return;
}
String old = sentence.getQuery();
while (true) {
rule.addNextTokenList(sentence);
if (!rule.matchRemove(sentence) || old.equals(sentence.getQuery())) {
break;
}
}
sentence.setQuery(old);
public String toString() {
return rule.toString();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论