@@ -40,12 +40,15 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
4040 {
4141 foreach ( Diagnostic diagnostic in context . Diagnostics )
4242 {
43- context . RegisterCodeFix (
44- CodeAction . Create (
45- DocumentationResources . PropertySummaryStartTextCodeFix ,
46- cancellationToken => GetTransformedDocumentAsync ( context . Document , diagnostic , cancellationToken ) ,
47- nameof ( PropertySummaryDocumentationCodeFixProvider ) ) ,
48- diagnostic ) ;
43+ if ( ! diagnostic . Properties . ContainsKey ( PropertySummaryDocumentationAnalyzer . NoCodeFixKey ) )
44+ {
45+ context . RegisterCodeFix (
46+ CodeAction . Create (
47+ DocumentationResources . PropertySummaryStartTextCodeFix ,
48+ cancellationToken => GetTransformedDocumentAsync ( context . Document , diagnostic , cancellationToken ) ,
49+ nameof ( PropertySummaryDocumentationCodeFixProvider ) ) ,
50+ diagnostic ) ;
51+ }
4952 }
5053
5154 return SpecializedTasks . CompletedTask ;
@@ -59,14 +62,18 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
5962 var documentation = node . GetDocumentationCommentTriviaSyntax ( ) ;
6063
6164 var summaryElement = ( XmlElementSyntax ) documentation . Content . GetFirstXmlElement ( XmlCommentHelper . SummaryXmlTag ) ;
62- var textElement = ( XmlTextSyntax ) summaryElement . Content . First ( ) ;
65+ var textElement = ( XmlTextSyntax ) summaryElement . Content . FirstOrDefault ( ) ;
66+ if ( textElement == null )
67+ {
68+ return document ;
69+ }
70+
6371 var textToken = textElement . TextTokens . First ( token => token . IsKind ( SyntaxKind . XmlTextLiteralToken ) ) ;
6472 var text = textToken . ValueText ;
65- var newTextBuilder = StringBuilderPool . Allocate ( ) ;
6673
6774 // preserve leading whitespace
6875 int index = 0 ;
69- while ( char . IsWhiteSpace ( text , index ) )
76+ while ( text . Length > index && char . IsWhiteSpace ( text , index ) )
7077 {
7178 index ++ ;
7279 }
@@ -85,14 +92,18 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
8592 modifiedText = text . Substring ( index ) ;
8693 }
8794
88- modifiedText = char . ToLowerInvariant ( modifiedText [ 0 ] ) + modifiedText . Substring ( 1 ) ;
95+ if ( modifiedText . Length > 0 )
96+ {
97+ modifiedText = char . ToLowerInvariant ( modifiedText [ 0 ] ) + modifiedText . Substring ( 1 ) ;
98+ }
8999
90100 // create the new text string
91101 var textToAdd = diagnostic . Properties [ PropertySummaryDocumentationAnalyzer . ExpectedTextKey ] ;
92102 var newText = $ "{ preservedWhitespace } { textToAdd } { modifiedText } ";
93103
94104 // replace the token
95- var newTextTokens = textElement . TextTokens . Replace ( textToken , SyntaxFactory . XmlTextLiteral ( textToken . LeadingTrivia , newText , newText , textToken . TrailingTrivia ) ) ;
105+ var newXmlTextLiteral = SyntaxFactory . XmlTextLiteral ( textToken . LeadingTrivia , newText , newText , textToken . TrailingTrivia ) ;
106+ var newTextTokens = textElement . TextTokens . Replace ( textToken , newXmlTextLiteral ) ;
96107 var newTextElement = textElement . WithTextTokens ( newTextTokens ) ;
97108
98109 var newSyntaxRoot = syntaxRoot . ReplaceNode ( textElement , newTextElement ) ;
0 commit comments