Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 473afce493cdab116765a9d93d6bf1b9ae49ba17 (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
/*
 * Copyright (c) 2013, 2015 QNX Software Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package org.eclipse.cdt.internal.qt.ui;

import java.util.Collection;
import java.util.Collections;

import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.internal.corext.template.c.CContextType;
import org.eclipse.cdt.internal.corext.template.c.TranslationUnitContext;
import org.eclipse.cdt.internal.corext.template.c.TranslationUnitContextType;
import org.eclipse.cdt.internal.ui.text.template.TemplateEngine.CTemplateProposal;
import org.eclipse.cdt.ui.text.contentassist.ICEditorContentAssistInvocationContext;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.templates.Template;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.ui.texteditor.ITextEditor;

@SuppressWarnings("restriction")
public class QObjectDeclarationCompletion {

	private static final String TEMPLATE = "class ${name} : public ${QObject}\n{\nQ_OBJECT\n\n${cursor}\n};";

	private final static TranslationUnitContextType context;
	static {
		context = new CContextType();
		context.setId(CContextType.ID);
	}

	public static Collection<ICompletionProposal> getProposals(ICEditorContentAssistInvocationContext ctx,
			IASTName name) {

		String token = name.getLastName().toString();
		if (token.isEmpty() || !"class".startsWith(token))
			return null;

		Position position = getPosition(ctx);
		if (position == null)
			return null;

		TranslationUnitContext tuCtx = context.createContext(ctx.getDocument(), position, ctx.getTranslationUnit());
		IRegion region = new Region(position.getOffset(), position.getLength());

		Template template = new Template("class", "QObject declaration", CContextType.ID, TEMPLATE, true);
		return Collections.<ICompletionProposal>singletonList(
				new CTemplateProposal(template, tuCtx, region, Activator.getQtLogo()));
	}

	private static Position getPosition(ICEditorContentAssistInvocationContext context) {
		ITextEditor textEditor = context.getEditor().getAdapter(ITextEditor.class);
		if (textEditor == null)
			return null;

		ISelectionProvider selectionProvider = textEditor.getSelectionProvider();
		if (selectionProvider == null)
			return null;

		ISelection selection = selectionProvider.getSelection();
		if (!(selection instanceof ITextSelection))
			return null;

		ITextSelection text = (ITextSelection) selection;
		return new Position(text.getOffset(), text.getLength());
	}
}

Back to the top