use of org.eclipse.jdt.core.search.TypeNameRequestor in project xtext-eclipse by eclipse.
the class JdtBasedSimpleTypeScope method internalGetAllElements.
@Override
protected Iterable<IEObjectDescription> internalGetAllElements() {
IJavaProject javaProject = getTypeProvider().getJavaProject();
if (javaProject == null)
return Collections.emptyList();
final List<IEObjectDescription> allScopedElements = Lists.newArrayListWithExpectedSize(25000);
try {
IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(new IJavaElement[] { javaProject });
for (Class<?> clazz : Primitives.ALL_PRIMITIVE_TYPES) {
IEObjectDescription primitive = createScopedElement(clazz.getName());
if (primitive != null)
allScopedElements.add(primitive);
}
TypeNameRequestor nameMatchRequestor = new TypeNameRequestor() {
@Override
public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) {
StringBuilder fqName = new StringBuilder(packageName.length + simpleTypeName.length + 1);
if (packageName.length != 0) {
fqName.append(packageName);
fqName.append('.');
}
for (char[] enclosingType : enclosingTypeNames) {
fqName.append(enclosingType);
fqName.append('.');
}
fqName.append(simpleTypeName);
String fullyQualifiedName = fqName.toString();
InternalEObject proxy = createProxy(fullyQualifiedName);
Map<String, String> userData = null;
if (enclosingTypeNames.length == 0) {
userData = ImmutableMap.of("flags", String.valueOf(modifiers));
} else {
userData = ImmutableMap.of("flags", String.valueOf(modifiers), "inner", "true");
}
IEObjectDescription eObjectDescription = EObjectDescription.create(getQualifiedNameConverter().toQualifiedName(fullyQualifiedName), proxy, userData);
if (eObjectDescription != null)
allScopedElements.add(eObjectDescription);
}
};
collectContents(searchScope, nameMatchRequestor);
} catch (JavaModelException e) {
// ignore
}
return allScopedElements;
}
use of org.eclipse.jdt.core.search.TypeNameRequestor in project xtext-eclipse by eclipse.
the class JavaTypeQuickfixes method getImportedTypesScope.
protected IScope getImportedTypesScope(EObject model, final String misspelled, final IScope actualScope, IJavaSearchScope scope) {
if (scope == null) {
return IScope.NULLSCOPE;
}
try {
final Set<String> visiblePackages = importsConfiguration.getImplicitlyImportedPackages((XtextResource) model.eResource());
final Set<String> importedTypes = Sets.newHashSet();
final Set<String> seen = Sets.newHashSet();
XImportSection importSection = importsConfiguration.getImportSection((XtextResource) model.eResource());
if (importSection != null) {
parseImportSection(importSection, new IAcceptor<String>() {
@Override
public void accept(String t) {
visiblePackages.add(t);
}
}, new IAcceptor<String>() {
@Override
public void accept(String t) {
importedTypes.add(t);
}
});
}
SearchEngine searchEngine = new SearchEngine();
final List<IEObjectDescription> validProposals = Lists.newArrayList();
for (String importedType : importedTypes) {
if (validProposals.size() <= 5 && seen.add(importedType)) {
int dot = importedType.lastIndexOf('.');
if (dot != -1) {
importedType = importedType.substring(dot + 1);
}
if (isSimilarTypeName(misspelled, importedType)) {
QualifiedName qualifiedName = qualifiedNameConverter.toQualifiedName(importedType);
for (IEObjectDescription element : actualScope.getElements(qualifiedName)) {
validProposals.add(new AliasedEObjectDescription(qualifiedName, element));
break;
}
}
}
}
try {
for (String visiblePackage : visiblePackages) {
if (validProposals.size() <= 5) {
searchEngine.searchAllTypeNames(visiblePackage.toCharArray(), SearchPattern.R_EXACT_MATCH, null, SearchPattern.R_EXACT_MATCH, IJavaSearchConstants.TYPE, scope, new TypeNameRequestor() {
@Override
public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) {
StringBuilder typeNameBuilder = new StringBuilder(simpleTypeName.length);
for (char[] enclosingType : enclosingTypeNames) {
typeNameBuilder.append(enclosingType);
typeNameBuilder.append('.');
}
typeNameBuilder.append(simpleTypeName);
String typeName = typeNameBuilder.toString();
if (isSimilarTypeName(misspelled, typeName)) {
String fqNameAsString = getQualifiedTypeName(packageName, enclosingTypeNames, simpleTypeName);
if (seen.add(fqNameAsString)) {
QualifiedName qualifiedName = qualifiedNameConverter.toQualifiedName(typeName);
for (IEObjectDescription element : actualScope.getElements(qualifiedName)) {
validProposals.add(new AliasedEObjectDescription(qualifiedName, element));
break;
}
}
}
}
}, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor() {
@Override
public boolean isCanceled() {
return validProposals.size() > 5;
}
});
}
}
} catch (OperationCanceledException exc) {
// enough proposals
}
return new SimpleScope(validProposals);
} catch (JavaModelException jme) {
return IScope.NULLSCOPE;
}
}
Aggregations