Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 326b1363de34738e220571575f674636add4010c (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
/*
 * Copyright (c) 2013 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.core.index;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Pattern;

import org.eclipse.cdt.core.dom.ILinkage;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexBinding;
import org.eclipse.cdt.core.index.IndexFilter;
import org.eclipse.cdt.internal.qt.core.ASTUtil;
import org.eclipse.cdt.internal.qt.core.pdom.AbstractQtPDOMClass;
import org.eclipse.cdt.internal.qt.core.pdom.QtPDOMQObject;
import org.eclipse.cdt.qt.core.QtKeywords;
import org.eclipse.cdt.qt.core.index.IQGadget;
import org.eclipse.cdt.qt.core.index.IQObject;
import org.eclipse.cdt.qt.core.index.IQmlRegistered;
import org.eclipse.cdt.qt.core.index.QtIndex;
import org.eclipse.core.runtime.CoreException;

public class QtIndexImpl extends QtIndex {

	private final CDTIndex cdtIndex;

	private static final Pattern QmlTypeNameRegex
		= Pattern.compile("^(?:"
				+ QtKeywords.QML_REGISTER_TYPE + '|' + QtKeywords.QML_REGISTER_UNCREATABLE_TYPE
				+ ")<.*>\0(.*)$");

	private static final IndexFilter QtLinkageFilter = new IndexFilter() {
		@Override
		public boolean acceptLinkage(ILinkage linkage) {
			return linkage.getLinkageID() == ILinkage.QT_LINKAGE_ID;
		}

		@Override
		public boolean acceptBinding(IBinding binding) throws CoreException {
			return true;
		}
	};

	public QtIndexImpl(CDTIndex cdtIndex) {
		this.cdtIndex = cdtIndex;
	}

	@Override
	public IQObject findQObject(String[] name) {
		return name == null ? null : cdtIndex.get(new QObjectImplAccessor(name));
	}

	@Override
	public IQGadget findQGadget(String[] name) {
		return name == null ? null : cdtIndex.get(new QGadgetImplAccessor(name));
	}

	@Override
	public Collection<IQmlRegistered> getQmlRegistered() {
		return cdtIndex.get(new QMLRegisteredAccessor());
	}

	private class QObjectImplAccessor implements CDTIndex.Accessor<IQObject> {

		private final char[] name;

		public QObjectImplAccessor(String[] qualName) {
			// QObjects are stored in the Qt linkage using their fully qualified name.
			name = ASTUtil.getFullyQualifiedName(qualName).toCharArray();
		}

		@Override
		public IQObject access(IIndex index) throws CoreException {

			// TODO can there be more than one result?
			for(IIndexBinding binding : index.findBindings(name, QtLinkageFilter, null))
				if (binding instanceof QtPDOMQObject)
					return new QObject(QtIndexImpl.this, cdtIndex, (QtPDOMQObject) binding);

			return null;
		}
	}

	private class QGadgetImplAccessor implements CDTIndex.Accessor<IQGadget> {

		private final char[][] name;

		public QGadgetImplAccessor(String[] qualName) {
			name = new char[qualName.length][];
			for(int i = 0; i < name.length; ++i)
				name[i] = qualName[i].toCharArray();
		}

		@Override
		public IQGadget access(IIndex index) throws CoreException {

			// TODO can there be more than one result?
			for(IIndexBinding binding : index.findBindings(name, QtLinkageFilter, null))
				if (binding instanceof AbstractQtPDOMClass)
					return new QGadget(QtIndexImpl.this, cdtIndex, (AbstractQtPDOMClass) binding);

			return null;
		}
	}

	private class QMLRegisteredAccessor implements CDTIndex.Accessor<Collection<IQmlRegistered>> {

		@Override
		public Collection<IQmlRegistered> access(IIndex index) throws CoreException {
			Collection<IQmlRegistered> types = null;
			for(IIndexBinding binding : index.findBindings(QmlTypeNameRegex, false, QtLinkageFilter, null)) {
				IQmlRegistered qml = QmlRegistered.create(QtIndexImpl.this, binding);
				if (qml != null) {
					if (types == null)
						types = new ArrayList<IQmlRegistered>();
					types.add(qml);
				}
			}

			return types == null ? Collections.<IQmlRegistered>emptyList() : types;
		}
	}
}

Back to the top