Skip to content

Commit

Permalink
fix loading from new file
Browse files Browse the repository at this point in the history
  • Loading branch information
uazo committed Jul 16, 2023
1 parent a1152ee commit 4498819
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions SuperPatch.Core/FileContentBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DiffPatch.Data;

Expand All @@ -24,21 +25,36 @@ public class FileContentBuilder
internal static async Task<FileContents> 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;
Expand Down

0 comments on commit 4498819

Please sign in to comment.