use of org.eclipse.jface.text.ITextSelection in project eclipse.platform.text by eclipse.
the class AbstractTextEditor method doSetSelection.
/**
* Sets the given selection.
*
* @param selection the selection
* @since 2.1
*/
protected void doSetSelection(ISelection selection) {
if (selection instanceof ITextSelection) {
ITextSelection textSelection = (ITextSelection) selection;
selectAndReveal(textSelection.getOffset(), textSelection.getLength());
}
}
use of org.eclipse.jface.text.ITextSelection 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.ITextSelection in project eclipse.platform.text by eclipse.
the class DeleteLineAction method run.
@Override
public void run() {
if (fTarget == null)
return;
ITextEditor editor = getTextEditor();
if (editor == null)
return;
if (!validateEditorInputState())
return;
IDocument document = getDocument(editor);
if (document == null)
return;
ITextSelection selection = getSelection(editor);
if (selection == null)
return;
try {
if (fTarget instanceof TextViewerDeleteLineTarget)
((TextViewerDeleteLineTarget) fTarget).deleteLine(document, selection, fType, fCopyToClipboard);
else
fTarget.deleteLine(document, selection.getOffset(), selection.getLength(), fType, fCopyToClipboard);
} catch (BadLocationException e) {
// should not happen
}
}
use of org.eclipse.jface.text.ITextSelection in project eclipse.platform.text by eclipse.
the class DeleteLineAction method getSelection.
/**
* Returns the editor's selection.
*
* @param editor the editor
* @return the editor's selection
*/
private static ITextSelection getSelection(ITextEditor editor) {
ISelectionProvider selectionProvider = editor.getSelectionProvider();
if (selectionProvider == null)
return null;
ISelection selection = selectionProvider.getSelection();
if (!(selection instanceof ITextSelection))
return null;
return (ITextSelection) selection;
}
use of org.eclipse.jface.text.ITextSelection in project eclipse.platform.text by eclipse.
the class AbstractDecoratedTextEditor method editorContextMenuAboutToShow.
@Override
protected void editorContextMenuAboutToShow(IMenuManager menu) {
super.editorContextMenuAboutToShow(menu);
IAction preferencesAction = getAction(ITextEditorActionConstants.CONTEXT_PREFERENCES);
menu.appendToGroup(IWorkbenchActionConstants.MB_ADDITIONS, new Separator(ITextEditorActionConstants.GROUP_SETTINGS));
menu.appendToGroup(ITextEditorActionConstants.GROUP_SETTINGS, preferencesAction);
menu.appendToGroup(ITextEditorActionConstants.GROUP_SAVE, new Separator(ITextEditorActionConstants.GROUP_OPEN));
IEditorInput editorInput = getEditorInput();
if ((editorInput.getAdapter(IResource.class)) instanceof IFile) {
MenuManager openWithSubMenu = new MenuManager(TextEditorMessages.AbstractDecoratedTextEditor_openWith_menu);
final IWorkbenchPage page = getEditorSite().getPage();
// XXX: Internal reference will get fixed during 3.7, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=307026
openWithSubMenu.add(new OpenWithMenu(page, editorInput) {
@Override
protected void openEditor(IEditorDescriptor editorDescriptor, boolean openUsingDescriptor) {
super.openEditor(editorDescriptor, openUsingDescriptor);
ISelection selection = getSelectionProvider().getSelection();
if (selection instanceof ITextSelection) {
revealInEditor(page.getActiveEditor(), ((ITextSelection) selection).getOffset(), ((ITextSelection) selection).getLength());
}
}
});
menu.appendToGroup(ITextEditorActionConstants.GROUP_OPEN, openWithSubMenu);
}
MenuManager showInSubMenu = new MenuManager(getShowInMenuLabel());
showInSubMenu.add(ContributionItemFactory.VIEWS_SHOW_IN.create(getEditorSite().getWorkbenchWindow()));
menu.appendToGroup(ITextEditorActionConstants.GROUP_OPEN, showInSubMenu);
}
Aggregations