// From https://github.com/KirillOsenkov/XmlParser/blob/master/src/Microsoft.Language.Xml.Editor/SmartIndent/SmartIndent.cs#L39 public static int FindTotalParentChainIndent( SyntaxNodeBase node, int position, int indent, int indentSize, IIndentationService indentationService, ISyntaxFactsService syntaxFactsService) { var textSpanOpt = syntaxFactsService.GetFileSpanRoot(node); if (textSpanOpt == null) { return(indent); } var textSpan = textSpanOpt.Value; if (!textSpan.IsInRootFile) { return(indent); } if (position < textSpan.Span.Start || position > textSpan.Span.End) { return(indent); } foreach (var child in node.ChildNodes) { var childSpan = syntaxFactsService.GetFileSpanRoot(child); if (childSpan == null || !childSpan.Value.IsInRootFile) { continue; } var shouldIndent = indentationService.ShouldIndent(child); if (shouldIndent) { indent += indentSize; } if (position <= childSpan.Value.Span.End) { return(FindTotalParentChainIndent(child, position, indent, indentSize, indentationService, syntaxFactsService)); } if (shouldIndent) { indent -= indentSize; } } return(indent); }