Search in sources :

Example 6 with ITextSelection

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

the class AbstractOpenHierarchyHandler method execute.

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    Object _xblockexpression = null;
    {
        final XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
        Object _xifexpression = null;
        if ((editor != null)) {
            Object _xblockexpression_1 = null;
            {
                final ISelection selection = editor.getSelectionProvider().getSelection();
                Object _xifexpression_1 = null;
                if ((selection instanceof ITextSelection)) {
                    Object _xblockexpression_2 = null;
                    {
                        final IWorkbenchWindow workbenchWindow = editor.getSite().getWorkbenchWindow();
                        final IUnitOfWork<Object, XtextResource> _function = (XtextResource it) -> {
                            Object _xblockexpression_3 = null;
                            {
                                this.openHierarchy(this._eObjectAtOffsetHelper.resolveElementAt(it, ((ITextSelection) selection).getOffset()), workbenchWindow);
                                _xblockexpression_3 = null;
                            }
                            return _xblockexpression_3;
                        };
                        _xblockexpression_2 = editor.getDocument().<Object>priorityReadOnly(_function);
                    }
                    _xifexpression_1 = _xblockexpression_2;
                }
                _xblockexpression_1 = _xifexpression_1;
            }
            _xifexpression = _xblockexpression_1;
        }
        _xblockexpression = _xifexpression;
    }
    return _xblockexpression;
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) ISelection(org.eclipse.jface.viewers.ISelection) EObject(org.eclipse.emf.ecore.EObject) XtextResource(org.eclipse.xtext.resource.XtextResource) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 7 with ITextSelection

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

the class AbstractJvmElementHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
    if (editor != null) {
        final ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
        editor.getDocument().priorityReadOnly(new IUnitOfWork<Void, XtextResource>() {

            @Override
            public java.lang.Void exec(XtextResource resource) throws Exception {
                JvmIdentifiableElement jvmIdentifiable = jvmElementAtOffsetHelper.getJvmIdentifiableElement(resource, selection.getOffset());
                if (jvmIdentifiable != null) {
                    IJavaElement javaType = javaElementFinder.findElementFor(jvmIdentifiable);
                    if (javaType != null)
                        openPresentation(editor, javaType, jvmIdentifiable);
                }
                return null;
            }
        });
    }
    return null;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) XtextResource(org.eclipse.xtext.resource.XtextResource) ITextSelection(org.eclipse.jface.text.ITextSelection) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 8 with ITextSelection

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

the class ToggleSLCommentAction method isSelectionCommented.

/**
 * Is the given selection single-line commented?
 *
 * @param selection Selection to check
 * @return <code>true</code> iff all selected lines are commented
 * @since 2.1
 */
protected boolean isSelectionCommented(ISelection selection) {
    if (!(selection instanceof ITextSelection))
        return false;
    ITextSelection textSelection = (ITextSelection) selection;
    if (textSelection.getStartLine() < 0 || textSelection.getEndLine() < 0)
        return false;
    IDocument document = getTextEditor().getDocumentProvider().getDocument(getTextEditor().getEditorInput());
    try {
        IRegion block = getTextBlockFromSelection(textSelection, document);
        ITypedRegion[] regions = TextUtilities.computePartitioning(document, fDocumentPartitioning, block.getOffset(), block.getLength(), false);
        // [startline, endline, startline, endline, ...]
        int[] lines = new int[regions.length * 2];
        for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
            // start line of region
            lines[j] = getFirstCompleteLineOfRegion(regions[i], document);
            // end line of region
            int length = regions[i].getLength();
            int offset = regions[i].getOffset() + length;
            if (length > 0)
                offset--;
            lines[j + 1] = (lines[j] == -1 ? -1 : document.getLineOfOffset(offset));
        }
        // Perform the check
        for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
            String[] prefixes = fPrefixesMap.get(regions[i].getType());
            if (prefixes != null && prefixes.length > 0 && lines[j] >= 0 && lines[j + 1] >= 0)
                if (!isBlockCommented(lines[j], lines[j + 1], prefixes, document))
                    return false;
        }
        return true;
    } catch (BadLocationException x) {
        // should not happen
        log.error(x.getMessage(), x);
    }
    return false;
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) ITextSelection(org.eclipse.jface.text.ITextSelection) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 9 with ITextSelection

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

the class OutlineWithEditorLinker method selectInTreeView.

protected void selectInTreeView(ISelection selection) {
    if (selection instanceof ITextSelection && !treeViewer.getTree().isDisposed()) {
        ITextSelection textSelection = (ITextSelection) selection;
        ITextRegion selectedTextRegion = new TextRegion(textSelection.getOffset(), textSelection.getLength());
        Object input = treeViewer.getInput();
        if (input instanceof IOutlineNode) {
            try {
                IOutlineNode nodeToBeSelected = findBestNode((IOutlineNode) input, selectedTextRegion);
                if (nodeToBeSelected != null)
                    treeViewer.setSelection(new StructuredSelection(nodeToBeSelected));
            } catch (Exception exc) {
            // ignore, editor can have a different state than the tree
            }
        }
    }
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ITextSelection(org.eclipse.jface.text.ITextSelection) IOutlineNode(org.eclipse.xtext.ui.editor.outline.IOutlineNode)

Example 10 with ITextSelection

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

the class ExpressionUtil method trimSelection.

protected ITextSelection trimSelection(XtextResource resource, ITextSelection selection) {
    IParseResult parseResult = resource.getParseResult();
    if (parseResult != null) {
        String model = parseResult.getRootNode().getText();
        String selectedText = model.substring(selection.getOffset(), selection.getOffset() + selection.getLength());
        String trimmedSelection = selectedText.trim();
        return new TextSelection(selection.getOffset() + selectedText.indexOf(trimmedSelection), trimmedSelection.length());
    }
    return null;
}
Also used : ITextSelection(org.eclipse.jface.text.ITextSelection) TextSelection(org.eclipse.jface.text.TextSelection) IParseResult(org.eclipse.xtext.parser.IParseResult)

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