Search in sources :

Example 1 with IBlockTextSelection

use of org.eclipse.jface.text.IBlockTextSelection 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 2 with IBlockTextSelection

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

the class SourceViewer method rememberSelection.

/**
 * Remembers and returns the current selection. The saved selection can be restored
 * by calling <code>restoreSelection()</code>.
 *
 * @return the current selection
 * @see org.eclipse.jface.text.ITextViewer#getSelectedRange()
 * @since 3.0
 */
protected Point rememberSelection() {
    final ITextSelection selection = (ITextSelection) getSelection();
    final IDocument document = getDocument();
    if (fSelections.isEmpty()) {
        fSelectionCategory = _SELECTION_POSITION_CATEGORY + hashCode();
        fSelectionUpdater = new NonDeletingPositionUpdater(fSelectionCategory);
        document.addPositionCategory(fSelectionCategory);
        document.addPositionUpdater(fSelectionUpdater);
    }
    try {
        final Position position;
        if (selection instanceof IBlockTextSelection)
            position = new ColumnPosition(selection.getOffset(), selection.getLength(), ((IBlockTextSelection) selection).getStartColumn(), ((IBlockTextSelection) selection).getEndColumn());
        else
            position = new Position(selection.getOffset(), selection.getLength());
        document.addPosition(fSelectionCategory, position);
        fSelections.push(position);
    } catch (BadLocationException exception) {
    // Should not happen
    } catch (BadPositionCategoryException exception) {
    // Should not happen
    }
    return new Point(selection.getOffset(), selection.getLength());
}
Also used : Position(org.eclipse.jface.text.Position) NonDeletingPositionUpdater(org.eclipse.jface.internal.text.NonDeletingPositionUpdater) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IBlockTextSelection(org.eclipse.jface.text.IBlockTextSelection) Point(org.eclipse.swt.graphics.Point) ITextSelection(org.eclipse.jface.text.ITextSelection) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadLocationException (org.eclipse.jface.text.BadLocationException)2 IBlockTextSelection (org.eclipse.jface.text.IBlockTextSelection)2 IDocument (org.eclipse.jface.text.IDocument)2 ITextSelection (org.eclipse.jface.text.ITextSelection)2 NonDeletingPositionUpdater (org.eclipse.jface.internal.text.NonDeletingPositionUpdater)1 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)1 IRegion (org.eclipse.jface.text.IRegion)1 ITextViewerExtension (org.eclipse.jface.text.ITextViewerExtension)1 Position (org.eclipse.jface.text.Position)1 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)1 StyledText (org.eclipse.swt.custom.StyledText)1 Point (org.eclipse.swt.graphics.Point)1