Search in sources :

Example 6 with Match

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

the class TextCursor method consumeUntilRegex.

public boolean consumeUntilRegex(String regex) {
    Pattern pattern = Pattern.create(regex);
    Match match = pattern.match(data_, index_);
    if (match == null)
        return false;
    index_ = match.getIndex();
    return true;
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) Match(org.rstudio.core.client.regex.Match)

Example 7 with Match

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

the class CodeSearchOracle method requestSuggestions.

@Override
public void requestSuggestions(final Request request, final Callback callback) {
    // invalidate any outstanding search
    searchInvalidation_.invalidate();
    // first see if we can serve the request from the cache
    for (int i = resultCache_.size() - 1; i >= 0; i--) {
        // get the previous result
        SearchResult res = resultCache_.get(i);
        // exact match of previous query
        if (request.getQuery().equals(res.getQuery())) {
            callback.onSuggestionsReady(request, new Response(res.getSuggestions()));
            return;
        }
        // previous query then satisfy it by filtering the previous results
        if (!res.getMoreAvailable() && request.getQuery().startsWith(res.getQuery())) {
            Pattern pattern = null;
            String query = request.getQuery();
            String queryLower = query.toLowerCase();
            if (queryLower.indexOf('*') != -1)
                pattern = patternForTerm(queryLower);
            ArrayList<CodeSearchSuggestion> suggestions = new ArrayList<CodeSearchSuggestion>();
            for (int s = 0; s < res.getSuggestions().size(); s++) {
                CodeSearchSuggestion sugg = res.getSuggestions().get(s);
                String name = sugg.getMatchedString().toLowerCase();
                if (pattern != null) {
                    Match match = pattern.match(name, 0);
                    if (match != null && match.getIndex() == 0)
                        suggestions.add(sugg);
                } else {
                    int colonIndex = query.indexOf(":");
                    if (colonIndex == -1)
                        colonIndex = query.length();
                    if (StringUtil.isSubsequence(name, query.substring(0, colonIndex), true))
                        suggestions.add(sugg);
                }
            }
            // process and cache suggestions. note that this adds an item to
            // the end of the resultCache_ (which we are currently iterating
            // over) no biggie because we are about to return from the loop
            suggestions = processSuggestions(request, suggestions, false);
            // sort suggestions
            sortSuggestions(suggestions, query);
            // return suggestions
            callback.onSuggestionsReady(request, new Response(suggestions));
            return;
        }
    }
    // failed to short-circuit via the cache, hit the server
    codeSearch_.enqueRequest(request, callback);
}
Also used : Pattern(org.rstudio.core.client.regex.Pattern) ArrayList(java.util.ArrayList) Match(org.rstudio.core.client.regex.Match)

Example 8 with Match

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

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

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

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