Dave Jarvis' Repositories

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

Use BiFunction for definition value match

AuthorDaveJarvis <email>
Date2020-07-11 12:43:54 GMT-0700
Commitcecd18a03216a2600db671ee91659770a32f7e51
Parent1b577db
Delta85 lines added, 93 lines removed, 8-line decrease
src/main/java/com/scrivenvar/editors/VariableNameInjector.java
import com.scrivenvar.decorators.VariableDecorator;
import com.scrivenvar.definition.DefinitionPane;
-import com.scrivenvar.definition.FindMode;
import com.scrivenvar.definition.VariableTreeItem;
import javafx.scene.control.TreeItem;
import javafx.scene.input.KeyEvent;
import org.fxmisc.richtext.StyledTextArea;
import java.nio.file.Path;
import java.text.BreakIterator;
-import static com.scrivenvar.definition.FindMode.*;
import static javafx.scene.input.KeyCode.SPACE;
import static javafx.scene.input.KeyCombination.CONTROL_DOWN;
if( item.isLeaf() ) {
- final var leaf = pane.findLeaf( item.getValue(), FindMode.EQUALS_EXACT );
+ final var leaf = pane.findLeafExact( item.getValue() );
final var editor = getEditor();
}
+ /**
+ * Looks for the given word, matching first by exact, next by a starts-with
+ * condition with diacritics replaced, then by containment.
+ *
+ * @param word
+ * @return
+ */
+ @SuppressWarnings("ConstantConditions")
private VariableTreeItem<String> findLeaf( final String word ) {
assert word != null;
- VariableTreeItem<String> leaf = findLeafExact( word );
+ final var pane = getDefinitionPane();
+ VariableTreeItem<String> leaf = null;
- leaf = leaf == null ? findLeafStartsWith( word ) : leaf;
- leaf = leaf == null ? findLeafContains( word ) : leaf;
- leaf = leaf == null ? findLeafLevenshtein( word ) : leaf;
+ leaf = leaf == null ? pane.findLeafExact( word ) : leaf;
+ leaf = leaf == null ? pane.findLeafStartsWith( word ) : leaf;
+ leaf = leaf == null ? pane.findLeafContains( word ) : leaf;
+ leaf = leaf == null ? pane.findLeafContainsNoCase( word ) : leaf;
return leaf;
- }
-
- private VariableTreeItem<String> findLeafExact( final String text ) {
- return findLeaf( text, EQUALS_EXACT );
- }
-
- private VariableTreeItem<String> findLeafContains( final String text ) {
- return findLeaf( text, CONTAINS_EXACT );
- }
-
- private VariableTreeItem<String> findLeafStartsWith( final String text ) {
- return findLeaf( text, STARTS_WITH_EXACT );
- }
-
- private VariableTreeItem<String> findLeafLevenshtein( final String text ) {
- return findLeaf( text, LEVENSHTEIN );
- }
-
- /**
- * Finds the first leaf having a value that starts with the given text, or
- * contains the text if contains is true.
- *
- * @param text The text to find in the definition tree.
- * @param findMode Dictates what search criteria to use for matching words.
- * @return The leaf that starts with the given text, or null if not found.
- */
- private VariableTreeItem<String> findLeaf(
- final String text, final FindMode findMode ) {
- return getDefinitionPane().findLeaf( text, findMode );
}
src/main/java/com/scrivenvar/definition/FindMode.java
-/*
- * Copyright 2020 White Magic Software, Ltd.
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * o Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * o Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-package com.scrivenvar.definition;
-
-/**
- * Used to find variable keys by matching values. The values are matched
- * according to the relationships provided in this enumeration.
- */
-public enum FindMode {
- EQUALS_EXACT,
- CONTAINS_EXACT,
- STARTS_WITH_EXACT,
- LEVENSHTEIN
-}
-
src/main/java/com/scrivenvar/definition/VariableTreeItem.java
import java.text.Normalizer;
import java.util.Stack;
+import java.util.function.BiFunction;
-import static com.scrivenvar.definition.FindMode.*;
import static java.text.Normalizer.Form.NFD;
public VariableTreeItem( final T value ) {
super( value );
+ }
+
+ /**
+ * Finds a leaf starting at the current node with text that matches the given
+ * value. Search is performed case-sensitively.
+ *
+ * @param text The text to match against each leaf in the tree.
+ * @return The leaf that has a value exactly matching the given text.
+ */
+ public VariableTreeItem<T> findLeafExact( final String text ) {
+ return findLeaf( text, VariableTreeItem::valueEquals );
+ }
+
+ /**
+ * Finds a leaf starting at the current node with text that matches the given
+ * value. Search is performed case-sensitively.
+ *
+ * @param text The text to match against each leaf in the tree.
+ * @return The leaf that has a value that contains the given text.
+ */
+ public VariableTreeItem<T> findLeafContains( final String text ) {
+ return findLeaf( text, VariableTreeItem::valueContains );
+ }
+
+ /**
+ * Finds a leaf starting at the current node with text that matches the given
+ * value. Search is performed case-insensitively.
+ *
+ * @param text The text to match against each leaf in the tree.
+ * @return The leaf that has a value that contains the given text.
+ */
+ public VariableTreeItem<T> findLeafContainsNoCase( final String text ) {
+ return findLeaf( text, VariableTreeItem::valueContainsNoCase );
+ }
+
+ /**
+ * Finds a leaf starting at the current node with text that matches the given
+ * value. Search is performed case-sensitively.
+ *
+ * @param text The text to match against each leaf in the tree.
+ * @return The leaf that has a value that starts with the given text.
+ */
+ public VariableTreeItem<T> findLeafStartsWith( final String text ) {
+ return findLeaf( text, VariableTreeItem::valueStartsWith );
}
/**
* 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, or {@code
- * null} if there was no match for the given {@link FindMode}.
+ * null} if there was no match found.
*/
public VariableTreeItem<T> findLeaf(
- final String text, final FindMode findMode ) {
+ final String text,
+ final BiFunction<VariableTreeItem<T>, String, Boolean> findMode ) {
final Stack<VariableTreeItem<T>> stack = new Stack<>();
stack.push( this );
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 ) {
+ if( found = findMode.apply( result, text ) ) {
return result;
}
/**
- * Returns true if this node is a leaf and its value starts with the given
- * text.
+ * Returns true if this node is a leaf and its value equals the given text.
*
* @param s The text to compare against the node value.
- * @return true Node is a leaf and its value starts with the given value.
+ * @return true Node is a leaf and its value equals the given value.
*/
- private boolean valueStartsWith( final String s ) {
- return isLeaf() && getDiacriticlessValue().startsWith( s );
+ private boolean valueEquals( final String s ) {
+ return isLeaf() && getValue().equals( s );
}
/**
- * Returns true if this node is a leaf and its value equals the given text.
+ * Returns true if this node is a leaf and its value contains the given text.
*
* @param s The text to compare against the node value.
- * @return true Node is a leaf and its value equals the given value.
+ * @return true Node is a leaf and its value contains the given value.
*/
- private boolean valueEquals( final String s ) {
- return isLeaf() && getValue().equals( s );
+ private boolean valueContainsNoCase( final String s ) {
+ return isLeaf() && getDiacriticlessValue()
+ .toLowerCase()
+ .contains( s.toLowerCase() );
+ }
+
+ /**
+ * Returns true if this node is a leaf and its value starts with the given
+ * text.
+ *
+ * @param s The text to compare against the node value.
+ * @return true Node is a leaf and its value starts with the given value.
+ */
+ private boolean valueStartsWith( final String s ) {
+ return isLeaf() && getDiacriticlessValue().startsWith( s );
}