public IFormattingRule CreateRule(Document document, int position)
            {
                MonoDevelopContainedDocument containedDocument = MonoDevelopContainedDocument.FromDocument(document);

                if (containedDocument == null)
                {
                    return(_noopRule);
                }

                List <TextSpan> spans = new List <TextSpan>();

                var root = document.GetSyntaxRootSynchronously(CancellationToken.None);
                var text = root.SyntaxTree.GetText(CancellationToken.None);

                spans.AddRange(containedDocument.GetEditorVisibleSpans());

                for (var i = 0; i < spans.Count; i++)
                {
                    var visibleSpan = spans[i];
                    if (visibleSpan.IntersectsWith(position) || visibleSpan.End == position)
                    {
                        return(containedDocument.GetBaseIndentationRule(root, text, spans, i));
                    }
                }

                // in razor (especially in @helper tag), it is possible for us to be asked for next line of visible span
                var line = text.Lines.GetLineFromPosition(position);

                if (line.LineNumber > 0)
                {
                    line = text.Lines[line.LineNumber - 1];

                    // find one that intersects with previous line
                    for (var i = 0; i < spans.Count; i++)
                    {
                        var visibleSpan = spans[i];
                        if (visibleSpan.IntersectsWith(line.Span))
                        {
                            return(containedDocument.GetBaseIndentationRule(root, text, spans, i));
                        }
                    }
                }

                throw new InvalidOperationException();
            }
            private bool IsContainedDocument(Document document)
            {
                MonoDevelopContainedDocument containedDocument = MonoDevelopContainedDocument.FromDocument(document);

                return(containedDocument != null);
            }