Search in sources :

Example 21 with IRegion

use of org.eclipse.jface.text.IRegion in project che by eclipse.

the class IndentUtil method getCurrentIndent.

/**
	 * Returns the indentation of the line <code>line</code> in <code>document</code>.
	 * The returned string may contain pairs of leading slashes that are considered
	 * part of the indentation. The space before the asterix in a javadoc-like
	 * comment is not considered part of the indentation.
	 *
	 * @param document the document
	 * @param line the line
	 * @return the indentation of <code>line</code> in <code>document</code>
	 * @throws BadLocationException if the document is changed concurrently
	 */
private static String getCurrentIndent(IDocument document, int line) throws BadLocationException {
    IRegion region = document.getLineInformation(line);
    int from = region.getOffset();
    int endOffset = region.getOffset() + region.getLength();
    // go behind line comments
    int to = from;
    while (to < endOffset - 2 && document.get(to, 2).equals(SLASHES)) to += 2;
    while (to < endOffset) {
        char ch = document.getChar(to);
        if (!Character.isWhitespace(ch))
            break;
        to++;
    }
    // don't count the space before javadoc like, asterix-style comment lines
    if (to > from && to < endOffset - 1 && document.get(to - 1, 2).equals(" *")) {
        //$NON-NLS-1$
        String type = TextUtilities.getContentType(document, IJavaPartitions.JAVA_PARTITIONING, to, true);
        if (type.equals(IJavaPartitions.JAVA_DOC) || type.equals(IJavaPartitions.JAVA_MULTI_LINE_COMMENT))
            to--;
    }
    return document.get(from, to - from);
}
Also used : IRegion(org.eclipse.jface.text.IRegion)

Example 22 with IRegion

use of org.eclipse.jface.text.IRegion in project che by eclipse.

the class IndentUtil method cutIndent.

/**
	 * Cuts the visual equivalent of <code>toDelete</code> characters out of the
	 * indentation of line <code>line</code> in <code>document</code>. Leaves
	 * leading comment signs alone.
	 *
	 * @param document the document
	 * @param line the line
	 * @param toDelete the number of space equivalents to delete.
	 * @throws BadLocationException on concurrent document modification
	 */
private static void cutIndent(IDocument document, int line, int toDelete, int tabSize, boolean[] commentLines, int relative) throws BadLocationException {
    IRegion region = document.getLineInformation(line);
    int from = region.getOffset();
    int endOffset = region.getOffset() + region.getLength();
    // go behind line comments
    while (from < endOffset - 2 && document.get(from, 2).equals(SLASHES)) from += 2;
    int to = from;
    while (toDelete > 0 && to < endOffset) {
        char ch = document.getChar(to);
        if (!Character.isWhitespace(ch))
            break;
        toDelete -= computeVisualLength(ch, tabSize);
        if (toDelete >= 0)
            to++;
        else
            break;
    }
    if (endOffset > to + 1 && document.get(to, 2).equals(SLASHES))
        commentLines[relative] = true;
    document.replace(from, to - from, null);
}
Also used : IRegion(org.eclipse.jface.text.IRegion)

Example 23 with IRegion

use of org.eclipse.jface.text.IRegion in project che by eclipse.

the class JavaIndenter method getLeadingWhitespace.

/**
	 * Returns the indentation of the line at <code>offset</code> as a
	 * <code>StringBuffer</code>. If the offset is not valid, the empty string
	 * is returned.
	 *
	 * @param offset the offset in the document
	 * @return the indentation (leading whitespace) of the line in which
	 * 		   <code>offset</code> is located
	 */
private StringBuffer getLeadingWhitespace(int offset) {
    StringBuffer indent = new StringBuffer();
    try {
        IRegion line = fDocument.getLineInformationOfOffset(offset);
        int lineOffset = line.getOffset();
        int nonWS = fScanner.findNonWhitespaceForwardInAnyPartition(lineOffset, lineOffset + line.getLength());
        indent.append(fDocument.get(lineOffset, nonWS - lineOffset));
        return indent;
    } catch (BadLocationException e) {
        return indent;
    }
}
Also used : IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 24 with IRegion

use of org.eclipse.jface.text.IRegion in project che by eclipse.

the class TextChange method getPreviewContent.

/**
	 * Returns a preview of the text change clipped to a specific region.
	 * The preview is created by applying the text edits managed by the
	 * given array of {@link TextEditChangeGroup text edit change groups}.
	 * The region is determined as follows:
	 * <ul>
	 *   <li>if <code>expandRegionToFullLine</code> is <code>false</code>
	 *       then the parameter <code>region</code> determines the clipping.
	 *   </li>
	 *   <li>if <code>expandRegionToFullLine</code> is <code>true</code>
	 *       then the region determined by the parameter <code>region</code>
	 *       is extended to cover full lines.
	 *   </li>
	 *   <li>if <code>surroundingLines</code> &gt; 0 then the given number
	 *       of surrounding lines is added. The value of <code>surroundingLines
	 *       </code> is only considered if <code>expandRegionToFullLine</code>
	 *       is <code>true</code>
	 *   </li>
	 * </ul>
	 *
	 * @param changeGroups a set of change groups for which a preview is to be
	 *  generated
	 * @param region the starting region for the clipping
	 * @param expandRegionToFullLine if <code>true</code> is passed the region
	 *  is extended to cover full lines
	 * @param surroundingLines the number of surrounding lines to be added to
	 *  the clipping region. Is only considered if <code>expandRegionToFullLine
	 *  </code> is <code>true</code>
	 * @param pm a progress monitor to report progress or <code>null</code>
	 *  if no progress reporting is desired
	 *
	 * @return the current content of the text change clipped to a region
	 *  determined by the given parameters.
	 *
	 * @throws CoreException if an exception occurs while generating the preview
	 *
	 * @see #getCurrentContent(IRegion, boolean, int, IProgressMonitor)
	 *
	 * @since 3.2
	 */
public String getPreviewContent(TextEditBasedChangeGroup[] changeGroups, IRegion region, boolean expandRegionToFullLine, int surroundingLines, IProgressMonitor pm) throws CoreException {
    IRegion currentRegion = getRegion(changeGroups);
    Assert.isTrue(region.getOffset() <= currentRegion.getOffset() && currentRegion.getOffset() + currentRegion.getLength() <= region.getOffset() + region.getLength());
    // Make sure that all edits in the change groups are rooted under the edit the text change stand for.
    TextEdit root = getEdit();
    //$NON-NLS-1$
    Assert.isNotNull(root, "No root edit");
    for (int c = 0; c < changeGroups.length; c++) {
        TextEditBasedChangeGroup group = changeGroups[c];
        TextEdit[] edits = group.getTextEdits();
        for (int e = 0; e < edits.length; e++) {
        // TODO: enable once following bug is fixed
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=130909
        // Assert.isTrue(root == edits[e].getRoot(), "Wrong root edit"); //$NON-NLS-1$
        }
    }
    PreviewAndRegion result = getPreviewDocument(changeGroups, pm);
    int delta;
    if (result.region == null) {
        // all edits were delete edits so no new region
        delta = -currentRegion.getLength();
    } else {
        delta = result.region.getLength() - currentRegion.getLength();
    }
    return getContent(result.document, new Region(region.getOffset(), region.getLength() + delta), expandRegionToFullLine, surroundingLines);
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IRegion(org.eclipse.jface.text.IRegion)

Example 25 with IRegion

use of org.eclipse.jface.text.IRegion in project che by eclipse.

the class TextEditBasedChange method getContent.

String getContent(IDocument document, IRegion region, boolean expandRegionToFullLine, int surroundingLines) throws CoreException {
    try {
        if (expandRegionToFullLine) {
            int startLine = Math.max(document.getLineOfOffset(region.getOffset()) - surroundingLines, 0);
            int endLine;
            if (region.getLength() == 0) {
                // or else spurious changes show up that look like deletes from the source
                if (surroundingLines == 0) {
                    //$NON-NLS-1$
                    return "";
                }
                endLine = Math.min(document.getLineOfOffset(region.getOffset()) + surroundingLines - 1, document.getNumberOfLines() - 1);
            } else {
                endLine = Math.min(document.getLineOfOffset(region.getOffset() + region.getLength() - 1) + surroundingLines, document.getNumberOfLines() - 1);
            }
            int offset = document.getLineInformation(startLine).getOffset();
            IRegion endLineRegion = document.getLineInformation(endLine);
            int length = endLineRegion.getOffset() + endLineRegion.getLength() - offset;
            return document.get(offset, length);
        } else {
            return document.get(region.getOffset(), region.getLength());
        }
    } catch (BadLocationException e) {
        throw Changes.asCoreException(e);
    }
}
Also used : IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

IRegion (org.eclipse.jface.text.IRegion)668 BadLocationException (org.eclipse.jface.text.BadLocationException)291 Region (org.eclipse.jface.text.Region)265 IDocument (org.eclipse.jface.text.IDocument)143 Test (org.junit.Test)108 Point (org.eclipse.swt.graphics.Point)76 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)57 ITypedRegion (org.eclipse.jface.text.ITypedRegion)55 Position (org.eclipse.jface.text.Position)50 ArrayList (java.util.ArrayList)37 ITextViewerExtension5 (org.eclipse.jface.text.ITextViewerExtension5)35 IFile (org.eclipse.core.resources.IFile)33 ITextSelection (org.eclipse.jface.text.ITextSelection)32 IEditorPart (org.eclipse.ui.IEditorPart)29 FindReplaceDocumentAdapter (org.eclipse.jface.text.FindReplaceDocumentAdapter)24 StyledText (org.eclipse.swt.custom.StyledText)22 CoreException (org.eclipse.core.runtime.CoreException)19 List (java.util.List)18 Document (org.eclipse.jface.text.Document)17 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)17