Search in sources :

Example 36 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project eclipse.platform.text by eclipse.

the class ProjectionViewer method handleVerifyEvent.

@Override
protected void handleVerifyEvent(VerifyEvent e) {
    if (getTextWidget().getBlockSelection()) {
        ITextSelection selection = (ITextSelection) getSelection();
        if (exposeModelRange(new Region(selection.getOffset(), selection.getLength())))
            setSelection(selection);
        super.handleVerifyEvent(e);
        return;
    }
    Point selection = getSelectedRange();
    IRegion modelRange = event2ModelRange(e);
    if (exposeModelRange(modelRange)) {
        e.doit = false;
        try {
            if (selection.y == 0 && e.text.length() <= 1 && modelRange.getLength() == 1) {
                selection.y = 1;
                if (selection.x - 1 == modelRange.getOffset())
                    selection.x--;
            }
            getDocument().replace(selection.x, selection.y, e.text);
            setSelectedRange(selection.x + e.text.length(), 0);
        } catch (BadLocationException e1) {
        // ignore as nothing bad happens (no log at this level)
        }
    } else
        super.handleVerifyEvent(e);
}
Also used : Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) Point(org.eclipse.swt.graphics.Point) ITextSelection(org.eclipse.jface.text.ITextSelection) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 37 with ITextSelection

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

the class DerivedSourceView method mapTextRegion.

private TextRegion mapTextRegion(IWorkbenchPartSelection workbenchPartSelection) {
    ITextSelection textSelection = (ITextSelection) workbenchPartSelection.getSelection();
    TextRegion localRegion = new TextRegion(textSelection.getOffset(), textSelection.getLength());
    return localRegion;
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 38 with ITextSelection

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

the class XtendOutlineWithEditorLinker method selectInTreeView.

@Override
public void selectInTreeView(final ISelection selection) {
    if (((selection instanceof ITextSelection) && (!this.treeViewer.getTree().isDisposed()))) {
        final ITextSelection textSelection = ((ITextSelection) selection);
        int _offset = textSelection.getOffset();
        int _length = textSelection.getLength();
        final TextRegion selectedTextRegion = new TextRegion(_offset, _length);
        final Object input = this.treeViewer.getInput();
        if ((input instanceof IOutlineNode)) {
            final List<IOutlineNode> matchingNodes = CollectionLiterals.<IOutlineNode>newArrayList();
            this.findNodesInRange(((IOutlineNode) input), selectedTextRegion, matchingNodes);
            boolean _isEmpty = matchingNodes.isEmpty();
            boolean _not = (!_isEmpty);
            if (_not) {
                final Function1<IOutlineNode, Integer> _function = (IOutlineNode it) -> {
                    return Integer.valueOf(it.getFullTextRegion().getLength());
                };
                final Integer smallestMatch = IterableExtensions.<Integer>min(ListExtensions.<IOutlineNode, Integer>map(matchingNodes, _function));
                final Function1<IOutlineNode, Boolean> _function_1 = (IOutlineNode it) -> {
                    int _length_1 = it.getFullTextRegion().getLength();
                    return Boolean.valueOf((_length_1 == (smallestMatch).intValue()));
                };
                final Iterable<IOutlineNode> nodesToBeSelected = IterableExtensions.<IOutlineNode>filter(matchingNodes, _function_1);
                boolean _isEmpty_1 = IterableExtensions.isEmpty(nodesToBeSelected);
                boolean _not_1 = (!_isEmpty_1);
                if (_not_1) {
                    Object[] _array = IterableExtensions.<IOutlineNode>toList(nodesToBeSelected).toArray();
                    StructuredSelection _structuredSelection = new StructuredSelection(_array);
                    this.treeViewer.setSelection(_structuredSelection);
                }
            }
        }
    }
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion) TextRegion(org.eclipse.xtext.util.TextRegion) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) ITextSelection(org.eclipse.jface.text.ITextSelection) IOutlineNode(org.eclipse.xtext.ui.editor.outline.IOutlineNode)

Example 39 with ITextSelection

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

the class RichStringAwareSourceViewer method shift.

@SuppressWarnings("deprecation")
@Override
protected void shift(boolean useDefaultPrefixes, boolean right, boolean ignoreWhitespace) {
    if (useDefaultPrefixes && ignoreWhitespace) {
        // let's assume we toggled comments
        if (fUndoManager != null)
            fUndoManager.beginCompoundChange();
        IDocument d = getDocument();
        @SuppressWarnings("rawtypes") Map partitioners = null;
        DocumentRewriteSession rewriteSession = null;
        try {
            ITextSelection selection = (ITextSelection) getSelection();
            IRegion block = copiedGetTextBlockFromSelection(selection);
            ITypedRegion[] regions = TextUtilities.computePartitioning(d, getDocumentPartitioning(), block.getOffset(), block.getLength(), false);
            regions = merger.merge(regions);
            int lineCount = 0;
            // [start line, end line, start line, end line, ...]
            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] = copiedGetFirstCompleteLineOfRegion(regions[i]);
                // 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 : d.getLineOfOffset(offset));
                lineCount += lines[j + 1] - lines[j] + 1;
            }
            if (d instanceof IDocumentExtension4) {
                IDocumentExtension4 extension = (IDocumentExtension4) d;
                rewriteSession = extension.startRewriteSession(DocumentRewriteSessionType.SEQUENTIAL);
            } else {
                setRedraw(false);
                startSequentialRewriteMode(true);
            }
            if (lineCount >= 20)
                partitioners = TextUtilities.removeDocumentPartitioners(d);
            // Perform the shift operation.
            @SuppressWarnings("rawtypes") Map map = fDefaultPrefixChars;
            for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
                String[] prefixes = (String[]) copiedSelectContentTypePlugin(regions[i].getType(), map);
                if (prefixes != null && prefixes.length > 0 && lines[j] >= 0 && lines[j + 1] >= 0) {
                    if (right)
                        copiedShiftRight(lines[j], lines[j + 1], prefixes[0]);
                    else
                        copiedShiftLeft(lines[j], lines[j + 1], prefixes, ignoreWhitespace);
                }
            }
        } catch (BadLocationException x) {
            log.debug(x.getMessage(), x);
        } finally {
            if (partitioners != null)
                TextUtilities.addDocumentPartitioners(d, partitioners);
            if (d instanceof IDocumentExtension4) {
                IDocumentExtension4 extension = (IDocumentExtension4) d;
                extension.stopRewriteSession(rewriteSession);
            } else {
                stopSequentialRewriteMode();
                setRedraw(true);
            }
            if (fUndoManager != null)
                fUndoManager.endCompoundChange();
        }
    } else {
        super.shift(useDefaultPrefixes, right, ignoreWhitespace);
    }
}
Also used : IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) ITextSelection(org.eclipse.jface.text.ITextSelection) IRegion(org.eclipse.jface.text.IRegion) DocumentRewriteSession(org.eclipse.jface.text.DocumentRewriteSession) ITypedRegion(org.eclipse.jface.text.ITypedRegion) Map(java.util.Map) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 40 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project linuxtools by eclipse.

the class SpecfileCompletionProcessor method computeTemplateProposals.

/**
 * Compute the templates proposals, these proposals are contextual on
 * sections. Return an array of template proposals for the given viewer,
 * region, specfile, prefix.
 *
 * @param viewer
 *            the viewer for which the context is created
 * @param region
 *            the region into <code>document</code> for which the context is
 *            created
 * @param specfile
 *            the specfile element
 * @param prefix
 *            the prefix string
 * @return a ICompletionProposal[]
 */
private List<? extends ICompletionProposal> computeTemplateProposals(ITextViewer viewer, IRegion region, Specfile specfile, String prefix) {
    TemplateContext context = createContext(viewer, region, specfile);
    List<TemplateProposal> matches = new ArrayList<>();
    if (context == null) {
        return matches;
    }
    ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
    // $NON-NLS-1$
    context.setVariable("selection", selection.getText());
    String id = context.getContextType().getId();
    Template[] templates = Activator.getDefault().getTemplateStore().getTemplates(id);
    for (Template template : templates) {
        try {
            context.getContextType().validate(template.getPattern());
        } catch (TemplateException e) {
            continue;
        }
        int relevance = getRelevance(template, prefix);
        if (relevance > 0) {
            matches.add(new TemplateProposal(template, context, region, Activator.getDefault().getImage(TEMPLATE_ICON), relevance));
        }
    }
    Collections.sort(matches, (t1, t2) -> (t2.getRelevance() - t1.getRelevance()));
    return matches;
}
Also used : TemplateProposal(org.eclipse.jface.text.templates.TemplateProposal) TemplateException(org.eclipse.jface.text.templates.TemplateException) ArrayList(java.util.ArrayList) DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) TemplateContext(org.eclipse.jface.text.templates.TemplateContext) ITextSelection(org.eclipse.jface.text.ITextSelection) Template(org.eclipse.jface.text.templates.Template)

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