static TextSpan GetCommentRegion(this XContainer node, int position, TextSpan span)
        {
            var nodeAtPosition = node.FindAtOffset(position);

            // if the selection starts or ends in text, we want to preserve the
            // exact span the user has selected and split the text at that boundary
            if (nodeAtPosition is XText)
            {
                return(span);
            }

            if (nodeAtPosition is XComment ||
                nodeAtPosition is XProcessingInstruction ||
                nodeAtPosition is XCData)
            {
                return(nodeAtPosition.Span);
            }

            var nearestParentElement = nodeAtPosition.SelfAndParentsOfType <XElement> ().FirstOrDefault();

            if (nearestParentElement == null)
            {
                return(new TextSpan(position, 0));
            }

            var endSpan = nearestParentElement.ClosingTag;

            if (endSpan == null)
            {
                return(nodeAtPosition.Span);
            }

            int start = nearestParentElement.Span.Start;

            return(new TextSpan(start, endSpan.Span.End - start));
        }