Search in sources :

Example 1 with IEditingSupport

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

the class CompletionProposalPopup 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
 * @param offset the offset
 * @since 2.1
 */
void insertProposal(ICompletionProposal p, char trigger, int stateMask, final int offset) {
    fInserting = true;
    IRewriteTarget target = null;
    IEditingSupport helper = new IEditingSupport() {

        @Override
        public boolean isOriginator(DocumentEvent event, IRegion focus) {
            return focus.getOffset() <= offset && focus.getOffset() + focus.getLength() >= offset;
        }

        @Override
        public boolean ownsFocusShell() {
            return false;
        }
    };
    try {
        IDocument document = fContentAssistSubjectControlAdapter.getDocument();
        if (fViewer instanceof ITextViewerExtension) {
            ITextViewerExtension extension = (ITextViewerExtension) fViewer;
            target = extension.getRewriteTarget();
        }
        if (target != null)
            target.beginCompoundChange();
        if (fViewer instanceof IEditingSupportRegistry) {
            IEditingSupportRegistry registry = (IEditingSupportRegistry) fViewer;
            registry.register(helper);
        }
        if (p instanceof ICompletionProposalExtension2 && fViewer != null) {
            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);
        }
        fireAppliedEvent(p);
        Point selection = p.getSelection(document);
        if (selection != null) {
            fContentAssistSubjectControlAdapter.setSelectedRange(selection.x, selection.y);
            fContentAssistSubjectControlAdapter.revealRange(selection.x, selection.y);
        }
        IContextInformation info = p.getContextInformation();
        if (info != null) {
            int contextInformationOffset;
            if (p instanceof ICompletionProposalExtension) {
                ICompletionProposalExtension e = (ICompletionProposalExtension) p;
                contextInformationOffset = e.getContextInformationPosition();
            } else {
                if (selection == null)
                    selection = fContentAssistSubjectControlAdapter.getSelectedRange();
                contextInformationOffset = selection.x + selection.y;
            }
            fContentAssistant.showContextInformation(info, contextInformationOffset);
        } else
            fContentAssistant.showContextInformation(null, -1);
    } finally {
        if (target != null)
            target.endCompoundChange();
        if (fViewer instanceof IEditingSupportRegistry) {
            IEditingSupportRegistry registry = (IEditingSupportRegistry) fViewer;
            registry.unregister(helper);
        }
        fInserting = false;
    }
}
Also used : IEditingSupport(org.eclipse.jface.text.IEditingSupport) ITextViewerExtension(org.eclipse.jface.text.ITextViewerExtension) IRewriteTarget(org.eclipse.jface.text.IRewriteTarget) IEditingSupportRegistry(org.eclipse.jface.text.IEditingSupportRegistry) Point(org.eclipse.swt.graphics.Point) DocumentEvent(org.eclipse.jface.text.DocumentEvent) IRegion(org.eclipse.jface.text.IRegion) IDocument(org.eclipse.jface.text.IDocument) Point(org.eclipse.swt.graphics.Point)

Example 2 with IEditingSupport

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

the class CompletionProposalPopup method displayProposals.

/**
 * Displays this popup and install the additional info controller, so that additional info
 * is displayed when a proposal is selected and additional info is available.
 */
void displayProposals() {
    if (!Helper.okToUse(fProposalShell) || !Helper.okToUse(fProposalTable))
        return;
    if (fContentAssistant.addContentAssistListener(this, ContentAssistant.PROPOSAL_SELECTOR)) {
        ensureDocumentListenerInstalled();
        if (fFocusHelper == null) {
            fFocusHelper = new IEditingSupport() {

                @Override
                public boolean isOriginator(DocumentEvent event, IRegion focus) {
                    // this helper just covers the focus change to the proposal shell, no remote editions
                    return false;
                }

                @Override
                public boolean ownsFocusShell() {
                    return true;
                }
            };
        }
        if (fViewer instanceof IEditingSupportRegistry) {
            IEditingSupportRegistry registry = (IEditingSupportRegistry) fViewer;
            registry.register(fFocusHelper);
        }
        /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=52646
			 * on GTK, setVisible and such may run the event loop
			 * (see also https://bugs.eclipse.org/bugs/show_bug.cgi?id=47511)
			 * Since the user may have already canceled the popup or selected
			 * an entry (ESC or RETURN), we have to double check whether
			 * the table is still okToUse. See comments below
			 */
        // may run event loop on GTK
        fProposalShell.setVisible(true);
        // transfer focus since no verify key listener can be attached
        if (!fContentAssistSubjectControlAdapter.supportsVerifyKeyListener() && Helper.okToUse(fProposalShell))
            // may run event loop on GTK ??
            fProposalShell.setFocus();
        if (fAdditionalInfoController != null && Helper.okToUse(fProposalTable)) {
            fAdditionalInfoController.install(fProposalTable);
            fAdditionalInfoController.handleTableSelectionChanged();
        }
    } else
        hide();
}
Also used : IEditingSupport(org.eclipse.jface.text.IEditingSupport) IEditingSupportRegistry(org.eclipse.jface.text.IEditingSupportRegistry) DocumentEvent(org.eclipse.jface.text.DocumentEvent) IRegion(org.eclipse.jface.text.IRegion)

Aggregations

DocumentEvent (org.eclipse.jface.text.DocumentEvent)2 IEditingSupport (org.eclipse.jface.text.IEditingSupport)2 IEditingSupportRegistry (org.eclipse.jface.text.IEditingSupportRegistry)2 IRegion (org.eclipse.jface.text.IRegion)2 IDocument (org.eclipse.jface.text.IDocument)1 IRewriteTarget (org.eclipse.jface.text.IRewriteTarget)1 ITextViewerExtension (org.eclipse.jface.text.ITextViewerExtension)1 Point (org.eclipse.swt.graphics.Point)1