예제 #1
0
        public static IDeclaredElement GetPrimaryDeclaredElementForClass(IClass @class)
        {
            string            className = @class.ShortName;
            var               cache     = @class.GetPsiServices().Solution.GetComponent <PsiCache>();
            List <IPsiSymbol> symbols   = cache.GetSymbols(PsiRenamesFactory.NameFromCamelCase(className)).ToList();

            if (symbols.Count > 0)
            {
                IPsiSymbol symbol  = symbols.ToArray()[0];
                ITreeNode  element =
                    symbol.SourceFile.GetPsiFile <PsiLanguage>(new DocumentRange(symbol.SourceFile.Document, 0)).FindNodeAt(new TreeTextRange(new TreeOffset(symbol.Offset), 1));
                while (element != null)
                {
                    if (element is IDeclaredElement)
                    {
                        {
                            return((IDeclaredElement)element);
                        }
                    }
                    element = element.Parent;
                }
                {
                    return(null);
                }
            }
            return(null);
        }
예제 #2
0
        public static IDeclaredElement GetPrimaryDeclaredElementForMethod(IMethod declaredElement)
        {
            string methodName = declaredElement.ShortName;

            if ("parse".Equals(methodName.Substring(0, "parse".Length)))
            {
                string            ruleName = methodName.Substring("parse".Length, methodName.Length - "parse".Length);
                var               cache    = declaredElement.GetPsiServices().Solution.GetComponent <PsiCache>();
                List <IPsiSymbol> symbols  = cache.GetSymbols(PsiRenamesFactory.NameFromCamelCase(ruleName)).ToList();
                if (symbols.Count > 0)
                {
                    IPsiSymbol               symbol            = symbols.ToArray()[0];
                    ITreeNode                element           = symbol.SourceFile.GetPsiFile <PsiLanguage>(new DocumentRange(symbol.SourceFile.Document, 0)).FindNodeAt(new TreeTextRange(new TreeOffset(symbol.Offset), 1));
                    List <PsiOptionSymbol>   parserPackageName = cache.GetOptionSymbols("parserPackage").ToList();
                    List <PsiOptionSymbol>   parserClassName   = cache.GetOptionSymbols("parserClassName").ToList();
                    IList <IDeclaredElement> classes           = new List <IDeclaredElement>();
                    foreach (PsiOptionSymbol packageName in parserPackageName)
                    {
                        foreach (PsiOptionSymbol className in parserClassName)
                        {
                            if (packageName.SourceFile == className.SourceFile)
                            {
                                IPsiSourceFile sourceFile = packageName.SourceFile;
                                CollectionUtil.AddRange(
                                    classes, sourceFile.PsiModule.GetPsiServices().CacheManager.GetDeclarationsCache(sourceFile.PsiModule, false, true).
                                    GetTypeElementsByCLRName(packageName.Value + "." + className.Value));
                            }
                        }
                    }

                    var parentClass = declaredElement.GetContainingType() as IClass;
                    if (parentClass != null)
                    {
                        if (classes.Contains(parentClass))
                        {
                            while (element != null)
                            {
                                if (element is IDeclaredElement)
                                {
                                    {
                                        return((IDeclaredElement)element);
                                    }
                                }
                                element = element.Parent;
                            }
                        }
                    }
                    {
                        return(null);
                    }
                }
            }
            else
            {
                return(GetPrimaryDeclaredElementForVisitorMethod(declaredElement, declaredElement));
            }
            return(null);
        }
        public static IDeclaredElement GetPrimaryDeclaredElementForInterface(IInterface @interface)
        {
            string interfaceName = @interface.ShortName;
            var    cache         = @interface.GetPsiServices().Solution.GetComponent <PsiCache>();
            IEnumerable <PsiOptionSymbol> prefixes = cache.GetOptionSymbols("\"interfaceNamePrefix\"");
            IList <IPsiSymbol>            symbols  = new List <IPsiSymbol>();

            foreach (PsiOptionSymbol prefix in prefixes)
            {
                string prefixValue = prefix.Value;
                if ((prefixValue.Length < interfaceName.Length) && (prefixValue.Equals(interfaceName.Substring(0, prefixValue.Length))))
                {
                    string            shortInterfaceName = PsiRenamesFactory.NameFromCamelCase(interfaceName.Substring(prefixValue.Length, interfaceName.Length - prefixValue.Length));
                    List <IPsiSymbol> allSymbols         = cache.GetSymbols(PsiRenamesFactory.NameFromCamelCase(shortInterfaceName)).ToList();
                    foreach (IPsiSymbol symbol in allSymbols)
                    {
                        if (symbol.SourceFile == prefix.SourceFile)
                        {
                            symbols.Add(symbol);
                        }
                    }
                }
            }

            if (symbols.Count == 0)
            {
                return(null);
            }

            // TODO: move symbol binding logic to the cache, remove copy-paste.
            IPsiSymbol returnSymbol = Enumerable.ToArray(symbols)[0];
            var        psiFile      = returnSymbol.SourceFile.GetPsiFile <PsiLanguage>(new DocumentRange(returnSymbol.SourceFile.Document, 0));

            if (psiFile != null)
            {
                ITreeNode element = psiFile.FindNodeAt(new TreeTextRange(new TreeOffset(returnSymbol.Offset), 1));
                while (element != null)
                {
                    var ret = element as IDeclaredElement;
                    if (ret != null)
                    {
                        return(ret);
                    }

                    element = element.Parent;
                }
            }
            return(null);
        }
예제 #4
0
 public FindResultPsiSymbol([NotNull] IPsiSymbol symbol, IProjectFile projectFile)
 {
     mySymbol      = symbol;
     myProjectFile = projectFile;
 }
 public FindResultPsiSymbol([NotNull] IPsiSymbol symbol, IProjectFile projectFile)
 {
   mySymbol = symbol;
   myProjectFile = projectFile;
 }