diff --git a/SuperPatch.Core/FileContentBuilder.cs b/SuperPatch.Core/FileContentBuilder.cs index c6d69d7..542ae25 100644 --- a/SuperPatch.Core/FileContentBuilder.cs +++ b/SuperPatch.Core/FileContentBuilder.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using DiffPatch.Data; @@ -24,21 +25,36 @@ public class FileContentBuilder internal static async Task LoadAsync(Workspace wrk, FileDiff file, Status.StatusDelegate status) { status?.InvokeAsync($"Loading {file.From}"); - byte[] fileContent = null; - try - { - fileContent = await wrk.Storage.GetFileAsync(file); - } - catch(System.Exception ex) - { - status?.InvokeAsync($"Error {ex.Message}"); - } + string fileContent = null; + + var diffAdd = wrk.PatchsSet + .SelectMany(x => x.Diff) + .FirstOrDefault(x => x.Type == FileChangeType.Add && + x.To == file.From); + if (diffAdd != null) + { + fileContent = + DiffPatch.PatchHelper.Patch(string.Empty, diffAdd.Chunks, "\n"); + } + + if (fileContent == null) + { + try + { + fileContent = System.Text.Encoding.UTF8.GetString( + await wrk.Storage.GetFileAsync(file)); + } + catch (System.Exception ex) + { + status?.InvokeAsync($"Error {ex.Message}"); + } + } status?.InvokeAsync($"Loaded {file.From}"); var contents = new FileContents() { FileName = file.From, - Contents = fileContent == null ? String.Empty : System.Text.Encoding.UTF8.GetString(fileContent), + Contents = fileContent ?? string.Empty, Status = FileContentsStatus.Loaded }; return contents;