Skip to main content
summaryrefslogtreecommitdiffstats
blob: 189e37fa84fb46327d56299488599096e3321024 (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
/*******************************************************************************
 * Copyright (c) 2005 - 2007 committers of openArchitectureWare 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: committers of openArchitectureWare - initial API and
 * implementation
 ******************************************************************************/
package org.eclipse.xtend.shared.ui.core.search;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.internal.xpand2.ast.Definition;
import org.eclipse.internal.xpand2.ast.ExpandStatement;
import org.eclipse.internal.xpand2.ast.Template;
import org.eclipse.internal.xpand2.model.XpandDefinition;
import org.eclipse.internal.xtend.expression.ast.Identifier;
import org.eclipse.internal.xtend.expression.ast.OperationCall;
import org.eclipse.internal.xtend.expression.ast.SyntaxElement;
import org.eclipse.internal.xtend.xtend.ast.AbstractExtension;
import org.eclipse.internal.xtend.xtend.ast.Extension;
import org.eclipse.internal.xtend.xtend.ast.ExtensionFile;
import org.eclipse.xtend.shared.ui.Activator;
import org.eclipse.xtend.shared.ui.core.IXtendXpandProject;
import org.eclipse.xtend.shared.ui.core.IXtendXpandResource;
import org.eclipse.xtend.shared.ui.internal.XtendLog;

/**
 * Search engine for oAW.
 * 
 * @author Sven Efftinge (http://www.efftinge.de)
 * @author Peter Friese
 */
public class XtendXpandSearchEngine {

	public static List<Definition> findAllDefines(IXtendXpandProject project) {
		List<Definition> matches = new ArrayList<Definition>();
		IXtendXpandResource[] resources = project.getRegisteredResources();
		for (IXtendXpandResource res : resources) {

			if (res.getExtXptResource() instanceof Template) {
				Template tpl = (Template) res.getExtXptResource();
				for (XpandDefinition xdef : tpl.getDefinitions()) {
					if (xdef instanceof Definition) {
						Definition def = (Definition) xdef;
						matches.add(def);
					}
				}
			}
		}
		return matches;
	}

	public static List<SearchMatch> findAllOccurrences(IXtendXpandProject project, String identifier) {
		List<SearchMatch> matches = new ArrayList<SearchMatch>();
		IXtendXpandResource[] resources = project.getRegisteredResources();
		for (IXtendXpandResource res : resources) {

			if (res.getExtXptResource() instanceof Template) {
				Template tpl = (Template) res.getExtXptResource();
				for (XpandDefinition xdef : tpl.getDefinitions()) {
					if (xdef instanceof Definition) {
						Definition def = (Definition) xdef;
						if (def.getName().equals(identifier)) {
							int startOfDefnitionName = def.getDefName().getStart() - 1;
							int lengthOfDefinitionName = def.getDefName().getEnd() - def.getDefName().getStart();
							matches.add(new SearchMatch(startOfDefnitionName, lengthOfDefinitionName, res
									.getUnderlyingStorage()));
						}
					}
				}
				;
			}

			// there is no visitor api or something similar so far, so we have
			// to use a reflective mechanism...
			Set<ExpandStatement> ops = findRec(res.getExtXptResource(), ExpandStatement.class, new HashSet<Object>());
			for (ExpandStatement expr : ops) {
				Identifier definition = expr.getDefinition();
				String definitionFQN = definition.getValue();

				int lastIndexOf = definitionFQN.lastIndexOf(identifier);
				if (lastIndexOf > -1) {
					int startOfDefinition = definition.getStart() + lastIndexOf - 1;
					int lengthOfDefinition = identifier.length();
					matches.add(new SearchMatch(startOfDefinition, lengthOfDefinition, res.getUnderlyingStorage()));

				}

			}

		}
		for (IProject p : project.getProject().getProject().getReferencingProjects()) {
			IXtendXpandProject oawp = Activator.getExtXptModelManager().findProject(p);
			if (oawp != null) {
				matches.addAll(findAllOccurrences(oawp, identifier));
			}
		}
		return sort(matches);
	}

	/**
	 * finds all operation invocations for the given identifier. Ignores
	 * parameters and types as well as resource references (i.e. imports)
	 */
	public static List<SearchMatch> findReferences(IXtendXpandProject project, String identifier) {
		List<SearchMatch> matches = new ArrayList<SearchMatch>();
		IXtendXpandResource[] resources = project.getRegisteredResources();
		for (IXtendXpandResource res : resources) {
			// there is no visitor api or something similar so far, so we have
			// to use a reflective mechanism...
			Set<OperationCall> ops = findRec(res.getExtXptResource(), OperationCall.class, new HashSet<Object>());
			for (OperationCall expr : ops) {
				if (expr.getName().getValue().equals(identifier)) {
					matches.add(new SearchMatch(expr.getName().getStart(), expr.getName().getEnd() - expr.getStart()
							- 1, res.getUnderlyingStorage()));
				}
			}
		}
		for (IProject p : project.getProject().getProject().getReferencingProjects()) {
			IXtendXpandProject oawp = Activator.getExtXptModelManager().findProject(p);
			if (oawp != null) {
				matches.addAll(findReferences(oawp, identifier));
			}
		}
		return sort(matches);
	}

	/**
	 * finds all extension declarations for the given identifier. Ignores
	 * parameters and types as well as resource references (i.e. imports)
	 */
	public static List<SearchMatch> findDeclarations(IXtendXpandProject project, String identifier) {
		List<SearchMatch> matches = new ArrayList<SearchMatch>();
		IXtendXpandResource[] resources = project.getRegisteredResources();
		for (IXtendXpandResource res : resources) {
			if (res.getExtXptResource() instanceof ExtensionFile) {
				ExtensionFile ef = (ExtensionFile) res.getExtXptResource();
				for (Extension ext : ef.getExtensions()) {
					if (ext instanceof AbstractExtension) {
						AbstractExtension ae = (AbstractExtension) ext;
						if (ext.getName().equals(identifier)) {
							Identifier id = ae.getNameIdentifier();
							matches.add(new SearchMatch(id.getStart(), id.getEnd() - id.getStart() + 1 /*
																										 * sorry
																										 * for
																										 * the
																										 * "+1"-hack
																										 */, res.getUnderlyingStorage()));
						}
					}
				}
				;
			}
			if (res.getExtXptResource() instanceof Template) {
				Template tpl = (Template) res.getExtXptResource();
				for (XpandDefinition xdef : tpl.getDefinitions()) {
					if (xdef instanceof Definition) {
						Definition def = (Definition) xdef;
						if (def.getName().equals(identifier)) {
							matches.add(new SearchMatch(def.getDefName().getStart(), def.getDefName().getEnd()
									- def.getDefName().getStart(), res.getUnderlyingStorage()));
						}
					}
				}
				;
			}
		}
		try {
			for (IProject p : project.getProject().getProject().getReferencedProjects()) {
				IXtendXpandProject oawp = Activator.getExtXptModelManager().findProject(p);
				if (oawp != null) {
					matches.addAll(findDeclarations(oawp, identifier));
				}
			}
		} catch (CoreException e) {
			XtendLog.logError(e);
		}
		return sort(matches);
	}

	private static List<SearchMatch> sort(List<SearchMatch> searchmatches) {
		Collections.sort(searchmatches, new Comparator<SearchMatch>() {

			public int compare(SearchMatch o1, SearchMatch o2) {
				if (o1.getFile() == null) {
					return -1;
				}
				if (o2.getFile() == null) {
					return 1;
				}
				int fileCompare = o1.getFile().getName().compareTo(o2.getFile().getName());
				if (fileCompare == 0) {
					return ((Integer) o1.getOffSet()).compareTo(o2.getOffSet());
				}
				return fileCompare;
			}
		});
		return searchmatches;
	}

	private static <T extends SyntaxElement> Set<T> findRec(Object res, Class<T> clazz, Set<Object> visitedNodes) {
		if (visitedNodes.contains(res)) {
			return Collections.emptySet();
		}
		visitedNodes.add(res);
		Set<T> result = new HashSet<T>();
		if (clazz.isInstance(res)) {
			result.add((T) res);
		}

		Class instanceClass = res.getClass();
		Method[] methods = instanceClass.getMethods();
		for (Method method : methods) {
			int mod = method.getModifiers();
			if (Modifier.isPublic(mod) && !Modifier.isStatic(mod) && method.getName().startsWith("get")
					&& method.getParameterTypes().length == 0) {
				Class<?> pType = method.getReturnType();
				// if it's a SyntaxElement navigate it
				if (Collection.class.isAssignableFrom(pType) || SyntaxElement.class.isAssignableFrom(pType)) {
					Object invRes;
					try {
						invRes = method.invoke(res, new Object[] {});
						if (invRes instanceof Collection) {
							for (Object o : (Collection<?>) invRes) {
								result.addAll(findRec(o, clazz, visitedNodes));
							}
						} else if (invRes != null) {
							result.addAll(findRec(invRes, clazz, visitedNodes));
						}
					} catch (Exception e) {
						XtendLog.logError(e);
					}
				}
			}
		}
		return result;
	}
}

Back to the top