Search in sources :

Example 21 with Pattern

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

the class FindReplace method replace.

private void replace() {
    String searchString = display_.getFindValue().getValue();
    if (searchString.length() == 0)
        return;
    Pattern pattern = createPattern();
    String line = editor_.getCurrentLine();
    Match m = pattern.match(line, editor_.getSelectionStart().getColumn());
    if (m != null && m.getIndex() == editor_.getSelectionStart().getColumn() && m.getValue().length() == editor_.getSelectionValue().length()) {
        String replacement = display_.getReplaceValue().getValue();
        editor_.replaceSelection(display_.getRegex().getValue() ? substitute(m, replacement, line) : replacement);
        if (targetSelection_ != null)
            targetSelection_.syncMarker();
    }
    find(defaultForward_ ? FindType.Forward : FindType.Reverse);
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) Match(org.rstudio.core.client.regex.Match)

Example 22 with Pattern

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

the class FindReplace method replaceAll.

private void replaceAll() {
    String code = null;
    if (targetSelection_ != null) {
        Range range = targetSelection_.getRange();
        code = editor_.getCode(range.getStart(), range.getEnd());
    } else {
        code = editor_.getCode();
    }
    boolean regex = display_.getRegex().getValue();
    String find = display_.getFindValue().getValue();
    String repl = display_.getReplaceValue().getValue();
    int occurrences = 0;
    if (find.length() > 0) {
        Pattern pattern = createPattern();
        StringBuilder result = new StringBuilder();
        // pointer into original string
        int pos = 0;
        for (Match m = pattern.match(code, 0); m != null; m = m.nextMatch()) {
            occurrences++;
            // Add everything between the end of the last match, and this one
            int index = m.getIndex();
            result.append(code, pos, index);
            // Add the replacement value
            if (regex)
                result.append(substitute(m, repl, code));
            else
                result.append(repl);
            // Point to the end of this match
            pos = index + m.getValue().length();
            // we'll loop forever (see case 4191). Bail out.
            if (m.getValue().length() == 0) {
                break;
            }
        }
        result.append(code, pos, code.length());
        String newCode = result.toString();
        // either replace all or replace just the target range
        if (targetSelection_ != null) {
            // restore and then replace the selection
            editor_.setSelectionRange(targetSelection_.getRange());
            editor_.replaceSelection(newCode, false);
            // reset the target selection
            resetTargetSelection();
        } else {
            editor_.replaceCode(newCode);
        }
    }
    globalDisplay_.showMessage(GlobalDisplay.MSG_INFO, errorCaption_, occurrences + " occurrences replaced.");
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range) Match(org.rstudio.core.client.regex.Match)

Example 23 with Pattern

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

the class DefaultChunkOptionsPopupPanel method parseChunkHeader.

private void parseChunkHeader(String line, HashMap<String, String> chunkOptions) {
    String modeId = display_.getModeId();
    Pattern pattern = null;
    if (modeId.equals("mode/rmarkdown"))
        pattern = RegexUtil.RE_RMARKDOWN_CHUNK_BEGIN;
    else if (modeId.equals("mode/sweave"))
        pattern = RegexUtil.RE_SWEAVE_CHUNK_BEGIN;
    else if (modeId.equals("mode/rhtml"))
        pattern = RegexUtil.RE_RHTML_CHUNK_BEGIN;
    if (pattern == null)
        return;
    Match match = pattern.match(line, 0);
    if (match == null)
        return;
    String extracted = match.getGroup(1);
    chunkPreamble_ = extractChunkPreamble(extracted, modeId);
    String chunkLabel = extractChunkLabel(extracted);
    if (!StringUtil.isNullOrEmpty(chunkLabel))
        tbChunkLabel_.setText(extractChunkLabel(extracted));
    // if we had a chunk label, then we want to navigate our cursor to
    // the first comma in the chunk header; otherwise, we start at the
    // first space. this is done to accept chunk headers of the form
    //
    //    ```{r message=FALSE}
    //
    // ie, those with no comma after the engine used
    int argsStartIdx = StringUtil.isNullOrEmpty(chunkLabel) ? extracted.indexOf(' ') : extracted.indexOf(',');
    String arguments = extracted.substring(argsStartIdx + 1);
    TextCursor cursor = new TextCursor(arguments);
    // consume commas and whitespace if needed
    cursor.consumeUntilRegex("[^\\s,]");
    int startIndex = 0;
    do {
        if (!cursor.fwdToCharacter('=', false))
            break;
        int equalsIndex = cursor.getIndex();
        int endIndex = arguments.length();
        if (cursor.fwdToCharacter(',', true) || cursor.fwdToCharacter(' ', true)) {
            endIndex = cursor.getIndex();
        }
        chunkOptions.put(arguments.substring(startIndex, equalsIndex).trim(), arguments.substring(equalsIndex + 1, endIndex).trim());
        startIndex = cursor.getIndex() + 1;
    } while (cursor.moveToNextCharacter());
}
Also used : TextCursor(org.rstudio.core.client.TextCursor) Pattern(org.rstudio.core.client.regex.Pattern) Match(org.rstudio.core.client.regex.Match)

Example 24 with Pattern

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

the class RequestLogEntry method getRequestMethodName.

public String getRequestMethodName() {
    if (requestData_.equals("[REDACTED]"))
        return requestData_;
    Pattern p = Pattern.create("\\\"method\\\":\\s*\\\"([^\"]+)\\\"");
    Match match = p.match(requestData_, 0);
    if (match == null)
        return null;
    return match.getGroup(1);
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) Match(org.rstudio.core.client.regex.Match)

Example 25 with Pattern

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

the class DomUtils method countLinesInternal.

private static int countLinesInternal(Text textNode, boolean pre) {
    if (!pre)
        return 0;
    String value = textNode.getData();
    Pattern pattern = Pattern.create("\\n");
    int count = 0;
    Match m = pattern.match(value, 0);
    while (m != null) {
        count++;
        m = m.nextMatch();
    }
    return count;
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) JsArrayString(com.google.gwt.core.client.JsArrayString) Point(org.rstudio.core.client.Point) Match(org.rstudio.core.client.regex.Match)

Aggregations

Pattern (org.rstudio.core.client.regex.Pattern)28 Match (org.rstudio.core.client.regex.Match)22 JsArrayString (com.google.gwt.core.client.JsArrayString)8 ArrayList (java.util.ArrayList)3 Breakpoint (org.rstudio.studio.client.common.debugging.model.Breakpoint)3 Point (org.rstudio.core.client.Point)2 Position (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)2 Iterator (java.util.Iterator)1 TextCursor (org.rstudio.core.client.TextCursor)1 FileSystemItem (org.rstudio.core.client.files.FileSystemItem)1 EditableFileType (org.rstudio.studio.client.common.filetypes.EditableFileType)1 FileType (org.rstudio.studio.client.common.filetypes.FileType)1 TextFileType (org.rstudio.studio.client.common.filetypes.TextFileType)1 OpenFileInBrowserEvent (org.rstudio.studio.client.common.filetypes.events.OpenFileInBrowserEvent)1 ServerError (org.rstudio.studio.client.server.ServerError)1 InputEditorPosition (org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition)1 AceFold (org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceFold)1 Range (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)1