Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 13627e3c13a02ec28d90c2dd6c6b2aa2deaacf0a (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * Copyright (c) 2013, 2015 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
 */
package org.eclipse.cdt.internal.qt.ui;

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

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTCompletionContext;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFieldReference;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.internal.qt.core.ASTUtil;
import org.eclipse.cdt.internal.qt.core.QtFunctionCallUtil;
import org.eclipse.cdt.internal.qt.core.QtKeywords;
import org.eclipse.cdt.internal.qt.core.index.IQMethod;
import org.eclipse.cdt.internal.qt.core.index.IQObject;
import org.eclipse.cdt.internal.qt.core.index.QtIndex;
import org.eclipse.cdt.internal.ui.text.contentassist.CCompletionProposal;
import org.eclipse.cdt.internal.ui.text.contentassist.RelevanceConstants;
import org.eclipse.cdt.ui.text.contentassist.ICEditorContentAssistInvocationContext;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.contentassist.ICompletionProposal;

@SuppressWarnings("restriction")
public class QObjectConnectCompletion {
	// These suggestions are populated from the index, so the case is always an exact match.
	// Secondly, these suggestions should appear above generic variable and method matches, since
	// have based the calculation on the exact function that is being called.

	private static final int MACRO_RELEVANCE
		= RelevanceConstants.CASE_MATCH_RELEVANCE + RelevanceConstants.LOCAL_VARIABLE_TYPE_RELEVANCE + 2;
	private static final int MACRO_PARAM_RELEVANCE
		= RelevanceConstants.CASE_MATCH_RELEVANCE + RelevanceConstants.METHOD_TYPE_RELEVANCE + 1;

	/**
	 * Different suggestions should be proposed for each parameter of the QObject::connect
	 * function call.  The 'sender' parameter should suggest SIGNAL, but 'member' can be
	 * either SLOT or SIGNAL.
	 */
	public enum Param {
		Signal,
		Member,
		Generic
	}

	private final Param param;
	private final Data data;

	public QObjectConnectCompletion(Param param) {
		this.param = param;
		this.data = null;
	}

	public QObjectConnectCompletion(String replacement) {
		this.param = Param.Generic;
		this.data = new Data(replacement);
	}

	/**
	 * The data used to produce the completions varies depending on the role of the
	 * parameter that is being completed.
	 */
	private static class Data
	{
		public final String replacement;
		public final String display;
		public final int cursorOffset;

		public static final Data SIGNAL = new Data("SIGNAL()", "SIGNAL(a)", -1);
		public static final Data SLOT = new Data("SLOT()", "SLOT(a)", -1);

		public Data(String replacement) {
			this(replacement, replacement, 0);
		}

		public Data(String replacement, String display, int cursorOffset) {
			this.replacement = replacement;
			this.display = display;
			this.cursorOffset = cursorOffset;
		}

		public ICompletionProposal createProposal(ICEditorContentAssistInvocationContext context, int relevance) {
			int repLength = replacement.length();
			int repOffset = context.getInvocationOffset();
			CCompletionProposal p
				= new CCompletionProposal(replacement, repOffset, repLength, Activator.getQtLogo(), display, relevance, context.getViewer());
			p.setCursorPosition(repLength + cursorOffset);
			return p;
		}
	}

	private static void addProposal(Collection<ICompletionProposal> proposals, ICEditorContentAssistInvocationContext context, Data data, int relevance) {
		if (data == null)
			return;

		ICompletionProposal proposal = data.createProposal(context, relevance);
		if (proposal != null)
			proposals.add(proposal);
	}

	private void addProposals(Collection<ICompletionProposal> proposals, ICEditorContentAssistInvocationContext context) {

		if (data != null)
			addProposal(proposals, context, data, MACRO_PARAM_RELEVANCE);
		else
			switch(param) {
			case Signal:
				addProposal(proposals, context, Data.SIGNAL, MACRO_RELEVANCE);
				break;
			case Member:
				addProposal(proposals, context, Data.SLOT,   MACRO_RELEVANCE);
				addProposal(proposals, context, Data.SIGNAL, MACRO_RELEVANCE - 1);
				break;
			default:
				break;
			}
	}

	// Copied from org.eclipse.cdt.internal.ui.text.CParameterListValidator
	private static int indexOfClosingPeer(String code, char left, char right, int pos) {
		int level = 0;
		final int length = code.length();
		while (pos < length) {
			char ch = code.charAt(pos);
			if (ch == left) {
				++level;
			} else if (ch == right) {
				if (--level == 0) {
					return pos;
				}
			}
			++pos;
		}
		return -1;
	}

	// Copied from org.eclipse.cdt.internal.ui.text.CParameterListValidator
	private static int[] computeCommaPositions(String code) {
		final int length = code.length();
		int pos = 0;
		List<Integer> positions = new ArrayList<Integer>();
		positions.add(new Integer(-1));
		while (pos < length && pos != -1) {
			char ch = code.charAt(pos);
			switch (ch) {
			case ',':
				positions.add(new Integer(pos));
				break;
			case '(':
				pos = indexOfClosingPeer(code, '(', ')', pos);
				break;
			case '<':
				pos = indexOfClosingPeer(code, '<', '>', pos);
				break;
			case '[':
				pos = indexOfClosingPeer(code, '[', ']', pos);
				break;
			default:
				break;
			}
			if (pos != -1)
				pos++;
		}
		positions.add(new Integer(length));

		int[] fields = new int[positions.size()];
		for (int i = 0; i < fields.length; i++)
			fields[i] = positions.get(i).intValue();
		return fields;
	}

	private static Collection<QObjectConnectCompletion> getCompletionsFor(IType targetType, IASTInitializerClause arg) {

		if (!(targetType instanceof ICPPClassType))
			return null;
		ICPPClassType cls = (ICPPClassType) targetType;

		QtIndex qtIndex = QtIndex.getIndex(ASTUtil.getProject(arg));
		if (qtIndex == null)
			return null;

		IQObject qobj = null;
		try {
			qobj = qtIndex.findQObject(cls.getQualifiedName());
		} catch(DOMException e) {
			CCorePlugin.log(e);
		}

		// QtIndex.findQObject will return null in some cases, e.g., when the parameter is null
		if (qobj == null)
			return null;

		Collection<QObjectConnectCompletion> completions = new ArrayList<QObjectConnectCompletion>();
		String raw = arg.getRawSignature();
		if (raw.startsWith(QtKeywords.SIGNAL))
			for(IQMethod method : qobj.getSignals().withoutOverrides())
				for(String signature : method.getSignatures())
					completions.add(new QObjectConnectCompletion(signature));
		if (raw.startsWith(QtKeywords.SLOT))
			for(IQMethod method : qobj.getSlots().withoutOverrides())
				for(String signature : method.getSignatures())
					completions.add(new QObjectConnectCompletion(signature));
		return completions;
	}

	public static Collection<QObjectConnectCompletion> getConnectProposals(
			ICEditorContentAssistInvocationContext context, IASTName name, IASTCompletionContext astContext, IASTNode astNode) {

		if (QtFunctionCallUtil.isQObjectFunctionCall(astContext, !context.isContextInformationStyle(), name)) {
			int parseOffset = context.getParseOffset();
			int invocationOffset = context.getInvocationOffset();

			String unparsed = "";
			try {
				unparsed = context.getDocument().get(parseOffset, invocationOffset - parseOffset);
			} catch (BadLocationException e) {
				CCorePlugin.log(e);
			}

			if (unparsed.length() > 0 && unparsed.charAt(0) == '(')
				unparsed = unparsed.substring(1);

			int[] commas = computeCommaPositions(unparsed);
			switch (commas.length) {
			case 2:
			case 3:
				// Across all possible connect/disconnect overloads, the first and second arguments
				// can be SIGNAL expansion.
				return Collections.singletonList(new QObjectConnectCompletion(QObjectConnectCompletion.Param.Signal));
			case 4:
			case 5:
				// Across all possible connect/disconnect overloads, the first and second arguments
				// can be SIGNAL or SLOT expansions.
				return Collections.singletonList(new QObjectConnectCompletion(QObjectConnectCompletion.Param.Member));
			}

			return null;
		}

		if (astNode.getPropertyInParent() == IASTFunctionCallExpression.ARGUMENT) {
			IASTNode parent = astNode.getParent();
			if (!(parent instanceof IASTFunctionCallExpression))
				return null;

			// NOTE: QtConnectFunctionCall cannot be used here because that class expects a
			//       valid expression.  During content assist the function is still being
			//       created.

			IASTFunctionCallExpression call = (IASTFunctionCallExpression) parent;
			IASTExpression nameExpr = call.getFunctionNameExpression();
			IASTName funcName = null;
			if (nameExpr instanceof IASTIdExpression)
				funcName = ((IASTIdExpression) nameExpr).getName();
			else if (nameExpr instanceof ICPPASTFieldReference)
				funcName = ((ICPPASTFieldReference) nameExpr).getFieldName();

			// If this isn't a QObject::connect or QObject::disconnect function call then
			// look no further.
			if (!QtFunctionCallUtil.isQObjectFunctionCall(astContext, !context.isContextInformationStyle(), funcName))
				return null;

			// In a content assist context the argument that is currently being entered is
			// last in the function call.
			IASTInitializerClause[] args = call.getArguments();
			if (args == null
			 || args.length < 0)
				return null;
			int argIndex = args.length - 1;

			// Find the type node that is used for this expansion.
			IType targetType = QtFunctionCallUtil.getTargetType(call, args, argIndex);
			if (targetType == null)
				return null;

			// Returns completions for the given expansion using the given type as the
			// source for Qt methods.
			return getCompletionsFor(targetType, args[argIndex]);
		}

		return null;
	}

	public static Collection<ICompletionProposal> getProposals(
			ICEditorContentAssistInvocationContext context, IASTName name, IASTCompletionContext astContext, IASTNode astNode) {

		Collection<QObjectConnectCompletion> qtProposals = getConnectProposals(context, name, astContext, astNode);
		if (qtProposals == null
		 || qtProposals.isEmpty())
			return null;

		Collection<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
		for (QObjectConnectCompletion qtProposal : qtProposals)
			qtProposal.addProposals(proposals, context);
		return proposals;
	}
}

Back to the top