Get all IDeclaredType-s in current project (for a ReSharper Generator plugin)

105 views Asked by At

I'm writing a ReSharper 7.1 Generator plugin and need to get a list of all types declared in the current project (classes, interfaces and structs - IDeclaredType-s) for the GeneratorProviderBase<CSharpGeneratorContext>.Populate method.

With regular reflection it would be as simple as Assembly.GetTypes(), but here it's proven to be quite a challenge. Is there a way to do this?

I've searched high and low, the docs and samples didn't help, then looked through every *Extensions and *Util class, but couldn't find anything useful...

2

There are 2 answers

0
mbinic On BEST ANSWER

Here's what I came up with using the cache. It still doesn't seem right, but I expect it to be better than the previous approach. This is still called from a GeneratorProvider.Populate:

public static IEnumerable<ICSharpTypeDeclaration> GetAllPublicTypeDeclarations(this IPsiModule module)
{
    var declarationCache = module.GetPsiServices().CacheManager.GetDeclarationsCache(module, false, true);
    var declarations = new List<ICSharpTypeDeclaration>();

    foreach (var shortName in declarationCache.GetAllShortNames())
    {
        var declaredElements = declarationCache.GetElementsByShortName(shortName).OfType<ITypeElement>().Where(e => 
        {
            var elementType = e.GetElementType();
            return elementType == CLRDeclaredElementType.CLASS || elementType == CLRDeclaredElementType.INTERFACE || elementType == CLRDeclaredElementType.ENUM;
        });

        foreach (ITypeElement declaredElement in declaredElements)
        {
            var declaration = declaredElement.GetDeclarations().OfType<ICSharpTypeDeclaration>().FirstOrDefault(d => d.GetAccessRights() == AccessRights.PUBLIC);
            if (declaration != null)
            {
                declarations.Add(declaration);
            }
        }
    }

    return declarations;
}

Note: The results won't be the same as in the first answer, cause I've changed some restrictions. Also, in both cases, I'm not concerned with partial classes.

6
mbinic On

I managed to do what I needed, but I'm not really sure if it's the right/best approach:

[GeneratorElementProvider("MyGeneratorProvider", typeof(CSharpLanguage))]
public class MyGeneratorProvider : GeneratorProviderBase<CSharpGeneratorContext>
{
    public override double Priority
    {
        get { return 0; }
    }

    public override void Populate(CSharpGeneratorContext context)
    {
        var projectCsFiles = GetAllCSharpFilesInProject(context.PsiModule);
        var declarations = projectCsFiles.SelectMany(GetDeclarationsFromCSharpFile).ToList();
        context.ProvidedElements.AddRange(declarations.Select(d => new GeneratorDeclarationElement(d)));
    }

    private static IEnumerable<ICSharpFile> GetAllCSharpFilesInProject(IPsiModule projectModule)
    {
        PsiManager psiManager = projectModule.GetPsiServices().PsiManager;
        return projectModule.SourceFiles.SelectMany(f => psiManager.GetPsiFiles<CSharpLanguage>(f).OfType<ICSharpFile>());
    }

    private static IEnumerable<ITypeDeclaration> GetDeclarationsFromCSharpFile(ICSharpFile file)
    {
        return file.NamespaceDeclarationNodes.SelectMany(GetDeclarationsFromCSharpNamespace);
    }

    private static IEnumerable<ITypeDeclaration> GetDeclarationsFromCSharpNamespace(ICSharpNamespaceDeclaration namespaceDeclaration)
    {
        foreach (var namespaceChild in namespaceDeclaration.Body.Children())
        {
            var classDeclaration = namespaceChild as IClassDeclaration;
            if (classDeclaration != null)
            {
                yield return classDeclaration;
            }
            else
            {
                var childNamespace = namespaceChild as ICSharpNamespaceDeclaration;
                if (childNamespace != null)
                {
                    foreach (var declaration in GetDeclarationsFromCSharpNamespace(childNamespace))
                    {
                        yield return declaration;
                    }
                }
            }
        }
    }
}

Any comments or simpler (maybe even built-in) ways of doing this?