Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Fix exact match when inserting definition

AuthorDaveJarvis <email>
Date2020-07-11 12:03:07 GMT-0700
Commit1b577db1fe20db12101521bb74f4f22736c4151b
Parentc6db50e
Delta27 lines added, 41 lines removed, 14-line decrease
src/main/java/com/scrivenvar/editors/VariableNameInjector.java
if( item.isLeaf() ) {
- final var leaf = pane.findLeaf( item.getValue(), FindMode.EXACT );
+ final var leaf = pane.findLeaf( item.getValue(), FindMode.EQUALS_EXACT );
final var editor = getEditor();
private VariableTreeItem<String> findLeafExact( final String text ) {
- return findLeaf( text, EXACT );
+ return findLeaf( text, EQUALS_EXACT );
}
private VariableTreeItem<String> findLeafContains( final String text ) {
- return findLeaf( text, CONTAINS );
+ return findLeaf( text, CONTAINS_EXACT );
}
private VariableTreeItem<String> findLeafStartsWith( final String text ) {
- return findLeaf( text, STARTS_WITH );
+ return findLeaf( text, STARTS_WITH_EXACT );
}
src/main/java/com/scrivenvar/definition/FindMode.java
*/
public enum FindMode {
- EXACT,
- CONTAINS,
- STARTS_WITH,
+ EQUALS_EXACT,
+ CONTAINS_EXACT,
+ STARTS_WITH_EXACT,
LEVENSHTEIN
}
src/main/java/com/scrivenvar/definition/VariableTreeItem.java
* value.
*
- * @param text The text to match against each leaf in the tree.
- * @return The leaf that has a value starting with the given text.
- */
- public VariableTreeItem<T> findLeaf( final String text ) {
- return findLeaf( text, STARTS_WITH );
- }
-
- /**
- * Finds a leaf starting at the current node with text that matches the given
- * value.
- *
* @param text The text to match against each leaf in the tree.
* @param findMode What algorithm is used to match the given text.
- * @return The leaf that has a value starting with the given text.
+ * @return The leaf that has a value starting with the given text, or {@code
+ * null} if there was no match for the given {@link FindMode}.
*/
public VariableTreeItem<T> findLeaf(
final String text, final FindMode findMode ) {
final Stack<VariableTreeItem<T>> stack = new Stack<>();
- final VariableTreeItem<T> root = this;
-
- stack.push( root );
+ stack.push( this );
- // Don't try to find keys for blank/empty variable values.
+ // Don't hunt for blank (empty) keys.
boolean found = text.isBlank();
- VariableTreeItem<T> node = null;
while( !found && !stack.isEmpty() ) {
- node = stack.pop();
+ final VariableTreeItem<T> node = stack.pop();
- if( findMode == EXACT && node.valueEquals( text ) ) {
- found = true;
- }
- else if( findMode == CONTAINS && node.valueContains( text ) ) {
- found = true;
- }
- else if( findMode == STARTS_WITH && node.valueStartsWith( text ) ) {
- found = true;
- }
- else {
- for( final TreeItem<T> child : node.getChildren() ) {
- stack.push( (VariableTreeItem<T>) child );
- }
+ for( final TreeItem<T> child : node.getChildren() ) {
+ final VariableTreeItem<T> result = (VariableTreeItem<T>) child;
- // No match found, yet.
- node = null;
+ if( result.isLeaf() ) {
+ found = (findMode == EQUALS_EXACT && result.valueEquals( text )) ||
+ (findMode == CONTAINS_EXACT && result.valueContains( text )) ||
+ (findMode == STARTS_WITH_EXACT && result.valueStartsWith( text ));
+
+ if( found ) {
+ return result;
+ }
+ }
+ else {
+ stack.push( result );
+ }
}
}
- return node;
+ return null;
}