Search in sources :

Example 16 with Pattern

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

the class AceEditorBackgroundLinkHighlighter method onWebLinkHighlight.

private void onWebLinkHighlight(AceEditor editor, String line, int row) {
    // use a regex that captures all non-space characters within
    // a web link, and then fix up the captured link by removing
    // trailing punctuation, etc. as required
    Pattern reWebLink = createWebLinkPattern();
    for (Match match = reWebLink.match(line, 0); match != null; match = match.nextMatch()) {
        // compute start, end index for discovered URL
        int startIdx = match.getIndex();
        int endIdx = match.getIndex() + match.getValue().length();
        // ensure that the discovered url is not within a string
        Token token = editor_.getTokenAt(Position.create(row, startIdx));
        if (token.hasType("string"))
            continue;
        String url = match.getValue();
        // trim off enclosing brackets
        if (!url.matches(reWebLink())) {
            startIdx++;
            endIdx--;
            url = url.substring(1, url.length() - 1);
        }
        // trim off trailing punctuation (characters unlikely
        // to be found at the end of a url)
        String trimmed = url.replaceAll("[,.?!@#$%^&*;:-]+$", "");
        endIdx -= (url.length() - trimmed.length());
        url = trimmed;
        // perform highlighting
        highlight(editor, row, startIdx, endIdx);
    }
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) Match(org.rstudio.core.client.regex.Match)

Example 17 with Pattern

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

the class AceEditorBackgroundLinkHighlighter method onTestthatErrorHighlight.

private void onTestthatErrorHighlight(AceEditor editor, String line, int row) {
    Pattern reTestthatError = Pattern.create("\\(@[^#]+#\\d+\\)");
    for (Match match = reTestthatError.match(line, 0); match != null; match = match.nextMatch()) {
        int startIdx = match.getIndex() + 1;
        int endIdx = match.getIndex() + match.getValue().length() - 1;
        highlight(editor, row, startIdx, endIdx);
    }
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) Match(org.rstudio.core.client.regex.Match)

Example 18 with Pattern

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

the class AceEditorBackgroundLinkHighlighter method onMarkdownLinkHighlight.

private void onMarkdownLinkHighlight(AceEditor editor, String line, int row) {
    Pattern reMarkdownLink = Pattern.create("(\\[[^\\]]+\\])(\\([^\\)]+\\))");
    for (Match match = reMarkdownLink.match(line, 0); match != null; match = match.nextMatch()) {
        int startIdx = match.getIndex() + match.getGroup(1).length() + 1;
        int endIdx = match.getIndex() + match.getValue().length() - 1;
        highlight(editor, row, startIdx, endIdx);
    }
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) Match(org.rstudio.core.client.regex.Match)

Example 19 with Pattern

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

the class CppCompletionUtils method getCompletionPosition.

public static CompletionPosition getCompletionPosition(DocDisplay docDisplay, boolean explicit, boolean alwaysComplete, int autoChars) {
    // get the current line of code
    String line = docDisplay.getCurrentLine();
    // is this an '#include' line?
    Pattern reInclude = Pattern.create("^\\s*#+\\s*include", "");
    boolean isInclude = reInclude.test(line);
    // get the cursor position
    Position position = docDisplay.getCursorPosition();
    // if so then bail
    if ((position.getColumn() < line.length()) && CppCompletionUtils.isCppIdentifierChar(line.charAt(position.getColumn()))) {
        return null;
    }
    // determine the column right before this one
    int inputCol = position.getColumn() - 1;
    // walk backwards across C++ identifer symbols 
    int col = inputCol;
    if (isInclude) {
        while (col >= 0 && !StringUtil.isOneOf(line.charAt(col), '/', '<', '"')) {
            col--;
        }
    } else {
        while ((col >= 0) && CppCompletionUtils.isCppIdentifierChar(line.charAt(col))) {
            col--;
        }
    }
    // record source position
    Position startPos = Position.create(position.getRow(), col + 1);
    // check for a completion triggering sequence
    char ch = line.charAt(col);
    char prefixCh = line.charAt(col - 1);
    // member
    if (ch == '.' || (prefixCh == '-' && ch == '>')) {
        return new CompletionPosition(startPos, // no user text (get all completions)
        "", CompletionPosition.Scope.Member);
    } else // scope
    if (prefixCh == ':' && ch == ':') {
        return new CompletionPosition(startPos, // no user text (get all completions)
        "", CompletionPosition.Scope.Namespace);
    } else // directory
    if (isInclude && ch == '/') {
        return new CompletionPosition(startPos, // no user test (get all completions)
        "", CompletionPosition.Scope.File);
    } else // minimum character threshold
    if (// either always completing or explicit
    (alwaysComplete || explicit) && // meets the character threshold
    ((inputCol - col) >= (explicit ? 1 : autoChars)) && // not a quote character
    (isInclude || ch != '"')) {
        // calculate user text (up to two characters of additional
        // server side filter)
        String userText = line.substring(col + 1, Math.min(col + 3, position.getColumn()));
        CompletionPosition.Scope scope = isInclude ? CompletionPosition.Scope.File : CompletionPosition.Scope.Global;
        return new CompletionPosition(startPos, userText, scope);
    } else {
        return null;
    }
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)

Example 20 with Pattern

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

the class TextEditingTargetCompilePdfHelper method getRnwOptionsStart.

@Override
public int getRnwOptionsStart(String line, int cursorPos) {
    Pattern pattern = docDisplay_.getFileType().getRnwStartPatternBegin();
    if (pattern == null)
        return -1;
    String linePart = line.substring(0, cursorPos);
    Match match = pattern.match(linePart, 0);
    if (match == null)
        return -1;
    // See if the cursor is already past the end of the chunk header,
    // for example <<foo>>=[CURSOR].
    Pattern patternEnd = docDisplay_.getFileType().getRnwStartPatternEnd();
    if (patternEnd != null && patternEnd.match(linePart, 0) != null)
        return -1;
    return match.getValue().length();
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) 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