org.eclipse.jface.text.contentassist.ICompletionProposalExtension2 Java Examples

The following examples show how to use org.eclipse.jface.text.contentassist.ICompletionProposalExtension2. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContentAssistProcessorTestBuilder.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
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());
}
 
Example #2
Source File: ContentAssistProcessorTestBuilder.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
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());
}
 
Example #3
Source File: SnippetTemplateProposal.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void apply(final ITextViewer viewer, char trigger, int stateMask, final int offset)
{
	delegateTemplateProposal = null;
	if (contains(triggerChars, trigger))
	{
		if (triggerChar == trigger)
		{
			doApply(viewer, trigger, stateMask, offset);
		}
		else
		{
			delegateTemplateProposal = templateProposals[trigger - '1'];
			((ICompletionProposalExtension2) templateProposals[trigger - '1']).apply(viewer, trigger, stateMask,
					offset);
		}
	}
	else
	{
		doApply(viewer, trigger, stateMask, offset);
	}
}
 
Example #4
Source File: AbstractPyCreateClassOrMethodOrField.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * When executed it'll create a proposal and apply it.
 */
/*default*/void execute(RefactoringInfo refactoringInfo, String actTok, List<String> parametersAfterCall,
        int locationStrategy) {
    try {
        ICompletionProposalHandle proposal = createProposal(refactoringInfo, actTok, locationStrategy,
                parametersAfterCall);
        if (proposal != null) {
            if (proposal instanceof ICompletionProposalExtension2) {
                ICompletionProposalExtension2 extension2 = (ICompletionProposalExtension2) proposal;
                extension2.apply(((PyEdit) targetEditor).getPySourceViewer(), '\n', 0, 0);
            } else {
                proposal.apply(refactoringInfo.getDocument());
            }
        }

    } catch (Exception e) {
        Log.log(e);
    }
}
 
Example #5
Source File: TddTestWorkbench.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
private ICompletionProposalExtension2 waitForQuickFixProps(TddCodeGenerationQuickFixParticipant quickFix,
        PySelection ps, int offset, String expectedCompletion) throws MisconfigurationException,
        BadLocationException {

    long initialTime = System.currentTimeMillis();
    List<ICompletionProposalHandle> props;
    while (true) {
        props = waitForQuickFixProps(quickFix, ps, offset);
        ICompletionProposalExtension2 completion = findCompletion(props, expectedCompletion, false);
        if (completion != null) {
            return completion;
        }
        if (System.currentTimeMillis() - initialTime > 10000) {
            break; //Give it 10 seconds to work.
        }
        goToManual(200);
    }
    // throws exception if not used.
    return findCompletion(props, expectedCompletion);

}
 
Example #6
Source File: TddTestWorkbench.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
private ICompletionProposalExtension2 findCompletion(List<ICompletionProposalHandle> props,
        String expectedCompletion,
        boolean throwException) {
    List<String> buf = new ArrayList<String>(1 + (2 * props.size()));
    buf.add("Available:");
    for (ICompletionProposalHandle iCompletionProposal : props) {
        if (iCompletionProposal.getDisplayString().equals(expectedCompletion)) {
            ICompletionProposalExtension2 p = (ICompletionProposalExtension2) iCompletionProposal;
            return p;
        }
        buf.add("\n");
        buf.add(iCompletionProposal.getDisplayString());
    }
    if (throwException) {
        throw new AssertionError("Could not find completion: " + expectedCompletion +
                "\n"
                + StringUtils.join("\n", buf));
    }
    return null;
}
 
Example #7
Source File: CompletionProposalPopup.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Unregister this completion proposal popup.
 * 
 * @since 3.0
 */
private void unregister()
{
	if (fDocumentListener != null)
	{
		IDocument document = fContentAssistSubjectControlAdapter.getDocument();
		if (document != null)
		{
			document.removeDocumentListener(fDocumentListener);
		}
		fDocumentListener = null;
	}
	fDocumentEvents.clear();

	if (fKeyListener != null && Helper.okToUse(fContentAssistSubjectControlAdapter.getControl()))
	{
		fContentAssistSubjectControlAdapter.removeKeyListener(fKeyListener);
		fKeyListener = null;
	}

	if (fLastProposal != null)
	{
		if (fLastProposal instanceof ICompletionProposalExtension2 && fViewer != null)
		{
			ICompletionProposalExtension2 extension = (ICompletionProposalExtension2) fLastProposal;
			extension.unselected(fViewer);
		}
		fLastProposal = null;
	}

	fFilteredProposals = null;
	fComputedProposals = null;

	fContentAssistant.possibleCompletionsClosed();
}
 
Example #8
Source File: CompletionProposalPopup.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Selects the entry with the given index in the proposal selector and feeds the selection to the additional info
 * controller.
 * 
 * @param index
 *            the index in the list
 * @param smartToggle
 *            <code>true</code> if the smart toggle key has been pressed
 * @param autoScroll
 *            Do we scroll the item into view at the middle of the list
 * @since 2.1
 */
private void selectProposal(int index, boolean smartToggle, boolean autoScroll)
{
	if (fFilteredProposals == null)
	{
		return;
	}

	ICompletionProposal oldProposal = getSelectedProposal();
	if (oldProposal instanceof ICompletionProposalExtension2 && fViewer != null)
	{
		((ICompletionProposalExtension2) oldProposal).unselected(fViewer);
	}

	ICompletionProposal proposal = fFilteredProposals[index];
	if (proposal instanceof ICompletionProposalExtension2 && fViewer != null)
	{
		((ICompletionProposalExtension2) proposal).selected(fViewer, smartToggle);
	}

	fLastProposal = proposal;
	fProposalTable.setSelection(index);

	if (autoScroll)
	{
		setScroll(index);
	}
	fProposalTable.showSelection();

	if (fAdditionalInfoController != null)
	{
	    fAdditionalInfoController.handleTableSelectionChanged();
    }
}
 
Example #9
Source File: SurroundWithTemplateMenuAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static IAction[] getActionsFromProposals(IJavaCompletionProposal[] proposals, final int offset, final ITextViewer viewer) {
	List<Action> result= new ArrayList<Action>();

	for (int i= 0, j= 1; i < proposals.length; i++) {
		if (proposals[i] instanceof ICompletionProposalExtension2) {
			final IJavaCompletionProposal proposal= proposals[i];

			StringBuffer actionName= new StringBuffer();
			if (j<10) {
				actionName.append('&').append(j).append(' ');
			}
			actionName.append(proposals[i].getDisplayString());

			Action action= new Action(actionName.toString()) {
				/**
				 * {@inheritDoc}
				 */
				@Override
				public void run() {
					applyProposal(proposal, viewer, (char)0, 0, offset);
				}
			};
			action.setImageDescriptor(JavaPluginImages.DESC_OBJS_TEMPLATE);

			result.add(action);
			j++;
		}
	}
	if (result.size() == 0)
		return null;

	return result.toArray(new IAction[result.size()]);
}
 
Example #10
Source File: CompletionProposalPopup.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Initializes the proposal selector with these given proposals.
 * 
 * @param proposals
 *            the proposals
 * @param isFilteredSubset
 *            if <code>true</code>, the proposal table is not cleared, but the proposals that are not in the passed
 *            array are removed from the displayed set
 */
private void setProposals(ICompletionProposal[] proposals, boolean isFilteredSubset)
{
	ICompletionProposal[] oldProposals = fFilteredProposals;
	ICompletionProposal oldProposal = getSelectedProposal(); // may trigger filtering and a reentrant call to
               // setProposals()
	if (oldProposals != fFilteredProposals) 
		return;
	if (!(Helper.okToUse(fProposalTable)))
		return;
	if ((oldProposal instanceof ICompletionProposalExtension2) && (fViewer != null))
	{
		((ICompletionProposalExtension2) oldProposal).unselected(fViewer);
	}
	if (proposals == null)
	{
		proposals = new ICompletionProposal[0];
	}

	fFilteredProposals = proposals;
	int length = proposals.length;
	TableItem[] items;
	if (USE_VIRTUAL)
	{
		fProposalTable.clearAll();
		fProposalTable.setItemCount(length);
		items = fProposalTable.getItems();
		for (int i = 0; i < items.length; ++i)
		{
			setItem(items, i);
		}
	}
	else
	{
		fProposalTable.setRedraw(false);
		fProposalTable.setItemCount(length);
		items = fProposalTable.getItems();
		for (int i = 0; i < items.length; i++)
		{
			TableItem item = items[i];
			ICompletionProposal proposal = proposals[i];
			item.setData("isAlt", "false");
				
			item.setImage(0, proposal.getImage());
			item.setText(1, proposal.getDisplayString());
			setDefaultStyle(item);

			item.setData(proposal);
		}
		fProposalTable.setRedraw(true);
	}

	// Custom code for modifying selection/size
	int defaultIndex = -1;
	int suggestedIndex = -1;

	// select the first proposal
	if (proposals.length > 0)
	{
		defaultIndex = 0;
		suggestedIndex = 0;
	}
	modifySelection(defaultIndex, suggestedIndex);

	if (!(isFilteredSubset))
	{
		resizeTable(0, 0);
	}

}
 
Example #11
Source File: CompletionProposalPopup.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Computes the subset of already computed proposals 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 3.0
 */
private ICompletionProposal[] computeFilteredProposals(int offset, DocumentEvent event)
{

	if (offset == fInvocationOffset && event == null)
	{
		fIsFilteredSubset = false;
		return fComputedProposals;
	}
	IDocument document = fContentAssistSubjectControlAdapter.getDocument();
	// this does go through the array twice (once to figure out if it's okay to use the else case, and the second
	// time to actual filter the proposals, but it is what the original logic suggests
	if ((offset < fInvocationOffset) || ((event != null) && (event.fText.startsWith("("))))
	{
		fIsFilteredSubset = false;
		fInvocationOffset = offset;
		fComputedProposals = computeProposals(fInvocationOffset, false);
		fComputedProposals = filterProposals(fComputedProposals, document, fInvocationOffset, event);
		return fComputedProposals;
	}

	ICompletionProposal[] proposals;
	if (offset < fFilterOffset)
	{
		proposals = fComputedProposals;
		fIsFilteredSubset = false;
	}
	else
	{
		proposals = fFilteredProposals;
		fIsFilteredSubset = true;
	}

	if (proposals == null)
	{
		fIsFilteredSubset = false;
		return null;
	}

	for (int i = 0; i < proposals.length; i++)
	{
		ICompletionProposal proposal = proposals[i];
		if ((proposal instanceof ICompletionProposalExtension2)
				|| (proposal instanceof ICompletionProposalExtension))
		{
			continue;
		}
		fIsFilteredSubset = false;
		fInvocationOffset = offset;
		fComputedProposals = computeProposals(fInvocationOffset, false);

		return fComputedProposals;
	}

	ICompletionProposal[] filtered = filterProposals(proposals, document, offset, event);
	return filtered;
}
 
Example #12
Source File: TddTestWorkbench.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
private ICompletionProposalExtension2 findCompletion(List<ICompletionProposalHandle> props,
        String expectedCompletion) {
    return findCompletion(props, expectedCompletion, true);
}