11using System ;
22using System . Collections . Generic ;
3+ using System . Linq ;
34using System . Threading . Tasks ;
45using DiffPatch . Data ;
56
@@ -24,21 +25,36 @@ public class FileContentBuilder
2425 internal static async Task < FileContents > LoadAsync ( Workspace wrk , FileDiff file , Status . StatusDelegate status )
2526 {
2627 status ? . InvokeAsync ( $ "Loading { file . From } ") ;
27- byte [ ] fileContent = null ;
28- try
29- {
30- fileContent = await wrk . Storage . GetFileAsync ( file ) ;
31- }
32- catch ( System . Exception ex )
33- {
34- status ? . InvokeAsync ( $ "Error { ex . Message } ") ;
35- }
28+ string fileContent = null ;
29+
30+ var diffAdd = wrk . PatchsSet
31+ . SelectMany ( x => x . Diff )
32+ . FirstOrDefault ( x => x . Type == FileChangeType . Add &&
33+ x . To == file . From ) ;
34+ if ( diffAdd != null )
35+ {
36+ fileContent =
37+ DiffPatch . PatchHelper . Patch ( string . Empty , diffAdd . Chunks , "\n " ) ;
38+ }
39+
40+ if ( fileContent == null )
41+ {
42+ try
43+ {
44+ fileContent = System . Text . Encoding . UTF8 . GetString (
45+ await wrk . Storage . GetFileAsync ( file ) ) ;
46+ }
47+ catch ( System . Exception ex )
48+ {
49+ status ? . InvokeAsync ( $ "Error { ex . Message } ") ;
50+ }
51+ }
3652 status ? . InvokeAsync ( $ "Loaded { file . From } ") ;
3753
3854 var contents = new FileContents ( )
3955 {
4056 FileName = file . From ,
41- Contents = fileContent == null ? String . Empty : System . Text . Encoding . UTF8 . GetString ( fileContent ) ,
57+ Contents = fileContent ?? string . Empty ,
4258 Status = FileContentsStatus . Loaded
4359 } ;
4460 return contents ;
0 commit comments