Search in sources :

Example 26 with Match

use of org.rstudio.core.client.regex.Match in project rstudio by rstudio.

the class DomUtils method trimLines.

private static int trimLines(NodeList<Node> nodes, final int linesToTrim) {
    if (nodes == null || nodes.getLength() == 0 || linesToTrim == 0)
        return 0;
    int linesLeft = linesToTrim;
    Node node = nodes.getItem(0);
    while (node != null && linesLeft > 0) {
        switch(node.getNodeType()) {
            case Node.ELEMENT_NODE:
                if (((Element) node).getTagName().equalsIgnoreCase("br")) {
                    linesLeft--;
                    node = removeAndGetNext(node);
                    continue;
                } else {
                    int trimmed = trimLines(node.getChildNodes(), linesLeft);
                    linesLeft -= trimmed;
                    if (!node.hasChildNodes())
                        node = removeAndGetNext(node);
                    continue;
                }
            case Node.TEXT_NODE:
                String text = ((Text) node).getData();
                Match lastMatch = null;
                Match match = NEWLINE.match(text, 0);
                while (match != null && linesLeft > 0) {
                    lastMatch = match;
                    linesLeft--;
                    match = match.nextMatch();
                }
                if (linesLeft > 0 || lastMatch == null) {
                    node = removeAndGetNext(node);
                    continue;
                } else {
                    int index = lastMatch.getIndex() + 1;
                    if (text.length() == index)
                        node.removeFromParent();
                    else
                        ((Text) node).deleteData(0, index);
                    break;
                }
        }
    }
    return linesToTrim - linesLeft;
}
Also used : JsArrayString(com.google.gwt.core.client.JsArrayString) Point(org.rstudio.core.client.Point) Match(org.rstudio.core.client.regex.Match)

Example 27 with Match

use of org.rstudio.core.client.regex.Match in project rstudio by rstudio.

the class FileSystemItem method getIcon.

// RStudio-specific code should use FileTypeRegistry.getIconForFile() instead
public final ImageResource getIcon() {
    if (isDirectory()) {
        if (isPublicFolder())
            return new ImageResource2x(RES.iconPublicFolder2x());
        else
            return new ImageResource2x(RES.iconFolder2x());
    }
    Match m = EXT_PATTERN.match(getName(), 0);
    if (m == null)
        return new ImageResource2x(RES.iconText2x());
    String lowerExt = m.getValue().toLowerCase();
    if (lowerExt.equals(".csv")) {
        return new ImageResource2x(RES.iconCsv2x());
    } else if (lowerExt.equals(".pdf")) {
        return new ImageResource2x(RES.iconPdf2x());
    } else if (lowerExt.equals(".jpg") || lowerExt.equals(".jpeg") || lowerExt.equals(".gif") || lowerExt.equals(".bmp") || lowerExt.equals(".tiff") || lowerExt.equals(".tif") || lowerExt.equals(".png")) {
        return new ImageResource2x(RES.iconPng2x());
    } else {
        return new ImageResource2x(RES.iconText2x());
    }
}
Also used : ImageResource2x(org.rstudio.core.client.resources.ImageResource2x) Match(org.rstudio.core.client.regex.Match)

Example 28 with Match

use of org.rstudio.core.client.regex.Match in project rstudio by rstudio.

the class VirtualConsole method submit.

public void submit(String data, String clazz) {
    if (CONTROL.match(data, 0) == null) {
        text(data, clazz);
        return;
    }
    int tail = 0;
    Match match = CONTROL.match(data, 0);
    while (match != null) {
        int pos = match.getIndex();
        // character, add it.
        if (tail != pos)
            text(data.substring(tail, pos), clazz);
        tail = pos + 1;
        switch(data.charAt(pos)) {
            case '\r':
                carriageReturn();
                break;
            case '\b':
                backspace();
                break;
            case '\n':
                newline(clazz);
                break;
            case '\f':
                formfeed();
                break;
            default:
                assert false : "Unknown control char, please check regex";
                text(data.charAt(pos) + "", clazz);
                break;
        }
        match = match.nextMatch();
    }
    // If there was any plain text after the last control character, add it
    if (tail < data.length())
        text(data.substring(tail), clazz);
}
Also used : Match(org.rstudio.core.client.regex.Match)

Example 29 with Match

use of org.rstudio.core.client.regex.Match 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 30 with Match

use of org.rstudio.core.client.regex.Match in project rstudio by rstudio.

the class RTokenizer method eatUntil.

private String eatUntil(String regex, boolean eatAllOnFailure) {
    int start = pos_;
    Match match = Pattern.create(regex).match(data_, pos_);
    if (match == null) {
        if (eatAllOnFailure) {
            pos_ = data_.length();
            return data_.substring(start);
        } else {
            return null;
        }
    } else {
        pos_ = match.getIndex();
        return data_.substring(start, pos_);
    }
}
Also used : Match(org.rstudio.core.client.regex.Match)

Aggregations

Match (org.rstudio.core.client.regex.Match)33 Pattern (org.rstudio.core.client.regex.Pattern)22 JsArrayString (com.google.gwt.core.client.JsArrayString)10 ArrayList (java.util.ArrayList)5 Point (org.rstudio.core.client.Point)3 Breakpoint (org.rstudio.studio.client.common.debugging.model.Breakpoint)2 Range (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)2 JsArrayInteger (com.google.gwt.core.client.JsArrayInteger)1 Iterator (java.util.Iterator)1 Pair (org.rstudio.core.client.Pair)1 TextCursor (org.rstudio.core.client.TextCursor)1 ReplaceOperation (org.rstudio.core.client.regex.Pattern.ReplaceOperation)1 ImageResource2x (org.rstudio.core.client.resources.ImageResource2x)1 InputEditorPosition (org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition)1 Position (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)1