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