Search in sources :

Example 6 with BadLocationException

use of org.eclipse.che.ide.api.editor.text.BadLocationException 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 7 with BadLocationException

use of org.eclipse.che.ide.api.editor.text.BadLocationException in project che by eclipse.

the class ListLineTracker method getLineNumberOfOffset.

/* @see org.eclipse.jface.text.ILineTracker#getLineNumberOfOffset(int) */
public final int getLineNumberOfOffset(int position) throws BadLocationException {
    if (position < 0 || position > fTextLength)
        throw new BadLocationException();
    if (position == fTextLength) {
        int lastLine = fLines.size() - 1;
        if (lastLine < 0)
            return 0;
        Line l = (Line) fLines.get(lastLine);
        return (l.delimiter != null ? lastLine + 1 : lastLine);
    }
    return findLine(position);
}
Also used : BadLocationException(org.eclipse.che.ide.api.editor.text.BadLocationException)

Example 8 with BadLocationException

use of org.eclipse.che.ide.api.editor.text.BadLocationException in project che by eclipse.

the class ListLineTracker method getLineOffset.

/* @see org.eclipse.jface.text.ILineTracker#getLineOffset(int) */
public final int getLineOffset(int line) throws BadLocationException {
    int lines = fLines.size();
    if (line < 0 || line > lines)
        throw new BadLocationException();
    if (lines == 0)
        return 0;
    if (line == lines) {
        Line l = (Line) fLines.get(line - 1);
        if (l.delimiter != null)
            return l.offset + l.length;
        throw new BadLocationException();
    }
    Line l = (Line) fLines.get(line);
    return l.offset;
}
Also used : BadLocationException(org.eclipse.che.ide.api.editor.text.BadLocationException)

Example 9 with BadLocationException

use of org.eclipse.che.ide.api.editor.text.BadLocationException in project che by eclipse.

the class ListLineTracker method getLineInformationOfOffset.

/* @see org.eclipse.jface.text.ILineTracker#getLineInformationOfOffset(int) */
public final Region getLineInformationOfOffset(int position) throws BadLocationException {
    if (position > fTextLength)
        throw new BadLocationException();
    if (position == fTextLength) {
        int size = fLines.size();
        if (size == 0)
            return new RegionImpl(0, 0);
        Line l = (Line) fLines.get(size - 1);
        return (l.delimiter != null ? new Line(fTextLength, 0) : new Line(fTextLength - l.length, l.length));
    }
    return getLineInformation(findLine(position));
}
Also used : RegionImpl(org.eclipse.che.ide.api.editor.text.RegionImpl) BadLocationException(org.eclipse.che.ide.api.editor.text.BadLocationException)

Example 10 with BadLocationException

use of org.eclipse.che.ide.api.editor.text.BadLocationException in project che by eclipse.

the class ListLineTracker method getLineDelimiter.

/* @see org.eclipse.jface.text.ILineTracker#getLineDelimiter(int) */
public final String getLineDelimiter(int line) throws BadLocationException {
    int lines = fLines.size();
    if (line < 0 || line > lines)
        throw new BadLocationException();
    if (lines == 0)
        return null;
    if (line == lines)
        return null;
    Line l = (Line) fLines.get(line);
    return l.delimiter;
}
Also used : BadLocationException(org.eclipse.che.ide.api.editor.text.BadLocationException)

Aggregations

BadLocationException (org.eclipse.che.ide.api.editor.text.BadLocationException)11 BadPositionCategoryException (org.eclipse.che.ide.api.editor.text.BadPositionCategoryException)3 Position (org.eclipse.che.ide.api.editor.text.Position)2 RegionImpl (org.eclipse.che.ide.api.editor.text.RegionImpl)2 TypedPosition (org.eclipse.che.ide.api.editor.text.TypedPosition)2 MatchResult (com.google.gwt.regexp.shared.MatchResult)1 TypedRegionImpl (org.eclipse.che.ide.api.editor.text.TypedRegionImpl)1 Token (org.eclipse.che.ide.api.editor.text.rules.Token)1