Search in sources :

Example 31 with MatchResult

use of com.google.gwt.regexp.shared.MatchResult in project che by eclipse.

the class FindReplaceDocumentAdapter method findReplace.

/**
     * Stateful findReplace executes a FIND, REPLACE, REPLACE_FIND or FIND_FIRST operation. In case of REPLACE and REPLACE_FIND it
     * sends a <code>DocumentEvent</code> to all registered <code>IDocumentListener</code>.
     *
     * @param startOffset
     *         document offset at which search starts this value is only used in the FIND_FIRST operation and otherwise
     *         ignored
     * @param findString
     *         the string to find this value is only used in the FIND_FIRST operation and otherwise ignored
     * @param replaceText
     *         the string to replace the current match this value is only used in the REPLACE and REPLACE_FIND
     *         operations and otherwise ignored
     * @param forwardSearch
     *         the search direction
     * @param caseSensitive
     *         indicates whether lower and upper case should be distinguished
     * @param wholeWord
     *         indicates whether the findString should be limited by white spaces as defined by Character.isWhiteSpace.
     *         Must not be used in combination with <code>regExSearch</code>.
     * @param regExSearch
     *         if <code>true</code> this operation represents a regular expression Must not be used in combination with
     *         <code>wholeWord</code>.
     * @param operationCode
     *         specifies what kind of operation is executed
     * @return the find or replace region or <code>null</code> if there was no match
     * @throws org.eclipse.che.ide.api.editor.text.BadLocationException
     *         if startOffset is an invalid document offset
     * @throws IllegalStateException
     *         if a REPLACE or REPLACE_FIND operation is not preceded by a successful FIND operation
     * @throws PatternSyntaxException
     *         if a regular expression has invalid syntax
     */
private Region findReplace(final FindReplaceOperationCode operationCode, int startOffset, String findString, String replaceText, boolean forwardSearch, boolean caseSensitive, boolean wholeWord) throws BadLocationException {
    // Validate state
    if ((operationCode == REPLACE || operationCode == REPLACE_FIND_NEXT) && (fFindReplaceState != FIND_FIRST && fFindReplaceState != FIND_NEXT))
        //$NON-NLS-1$
        throw new IllegalStateException("illegal findReplace state: cannot replace without preceding find");
    if (operationCode == FIND_FIRST) {
        if (findString == null || findString.length() == 0)
            return null;
        // Validate start offset
        if (startOffset < 0 || startOffset >= length())
            throw new BadLocationException();
        String patternFlags = "g";
        if (caseSensitive)
            patternFlags += "i";
        if (wholeWord)
            //$NON-NLS-1$ //$NON-NLS-2$
            findString = "\\b" + findString + "\\b";
        if (!wholeWord)
            findString = asRegPattern(findString);
        fFindReplaceMatchOffset = startOffset;
        regExp = RegExp.compile(findString, patternFlags);
        regExp.setLastIndex(fFindReplaceMatchOffset);
    }
    // Set state
    fFindReplaceState = operationCode;
    if (operationCode != REPLACE) {
        if (forwardSearch) {
            MatchResult matchResult = regExp.exec(String.valueOf(this));
            if (matchResult != null && matchResult.getGroupCount() > 0 && !matchResult.getGroup(0).isEmpty())
                return new RegionImpl(matchResult.getIndex(), matchResult.getGroup(0).length());
            return null;
        }
        // backward search
        regExp.setLastIndex(0);
        MatchResult matchResult = regExp.exec(String.valueOf(this));
        boolean found = matchResult != null;
        int index = -1;
        int length = -1;
        while (found && matchResult.getIndex() + matchResult.getGroup(0).length() <= fFindReplaceMatchOffset + 1) {
            index = matchResult.getIndex();
            length = matchResult.getGroup(0).length();
            regExp.setLastIndex(index + 1);
            matchResult = regExp.exec(String.valueOf(this));
            found = matchResult != null;
        }
        fFindReplaceMatchOffset = index;
        if (index > -1) {
            // must set matcher to correct position
            regExp.setLastIndex(index);
            matchResult = regExp.exec(String.valueOf(this));
            return new RegionImpl(index, length);
        }
        return null;
    }
    return null;
}
Also used : RegionImpl(org.eclipse.che.ide.api.editor.text.RegionImpl) MatchResult(com.google.gwt.regexp.shared.MatchResult) BadLocationException(org.eclipse.che.ide.api.editor.text.BadLocationException)

Example 32 with MatchResult

use of com.google.gwt.regexp.shared.MatchResult in project rstudio by rstudio.

the class YamlFrontMatter method getFrontMatterRange.

public static Range getFrontMatterRange(DocDisplay display) {
    // front matter can end with ... rather than ---; see spec:
    // http://www.yaml.org/spec/1.2/spec.html#id2760395
    RegExp frontMatterBegin = RegExp.compile("^---\\s*$", "gm");
    RegExp frontMatterEnd = RegExp.compile("^(---|\\.\\.\\.)\\s*$", "gm");
    Position begin = null;
    Position end = null;
    for (int i = 0; i < display.getRowCount(); i++) {
        String code = display.getLine(i);
        if (begin == null) {
            // haven't found front matter begin yet; test this line
            MatchResult beginMatch = frontMatterBegin.exec(code);
            if (beginMatch == null) {
                // Markdown package)
                if (!code.matches("\\s*"))
                    break;
            } else {
                begin = Position.create(i + 1, 0);
                continue;
            }
        } else if (end == null) {
            // haven't found front matter end yet; test this line
            MatchResult endMatch = frontMatterEnd.exec(code);
            if (endMatch != null) {
                end = Position.create(i, 0);
                break;
            }
        }
    }
    if (begin == null || end == null)
        return null;
    return Range.fromPoints(begin, end);
}
Also used : RegExp(com.google.gwt.regexp.shared.RegExp) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) MatchResult(com.google.gwt.regexp.shared.MatchResult)

Example 33 with MatchResult

use of com.google.gwt.regexp.shared.MatchResult in project rstudio by rstudio.

the class NewConnectionSnippetHost method parseSnippet.

private ArrayList<NewConnectionSnippetParts> parseSnippet(String input) {
    ArrayList<NewConnectionSnippetParts> parts = new ArrayList<NewConnectionSnippetParts>();
    RegExp regExp = RegExp.compile(pattern_, "g");
    for (MatchResult matcher = regExp.exec(input); matcher != null; matcher = regExp.exec(input)) {
        if (matcher.getGroupCount() >= 2) {
            int order = 0;
            try {
                order = Integer.parseInt(matcher.getGroup(1));
            } catch (NumberFormatException e) {
            }
            String key = matcher.getGroup(2);
            String value = matcher.getGroupCount() >= 4 ? matcher.getGroup(4) : null;
            String connStringField = matcher.getGroupCount() >= 6 ? matcher.getGroup(6) : null;
            if (value != null) {
                value = value.replaceAll("\\$colon\\$", ":");
                value = value.replaceAll("\\$equal\\$", "=");
            }
            parts.add(new NewConnectionSnippetParts(order, key, value, connStringField));
        }
    }
    Collections.sort(parts, new Comparator<NewConnectionSnippetParts>() {

        @Override
        public int compare(NewConnectionSnippetParts p1, NewConnectionSnippetParts p2) {
            return p1.getOrder() - p2.getOrder();
        }
    });
    return parts;
}
Also used : RegExp(com.google.gwt.regexp.shared.RegExp) ArrayList(java.util.ArrayList) MatchResult(com.google.gwt.regexp.shared.MatchResult)

Example 34 with MatchResult

use of com.google.gwt.regexp.shared.MatchResult in project flow by vaadin.

the class DefaultConnectionStateHandler method xhrInvalidContent.

@Override
public void xhrInvalidContent(XhrConnectionError xhrConnectionError) {
    debug("xhrInvalidContent");
    endRequest();
    String responseText = xhrConnectionError.getXhr().getResponseText();
    /*
         * A servlet filter or equivalent may have intercepted the request and
         * served non-UIDL content (for instance, a login page if the session
         * has expired.) If the response contains a magic substring, do a
         * synchronous refresh. See #8241.
         */
    MatchResult refreshToken = RegExp.compile(UIDL_REFRESH_TOKEN + "(:\\s*(.*?))?(\\s|$)").exec(responseText);
    if (refreshToken != null) {
        WidgetUtil.redirect(refreshToken.getGroup(2));
    } else {
        handleUnrecoverableCommunicationError("Invalid JSON response from server: " + responseText, xhrConnectionError);
    }
}
Also used : MatchResult(com.google.gwt.regexp.shared.MatchResult)

Example 35 with MatchResult

use of com.google.gwt.regexp.shared.MatchResult in project kie-wb-common by kiegroup.

the class SvgDataUriGenerator method removeAll.

private static String removeAll(final RegExp exp, final int group, final String content) {
    String result = content;
    while (exp.test(result)) {
        final MatchResult matchResult = exp.exec(result);
        if (matchResult != null) {
            String toReplace = matchResult.getGroup(group);
            result = result.replace(toReplace, "");
        }
    }
    return result;
}
Also used : MatchResult(com.google.gwt.regexp.shared.MatchResult)

Aggregations

MatchResult (com.google.gwt.regexp.shared.MatchResult)47 RegExp (com.google.gwt.regexp.shared.RegExp)23 ArrayList (java.util.ArrayList)7 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)3 ProjectId (edu.stanford.bmir.protege.web.shared.project.ProjectId)3 JsArrayString (com.google.gwt.core.client.JsArrayString)1 Node (com.google.gwt.dom.client.Node)1 PreElement (com.google.gwt.dom.client.PreElement)1 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)1 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)1 SafeHtml (com.google.gwt.safehtml.shared.SafeHtml)1 BasicOverlayType (cz.metacentrum.perun.webgui.model.BasicOverlayType)1 ItemTexts (cz.metacentrum.perun.webgui.model.ItemTexts)1 AutoCompletionChoice (edu.stanford.bmir.gwtcodemirror.client.AutoCompletionChoice)1 AutoCompletionResult (edu.stanford.bmir.gwtcodemirror.client.AutoCompletionResult)1 EditorPosition (edu.stanford.bmir.gwtcodemirror.client.EditorPosition)1 CollectionId (edu.stanford.bmir.protege.web.shared.collection.CollectionId)1 CollectionItem (edu.stanford.bmir.protege.web.shared.collection.CollectionItem)1 FormId (edu.stanford.bmir.protege.web.shared.form.FormId)1 GetUserIdCompletionsAction (edu.stanford.bmir.protege.web.shared.itemlist.GetUserIdCompletionsAction)1