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); }
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]; ITreeNode element = returnSymbol.SourceFile.GetPsiFile<PsiLanguage>(new DocumentRange(returnSymbol.SourceFile.Document, 0)).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; }