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();
}
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());
}
Aggregations