Search in sources :

Example 16 with Range

use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.

the class ImagePreviewer method onPreviewLink.

public static void onPreviewLink(DocDisplay display, DocUpdateSentinel sentinel, UIPrefs prefs, Position position) {
    Token token = display.getTokenAt(position);
    if (token == null)
        return;
    if (!token.hasType("href"))
        return;
    Range tokenRange = Range.fromPoints(Position.create(position.getRow(), token.getColumn()), Position.create(position.getRow(), token.getColumn() + token.getValue().length()));
    String href = token.getValue();
    if (ImagePreviewer.isImageHref(href)) {
        // extract HTML attributes from line for markdown links, e.g.
        //
        //    ![](plot.png){width=400 height=400}
        //
        String attributes = null;
        String line = display.getLine(position.getRow());
        if (isStandaloneMarkdownLink(line)) {
            int startBraceIdx = line.indexOf("){");
            int endBraceIdx = line.lastIndexOf("}");
            if (startBraceIdx != -1 && endBraceIdx != -1 && endBraceIdx > startBraceIdx) {
                attributes = line.substring(startBraceIdx + 2, endBraceIdx).trim();
            }
        }
        onPreviewImage(display, sentinel, prefs, href, attributes, position, tokenRange);
    }
}
Also used : Token(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)

Example 17 with Range

use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.

the class MathJaxUtil method findLatexChunks.

public static List<Range> findLatexChunks(DocDisplay docDisplay) {
    docDisplay.tokenizeDocument();
    List<Range> ranges = new ArrayList<Range>();
    Position startPos = null;
    for (int i = 0, n = docDisplay.getRowCount(); i < n; i++) {
        Position pos = Position.create(i, 0);
        Token token = docDisplay.getTokenAt(Position.create(i, 0));
        if (token == null)
            continue;
        if (token.hasAllTypes("latex", "begin") && token.getValue().equals("$$")) {
            startPos = pos;
            // get the length of this line to see if it could be an inline
            // LaTeX chunk (e.g. $$ x = y $$)
            int length = docDisplay.getLength(i);
            if (length < 5)
                continue;
            // get the last token on the row; if it's a LaTeX end token then
            // consider the row to be an inline LaTeX chunk
            Token endLineToken = docDisplay.getTokenAt(Position.create(i, docDisplay.getLength(i)));
            if (endLineToken != null && endLineToken.hasAllTypes("latex", "end")) {
                ranges.add(Range.fromPoints(startPos, Position.create(i, length)));
            }
            continue;
        }
        if (token.hasAllTypes("latex", "end") && token.getValue().equals("$$")) {
            ranges.add(Range.fromPoints(startPos, Position.create(i, 2)));
            continue;
        }
    }
    return ranges;
}
Also used : Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) ArrayList(java.util.ArrayList) Token(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)

Example 18 with Range

use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.

the class RoxygenHelper method amendExistingRoxygenBlock.

private void amendExistingRoxygenBlock(int row, String objectName, JsArrayString argNames, JsArrayString argTypes, String tagName, Pattern pattern) {
    // Get the range encompassing this Roxygen block.
    Range range = getRoxygenBlockRange(row);
    // Extract that block (as an array of strings)
    JsArrayString block = extractRoxygenBlock(editor_.getWidget().getEditor(), range);
    // bail.
    for (int i = 0; i < block.length(); i++) {
        if (RE_ROXYGEN_NONLOCAL.test(block.get(i))) {
            view_.showWarningBar("Cannot automatically update roxygen blocks " + "that are not self-contained.");
            return;
        }
    }
    String roxygenDelim = RE_ROXYGEN.match(block.get(0), 0).getGroup(1);
    // The replacement block (we build by munging parts of
    // the old block
    JsArrayString replacement = JsArray.createArray().cast();
    // Scan through the block to get the names of
    // pre-existing parameters.
    JsArrayString params = listParametersInRoxygenBlock(block, pattern);
    // Figure out what parameters need to be removed, and remove them.
    // Any parameter not mentioned in the current function's argument list
    // should be stripped out.
    JsArrayString paramsToRemove = setdiff(params, argNames);
    int blockLength = block.length();
    for (int i = 0; i < blockLength; i++) {
        // If we encounter a param we don't want to extract, then
        // move over it.
        Match match = pattern.match(block.get(i), 0);
        if (match != null && contains(paramsToRemove, match.getGroup(1))) {
            i++;
            while (i < blockLength && !RE_ROXYGEN_WITH_TAG.test(block.get(i))) i++;
            i--;
            continue;
        }
        replacement.push(block.get(i));
    }
    // Now, add example roxygen for any parameters that are
    // present in the function prototype, but not present
    // within the roxygen block.
    int insertionPosition = findParamsInsertionPosition(replacement, pattern);
    JsArrayInteger indices = setdiffIndices(argNames, params);
    // NOTE: modifies replacement
    insertNewTags(replacement, argNames, argTypes, indices, roxygenDelim, tagName, insertionPosition);
    // Ensure space between final param and next tag
    ensureSpaceBetweenFirstParamAndPreviousEntry(replacement, roxygenDelim, pattern);
    ensureSpaceBetweenFinalParamAndNextTag(replacement, roxygenDelim, pattern);
    // Apply the replacement.
    editor_.getSession().replace(range, replacement.join("\n") + "\n");
}
Also used : JsArrayInteger(com.google.gwt.core.client.JsArrayInteger) JsArrayString(com.google.gwt.core.client.JsArrayString) JsArrayString(com.google.gwt.core.client.JsArrayString) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range) Match(org.rstudio.core.client.regex.Match)

Example 19 with Range

use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.

the class MathJaxRenderQueue method renderNext.

// Private Methods ----
private boolean renderNext() {
    Range range = ranges_.poll();
    if (range == null) {
        isRunning_ = false;
        return false;
    }
    isRunning_ = true;
    mathjax_.renderLatex(range, false, callback_);
    return true;
}
Also used : Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)

Example 20 with Range

use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range in project rstudio by rstudio.

the class MathJaxUtil method isSelectionWithinLatexChunk.

public static boolean isSelectionWithinLatexChunk(DocDisplay docDisplay) {
    Range range = getLatexRange(docDisplay);
    if (range == null)
        return false;
    Token startToken = docDisplay.getTokenAt(range.getStart().getRow(), 0);
    if (startToken == null || !startToken.getValue().equals("$$"))
        return false;
    Token endToken = docDisplay.getTokenAt(range.getEnd().getRow(), 0);
    if (endToken == null || !endToken.getValue().equals("$$"))
        return false;
    return true;
}
Also used : Token(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)

Aggregations

Range (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)37 Position (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)11 JsArrayString (com.google.gwt.core.client.JsArrayString)7 ScheduledCommand (com.google.gwt.core.client.Scheduler.ScheduledCommand)7 Breakpoint (org.rstudio.studio.client.common.debugging.model.Breakpoint)5 Token (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Token)5 ArrayList (java.util.ArrayList)4 AnchoredRange (org.rstudio.studio.client.workbench.views.source.editors.text.ace.AnchoredRange)4 Scope (org.rstudio.studio.client.workbench.views.source.editors.text.Scope)3 JsArray (com.google.gwt.core.client.JsArray)2 Command (com.google.gwt.user.client.Command)2 NativePreviewEvent (com.google.gwt.user.client.Event.NativePreviewEvent)2 NativePreviewHandler (com.google.gwt.user.client.Event.NativePreviewHandler)2 Handler (org.rstudio.core.client.command.Handler)2 EnsureHeightHandler (org.rstudio.core.client.events.EnsureHeightHandler)2 EnsureVisibleHandler (org.rstudio.core.client.events.EnsureVisibleHandler)2 Match (org.rstudio.core.client.regex.Match)2 ChangeFontSizeHandler (org.rstudio.studio.client.application.events.ChangeFontSizeHandler)2 FileChangeHandler (org.rstudio.studio.client.workbench.views.files.events.FileChangeHandler)2 HideMessageHandler (org.rstudio.studio.client.workbench.views.source.editors.text.status.StatusBar.HideMessageHandler)2