| Author | djarvis <email> |
|---|---|
| Date | 2016-12-26 20:35:14 GMT-0800 |
| Commit | f38bac3a410a4c61a477b9944330cfc9bc4e8751 |
| Parent | a3f586e |
| Delta | 210 lines added, 24 lines removed, 186-line increase |
| public abstract class AbstractProcessor<T> implements Processor<T> { | ||
| + protected static final char NEWLINE = '\n'; | ||
| + | ||
| + /** | ||
| + * When performing string searches using indexOf, a return value of -1 | ||
| + * indicates that the string could not be found. | ||
| + */ | ||
| + protected static final int INDEX_NOT_FOUND = -1; | ||
| + | ||
| /** | ||
| * Used while processing the entire chain; null to signify no more links. |
| private final IntegerProperty caretPosition = new SimpleIntegerProperty(); | ||
| + private final static String NEWLINE_CARET_POSITION_MD = NEWLINE + CARET_POSITION_MD; | ||
| public CaretInsertionProcessor( | ||
| * | ||
| * @param text The text document to change. | ||
| - * @param i The caret position token insertion point to use, or -1 to | ||
| - * return the text without any injection. | ||
| + * @param i The caret position token insertion point to use, or -1 to return | ||
| + * the text without any injection. | ||
| * | ||
| * @return The given text with a caret position token inserted at the given | ||
| * offset. | ||
| */ | ||
| protected String inject( final String text, final int i ) { | ||
| - return i > 0 && i <= text.length() | ||
| - ? new StringBuilder( text ).replace( i, i, CARET_POSITION_MD ).toString() | ||
| - : text; | ||
| + if( i > 0 && i <= text.length() ) { | ||
| + final String replacement = text.charAt( i - 1 ) == NEWLINE | ||
| + ? NEWLINE_CARET_POSITION_MD | ||
| + : CARET_POSITION_MD; | ||
| + | ||
| + return new StringBuilder( text ).replace( i, i, replacement ).toString(); | ||
| + } | ||
| + | ||
| + return text; | ||
| + } | ||
| + | ||
| + /** | ||
| + * Returns true if i is greater than or equal to min and less than or equal to | ||
| + * max. | ||
| + * | ||
| + * @param i The value to check. | ||
| + * @param min The lower bound. | ||
| + * @param max The upper bound. | ||
| + * | ||
| + * @return false The value of i is either lower than min or greater than max. | ||
| + */ | ||
| + protected boolean isBetween( int i, int min, int max ) { | ||
| + return i >= min && i <= max; | ||
| } | ||
| */ | ||
| public class CaretReplacementProcessor extends AbstractProcessor<String> { | ||
| - private static final int INDEX_NOT_FOUND = -1; | ||
| public CaretReplacementProcessor( final Processor<String> processor ) { |
| final ObservableValue<Integer> caret = tab.caretPositionProperty(); | ||
| final Processor<String> tpc = getCommonProcessor(); | ||
| - final Processor<String> cip = createInsertionProcessor( tpc, caret ); | ||
| + final Processor<String> cip = createMarkdownInsertionProcessor( tpc, caret ); | ||
| final Processor<String> dvp = new DefaultVariableProcessor( cip, getResolvedMap() ); | ||
| return dvp; | ||
| - } | ||
| - | ||
| - protected Processor<String> createRProcessor( final FileEditorTab tab ) { | ||
| - final ObservableValue<Integer> caret = tab.caretPositionProperty(); | ||
| - final Processor<String> tpc = getCommonProcessor(); | ||
| - final Processor<String> cip = createInsertionProcessor( tpc, caret ); | ||
| - final Processor<String> rp = new InlineRProcessor( cip, getResolvedMap(), tab.getPath() ); | ||
| - final Processor<String> rvp = new RVariableProcessor( rp, getResolvedMap() ); | ||
| - | ||
| - return rvp; | ||
| } | ||
| return dvp; | ||
| + } | ||
| + | ||
| + protected Processor<String> createRProcessor( final FileEditorTab tab ) { | ||
| + final ObservableValue<Integer> caret = tab.caretPositionProperty(); | ||
| + final Processor<String> tpc = getCommonProcessor(); | ||
| + final Processor<String> rp = new InlineRProcessor( tpc, getResolvedMap(), tab.getPath() ); | ||
| + final Processor<String> rvp = new RVariableProcessor( rp, getResolvedMap() ); | ||
| + final Processor<String> cip = createRInsertionProcessor( rvp, caret ); | ||
| + | ||
| + return cip; | ||
| } | ||
| } | ||
| - private Processor<String> createInsertionProcessor( | ||
| + private Processor<String> createMarkdownInsertionProcessor( | ||
| final Processor<String> tpc, final ObservableValue<Integer> caret ) { | ||
| return new MarkdownCaretInsertionProcessor( tpc, caret ); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Create an insertion processor that is aware of R statements and will insert | ||
| + * a caret outside of any statement the caret falls within. | ||
| + * | ||
| + * @param processor Another link in the processor chain. | ||
| + * @param caret The caret insertion point. | ||
| + * | ||
| + * @return A processor that can insert a caret token without disturbing any R | ||
| + * code. | ||
| + */ | ||
| + private Processor<String> createRInsertionProcessor( | ||
| + final Processor<String> processor, final ObservableValue<Integer> caret ) { | ||
| + return new RMarkdownCaretInsertionProcessor( processor, caret ); | ||
| } | ||
| +/* | ||
| + * Copyright 2016 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.processors; | ||
| + | ||
| +import static com.scrivenvar.decorators.RVariableDecorator.PREFIX; | ||
| +import static com.scrivenvar.decorators.RVariableDecorator.SUFFIX; | ||
| +import static java.lang.Integer.max; | ||
| +import javafx.beans.value.ObservableValue; | ||
| + | ||
| +/** | ||
| + * Responsible for inserting a caret position token into an R document. | ||
| + * | ||
| + * @author White Magic Software, Ltd. | ||
| + */ | ||
| +public class RMarkdownCaretInsertionProcessor | ||
| + extends MarkdownCaretInsertionProcessor { | ||
| + | ||
| + /** | ||
| + * Constructs a processor capable of inserting a caret marker into Markdown. | ||
| + * | ||
| + * @param processor The next processor in the chain. | ||
| + * @param position The caret's current position in the text. | ||
| + */ | ||
| + public RMarkdownCaretInsertionProcessor( | ||
| + final Processor<String> processor, | ||
| + final ObservableValue<Integer> position ) { | ||
| + super( processor, position ); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Changes the text to insert a "caret" at the caret position. This will | ||
| + * insert the unique key of Constants.MD_CARET_POSITION into the document. | ||
| + * | ||
| + * @param text The text document to process. | ||
| + * | ||
| + * @return The text with the caret position token inserted at the caret | ||
| + * position. | ||
| + */ | ||
| + @Override | ||
| + public String processLink( final String text ) { | ||
| + int offset = getCaretPosition(); | ||
| + | ||
| + // Search for inline R code from the start of the caret's paragraph. | ||
| + int index = text.lastIndexOf( NEWLINE, offset ); | ||
| + | ||
| + if( index == INDEX_NOT_FOUND ) { | ||
| + index = 0; | ||
| + } | ||
| + | ||
| + // Scan for an inline R statement, either from the nearest paragraph or | ||
| + // the beginning of the file, whichever was found first. | ||
| + index = text.indexOf( PREFIX, index ); | ||
| + | ||
| + // If there was no R prefix then insert at the caret's initial offset... | ||
| + if( index != INDEX_NOT_FOUND ) { | ||
| + // Otherwise, retain the starting index of the first R statement in the | ||
| + // paragraph. | ||
| + int rPrefix = index + 1; | ||
| + | ||
| + // Scan for inline R prefixes until the text is exhausted or indexed | ||
| + // beyond the caret position. | ||
| + while( index != INDEX_NOT_FOUND && index < offset ) { | ||
| + // Set rPrefix to the index that might precede the caret. | ||
| + rPrefix = index + 1; | ||
| + | ||
| + // If there are no more R prefixes, exit the loop and look for a | ||
| + // suffix starting from the rPrefix position. | ||
| + index = text.indexOf( PREFIX, rPrefix ); | ||
| + } | ||
| + | ||
| + // Scan from the character after the R prefix up to any R suffix. | ||
| + final int rSuffix = max( text.indexOf( SUFFIX, rPrefix ), rPrefix ); | ||
| + | ||
| + final boolean between = isBetween( offset, rPrefix, rSuffix ); | ||
| + | ||
| + // Insert the caret marker at the start of the R statement. | ||
| + if( between ) { | ||
| + offset = rPrefix - 1; | ||
| + } | ||
| + } | ||
| + | ||
| + return inject( text, offset ); | ||
| + } | ||
| +} | ||
| for( final String key : map.keySet() ) { | ||
| - rMap.put( toR( key ), '\'' + map.get( key ) + '\'' ); | ||
| + rMap.put( toRKey( key ), toRValue( map.get( key ) ) ); | ||
| } | ||
| * @return The transformed variable name. | ||
| */ | ||
| - private String toR( final String key ) { | ||
| + private String toRKey( final String key ) { | ||
| // Replace all the periods with dollar symbols. | ||
| final StringBuilder sb = new StringBuilder( 'v' + key ); | ||
| return sb.toString(); | ||
| + } | ||
| + | ||
| + private String toRValue( final String value ) { | ||
| + return '\'' + escape( value, '\'', "\\'" ) + '\''; | ||
| + } | ||
| + | ||
| + /** | ||
| + * TODO: Make generic method for replacing text. | ||
| + * | ||
| + * @see CaretReplacementProcessor.replace | ||
| + * | ||
| + * @param haystack Search this string for the needle, must not be null. | ||
| + * @param needle The character to find in the haystack. | ||
| + * @param thread Replace the needle with this text, if the needle is found. | ||
| + * | ||
| + * @return The haystack with the all instances of needle replaced with thread. | ||
| + */ | ||
| + private String escape( | ||
| + final String haystack, final char needle, final String thread ) { | ||
| + int end = haystack.indexOf( needle ); | ||
| + | ||
| + if( end < 0 ) { | ||
| + return haystack; | ||
| + } | ||
| + | ||
| + final int length = haystack.length(); | ||
| + int start = 0; | ||
| + | ||
| + // Replace up to 32 occurrences before the string reallocates its buffer. | ||
| + final StringBuilder sb = new StringBuilder( length + 32 ); | ||
| + | ||
| + while( end >= 0 ) { | ||
| + sb.append( haystack.substring( start, end ) ).append( thread ); | ||
| + start = end + 1; | ||
| + end = haystack.indexOf( needle, start ); | ||
| + } | ||
| + | ||
| + return sb.append( haystack.substring( start ) ).toString(); | ||
| } | ||
| } | ||
| } | ||
| - private boolean isBetween( int i, int min, int max ) { | ||
| - return i >= min && i <= max; | ||
| - } | ||
| - | ||
| /** | ||
| * Parses the given XML document and returns a high-performance navigator |