Skip to main content
summaryrefslogtreecommitdiffstats
blob: d9d5effbe7a2bb15d00b486a9a2bb42f976f4f90 (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.jsdt.internal.core;

import java.util.ArrayList;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.wst.jsdt.core.IJavaScriptElement;
import org.eclipse.wst.jsdt.core.IOpenable;
import org.eclipse.wst.jsdt.core.IPackageFragment;
import org.eclipse.wst.jsdt.core.IType;
import org.eclipse.wst.jsdt.core.JavaScriptCore;
import org.eclipse.wst.jsdt.core.JavaScriptModelException;
import org.eclipse.wst.jsdt.core.UnimplementedException;
import org.eclipse.wst.jsdt.core.WorkingCopyOwner;
import org.eclipse.wst.jsdt.core.compiler.CharOperation;
import org.eclipse.wst.jsdt.core.infer.IInferenceFile;
import org.eclipse.wst.jsdt.core.search.IJavaScriptSearchConstants;
import org.eclipse.wst.jsdt.core.search.IJavaScriptSearchScope;
import org.eclipse.wst.jsdt.core.search.SearchPattern;
import org.eclipse.wst.jsdt.internal.codeassist.ISearchRequestor;
import org.eclipse.wst.jsdt.internal.compiler.env.AccessRestriction;
import org.eclipse.wst.jsdt.internal.compiler.env.IBinaryType;
import org.eclipse.wst.jsdt.internal.compiler.env.ICompilationUnit;
import org.eclipse.wst.jsdt.internal.compiler.env.INameEnvironment;
import org.eclipse.wst.jsdt.internal.compiler.env.ISourceType;
import org.eclipse.wst.jsdt.internal.compiler.env.NameEnvironmentAnswer;
import org.eclipse.wst.jsdt.internal.compiler.impl.ITypeRequestor;
import org.eclipse.wst.jsdt.internal.core.search.BasicSearchEngine;
import org.eclipse.wst.jsdt.internal.core.search.IRestrictedAccessBindingRequestor;
import org.eclipse.wst.jsdt.internal.core.search.IRestrictedAccessTypeRequestor;

/**
 * This class provides a <code>SearchableBuilderEnvironment</code> for code
 * assist which uses the Java model as a search tool.
 */
public class SearchableEnvironment implements INameEnvironment,
		IJavaScriptSearchConstants {

	public NameLookup nameLookup;

	protected ICompilationUnit unitToSkip;

	protected org.eclipse.wst.jsdt.core.IJavaScriptUnit[] workingCopies;

	protected JavaProject javaProject;

	protected IJavaScriptSearchScope searchScope;

	protected boolean checkAccessRestrictions;

	/**
	 * Creates a SearchableEnvironment on the given project
	 */
	public SearchableEnvironment(JavaProject project,
			IRestrictedAccessBindingRequestor resolutionScope,
			org.eclipse.wst.jsdt.core.IJavaScriptUnit[] workingCopies)
			throws JavaScriptModelException {

		this.javaProject = project;
		this.checkAccessRestrictions = !JavaScriptCore.IGNORE.equals(project
				.getOption(JavaScriptCore.COMPILER_PB_FORBIDDEN_REFERENCE, true))
				|| !JavaScriptCore.IGNORE.equals(project.getOption(
						JavaScriptCore.COMPILER_PB_DISCOURAGED_REFERENCE, true));
		this.workingCopies = workingCopies;
		this.nameLookup = javaProject.newNameLookup(workingCopies);
		this.nameLookup.setRestrictedAccessRequestor(resolutionScope);
		// Create search scope with visible entry on the project's classpath
		if (false){//this.checkAccessRestrictions) {
			this.searchScope = BasicSearchEngine
					.createJavaSearchScope(this.nameLookup.packageFragmentRoots );
				} else {
			this.searchScope = BasicSearchEngine
					.createJavaSearchScope(this.nameLookup.packageFragmentRoots);
		}
		this.nameLookup.searchScope=this.searchScope;
	}
	public SearchableEnvironment(JavaProject project,

			org.eclipse.wst.jsdt.core.IJavaScriptUnit[] workingCopies)
			throws JavaScriptModelException {
		this(project,null,workingCopies);
	}
	/**
	 * Creates a SearchableEnvironment on the given project
	 */
	public SearchableEnvironment(JavaProject project, WorkingCopyOwner owner)
			throws JavaScriptModelException {
		this(project, owner == null ? null : JavaModelManager
				.getJavaModelManager()
				.getWorkingCopies(owner, true/* add primary WCs */));
	}

	public SearchableEnvironment(JavaProject project, IRestrictedAccessBindingRequestor resolutionScope,WorkingCopyOwner owner)
	throws JavaScriptModelException {
		this(project, resolutionScope, owner == null ? null : JavaModelManager
		.getJavaModelManager()
		.getWorkingCopies(owner, true/* add primary WCs */));
}
	private static int convertSearchFilterToModelFilter(int searchFilter) {
		switch (searchFilter) {
			case IJavaScriptSearchConstants.CLASS:
				return NameLookup.ACCEPT_CLASSES;
			case IJavaScriptSearchConstants.INTERFACE:
				return NameLookup.ACCEPT_INTERFACES;
			case IJavaScriptSearchConstants.ENUM:
				return NameLookup.ACCEPT_ENUMS;
			case IJavaScriptSearchConstants.ANNOTATION_TYPE:
				return NameLookup.ACCEPT_ANNOTATIONS;
			case IJavaScriptSearchConstants.CLASS_AND_ENUM:
				return NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_ENUMS;
			case IJavaScriptSearchConstants.CLASS_AND_INTERFACE:
				return NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES;
			default:
				return NameLookup.ACCEPT_ALL;
		}
	}

	/**
	 * Returns the given type in the the given package if it exists, otherwise
	 * <code>null</code>.
	 */
	protected NameEnvironmentAnswer find(String typeName, String packageName) {
		if (packageName == null)
			packageName = IPackageFragment.DEFAULT_PACKAGE_NAME;
		NameLookup.Answer answer = this.nameLookup.findType(typeName,
				packageName, false/* exact match */, NameLookup.ACCEPT_ALL,
				this.checkAccessRestrictions);
		if (answer != null) {
			// construct name env answer
			if (answer.type instanceof BinaryType) { // BinaryType
				try {
					return new NameEnvironmentAnswer(
							(IBinaryType) ((BinaryType) answer.type)
									.getElementInfo(), answer.restriction);
				} catch (JavaScriptModelException npe) {
					return null;
				}
			} else { // SourceType
				try {
					// retrieve the requested type
					SourceTypeElementInfo sourceType = (SourceTypeElementInfo) ((SourceType) answer.type)
							.getElementInfo();
					if (answer.type.isBinary())
					{
						ICompilationUnit compUnit=(ICompilationUnit)answer.type.getClassFile();
						return new NameEnvironmentAnswer(compUnit,answer.restriction);
					}
					ISourceType topLevelType = sourceType;
					while (topLevelType.getEnclosingType() != null) {
						topLevelType = topLevelType.getEnclosingType();
					}
					IType[] types = null;
					org.eclipse.wst.jsdt.core.IJavaScriptUnit compilationUnit = sourceType.getHandle().getJavaScriptUnit();
					// find all siblings (other types declared in same unit,
					// since may be used for name resolution)
					if (compilationUnit!=null)
					 types = compilationUnit.getTypes();
					else if (sourceType.getHandle().getClassFile()!=null)
						 types = sourceType.getHandle().getClassFile().getTypes();

					ISourceType[] sourceTypes = new ISourceType[types.length];

					// in the resulting collection, ensure the requested type is
					// the first one
					sourceTypes[0] = sourceType;
					int length = types.length;
					for (int i = 0, index = 1; i < length; i++) {
						ISourceType otherType = (ISourceType) ((JavaElement) types[i])
								.getElementInfo();
						if (!otherType.equals(topLevelType) && index < length) // check
																				// that
																				// the
																				// index
																				// is
																				// in
																				// bounds
																				// (see
																				// https://bugs.eclipse.org/bugs/show_bug.cgi?id=62861)
							sourceTypes[index++] = otherType;
					}
					return new NameEnvironmentAnswer(sourceTypes,
							answer.restriction);
				} catch (JavaScriptModelException npe) {
					return null;
				}
			}
		}
		return null;
	}

	/**
	 * Returns the given type in the the given package if it exists, otherwise
	 * <code>null</code>.
	 */
	protected NameEnvironmentAnswer findBinding(String typeName, String packageName,
			int type, boolean returnMultiple, String excludePath) {
		if (packageName == null)
			packageName = IPackageFragment.DEFAULT_PACKAGE_NAME;
		NameLookup.Answer answer =
			this.nameLookup.findBinding(
				typeName,
				packageName,
				type,
				false/* exact match */,
				NameLookup.ACCEPT_ALL,
				this.checkAccessRestrictions,  returnMultiple,  excludePath);
		if (answer != null && answer.element!=null) {
			if (answer.element instanceof IJavaScriptElement)
			{
				IOpenable openable = ((IJavaScriptElement)answer.element).getOpenable();

				ICompilationUnit compilationUnit=	null;
				if (openable instanceof ClassFile) {
					ClassFile classFile = (ClassFile) openable;
					compilationUnit=classFile;
				}
				else if (openable instanceof MetadataFile) {
					return new NameEnvironmentAnswer(((MetadataFile)openable).getAPIs());
				}
				else if (openable instanceof ICompilationUnit) {
					compilationUnit=(ICompilationUnit)openable;
				}
				return new NameEnvironmentAnswer(compilationUnit,answer.restriction);

			}
			else if (answer.element!=null && answer.element.getClass().isArray())
			{
				Object [] elements=(Object [])answer.element;
				ICompilationUnit[] units = new ICompilationUnit[elements.length];
				System.arraycopy(elements, 0, units, 0, elements.length);
				return new NameEnvironmentAnswer(units,answer.restriction);
			}
				// return new NameEnvironmentAnswer((IBinaryType) ((BinaryType)
				// answer.type).getElementInfo(), answer.restriction);
		}
		return null;
	}

	/**
	 * Find the packages that start with the given prefix. A valid prefix is a
	 * qualified name separated by periods (ex. java.util). The packages found
	 * are passed to: ISearchRequestor.acceptPackage(char[][] packageName)
	 */
	public void findPackages(char[] prefix, ISearchRequestor requestor) {
		this.nameLookup.seekPackageFragments(new String(prefix), true,
				new SearchableEnvironmentRequestor(requestor));
	}

	/**
	 * Find the top-level types that are defined
	 * in the current environment and whose simple name matches the given name.
	 *
	 * The types found are passed to one of the following methods (if additional
	 * information is known about the types):
	 *    ISearchRequestor.acceptType(char[][] packageName, char[] typeName)
	 *    ISearchRequestor.acceptClass(char[][] packageName, char[] typeName, int modifiers)
	 *    ISearchRequestor.acceptInterface(char[][] packageName, char[] typeName, int modifiers)
	 *
	 * This method can not be used to find member types... member
	 * types are found relative to their enclosing type.
	 */
	public void findExactTypes(char[] name, final boolean findMembers, int searchFor, final ISearchRequestor storage) {

		try {
			final String excludePath;
			if (this.unitToSkip != null) {
				if (!(this.unitToSkip instanceof IJavaScriptElement)) {
					// revert to model investigation
					findExactTypes(
						new String(name),
						storage,
						convertSearchFilterToModelFilter(searchFor));
					return;
				}
				excludePath = ((IJavaScriptElement) this.unitToSkip).getPath().toString();
			} else {
				excludePath = null;
			}

			IProgressMonitor progressMonitor = new IProgressMonitor() {
				boolean isCanceled = false;
				public void beginTask(String n, int totalWork) {
					// implements interface method
				}
				public void done() {
					// implements interface method
				}
				public void internalWorked(double work) {
					// implements interface method
				}
				public boolean isCanceled() {
					return isCanceled;
				}
				public void setCanceled(boolean value) {
					isCanceled = value;
				}
				public void setTaskName(String n) {
					// implements interface method
				}
				public void subTask(String n) {
					// implements interface method
				}
				public void worked(int work) {
					// implements interface method
				}
			};
			IRestrictedAccessTypeRequestor typeRequestor = new IRestrictedAccessTypeRequestor() {
				public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path, AccessRestriction access) {
					if (excludePath != null && excludePath.equals(path))
						return;
					if (!findMembers && enclosingTypeNames != null && enclosingTypeNames.length > 0)
						return; // accept only top level types
					storage.acceptType(packageName, path.toCharArray(),simpleTypeName, enclosingTypeNames, modifiers, access);
				}
			};
			try {
				new BasicSearchEngine(this.workingCopies).searchAllTypeNames(
					null,
					SearchPattern.R_EXACT_MATCH,
					name,
					SearchPattern.R_EXACT_MATCH,
					searchFor,
					this.searchScope,
					typeRequestor,
					CANCEL_IF_NOT_READY_TO_SEARCH,
					progressMonitor);
			} catch (OperationCanceledException e) {
				findExactTypes(
					new String(name),
					storage,
					convertSearchFilterToModelFilter(searchFor));
			}
		} catch (JavaScriptModelException e) {
			findExactTypes(
				new String(name),
				storage,
				convertSearchFilterToModelFilter(searchFor));
		}
	}

	/**
	 * Returns all types whose simple name matches with the given <code>name</code>.
	 */
	private void findExactTypes(String name, ISearchRequestor storage, int type) {
		SearchableEnvironmentRequestor requestor =
			new SearchableEnvironmentRequestor(storage, this.unitToSkip, this.javaProject, this.nameLookup);
		this.nameLookup.seekTypes(name, null, false, type, requestor);
	}


	/**
	 * @see org.eclipse.wst.jsdt.internal.compiler.env.INameEnvironment#findType(char[][])
	 */
	public NameEnvironmentAnswer findType(char[][] compoundTypeName,
			ITypeRequestor requestor) {
		if (compoundTypeName == null)
			return null;

		int length = compoundTypeName.length;
		if (length <= 1) {
			if (length == 0)
				return null;
			return find(new String(compoundTypeName[0]), null);
		}

		int lengthM1 = length - 1;
		char[][] packageName = new char[lengthM1][];
		System.arraycopy(compoundTypeName, 0, packageName, 0, lengthM1);

		return find(new String(compoundTypeName[lengthM1]), CharOperation
				.toString(packageName));
	}

	public NameEnvironmentAnswer findBinding(char[] typeName,
			char[][] packageName, int type, ITypeRequestor requestor, boolean returnMultiple, String excludePath) {
		if (typeName == null)
			return null;

		return findBinding(new String(typeName), packageName == null
				|| packageName.length == 0 ? null : CharOperation
				.toString(packageName), type,  returnMultiple,excludePath );
	}

	/**
	 * @see org.eclipse.wst.jsdt.internal.compiler.env.INameEnvironment#findType(char[],
	 *      char[][])
	 */
	public NameEnvironmentAnswer findType(char[] name, char[][] packageName,
			ITypeRequestor requestor) {
		if (name == null)
			return null;

		return find(new String(name), packageName == null
				|| packageName.length == 0 ? null : CharOperation
				.toString(packageName));
	}

	/**
	 * Find the top-level types that are defined in the
	 * current environment and whose name starts with the given prefix. The
	 * prefix is a qualified name separated by periods or a simple name (ex.
	 * java.util.V or V).
	 *
	 * The types found are passed to one of the following methods (if additional
	 * information is known about the types):
	 * ISearchRequestor.acceptType(char[][] packageName, char[] typeName)
	 * ISearchRequestor.acceptClass(char[][] packageName, char[] typeName, int
	 * modifiers) ISearchRequestor.acceptInterface(char[][] packageName, char[]
	 * typeName, int modifiers)
	 *
	 * This method can not be used to find member types... member types are
	 * found relative to their enclosing type.
	 */
	public void findTypes(char[] prefix, final boolean findMembers,
			boolean camelCaseMatch, int searchFor, final ISearchRequestor storage) {

		/*
		 * if (true){ findTypes(new String(prefix), storage,
		 * NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES); return; }
		 */
		try {
			final String excludePath;
			if (this.unitToSkip != null) {
				if (!(this.unitToSkip instanceof IJavaScriptElement)) {
					// revert to model investigation
					findTypes(new String(prefix), storage,
							convertSearchFilterToModelFilter(searchFor));
					return;
				}
				excludePath = ((IJavaScriptElement) this.unitToSkip).getPath()
						.toString();
			} else {
				excludePath = null;
			}
			int lastDotIndex = CharOperation.lastIndexOf('.', prefix);
			char[] qualification, simpleName;
			if (lastDotIndex < 0) {
				qualification = null;
				if (camelCaseMatch) {
					simpleName = prefix;
				} else {
					simpleName = CharOperation.toLowerCase(prefix);
				}
			} else {
				qualification = CharOperation.subarray(prefix, 0, lastDotIndex);
				if (camelCaseMatch) {
					simpleName = CharOperation.subarray(prefix,
							lastDotIndex + 1, prefix.length);
				} else {
					simpleName = CharOperation.toLowerCase(CharOperation
							.subarray(prefix, lastDotIndex + 1, prefix.length));
				}
			}

			IProgressMonitor progressMonitor = new IProgressMonitor() {
				boolean isCanceled = false;

				public void beginTask(String name, int totalWork) {
					// implements interface method
				}

				public void done() {
					// implements interface method
				}

				public void internalWorked(double work) {
					// implements interface method
				}

				public boolean isCanceled() {
					return isCanceled;
				}

				public void setCanceled(boolean value) {
					isCanceled = value;
				}

				public void setTaskName(String name) {
					// implements interface method
				}

				public void subTask(String name) {
					// implements interface method
				}

				public void worked(int work) {
					// implements interface method
				}
			};
			IRestrictedAccessTypeRequestor typeRequestor = new IRestrictedAccessTypeRequestor() {
				public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path, AccessRestriction access) {
					if (excludePath != null && excludePath.equals(path))
						return;
					if (!findMembers && enclosingTypeNames != null && enclosingTypeNames.length > 0)
						return; // accept only top level types
					storage.acceptType(packageName, path.toCharArray(),simpleTypeName, enclosingTypeNames, modifiers, access);
				}
			};
			try {
				int matchRule = SearchPattern.R_PREFIX_MATCH;
				if (camelCaseMatch)
					matchRule |= SearchPattern.R_CAMELCASE_MATCH;
				new BasicSearchEngine(this.workingCopies).searchAllTypeNames(
						qualification,
						SearchPattern.R_EXACT_MATCH,
						simpleName,
						matchRule, // not case sensitive
						searchFor,
						this.searchScope,
						typeRequestor, CANCEL_IF_NOT_READY_TO_SEARCH,
						progressMonitor);
			} catch (OperationCanceledException e) {
				findTypes(new String(prefix), storage, convertSearchFilterToModelFilter(searchFor));
			}
		} catch (JavaScriptModelException e) {
			findTypes(new String(prefix), storage, NameLookup.ACCEPT_ALL);
		}
	}



	/**
	 * Find the top-level types (classes and interfaces) that are defined in the
	 * current environment and whose name starts with the given prefix. The
	 * prefix is a qualified name separated by periods or a simple name (ex.
	 * java.util.V or V).
	 *
	 * The types found are passed to one of the following methods (if additional
	 * information is known about the types):
	 * ISearchRequestor.acceptType(char[][] packageName, char[] typeName)
	 * ISearchRequestor.acceptClass(char[][] packageName, char[] typeName, int
	 * modifiers) ISearchRequestor.acceptInterface(char[][] packageName, char[]
	 * typeName, int modifiers)
	 *
	 * This method can not be used to find member types... member types are
	 * found relative to their enclosing type.
	 */
	public void findBindings(char[] prefix, final int bindingType,
			boolean camelCaseMatch, final ISearchRequestor storage) {

		/*
		 * if (true){ findTypes(new String(prefix), storage,
		 * NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES); return; }
		 */
		try {
			final String excludePath;
			if (this.unitToSkip != null) {
				if (!(this.unitToSkip instanceof IJavaScriptElement)) {
					// revert to model investigation
					findBindings(new String(prefix),bindingType, storage,
							NameLookup.ACCEPT_ALL);
					return;
				}
				excludePath = ((IJavaScriptElement) this.unitToSkip).getPath()
						.toString();
			} else {
				excludePath = null;
			}
			int lastDotIndex = CharOperation.lastIndexOf('.', prefix);
			char[] qualification, simpleName;
			if (lastDotIndex < 0) {
				qualification = null;
				if (camelCaseMatch) {
					simpleName = prefix;
				} else {
					simpleName = CharOperation.toLowerCase(prefix);
				}
			} else {
				qualification = CharOperation.subarray(prefix, 0, lastDotIndex);
				if (camelCaseMatch) {
					simpleName = CharOperation.subarray(prefix,
							lastDotIndex + 1, prefix.length);
				} else {
					simpleName = CharOperation.toLowerCase(CharOperation
							.subarray(prefix, lastDotIndex + 1, prefix.length));
				}
			}

			IProgressMonitor progressMonitor = new IProgressMonitor() {
				boolean isCanceled = false;

				public void beginTask(String name, int totalWork) {
					// implements interface method
				}

				public void done() {
					// implements interface method
				}

				public void internalWorked(double work) {
					// implements interface method
				}

				public boolean isCanceled() {
					return isCanceled;
				}

				public void setCanceled(boolean value) {
					isCanceled = value;
				}

				public void setTaskName(String name) {
					// implements interface method
				}

				public void subTask(String name) {
					// implements interface method
				}

				public void worked(int work) {
					// implements interface method
				}
			};
			IRestrictedAccessBindingRequestor bindingRequestor = new IRestrictedAccessBindingRequestor() {
				String exclude;
				public boolean acceptBinding(int type,int modifiers, char[] packageName,
						char[] simpleTypeName,
						String path, AccessRestriction access) {
					if (exclude!=null && exclude.equals(path))
						return false;
					if (excludePath != null && excludePath.equals(path))
						return false;
					storage.acceptBinding(packageName, path.toCharArray(),simpleTypeName,type,
							  modifiers, access);
					return true;
				}


				public String getFoundPath() {

					return null;
				}

				public void reset() {}


				public ArrayList getFoundPaths() {
					return null;
				}


				public void setExcludePath(String excludePath) {
					this.exclude=excludePath;

				}
			};
			try {
				int matchRule = SearchPattern.R_PREFIX_MATCH;
				if (camelCaseMatch)
					matchRule |= SearchPattern.R_CAMELCASE_MATCH;
				new BasicSearchEngine(this.workingCopies).searchAllBindingNames(
						qualification,
						simpleName,
						bindingType,
						matchRule, // not case sensitive
						/*IJavaScriptSearchConstants.TYPE,*/ this.searchScope,
						bindingRequestor, CANCEL_IF_NOT_READY_TO_SEARCH,
						true,
						progressMonitor);
			} catch (OperationCanceledException e) {
				findBindings(new String(prefix),bindingType, storage, NameLookup.ACCEPT_ALL);
			}
		} catch (JavaScriptModelException e) {
			findTypes(new String(prefix), storage, NameLookup.ACCEPT_ALL);
		}
	}


	/**
	 * Returns all types whose name starts with the given (qualified)
	 * <code>prefix</code>.
	 *
	 * If the <code>prefix</code> is unqualified, all types whose simple name
	 * matches the <code>prefix</code> are returned.
	 */
	private void findTypes(String prefix, ISearchRequestor storage, int type) {
		// TODO (david) should add camel case support
		SearchableEnvironmentRequestor requestor = new SearchableEnvironmentRequestor(
				storage, this.unitToSkip, this.javaProject, this.nameLookup);
		int index = prefix.lastIndexOf('.');
		if (index == -1) {
			this.nameLookup.seekTypes(prefix, null, true, type, requestor);
		} else {
			String packageName = prefix.substring(0, index);
			JavaElementRequestor elementRequestor = new JavaElementRequestor();
			this.nameLookup.seekPackageFragments(packageName, false,
					elementRequestor);
			IPackageFragment[] fragments = elementRequestor
					.getPackageFragments();
			if (fragments != null) {
				String className = prefix.substring(index + 1);
				for (int i = 0, length = fragments.length; i < length; i++)
					if (fragments[i] != null)
						this.nameLookup.seekTypes(className, fragments[i],
								true, type, requestor);
			}
		}
	}

	private void findBindings(String prefix, int bindingType, ISearchRequestor storage, int type) {
		SearchableEnvironmentRequestor requestor = new SearchableEnvironmentRequestor(
				storage, this.unitToSkip, this.javaProject, this.nameLookup);
		int index = prefix.lastIndexOf('.');
		if (index == -1) {
			this.nameLookup.seekBindings(prefix, bindingType,null, true, type, requestor);
		} else {
			throw new UnimplementedException("shouldnt get here"); //$NON-NLS-1$
//			String packageName = prefix.substring(0, index);
//			JavaElementRequestor elementRequestor = new JavaElementRequestor();
//			this.nameLookup.seekPackageFragments(packageName, false,
//					elementRequestor);
//			IPackageFragment[] fragments = elementRequestor
//					.getPackageFragments();
//			if (fragments != null) {
//				String className = prefix.substring(index + 1);
//				for (int i = 0, length = fragments.length; i < length; i++)
//					if (fragments[i] != null)
//						this.nameLookup.seekTypes(className, fragments[i],
//								true, type, requestor);
//			}
		}
	}
	/**
	 * @see org.eclipse.wst.jsdt.internal.compiler.env.INameEnvironment#isPackage(char[][],
	 *      char[])
	 */
	public boolean isPackage(char[][] parentPackageName, char[] subPackageName) {
		String[] pkgName;
		if (parentPackageName == null)
			pkgName = new String[] { new String(subPackageName) };
		else {
			int length = parentPackageName.length;
			pkgName = new String[length + 1];
			for (int i = 0; i < length; i++)
				pkgName[i] = new String(parentPackageName[i]);
			pkgName[length] = new String(subPackageName);
		}
		return this.nameLookup.isPackage(pkgName);
	}

	/**
	 * Returns a printable string for the array.
	 */
	protected String toStringChar(char[] name) {
		return "[" //$NON-NLS-1$
				+ new String(name) + "]"; //$NON-NLS-1$
	}

	/**
	 * Returns a printable string for the array.
	 */
	protected String toStringCharChar(char[][] names) {
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < names.length; i++) {
			result.append(toStringChar(names[i]));
		}
		return result.toString();
	}

	public void cleanup() {
		// nothing to do
	}
	
	public void setCompilationUnit(IInferenceFile file)
	{
		nameLookup.setScriptFile(file);
	}
}

Back to the top