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