Search in sources :

Example 1 with ICompletionProposalExtension

use of org.eclipse.jface.text.contentassist.ICompletionProposalExtension in project eclipse.platform.text by eclipse.

the class CompletionProposalPopup2 method verifyKey.

@Override
public boolean verifyKey(VerifyEvent e) {
    if (!Helper2.okToUse(fProposalShell))
        return true;
    char key = e.character;
    if (key == 0) {
        int newSelection = fProposalTable.getSelectionIndex();
        int visibleRows = (fProposalTable.getSize().y / fProposalTable.getItemHeight()) - 1;
        switch(e.keyCode) {
            case SWT.ARROW_LEFT:
            case SWT.ARROW_RIGHT:
                filterProposals();
                return true;
            case SWT.ARROW_UP:
                newSelection -= 1;
                if (newSelection < 0)
                    newSelection = fProposalTable.getItemCount() - 1;
                break;
            case SWT.ARROW_DOWN:
                newSelection += 1;
                if (newSelection > fProposalTable.getItemCount() - 1)
                    newSelection = 0;
                break;
            case SWT.PAGE_DOWN:
                newSelection += visibleRows;
                if (newSelection >= fProposalTable.getItemCount())
                    newSelection = fProposalTable.getItemCount() - 1;
                break;
            case SWT.PAGE_UP:
                newSelection -= visibleRows;
                if (newSelection < 0)
                    newSelection = 0;
                break;
            case SWT.HOME:
                newSelection = 0;
                break;
            case SWT.END:
                newSelection = fProposalTable.getItemCount() - 1;
                break;
            default:
                if (e.keyCode != SWT.MOD1 && e.keyCode != SWT.MOD2 && e.keyCode != SWT.MOD3 && e.keyCode != SWT.MOD4)
                    hide();
                return true;
        }
        selectProposal(newSelection, (e.stateMask & SWT.CTRL) != 0);
        e.doit = false;
        return false;
    }
    // key != 0
    switch(key) {
        case // Esc
        0x1B:
            e.doit = false;
            hide();
            break;
        // Ctrl-Enter on w2k
        case '\n':
        case // Enter
        '\r':
            if ((e.stateMask & SWT.CTRL) == 0) {
                e.doit = !selectProposalWithMask(e.stateMask);
            }
            break;
        // plus: don't invalidate the event in order to give LinkedUI a chance to handle it
        case '\t':
            // hide();
            break;
        default:
            ICompletionProposal p = getSelectedProposal();
            if (p instanceof ICompletionProposalExtension) {
                ICompletionProposalExtension t = (ICompletionProposalExtension) p;
                char[] triggers = t.getTriggerCharacters();
                if (contains(triggers, key)) {
                    hide();
                    e.doit = false;
                    insertProposal(p, key, e.stateMask, fViewer.getSelectedRange().x);
                }
            }
    }
    return true;
}
Also used : ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ICompletionProposalExtension(org.eclipse.jface.text.contentassist.ICompletionProposalExtension) Point(org.eclipse.swt.graphics.Point)

Example 2 with ICompletionProposalExtension

use of org.eclipse.jface.text.contentassist.ICompletionProposalExtension in project eclipse.platform.text by eclipse.

the class CompletionProposalPopup2 method computeFilteredProposals.

/**
 * Computes the subset of already computed propsals that are still valid for
 * the given offset.
 *
 * @param offset the offset
 * @param event the merged document event
 * @return the set of filtered proposals
 * @since 2.0
 */
private ICompletionProposal[] computeFilteredProposals(int offset, DocumentEvent event) {
    if (offset == fInvocationOffset && event == null)
        return fComputedProposals;
    if (offset < fInvocationOffset) {
        return null;
    }
    ICompletionProposal[] proposals = fComputedProposals;
    if (offset > fFilterOffset)
        proposals = fFilteredProposals;
    if (proposals == null)
        return null;
    IDocument document = fViewer.getDocument();
    int length = proposals.length;
    List<Object> filtered = new ArrayList<>(length);
    for (int i = 0; i < length; i++) {
        if (proposals[i] instanceof ICompletionProposalExtension2) {
            ICompletionProposalExtension2 p = (ICompletionProposalExtension2) proposals[i];
            if (p.validate(document, offset, event))
                filtered.add(p);
        } else if (proposals[i] instanceof ICompletionProposalExtension) {
            ICompletionProposalExtension p = (ICompletionProposalExtension) proposals[i];
            if (p.isValidFor(document, offset))
                filtered.add(p);
        } else {
            // restore original behavior
            fInvocationOffset = offset;
            fComputedProposals = computeProposals(fInvocationOffset);
            return fComputedProposals;
        }
    }
    ICompletionProposal[] p = new ICompletionProposal[filtered.size()];
    filtered.toArray(p);
    return p;
}
Also used : ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) ICompletionProposalExtension(org.eclipse.jface.text.contentassist.ICompletionProposalExtension) ICompletionProposalExtension2(org.eclipse.jface.text.contentassist.ICompletionProposalExtension2) IDocument(org.eclipse.jface.text.IDocument) Point(org.eclipse.swt.graphics.Point)

Example 3 with ICompletionProposalExtension

use of org.eclipse.jface.text.contentassist.ICompletionProposalExtension in project liferay-ide by liferay.

the class LiferayCustomXmlHoverControl method _apply.

private void _apply(ICompletionProposal p, ITextViewer viewer, int offset, boolean multiFix) {
    // Focus needs to be in the text viewer, otherwise linked mode does not
    // work
    dispose();
    IRewriteTarget target = null;
    try {
        IDocument document = viewer.getDocument();
        if (viewer instanceof ITextViewerExtension) {
            ITextViewerExtension extension = (ITextViewerExtension) viewer;
            target = extension.getRewriteTarget();
        }
        if (target != null) {
            target.beginCompoundChange();
        }
        if (p instanceof ICompletionProposalExtension2) {
            ICompletionProposalExtension2 e = (ICompletionProposalExtension2) p;
            e.apply(viewer, (char) 0, multiFix ? SWT.CONTROL : SWT.NONE, offset);
        } else if (p instanceof ICompletionProposalExtension) {
            ICompletionProposalExtension e = (ICompletionProposalExtension) p;
            e.apply(document, (char) 0, offset);
        } else {
            p.apply(document);
        }
        Point selection = p.getSelection(document);
        if (selection != null) {
            viewer.setSelectedRange(selection.x, selection.y);
            viewer.revealRange(selection.x, selection.y);
        }
    } catch (Exception e) {
    } finally {
        if (target != null) {
            target.endCompoundChange();
        }
    }
}
Also used : ITextViewerExtension(org.eclipse.jface.text.ITextViewerExtension) IRewriteTarget(org.eclipse.jface.text.IRewriteTarget) ICompletionProposalExtension(org.eclipse.jface.text.contentassist.ICompletionProposalExtension) ICompletionProposalExtension2(org.eclipse.jface.text.contentassist.ICompletionProposalExtension2) Point(org.eclipse.swt.graphics.Point) IDocument(org.eclipse.jface.text.IDocument)

Example 4 with ICompletionProposalExtension

use of org.eclipse.jface.text.contentassist.ICompletionProposalExtension in project eclipse.platform.text by eclipse.

the class CompletionProposalPopup2 method insertProposal.

/**
 * Applies the given proposal at the given offset. The given character is the
 * one that triggered the insertion of this proposal.
 *
 * @param p the completion proposal
 * @param trigger the trigger character
 * @param stateMask the state mask of the keyboard event triggering the insertion
 * @param offset the offset
 * @since 2.1
 */
private void insertProposal(ICompletionProposal p, char trigger, int stateMask, final int offset) {
    fInserting = true;
    IRewriteTarget target = null;
    IEditingSupportRegistry registry = null;
    try {
        IDocument document = fViewer.getDocument();
        if (fViewer instanceof ITextViewerExtension) {
            ITextViewerExtension extension = (ITextViewerExtension) fViewer;
            target = extension.getRewriteTarget();
        }
        if (target != null)
            target.beginCompoundChange();
        if (fViewer instanceof IEditingSupportRegistry) {
            registry = (IEditingSupportRegistry) fViewer;
            registry.register(fModificationEditingSupport);
        }
        if (p instanceof ICompletionProposalExtension2) {
            ICompletionProposalExtension2 e = (ICompletionProposalExtension2) p;
            e.apply(fViewer, trigger, stateMask, offset);
        } else if (p instanceof ICompletionProposalExtension) {
            ICompletionProposalExtension e = (ICompletionProposalExtension) p;
            e.apply(document, trigger, offset);
        } else {
            p.apply(document);
        }
        Point selection = p.getSelection(document);
        if (selection != null) {
            fViewer.setSelectedRange(selection.x, selection.y);
            fViewer.revealRange(selection.x, selection.y);
        }
        IContextInformation info = p.getContextInformation();
        if (info != null) {
            int position;
            if (p instanceof ICompletionProposalExtension) {
                ICompletionProposalExtension e = (ICompletionProposalExtension) p;
                position = e.getContextInformationPosition();
            } else {
                if (selection == null)
                    selection = fViewer.getSelectedRange();
                position = selection.x + selection.y;
            }
            fContentAssistant.showContextInformation(info, position);
        }
        fContentAssistant.fireProposalChosen(p);
    } finally {
        if (target != null)
            target.endCompoundChange();
        if (registry != null)
            registry.unregister(fModificationEditingSupport);
        fInserting = false;
    }
}
Also used : ITextViewerExtension(org.eclipse.jface.text.ITextViewerExtension) IRewriteTarget(org.eclipse.jface.text.IRewriteTarget) IEditingSupportRegistry(org.eclipse.jface.text.IEditingSupportRegistry) ICompletionProposalExtension(org.eclipse.jface.text.contentassist.ICompletionProposalExtension) IContextInformation(org.eclipse.jface.text.contentassist.IContextInformation) ICompletionProposalExtension2(org.eclipse.jface.text.contentassist.ICompletionProposalExtension2) Point(org.eclipse.swt.graphics.Point) IDocument(org.eclipse.jface.text.IDocument) Point(org.eclipse.swt.graphics.Point)

Example 5 with ICompletionProposalExtension

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

the class ContentAssistProcessorTestBuilder method appendAndApplyProposal.

protected ContentAssistProcessorTestBuilder appendAndApplyProposal(ICompletionProposal proposal, ISourceViewer sourceViewer, String model, int position) throws Exception {
    IDocument document = sourceViewer.getDocument();
    int offset = position;
    if (model != null) {
        document.set(getModel() + model);
        offset += model.length();
    }
    if (proposal instanceof ICompletionProposalExtension2) {
        ICompletionProposalExtension2 proposalExtension2 = (ICompletionProposalExtension2) proposal;
        proposalExtension2.apply(sourceViewer, (char) 0, SWT.NONE, offset);
    } else if (proposal instanceof ICompletionProposalExtension) {
        ICompletionProposalExtension proposalExtension = (ICompletionProposalExtension) proposal;
        proposalExtension.apply(document, (char) 0, offset);
    } else {
        proposal.apply(document);
    }
    return reset().append(document.get());
}
Also used : ICompletionProposalExtension(org.eclipse.jface.text.contentassist.ICompletionProposalExtension) ICompletionProposalExtension2(org.eclipse.jface.text.contentassist.ICompletionProposalExtension2) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

ICompletionProposalExtension (org.eclipse.jface.text.contentassist.ICompletionProposalExtension)6 IDocument (org.eclipse.jface.text.IDocument)5 ICompletionProposalExtension2 (org.eclipse.jface.text.contentassist.ICompletionProposalExtension2)5 Point (org.eclipse.swt.graphics.Point)4 IRewriteTarget (org.eclipse.jface.text.IRewriteTarget)2 ITextViewerExtension (org.eclipse.jface.text.ITextViewerExtension)2 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)2 ArrayList (java.util.ArrayList)1 IEditingSupportRegistry (org.eclipse.jface.text.IEditingSupportRegistry)1 IContextInformation (org.eclipse.jface.text.contentassist.IContextInformation)1