Search in sources :

Example 21 with ITextSelection

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

the class RevertSelectionAction method computeEnablement.

@Override
public boolean computeEnablement() {
    if (!super.computeEnablement())
        return false;
    ITextSelection selection = getSelection();
    if (selection == null)
        return false;
    fStartLine = selection.getStartLine();
    fEndLine = selection.getEndLine();
    // only enable if mouse activity is inside line range
    int activityLine = getLastLine();
    if (activityLine == -1 || activityLine < fStartLine || activityLine > fEndLine + 1)
        // + 1 to cover the case where the selection goes to the offset of the next line
        return false;
    ILineDiffer differ = getDiffer();
    if (differ == null)
        return false;
    // only enable if selection covers at least two lines
    if (fEndLine > fStartLine) {
        for (int i = fStartLine; i <= fEndLine; i++) {
            ILineDiffInfo info = differ.getLineInfo(i);
            if (info != null && info.hasChanges()) {
                return true;
            }
        }
    }
    return false;
}
Also used : ILineDiffer(org.eclipse.jface.text.source.ILineDiffer) ILineDiffInfo(org.eclipse.jface.text.source.ILineDiffInfo) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 22 with ITextSelection

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

the class FileBufferOperationHandler method computeSelectedResources.

/**
 * Computes the selected resources.
 */
protected final void computeSelectedResources() {
    if (fResources != null || fLocation != null)
        return;
    ISelection selection = getSelection();
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        ArrayList<Object> resources = new ArrayList<>(structuredSelection.size());
        Iterator<?> e = structuredSelection.iterator();
        while (e.hasNext()) {
            Object element = e.next();
            if (element instanceof IResource)
                resources.add(element);
            else if (element instanceof IAdaptable) {
                IAdaptable adaptable = (IAdaptable) element;
                Object adapter = adaptable.getAdapter(IResource.class);
                if (adapter instanceof IResource)
                    resources.add(adapter);
            }
        }
        if (!resources.isEmpty())
            fResources = resources.toArray(new IResource[resources.size()]);
    } else if (selection instanceof ITextSelection) {
        IWorkbenchWindow window = getWorkbenchWindow();
        if (window != null) {
            IWorkbenchPart workbenchPart = window.getPartService().getActivePart();
            if (workbenchPart instanceof IEditorPart) {
                IEditorPart editorPart = (IEditorPart) workbenchPart;
                IEditorInput input = editorPart.getEditorInput();
                Object adapter = input.getAdapter(IResource.class);
                if (adapter instanceof IResource)
                    fResources = new IResource[] { (IResource) adapter };
                else {
                    adapter = input.getAdapter(ILocationProvider.class);
                    if (adapter instanceof ILocationProvider) {
                        ILocationProvider provider = (ILocationProvider) adapter;
                        fLocation = provider.getPath(input);
                    }
                }
            }
        }
    }
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IEditorPart(org.eclipse.ui.IEditorPart) ITextSelection(org.eclipse.jface.text.ITextSelection) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) ISelection(org.eclipse.jface.viewers.ISelection) IResource(org.eclipse.core.resources.IResource) IEditorInput(org.eclipse.ui.IEditorInput)

Example 23 with ITextSelection

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

the class RetrieverAction method extractSearchTextFromEditor.

protected final String extractSearchTextFromEditor(IEditorPart editor) {
    if (editor != null) {
        ITextSelection selection = null;
        ISelectionProvider provider = editor.getEditorSite().getSelectionProvider();
        if (provider != null) {
            ISelection s = provider.getSelection();
            if (s instanceof ITextSelection) {
                selection = (ITextSelection) s;
            }
        }
        if (selection != null) {
            if (selection.getLength() == 0) {
                ITextEditor txtEditor = getTextEditor(editor);
                if (txtEditor != null) {
                    IDocument document = txtEditor.getDocumentProvider().getDocument(txtEditor.getEditorInput());
                    selection = expandSelection(selection, document, null);
                }
            }
            if (selection.getLength() > 0 && selection.getText() != null) {
                return trimSearchString(selection.getText());
            }
        }
    }
    return null;
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) ISelection(org.eclipse.jface.viewers.ISelection) ITextSelection(org.eclipse.jface.text.ITextSelection) IDocument(org.eclipse.jface.text.IDocument)

Example 24 with ITextSelection

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

the class RetrieverAction method expandSelection.

private ITextSelection expandSelection(ITextSelection sel, IDocument document, String stopChars) {
    int offset = sel.getOffset();
    int length = sel.getLength();
    // left or right.
    if (length == 0) {
        // try right
        char chr = 0;
        char chl = 0;
        try {
            chr = document.getChar(offset);
        } catch (BadLocationException e2) {
        }
        try {
            chl = document.getChar(offset - 1);
        } catch (BadLocationException e2) {
        }
        if (isPartOfIdentifier(chr)) {
            length = 1;
        } else if (isPartOfIdentifier(chl)) {
            offset--;
            length = 1;
        } else if (stopChars != null && stopChars.indexOf(chr) == -1) {
            length = 1;
        } else if (stopChars != null && stopChars.indexOf(chl) == -1) {
            offset--;
            length = 1;
        } else {
            return sel;
        }
    }
    int a = offset + length - 1;
    int z = a;
    // move z one behind last character.
    try {
        char ch = document.getChar(z);
        while (isValidChar(stopChars, ch)) {
            ch = document.getChar(++z);
        }
    } catch (BadLocationException e2) {
    }
    // move a one before the first character
    try {
        char ch = document.getChar(a);
        while (isValidChar(stopChars, ch)) {
            ch = document.getChar(--a);
        }
    } catch (BadLocationException e2) {
    }
    if (a == z) {
        offset = a;
        length = 0;
    } else {
        offset = a + 1;
        length = z - a - 1;
    }
    return new TextSelection(document, offset, length);
}
Also used : ITextSelection(org.eclipse.jface.text.ITextSelection) TextSelection(org.eclipse.jface.text.TextSelection) Point(org.eclipse.swt.graphics.Point) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 25 with ITextSelection

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

the class TemplateCompletionProcessor method computeCompletionProposals.

@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
    ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
    // adjust offset to end of normalized selection
    if (selection.getOffset() == offset)
        offset = selection.getOffset() + selection.getLength();
    String prefix = extractPrefix(viewer, offset);
    Region region = new Region(offset - prefix.length(), prefix.length());
    TemplateContext context = createContext(viewer, region);
    if (context == null)
        return new ICompletionProposal[0];
    // name of the selection variables {line, word}_selection //$NON-NLS-1$
    context.setVariable("selection", selection.getText());
    Template[] templates = getTemplates(context.getContextType().getId());
    List<ICompletionProposal> matches = new ArrayList<>();
    for (Template template : templates) {
        try {
            context.getContextType().validate(template.getPattern());
        } catch (TemplateException e) {
            continue;
        }
        if (template.matches(prefix, context.getContextType().getId()))
            matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix)));
    }
    Collections.sort(matches, fgProposalComparator);
    return matches.toArray(new ICompletionProposal[matches.size()]);
}
Also used : ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) 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