Search in sources :

Example 26 with ITextSelection

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());
    }
}
Also used : ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 27 with ITextSelection

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();
}
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 28 with ITextSelection

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
    }
}
Also used : IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 29 with ITextSelection

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;
}
Also used : ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) ISelection(org.eclipse.jface.viewers.ISelection) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 30 with ITextSelection

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);
}
Also used : IFile(org.eclipse.core.resources.IFile) IAction(org.eclipse.jface.action.IAction) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) MenuManager(org.eclipse.jface.action.MenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) ISelection(org.eclipse.jface.viewers.ISelection) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) OpenWithMenu(org.eclipse.ui.actions.OpenWithMenu) Separator(org.eclipse.jface.action.Separator) IEditorInput(org.eclipse.ui.IEditorInput) IURIEditorInput(org.eclipse.ui.IURIEditorInput) ITextSelection(org.eclipse.jface.text.ITextSelection)

Aggregations

ITextSelection (org.eclipse.jface.text.ITextSelection)200 ISelection (org.eclipse.jface.viewers.ISelection)64 IDocument (org.eclipse.jface.text.IDocument)50 BadLocationException (org.eclipse.jface.text.BadLocationException)45 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)34 IRegion (org.eclipse.jface.text.IRegion)33 Region (org.eclipse.jface.text.Region)29 IEditorPart (org.eclipse.ui.IEditorPart)29 ISelectionProvider (org.eclipse.jface.viewers.ISelectionProvider)26 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)20 ArrayList (java.util.ArrayList)18 TextSelection (org.eclipse.jface.text.TextSelection)16 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)14 IResource (org.eclipse.core.resources.IResource)12 IFile (org.eclipse.core.resources.IFile)11 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)11 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)11 Template (org.eclipse.jface.text.templates.Template)10 TemplateContext (org.eclipse.jface.text.templates.TemplateContext)10 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)10