Skip to content

Commit 1aec7e2

Browse files
committed
refactor: code quality improvements
- Added try-catch blocks to async void event handlers for better error handling - Added capacity hints to List initializations to reduce allocations - Replaced string concatenation with string interpolation for better performance - Added ConfigureAwait(false) to async calls for better deadlock prevention - Improved error handling in TextDiffView with proper resource cleanup - Updated Count.cs documentation to clarify IDisposable requirement
1 parent db15d03 commit 1aec7e2

File tree

6 files changed

+93
-60
lines changed

6 files changed

+93
-60
lines changed

src/Commands/Diff.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ private void ProcessInlineHighlights()
271271
}
272272

273273
private readonly Models.DiffResult _result = new Models.DiffResult();
274-
private readonly List<Models.TextDiffLine> _deleted = new List<Models.TextDiffLine>();
275-
private readonly List<Models.TextDiffLine> _added = new List<Models.TextDiffLine>();
274+
private readonly List<Models.TextDiffLine> _deleted = new List<Models.TextDiffLine>(16);
275+
private readonly List<Models.TextDiffLine> _added = new List<Models.TextDiffLine>(16);
276276
private Models.TextDiffLine _last = null;
277277
private int _oldLine = 0;
278278
private int _newLine = 0;

src/Models/Count.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public Count(int value)
1313

1414
public void Dispose()
1515
{
16-
// Ignore
16+
// Empty implementation - required for DetailContext interface compatibility
1717
}
1818
}
1919
}

src/ViewModels/Preferences.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ private static Preferences Load()
626626
catch (Exception ex)
627627
{
628628
// Log the error and backup the corrupted file
629-
var backupPath = path + ".corrupted." + DateTime.Now.ToString("yyyyMMddHHmmss");
629+
var backupPath = $"{path}.corrupted.{DateTime.Now:yyyyMMddHHmmss}";
630630
try
631631
{
632632
File.Copy(path, backupPath, true);

src/ViewModels/Repository.GitOperations.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public async Task CompareBranchWithWorktreeAsync(Models.Branch branch)
187187
{
188188
SelectedSearchedCommit = null;
189189

190-
var target = await new Commands.QuerySingleCommit(_fullpath, branch.Head).GetResultAsync();
190+
var target = await new Commands.QuerySingleCommit(_fullpath, branch.Head).GetResultAsync().ConfigureAwait(false);
191191
_histories.AutoSelectedCommit = null;
192192
_histories.DetailContext = new RevisionCompare(_fullpath, target, null);
193193
}
@@ -218,7 +218,7 @@ public void CreateNewTag()
218218
/// <param name="tag">The tag to checkout</param>
219219
public async Task CheckoutTagAsync(Models.Tag tag)
220220
{
221-
var c = await new Commands.QuerySingleCommit(_fullpath, tag.SHA).GetResultAsync();
221+
var c = await new Commands.QuerySingleCommit(_fullpath, tag.SHA).GetResultAsync().ConfigureAwait(false);
222222
if (c != null)
223223
await _histories?.CheckoutBranchByCommitAsync(c);
224224
}

src/Views/StashesPage.axaml.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,26 @@ private void OnMainLayoutSizeChanged(object sender, SizeChangedEventArgs e)
2929

3030
private async void OnStashListKeyDown(object sender, KeyEventArgs e)
3131
{
32-
if (DataContext is ViewModels.StashesPage { SelectedStash: { } stash } vm)
32+
try
3333
{
34-
if (e.Key is Key.Delete or Key.Back)
34+
if (DataContext is ViewModels.StashesPage { SelectedStash: { } stash } vm)
3535
{
36-
vm.Drop(stash);
37-
e.Handled = true;
38-
}
39-
else if (e.Key is Key.C && e.KeyModifiers == (OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control))
40-
{
41-
await App.CopyTextAsync(stash.Message);
42-
e.Handled = true;
36+
if (e.Key is Key.Delete or Key.Back)
37+
{
38+
vm.Drop(stash);
39+
e.Handled = true;
40+
}
41+
else if (e.Key is Key.C && e.KeyModifiers == (OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control))
42+
{
43+
await App.CopyTextAsync(stash.Message);
44+
e.Handled = true;
45+
}
4346
}
4447
}
48+
catch (Exception ex)
49+
{
50+
App.RaiseException(string.Empty, $"Error in stash list key handler: {ex.Message}");
51+
}
4552
}
4653

4754
private void OnStashContextRequested(object sender, ContextRequestedEventArgs e)

src/Views/TextDiffView.axaml.cs

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -759,17 +759,24 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
759759

760760
private async void OnTextAreaKeyDown(object sender, KeyEventArgs e)
761761
{
762-
if (e.KeyModifiers.Equals(OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control))
762+
try
763763
{
764-
if (e.Key == Key.C)
764+
if (e.KeyModifiers.Equals(OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control))
765765
{
766-
await CopyWithoutIndicatorsAsync();
767-
e.Handled = true;
766+
if (e.Key == Key.C)
767+
{
768+
await CopyWithoutIndicatorsAsync();
769+
e.Handled = true;
770+
}
768771
}
769-
}
770772

771-
if (!e.Handled)
772-
base.OnKeyDown(e);
773+
if (!e.Handled)
774+
base.OnKeyDown(e);
775+
}
776+
catch (Exception ex)
777+
{
778+
App.RaiseException(string.Empty, $"Error in text area key handler: {ex.Message}");
779+
}
773780
}
774781

775782
private void OnBlockNavigationPropertyChanged(object _1, PropertyChangedEventArgs e)
@@ -1816,55 +1823,74 @@ private void RefreshBlockNavigation()
18161823

18171824
private async void OnStageChunk(object _1, RoutedEventArgs _2)
18181825
{
1819-
var chunk = SelectedChunk;
1820-
if (chunk == null)
1821-
return;
1822-
1823-
var diff = DataContext as Models.TextDiff;
1826+
try
1827+
{
1828+
var chunk = SelectedChunk;
1829+
if (chunk == null)
1830+
return;
18241831

1825-
var change = diff?.Option.WorkingCopyChange;
1826-
if (change == null)
1827-
return;
1832+
var diff = DataContext as Models.TextDiff;
18281833

1829-
var selection = diff.MakeSelection(chunk.StartIdx + 1, chunk.EndIdx + 1, chunk.Combined, chunk.IsOldSide);
1830-
if (!selection.HasChanges)
1831-
return;
1834+
var change = diff?.Option.WorkingCopyChange;
1835+
if (change == null)
1836+
return;
18321837

1833-
var repoView = this.FindAncestorOfType<Repository>();
1838+
var selection = diff.MakeSelection(chunk.StartIdx + 1, chunk.EndIdx + 1, chunk.Combined, chunk.IsOldSide);
1839+
if (!selection.HasChanges)
1840+
return;
18341841

1835-
if (repoView?.DataContext is not ViewModels.Repository repo)
1836-
return;
1842+
var repoView = this.FindAncestorOfType<Repository>();
18371843

1838-
repo.SetWatcherEnabled(false);
1844+
if (repoView?.DataContext is not ViewModels.Repository repo)
1845+
return;
18391846

1840-
if (!selection.HasLeftChanges)
1841-
{
1842-
await new Commands.Add(repo.FullPath, change).ExecAsync();
1843-
}
1844-
else
1845-
{
1846-
var tmpFile = Path.GetTempFileName();
1847-
if (change.WorkTree == Models.ChangeState.Untracked)
1848-
{
1849-
diff.GenerateNewPatchFromSelection(change, null, selection, false, tmpFile);
1850-
}
1851-
else if (chunk.Combined)
1847+
repo.SetWatcherEnabled(false);
1848+
try
18521849
{
1853-
var treeGuid = await new Commands.QueryStagedFileBlobGuid(diff.Repo, change.Path).GetResultAsync();
1854-
diff.GeneratePatchFromSelection(change, treeGuid, selection, false, tmpFile);
1850+
if (!selection.HasLeftChanges)
1851+
{
1852+
await new Commands.Add(repo.FullPath, change).ExecAsync();
1853+
}
1854+
else
1855+
{
1856+
var tmpFile = Path.GetTempFileName();
1857+
try
1858+
{
1859+
if (change.WorkTree == Models.ChangeState.Untracked)
1860+
{
1861+
diff.GenerateNewPatchFromSelection(change, null, selection, false, tmpFile);
1862+
}
1863+
else if (chunk.Combined)
1864+
{
1865+
var treeGuid = await new Commands.QueryStagedFileBlobGuid(diff.Repo, change.Path).GetResultAsync();
1866+
diff.GeneratePatchFromSelection(change, treeGuid, selection, false, tmpFile);
1867+
}
1868+
else
1869+
{
1870+
var treeGuid = await new Commands.QueryStagedFileBlobGuid(diff.Repo, change.Path).GetResultAsync();
1871+
diff.GeneratePatchFromSelectionSingleSide(change, treeGuid, selection, false, chunk.IsOldSide, tmpFile);
1872+
}
1873+
1874+
await new Commands.Apply(diff.Repo, tmpFile, true, "nowarn", "--cache --index").ExecAsync();
1875+
}
1876+
finally
1877+
{
1878+
if (File.Exists(tmpFile))
1879+
File.Delete(tmpFile);
1880+
}
1881+
}
1882+
1883+
repo.MarkWorkingCopyDirtyManually();
18551884
}
1856-
else
1885+
finally
18571886
{
1858-
var treeGuid = await new Commands.QueryStagedFileBlobGuid(diff.Repo, change.Path).GetResultAsync();
1859-
diff.GeneratePatchFromSelectionSingleSide(change, treeGuid, selection, false, chunk.IsOldSide, tmpFile);
1887+
repo.SetWatcherEnabled(true);
18601888
}
1861-
1862-
await new Commands.Apply(diff.Repo, tmpFile, true, "nowarn", "--cache --index").ExecAsync();
1863-
File.Delete(tmpFile);
18641889
}
1865-
1866-
repo.MarkWorkingCopyDirtyManually();
1867-
repo.SetWatcherEnabled(true);
1890+
catch (Exception ex)
1891+
{
1892+
App.RaiseException(string.Empty, $"Error staging chunk: {ex.Message}");
1893+
}
18681894
}
18691895

18701896
private async void OnUnstageChunk(object _1, RoutedEventArgs _2)

0 commit comments

Comments
 (0)