| Author | DaveJarvis <email> |
|---|---|
| Date | 2020-06-08 20:19:16 GMT-0700 |
| Commit | 9dcf851adee48eeaa046dbdc77c3fcb4914b9aac |
| Parent | bc403c3 |
| Delta | 63 lines added, 19 lines removed, 44-line increase |
| /** | ||
| + * Inserts the variable | ||
| + */ | ||
| + public void injectSelectedItem() { | ||
| + final TreeItem<String> item = getDefinitionPane().getSelectedItem(); | ||
| + | ||
| + if( item.isLeaf() ) { | ||
| + final VariableTreeItem<String> leaf = getDefinitionPane().findLeaf( | ||
| + item.getValue(), FindMode.EXACT ); | ||
| + final StyledTextArea<?, ?> editor = getEditor(); | ||
| + | ||
| + editor.insertText( editor.getCaretPosition(), decorate( leaf ) ); | ||
| + } | ||
| + } | ||
| + | ||
| + /** | ||
| * Traps Control+SPACE to auto-insert definition key names. | ||
| */ | ||
| if( leaf != null ) { | ||
| - replaceText( | ||
| - boundaries[ 0 ], | ||
| - boundaries[ 1 ], | ||
| - decorate( leaf.toPath() ) | ||
| - ); | ||
| - | ||
| + replaceText( boundaries[ 0 ], boundaries[ 1 ], decorate( leaf ) ); | ||
| expand( leaf ); | ||
| } | ||
| /** | ||
| - * Injects a variable using the syntax specific to the type of document | ||
| + * Decorates a {@link TreeItem} using the syntax specific to the type of | ||
| + * document being edited. | ||
| + * | ||
| + * @param leaf The path to the leaf (the definition key) to be decorated. | ||
| + */ | ||
| + private String decorate( final VariableTreeItem<String> leaf ) { | ||
| + return decorate( leaf.toPath() ); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Decorates a variable using the syntax specific to the type of document | ||
| * being edited. | ||
| * | ||
| * @param variable The variable to decorate in dot-notation without any | ||
| - * start or end tokens present. | ||
| + * start or end sigils present. | ||
| */ | ||
| private String decorate( final String variable ) { | ||
| assert word != null; | ||
| - VariableTreeItem<String> leaf; | ||
| + VariableTreeItem<String> leaf = findLeafExact( word ); | ||
| - leaf = findLeafStartsWith( word ); | ||
| + leaf = leaf == null ? findLeafStartsWith( word ) : leaf; | ||
| leaf = leaf == null ? findLeafContains( word ) : leaf; | ||
| leaf = leaf == null ? findLeafLevenshtein( word ) : leaf; | ||
| return leaf; | ||
| + } | ||
| + | ||
| + private VariableTreeItem<String> findLeafExact( final String text ) { | ||
| + return findLeaf( text, EXACT ); | ||
| } | ||
| */ | ||
| public enum FindMode { | ||
| + EXACT, | ||
| CONTAINS, | ||
| STARTS_WITH, |
| package com.scrivenvar.definition; | ||
| -import com.scrivenvar.decorators.YamlVariableDecorator; | ||
| - | ||
| import java.util.Map; | ||
| import java.util.regex.Matcher; | ||
| - | ||
| -import static com.scrivenvar.decorators.YamlVariableDecorator.REGEX_PATTERN; | ||
| +import java.util.regex.Pattern; | ||
| /** | ||
| * Responsible for performing string interpolation on key/value pairs stored | ||
| * in a map. The values in the map can use a delimited syntax to refer to | ||
| * keys in the map. | ||
| * | ||
| * @author White Magic Software, Ltd. | ||
| */ | ||
| public class MapInterpolator { | ||
| + | ||
| + /** | ||
| + * Matches variables delimited by dollar symbols. | ||
| + */ | ||
| + private final static String REGEX = "(\\$.*?\\$)"; | ||
| + | ||
| + /** | ||
| + * Compiled regular expression for matching delimited references. | ||
| + */ | ||
| + private final static Pattern REGEX_PATTERN = Pattern.compile( REGEX ); | ||
| + | ||
| private final static int GROUP_DELIMITED = 1; | ||
| * Performs string interpolation on the values in the given map. This will | ||
| * change any value in the map that contains a variable that matches | ||
| - * {@link YamlVariableDecorator#REGEX_PATTERN}. | ||
| + * {@link #REGEX_PATTERN}. | ||
| * | ||
| * @param map Contains values that represent references to keys. | ||
| while( matcher.find() ) { | ||
| final String keyName = matcher.group( GROUP_DELIMITED ); | ||
| - | ||
| final String keyValue = resolve( | ||
| map, map.getOrDefault( keyName, keyName ) | ||
| import java.util.Stack; | ||
| -import static com.scrivenvar.definition.FindMode.CONTAINS; | ||
| -import static com.scrivenvar.definition.FindMode.STARTS_WITH; | ||
| +import static com.scrivenvar.definition.FindMode.*; | ||
| import static java.text.Normalizer.Form.NFD; | ||
| node = stack.pop(); | ||
| - if( findMode == CONTAINS && node.valueContains( text ) ) { | ||
| + if( findMode == EXACT && node.valueEquals( text ) ) { | ||
| + found = true; | ||
| + } | ||
| + else if( findMode == CONTAINS && node.valueContains( text ) ) { | ||
| found = true; | ||
| } | ||
| private boolean valueContains( final String s ) { | ||
| return isLeaf() && getDiacriticlessValue().contains( s ); | ||
| + } | ||
| + | ||
| + /** | ||
| + * 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 equals the given value. | ||
| + */ | ||
| + private boolean valueEquals( final String s ) { | ||
| + return isLeaf() && getValue().equals( s ); | ||
| } | ||