public static IDirectory FindProjectRepositoryDirectory(this IDirectory root) { return root == null ? null : root.AncestorsAndSelf() .SelectMany(x => x.Directories("wraps")) .Where(x => x != null) .FirstOrDefault(); }
public static IEnumerable<XElement> SafeAncestorsAndSelf( this XElement element) { if (element == null) { yield break; } foreach (var elem in element.AncestorsAndSelf()) { yield return elem; } }
/// <summary> /// Gets the first namespace attribute for each prefix ascending in the hierarchy. This describes the current /// namespace prefixes available to the children nodes of the given <see cref="XElement"/>. /// </summary> /// <param name="element"></param> /// <returns></returns> public static IEnumerable<XAttribute> GetNamespacePrefixAttributes(this XElement element) { Contract.Requires<ArgumentNullException>(element != null); return element.AncestorsAndSelf() .Attributes() .Prepend(new XAttribute("xmlns", element.GetDefaultNamespace().NamespaceName)) .Where(i => i.IsNamespaceDeclaration) .GroupBy(i => i.Name.LocalName) .Select(i => i.First()) .Select(i => new XAttribute(i)); }
public static Uri AsUri(this XElement element) { var baseUris = ( from parent in element.AncestorsAndSelf() let xmlBase = parent.Attribute(XName.Get("base", NS_XML)) where xmlBase != null let parsedBaseUri = xmlBase.Value.ToUri() select parsedBaseUri ).Reverse(); if (baseUris.Count() == 0) return element.Value.ToUri(); if (baseUris.Count() == 1) return baseUris.First().Combine(element.Value); return baseUris.Skip(1).Aggregate(baseUris.First(), UriExtensions.Combine).Combine(element.Value); }
public static double Depth(this SyntaxNode node) { var b = node.AncestorsAndSelf().Count(); var d = 0; var q = new Queue<Tuple<SyntaxNode, int>>(); q.Enqueue(Tuple.Create(node, 0)); while (q.Count > 0) { var e = q.Dequeue(); foreach (var c in e.Item1.ChildNodes()) q.Enqueue(Tuple.Create(c, e.Item2 + 1)); if (e.Item2 > d) d = e.Item2; } return b + d; }
public static string GetPath(this XElement element) { return string.Join("\\", element.AncestorsAndSelf().Reverse() .Select(e => { var index = e.GetIndex(); if (index == 1) { return e.Name.LocalName; } return string.Format("{0}[{1}]", e.Name.LocalName, e.GetIndex()); })); }
/// <summary> /// Returns a filtered collection of <see cref="XElement"/>s within the containing <see cref="XDocument"/> /// which are reachable from the given <see cref="XElement"/> when considering ref scope. /// </summary> /// <param name="element"></param> /// <returns></returns> public static IEnumerable<XElement> RefElements(this XElement element) { Contract.Requires<ArgumentNullException>(element != null); // obtain root element var root = (XElement)element.AncestorsAndSelf() .Where(i => i.Interfaces<IRefRoot>().Any()) .DefaultIfEmpty(element.Document.Root) .FirstOrDefault(); // obtain all scopes this element is a member of var scopes = new HashSet<IRefScope>(element .Ancestors() .SelectMany(i => i.Interfaces<IRefScope>())); // return elements that share one of these scopes, and that are underneath the root foreach (var node in DescendantsAndSelfInRefScope(root, scopes)) yield return node; }
public static MemberDeclarationSyntax GetDeclaratedMember(this SyntaxNode node) { return node.AncestorsAndSelf().OfType<MemberDeclarationSyntax>().FirstOrDefault(); }
public static XElement GetAncestorOfSelfCI(this XElement x, string elementName) { XElement result = x.AncestorsAndSelf().Where(a => a.Name.LocalName.ToLower() == elementName).FirstOrDefault(); return result; }
public static string FullName(this ConvertedGameObject cg) { return cg.AncestorsAndSelf().Reverse().Aggregate("", (s, b) => s + "/" + b.name); }
public static Transform FindAncestorOrSelf(this ConvertedGameObject cg, Transform needle) { return cg.AncestorsAndSelf().FirstOrDefault((u) => u == needle); }
public static Transform FindAncestorOrSelf(this ConvertedGameObject cg, Func<Transform, bool> pred) { return cg.AncestorsAndSelf().FirstOrDefault(pred); }