11// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
22
3+ using System ;
34using System . Collections . Generic ;
45using System . Collections . Immutable ;
56using System . IO ;
@@ -49,18 +50,21 @@ protected abstract Task<SourceText> FormatFileAsync(
4950 /// <summary>
5051 /// Applies formatting and returns the changed <see cref="SourceText"/> for each <see cref="Document"/>.
5152 /// </summary>
52- private ImmutableArray < ( Document , Task < ( SourceText originalText , SourceText formattedText ) > ) > FormatFiles (
53+ private ImmutableArray < ( Document , Task < ( SourceText originalText , SourceText ? formattedText ) > ) > FormatFiles (
5354 Solution solution ,
5455 ImmutableArray < ( DocumentId , OptionSet , ICodingConventionsSnapshot ) > formattableDocuments ,
5556 FormatOptions formatOptions ,
5657 ILogger logger ,
5758 CancellationToken cancellationToken )
5859 {
59- var formattedDocuments = ImmutableArray . CreateBuilder < ( Document , Task < ( SourceText originalText , SourceText formattedText ) > ) > ( formattableDocuments . Length ) ;
60+ var formattedDocuments = ImmutableArray . CreateBuilder < ( Document , Task < ( SourceText originalText , SourceText ? formattedText ) > ) > ( formattableDocuments . Length ) ;
6061
6162 foreach ( var ( documentId , options , codingConventions ) in formattableDocuments )
6263 {
6364 var document = solution . GetDocument ( documentId ) ;
65+ if ( document is null )
66+ continue ;
67+
6468 var formatTask = Task . Run ( async ( ) => await GetFormattedSourceTextAsync ( document , options , codingConventions , formatOptions , logger , cancellationToken ) . ConfigureAwait ( false ) , cancellationToken ) ;
6569
6670 formattedDocuments . Add ( ( document , formatTask ) ) ;
@@ -72,7 +76,7 @@ protected abstract Task<SourceText> FormatFileAsync(
7276 /// <summary>
7377 /// Get formatted <see cref="SourceText"/> for a <see cref="Document"/>.
7478 /// </summary>
75- private async Task < ( SourceText originalText , SourceText formattedText ) > GetFormattedSourceTextAsync (
79+ private async Task < ( SourceText originalText , SourceText ? formattedText ) > GetFormattedSourceTextAsync (
7680 Document document ,
7781 OptionSet options ,
7882 ICodingConventionsSnapshot codingConventions ,
@@ -83,7 +87,7 @@ protected abstract Task<SourceText> FormatFileAsync(
8387 var originalSourceText = await document . GetTextAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
8488 var formattedSourceText = await FormatFileAsync ( document , originalSourceText , options , codingConventions , formatOptions , logger , cancellationToken ) . ConfigureAwait ( false ) ;
8589
86- return ! formattedSourceText . ContentEquals ( originalSourceText ) || ! formattedSourceText . Encoding . Equals ( originalSourceText . Encoding )
90+ return ! formattedSourceText . ContentEquals ( originalSourceText ) || ! formattedSourceText . Encoding ? . Equals ( originalSourceText . Encoding ) == true
8791 ? ( originalSourceText , formattedSourceText )
8892 : ( originalSourceText , null ) ;
8993 }
@@ -93,7 +97,7 @@ protected abstract Task<SourceText> FormatFileAsync(
9397 /// </summary>
9498 private async Task < Solution > ApplyFileChangesAsync (
9599 Solution solution ,
96- ImmutableArray < ( Document , Task < ( SourceText originalText , SourceText formattedText ) > ) > formattedDocuments ,
100+ ImmutableArray < ( Document , Task < ( SourceText originalText , SourceText ? formattedText ) > ) > formattedDocuments ,
97101 FormatOptions formatOptions ,
98102 ILogger logger ,
99103 List < FormattedFile > formattedFiles ,
@@ -108,6 +112,11 @@ private async Task<Solution> ApplyFileChangesAsync(
108112 return formattedSolution ;
109113 }
110114
115+ if ( document ? . FilePath is null )
116+ {
117+ continue ;
118+ }
119+
111120 var ( originalText , formattedText ) = await formatTask . ConfigureAwait ( false ) ;
112121 if ( formattedText is null )
113122 {
@@ -128,6 +137,10 @@ private IEnumerable<FileChange> GetFileChanges(FormatOptions formatOptions, stri
128137 var fileChanges = new List < FileChange > ( ) ;
129138 var workspaceFolder = Path . GetDirectoryName ( workspacePath ) ;
130139 var changes = formattedText . GetChangeRanges ( originalText ) ;
140+ if ( workspaceFolder is null )
141+ {
142+ throw new Exception ( $ "Unable to fine directory name for '{ workspacePath } '") ;
143+ }
131144
132145 foreach ( var change in changes )
133146 {
0 commit comments