Search in sources :

Example 86 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project erlide_eclipse by erlang.

the class FilteredModulesSelectionDialog method open.

@Override
public int open() {
    if (getInitialPattern() == null) {
        final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if (window != null) {
            final ISelection selection = window.getSelectionService().getSelection();
            if (selection instanceof ITextSelection) {
                String text = ((ITextSelection) selection).getText();
                if (text != null) {
                    text = text.trim();
                    if (!text.isEmpty()) {
                        final IWorkspace workspace = ResourcesPlugin.getWorkspace();
                        final IStatus result = workspace.validateName(text, IResource.FILE);
                        if (result.isOK()) {
                            setInitialPattern(text);
                        }
                    }
                }
            }
        }
    }
    return super.open();
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IStatus(org.eclipse.core.runtime.IStatus) IWorkspace(org.eclipse.core.resources.IWorkspace) ISelection(org.eclipse.jface.viewers.ISelection) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 87 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project erlide_eclipse by erlang.

the class IndentHandler method doAction.

@Override
protected void doAction(final ISelection sel, final ITextEditor textEditor) {
    final IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
    final ITextSelection selection = ErlangAbstractHandler.extendSelectionToWholeLines(document, (ITextSelection) sel);
    final ITextSelection getSelection = getTextSelection(document, selection, textEditor);
    String text;
    OtpErlangObject r1 = null;
    try {
        text = document.get(getSelection.getOffset(), getSelection.getLength());
        // call erlang, with selection within text
        r1 = callErlang(selection.getOffset() - getSelection.getOffset(), selection.getLength(), text);
    } catch (final Exception e) {
        ErlLogger.error(e);
    }
    final String newText = Util.stringValue(r1);
    if (newText == null) {
        final String msg = "call to " + getClass().getSimpleName() + " timed out; try a smaller selection.";
        final Status status = new Status(IStatus.ERROR, ErlangCore.PLUGIN_ID, ErlangStatus.INTERNAL_ERROR.getValue(), msg, null);
        ErlLogger.error("INTERNAL ERROR: " + msg);
        ErrorDialog.openError(textEditor.getSite().getShell(), ActionMessages.IndentAction_error_message, "Internal error", status);
        return;
    }
    final Display display = textEditor.getEditorSite().getShell().getDisplay();
    display.syncExec(() -> {
        final IRewriteTarget target = textEditor.getAdapter(IRewriteTarget.class);
        if (target != null) {
            target.beginCompoundChange();
            target.setRedraw(false);
        }
        try {
            if (!document.get(selection.getOffset(), selection.getLength()).equals(newText)) {
                document.replace(selection.getOffset(), selection.getLength(), newText);
            }
        } catch (final BadLocationException e) {
            ErlLogger.warn(e);
        }
        if (target != null) {
            target.endCompoundChange();
            target.setRedraw(true);
        }
    });
}
Also used : ErlangStatus(org.erlide.core.ErlangStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) IRewriteTarget(org.eclipse.jface.text.IRewriteTarget) IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException) RpcException(org.erlide.runtime.rpc.RpcException) BadLocationException(org.eclipse.jface.text.BadLocationException) Display(org.eclipse.swt.widgets.Display)

Example 88 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.

the class AbstractAutoEditTest method assertState.

protected void assertState(XtextEditor editor, String string) {
    int cursor = string.indexOf('|');
    assertEquals(string.replace("|", ""), editor.getDocument().get());
    ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
    assertEquals("unexpected cursor position:", cursor, selection.getOffset());
}
Also used : Point(org.eclipse.swt.graphics.Point) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 89 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.

the class AbstractCursorHandlingTest method assertState.

protected void assertState(String string, XtextEditor editor) {
    int cursor = string.indexOf('|');
    assertEquals(string.replace("|", ""), editor.getDocument().get());
    ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
    assertEquals("unexpected cursor position:", cursor, selection.getOffset());
}
Also used : ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 90 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.

the class AbstractEditorDoubleClickTextSelectionTest method assertSelectedText.

protected void assertSelectedText(AbstractTextEditor textEditor, CharSequence expectedSelectedText) {
    ISelection selection = textEditor.getSelectionProvider().getSelection();
    String actualSelectedText = ((ITextSelection) selection).getText();
    assertEquals(expectedSelectedText, actualSelectedText);
}
Also used : ISelection(org.eclipse.jface.viewers.ISelection) ITextSelection(org.eclipse.jface.text.ITextSelection)

Aggregations

ITextSelection (org.eclipse.jface.text.ITextSelection)205 ISelection (org.eclipse.jface.viewers.ISelection)65 IDocument (org.eclipse.jface.text.IDocument)52 BadLocationException (org.eclipse.jface.text.BadLocationException)46 IRegion (org.eclipse.jface.text.IRegion)34 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)34 Region (org.eclipse.jface.text.Region)31 IEditorPart (org.eclipse.ui.IEditorPart)29 ISelectionProvider (org.eclipse.jface.viewers.ISelectionProvider)25 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)21 ArrayList (java.util.ArrayList)18 TextSelection (org.eclipse.jface.text.TextSelection)17 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)14 Point (org.eclipse.swt.graphics.Point)13 IResource (org.eclipse.core.resources.IResource)12 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)12 IFile (org.eclipse.core.resources.IFile)11 StyledText (org.eclipse.swt.custom.StyledText)11 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)11 Template (org.eclipse.jface.text.templates.Template)10