Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1d6ee992058acd4093a2ffde6a1c9cf3fff6c088 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*******************************************************************************
 * Copyright (c) 2007, 2012 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Bryan Wilkinson (QNX) - Initial API and implementation
 *     Anton Leherbauer (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.text.contentassist;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.swt.graphics.Image;

import org.eclipse.cdt.core.dom.ast.IASTCompletionNode;
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.IFunctionSummary;
import org.eclipse.cdt.ui.text.ICHelpInvocationContext;
import org.eclipse.cdt.ui.text.IContentAssistHelpInvocationContext;

import org.eclipse.cdt.internal.ui.CHelpProviderManager;
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;

public class HelpCompletionProposalComputer extends ParsingBasedProposalComputer {

	@Override
	protected List<ICompletionProposal> computeCompletionProposals(
			CContentAssistInvocationContext cContext,
			IASTCompletionNode completionNode, String prefix)
			throws CoreException {
		
		boolean handleHelp = false;
		if (completionNode != null) {
			IASTName[] names = completionNode.getNames();
			for (int i = 0; i < names.length; ++i) {
				IASTName name = names[i];
				
				// ignore if not connected
				if (name.getTranslationUnit() == null)
					continue;
			
				// ignore if this is a member access
				if (name.getParent() instanceof IASTFieldReference)
					continue;
				
				handleHelp = true;
				break;
			}
		}
		
		if (!handleHelp) {
			return Collections.emptyList();
		}
		
		final ITranslationUnit tu = cContext.getTranslationUnit();
		final IASTCompletionNode cn = completionNode;
		final int cc = cContext.getInvocationOffset();
		
		// Find matching functions
		ICHelpInvocationContext helpContext = new IContentAssistHelpInvocationContext() {

			@Override
			public IProject getProject() {
				return tu.getCProject().getProject();
			}

			@Override
			public ITranslationUnit getTranslationUnit() {
				return tu;
			}
			
			@Override
			public int getInvocationOffset() {
				return cc;
			}
			
			@Override
			public IASTCompletionNode getCompletionNode() {
				return cn;
			}
		};

		IFunctionSummary[] summaries = CHelpProviderManager.getDefault()
				.getMatchingFunctions(helpContext, prefix);
		if (summaries == null)
			return Collections.emptyList();

		boolean doReplacement = !cContext.isContextInformationStyle();
		int repLength = doReplacement ? prefix.length() : 0;
		int repOffset = cContext.getInvocationOffset() - repLength;
		Image image = CUIPlugin.getImageDescriptorRegistry().get(
				CElementImageProvider.getFunctionImageDescriptor());

		List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();

		for (IFunctionSummary summary : summaries) {
			String fname = summary.getName() + "()"; //$NON-NLS-1$
			String fdesc = summary.getDescription();
			IFunctionSummary.IFunctionPrototypeSummary fproto = summary
					.getPrototype();
			String fargs = fproto.getArguments();

			String repString = doReplacement ? fname : "";  //$NON-NLS-1$
			
			int relevance = computeBaseRelevance(prefix, summary.getName()) + RelevanceConstants.HELP_TYPE_RELEVANCE;
			CCompletionProposal proposal;
			proposal = new CCompletionProposal(
					repString,
					repOffset,
					repLength,
					image,
					fproto.getPrototypeString(true),
					relevance,
					cContext.getViewer());

			if (fdesc != null) {
				proposal.setAdditionalProposalInfo(fdesc);
			}

			if (!cContext.isContextInformationStyle()) {
				if (fargs != null && fargs.length() > 0) {
					// set the cursor before the closing bracket
					proposal.setCursorPosition(fname.length() - 1);
				} else {
					// set the cursor behind the closing bracked
					proposal.setCursorPosition(fname.length());
				}
			}
			
			if (fargs != null && fargs.length() > 0) {
				CProposalContextInformation info = new CProposalContextInformation(image, fname, fargs);
				info.setContextInformationPosition(cContext.getContextInformationOffset());
				proposal.setContextInformation(info);

			}

			proposals.add(proposal);
		}

		return proposals;
	}
}

Back to the top