Search in sources :

Example 96 with IRegion

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

the class CaseAction method run.

@Override
public void run() {
    ITextEditor editor = getTextEditor();
    if (editor == null)
        return;
    if (!validateEditorInputState())
        return;
    ISourceViewer viewer = ((AbstractTextEditor) editor).getSourceViewer();
    if (viewer == null)
        return;
    IDocument document = viewer.getDocument();
    if (document == null)
        return;
    StyledText st = viewer.getTextWidget();
    if (st == null)
        return;
    ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
    int adjustment = 0;
    try {
        if (JFaceTextUtil.isEmpty(viewer, selection))
            return;
        IRegion[] ranges = JFaceTextUtil.getCoveredRanges(viewer, selection);
        if (ranges.length > 1 && viewer instanceof ITextViewerExtension)
            ((ITextViewerExtension) viewer).getRewriteTarget().beginCompoundChange();
        for (int i = 0; i < ranges.length; i++) {
            IRegion region = ranges[i];
            String target = document.get(region.getOffset(), region.getLength());
            String replacement = (fToUpper ? target.toUpperCase() : target.toLowerCase());
            if (!target.equals(replacement)) {
                document.replace(region.getOffset(), region.getLength(), replacement);
                // https://bugs.eclipse.org/bugs/show_bug.cgi?id=145326: replacement might be larger than the original
                adjustment = replacement.length() - target.length();
            }
        }
        if (ranges.length > 1 && viewer instanceof ITextViewerExtension)
            ((ITextViewerExtension) viewer).getRewriteTarget().endCompoundChange();
    } catch (BadLocationException x) {
        // ignore and return
        return;
    }
    // reinstall selection and move it into view
    if (!(selection instanceof IBlockTextSelection))
        viewer.setSelectedRange(selection.getOffset(), selection.getLength() + adjustment);
    else
        viewer.getSelectionProvider().setSelection(selection);
    // don't use the viewer's reveal feature in order to avoid jumping around
    st.showSelection();
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) ITextSelection(org.eclipse.jface.text.ITextSelection) IRegion(org.eclipse.jface.text.IRegion) ITextViewerExtension(org.eclipse.jface.text.ITextViewerExtension) IBlockTextSelection(org.eclipse.jface.text.IBlockTextSelection) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 97 with IRegion

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

the class FindReplaceDialog method useSelectedLines.

/**
 * Tells the dialog to perform searches only in the scope given by the actually selected lines.
 * @param selectedLines <code>true</code> if selected lines should be used
 * @since 2.0
 */
private void useSelectedLines(boolean selectedLines) {
    if (isIncrementalSearch() && !isRegExSearchAvailableAndChecked())
        initIncrementalBaseLocation();
    if (fTarget == null || !(fTarget instanceof IFindReplaceTargetExtension))
        return;
    IFindReplaceTargetExtension extensionTarget = (IFindReplaceTargetExtension) fTarget;
    if (selectedLines) {
        IRegion scope;
        if (fOldScope == null) {
            Point lineSelection = extensionTarget.getLineSelection();
            scope = new Region(lineSelection.x, lineSelection.y);
        } else {
            scope = fOldScope;
            fOldScope = null;
        }
        int offset = isForwardSearch() ? scope.getOffset() : scope.getOffset() + scope.getLength();
        extensionTarget.setSelection(offset, 0);
        extensionTarget.setScope(scope);
    } else {
        fOldScope = extensionTarget.getScope();
        extensionTarget.setScope(null);
    }
}
Also used : IRegion(org.eclipse.jface.text.IRegion) Region(org.eclipse.jface.text.Region) Point(org.eclipse.swt.graphics.Point) IFindReplaceTargetExtension(org.eclipse.jface.text.IFindReplaceTargetExtension) IRegion(org.eclipse.jface.text.IRegion) Point(org.eclipse.swt.graphics.Point)

Example 98 with IRegion

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

the class TextViewerDeleteLineTarget method deleteLine.

/**
 * Deletes the lines that intersect with the given <code>selection</code>.
 *
 * @param document the document
 * @param selection the selection to use to determine the document range to delete
 * @param type the line deletion type, must be one of
 * 	<code>WHOLE_LINE</code>, <code>TO_BEGINNING</code> or <code>TO_END</code>
 * @param copyToClipboard <code>true</code> if the deleted line should be copied to the clipboard
 * @throws BadLocationException if position is not valid in the given document
 * @since 3.5
 */
public void deleteLine(IDocument document, ITextSelection selection, int type, boolean copyToClipboard) throws BadLocationException {
    IRegion deleteRegion = getDeleteRegion(document, selection, type);
    int deleteOffset = deleteRegion.getOffset();
    int deleteLength = deleteRegion.getLength();
    if (deleteLength == 0)
        return;
    if (copyToClipboard) {
        fClipboard.checkState();
        try {
            fClipboard.append(document.get(deleteOffset, deleteLength));
        } catch (SWTError e) {
            if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD)
                throw e;
            // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=59459
            // don't delete if copy to clipboard fails, rather log & abort
            // log
            Status status = new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, e.code, EditorMessages.Editor_error_clipboard_copy_failed_message, e);
            TextEditorPlugin.getDefault().getLog().log(status);
            fClipboard.uninstall();
            // don't delete
            return;
        }
        fClipboard.setDeleting(true);
        // $NON-NLS-1$
        document.replace(deleteOffset, deleteLength, "");
        fClipboard.setDeleting(false);
        fClipboard.saveState();
    } else {
        // $NON-NLS-1$
        document.replace(deleteOffset, deleteLength, "");
    }
}
Also used : SWTError(org.eclipse.swt.SWTError) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IRegion(org.eclipse.jface.text.IRegion)

Example 99 with IRegion

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

the class TextViewerDeleteLineTarget method getDeleteRegion.

/**
 * Returns the document's delete region according to <code>selection</code> and <code>type</code>.
 *
 * @param document the document
 * @param selection the selection
 * @param type the line deletion type, must be one of <code>WHOLE_LINE</code>,
 *            <code>TO_BEGINNING</code> or <code>TO_END</code>
 * @return the document's delete region
 * @throws BadLocationException if the document is accessed with invalid offset or line
 */
private IRegion getDeleteRegion(IDocument document, ITextSelection selection, int type) throws BadLocationException {
    int offset = selection.getOffset();
    int line = selection.getStartLine();
    int resultOffset = 0;
    int resultLength = 0;
    switch(type) {
        case DeleteLineAction.WHOLE:
            resultOffset = document.getLineOffset(line);
            int endLine = selection.getEndLine();
            resultLength = document.getLineOffset(endLine) + document.getLineLength(endLine) - resultOffset;
            break;
        case DeleteLineAction.TO_BEGINNING:
            resultOffset = document.getLineOffset(line);
            resultLength = offset - resultOffset;
            break;
        case DeleteLineAction.TO_END:
            resultOffset = offset;
            IRegion lineRegion = document.getLineInformation(line);
            int end = lineRegion.getOffset() + lineRegion.getLength();
            if (offset == end) {
                String lineDelimiter = document.getLineDelimiter(line);
                resultLength = lineDelimiter == null ? 0 : lineDelimiter.length();
            } else {
                resultLength = end - resultOffset;
            }
            break;
        default:
            throw new IllegalArgumentException();
    }
    return clipToVisibleRegion(resultOffset, resultOffset + resultLength);
}
Also used : IRegion(org.eclipse.jface.text.IRegion)

Example 100 with IRegion

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

the class SpellingReconcileStrategy method reconcile.

@Override
public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
    try {
        IRegion startLineInfo = fDocument.getLineInformationOfOffset(subRegion.getOffset());
        IRegion endLineInfo = fDocument.getLineInformationOfOffset(subRegion.getOffset() + Math.max(0, subRegion.getLength() - 1));
        if (startLineInfo.getOffset() == endLineInfo.getOffset())
            subRegion = startLineInfo;
        else
            subRegion = new Region(startLineInfo.getOffset(), endLineInfo.getOffset() + Math.max(0, endLineInfo.getLength() - 1) - startLineInfo.getOffset());
    } catch (BadLocationException e) {
        subRegion = new Region(0, fDocument.getLength());
    }
    reconcile(subRegion);
}
Also used : Region(org.eclipse.jface.text.Region) DirtyRegion(org.eclipse.jface.text.reconciler.DirtyRegion) IRegion(org.eclipse.jface.text.IRegion) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

IRegion (org.eclipse.jface.text.IRegion)341 BadLocationException (org.eclipse.jface.text.BadLocationException)158 Region (org.eclipse.jface.text.Region)151 Test (org.junit.Test)94 IDocument (org.eclipse.jface.text.IDocument)68 Point (org.eclipse.swt.graphics.Point)49 Position (org.eclipse.jface.text.Position)38 ITextViewerExtension5 (org.eclipse.jface.text.ITextViewerExtension5)20 ITypedRegion (org.eclipse.jface.text.ITypedRegion)20 FindReplaceDocumentAdapter (org.eclipse.jface.text.FindReplaceDocumentAdapter)18 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)17 IEditorPart (org.eclipse.ui.IEditorPart)13 ArrayList (java.util.ArrayList)11 Document (org.eclipse.jface.text.Document)11 StyledText (org.eclipse.swt.custom.StyledText)10 ITextSelection (org.eclipse.jface.text.ITextSelection)9 IFile (org.eclipse.core.resources.IFile)8 Annotation (org.eclipse.jface.text.source.Annotation)7 HashMap (java.util.HashMap)6 CoreException (org.eclipse.core.runtime.CoreException)6