Skip to content

Commit ec17ea9

Browse files
authored
code_style: general cleanup (#1497)
1 parent baeef2d commit ec17ea9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+134
-236
lines changed

.github/workflows/build.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
container: ${{ matrix.container || '' }}
3232
steps:
3333
- name: Install common CLI tools
34-
if: ${{ startsWith(matrix.runtime, 'linux-') }}
34+
if: startsWith(matrix.runtime, 'linux-')
3535
run: |
3636
export DEBIAN_FRONTEND=noninteractive
3737
ln -fs /usr/share/zoneinfo/Etc/UTC /etc/localtime
@@ -45,7 +45,7 @@ jobs:
4545
with:
4646
dotnet-version: 9.0.x
4747
- name: Configure arm64 packages
48-
if: ${{ matrix.runtime == 'linux-arm64' }}
48+
if: matrix.runtime == 'linux-arm64'
4949
run: |
5050
sudo dpkg --add-architecture arm64
5151
echo 'deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports/ focal main restricted
@@ -55,7 +55,7 @@ jobs:
5555
sudo sed -i -e 's/^deb http/deb [arch=amd64] http/g' /etc/apt/sources.list
5656
sudo sed -i -e 's/^deb mirror/deb [arch=amd64] mirror/g' /etc/apt/sources.list
5757
- name: Install cross-compiling dependencies
58-
if: ${{ matrix.runtime == 'linux-arm64' }}
58+
if: matrix.runtime == 'linux-arm64'
5959
run: |
6060
sudo apt-get update
6161
sudo apt-get install -y llvm gcc-aarch64-linux-gnu
@@ -64,10 +64,10 @@ jobs:
6464
- name: Publish
6565
run: dotnet publish src/SourceGit.csproj -c Release -o publish -r ${{ matrix.runtime }}
6666
- name: Rename executable file
67-
if: ${{ startsWith(matrix.runtime, 'linux-') }}
67+
if: startsWith(matrix.runtime, 'linux-')
6868
run: mv publish/SourceGit publish/sourcegit
6969
- name: Tar artifact
70-
if: ${{ startsWith(matrix.runtime, 'linux-') || startsWith(matrix.runtime, 'osx-') }}
70+
if: startsWith(matrix.runtime, 'linux-') || startsWith(matrix.runtime, 'osx-')
7171
run: |
7272
tar -cvf "sourcegit.${{ matrix.runtime }}.tar" -C publish .
7373
rm -r publish/*

src/App.axaml.cs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,21 @@ public static void ShowWindow(object data, bool showAsDialog)
148148

149149
public static void RaiseException(string context, string message)
150150
{
151-
if (Current is App app && app._launcher != null)
151+
if (Current is App { _launcher: not null } app)
152152
app._launcher.DispatchNotification(context, message, true);
153153
}
154154

155155
public static void SendNotification(string context, string message)
156156
{
157-
if (Current is App app && app._launcher != null)
157+
if (Current is App { _launcher: not null } app)
158158
app._launcher.DispatchNotification(context, message, false);
159159
}
160160

161161
public static void SetLocale(string localeKey)
162162
{
163163
var app = Current as App;
164-
if (app == null)
165-
return;
166164

167-
var targetLocale = app.Resources[localeKey] as ResourceDictionary;
165+
var targetLocale = app?.Resources[localeKey] as ResourceDictionary;
168166
if (targetLocale == null || targetLocale == app._activeLocale)
169167
return;
170168

@@ -286,22 +284,14 @@ public static void SetFonts(string defaultFont, string monospaceFont, bool onlyU
286284

287285
public static async void CopyText(string data)
288286
{
289-
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
290-
{
291-
if (desktop.MainWindow?.Clipboard is { } clipboard)
292-
await clipboard.SetTextAsync(data ?? "");
293-
}
287+
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow.Clipboard: { } clipboard })
288+
await clipboard.SetTextAsync(data ?? "");
294289
}
295290

296291
public static async Task<string> GetClipboardTextAsync()
297292
{
298-
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
299-
{
300-
if (desktop.MainWindow?.Clipboard is { } clipboard)
301-
{
302-
return await clipboard.GetTextAsync();
303-
}
304-
}
293+
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow.Clipboard: { } clipboard })
294+
return await clipboard.GetTextAsync();
305295
return null;
306296
}
307297

@@ -562,7 +552,7 @@ private void TryLaunchAsNormal(IClassicDesktopStyleApplicationLifetime desktop)
562552
Models.AvatarManager.Instance.Start();
563553

564554
string startupRepo = null;
565-
if (desktop.Args != null && desktop.Args.Length == 1 && Directory.Exists(desktop.Args[0]))
555+
if (desktop.Args is { Length: 1 } && Directory.Exists(desktop.Args[0]))
566556
startupRepo = desktop.Args[0];
567557

568558
var pref = ViewModels.Preferences.Instance;

src/Commands/Command.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public bool Exec()
4343
proc.OutputDataReceived += (_, e) => HandleOutput(e.Data, errs);
4444
proc.ErrorDataReceived += (_, e) => HandleOutput(e.Data, errs);
4545

46-
var dummy = null as Process;
46+
Process dummy = null;
4747
var dummyProcLock = new object();
4848
try
4949
{

src/Commands/QueryCommits.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,7 @@ private void MarkFirstMerged()
132132
if (shas.Length == 0)
133133
return;
134134

135-
var set = new HashSet<string>();
136-
foreach (var sha in shas)
137-
set.Add(sha);
135+
var set = new HashSet<string>(shas);
138136

139137
foreach (var c in _commits)
140138
{

src/Commands/QueryRevisionFileNames.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ public List<string> Result()
1818
return [];
1919

2020
var lines = rs.StdOut.Split('\0', System.StringSplitOptions.RemoveEmptyEntries);
21-
var outs = new List<string>();
22-
foreach (var line in lines)
23-
outs.Add(line);
24-
return outs;
21+
return [.. lines];
2522
}
2623
}
2724
}

src/Commands/Worktree.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public Worktree(string repo)
1818

1919
var rs = ReadToEnd();
2020
var worktrees = new List<Models.Worktree>();
21-
var last = null as Models.Worktree;
21+
Models.Worktree last = null;
2222
if (rs.IsSuccess)
2323
{
2424
var lines = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);

src/Models/AvatarManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void Start()
5555
{
5656
while (true)
5757
{
58-
var email = null as string;
58+
string email = null;
5959

6060
lock (_synclock)
6161
{
@@ -79,7 +79,7 @@ public void Start()
7979
$"https://www.gravatar.com/avatar/{md5}?d=404";
8080

8181
var localFile = Path.Combine(_storePath, md5);
82-
var img = null as Bitmap;
82+
Bitmap img = null;
8383
try
8484
{
8585
var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(2) };
@@ -113,7 +113,7 @@ public void Start()
113113
_requesting.Remove(email);
114114
}
115115

116-
Dispatcher.UIThread.InvokeAsync(() =>
116+
Dispatcher.UIThread.Post(() =>
117117
{
118118
_resources[email] = img;
119119
NotifyResourceChanged(email, img);

src/Models/CommitGraph.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static CommitGraph Parse(List<Commit> commits, bool firstParentOnlyEnable
8282

8383
foreach (var commit in commits)
8484
{
85-
var major = null as PathHelper;
85+
PathHelper major = null;
8686
var isMerged = commit.IsMerged;
8787

8888
// Update current y offset

src/Models/CommitLink.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ public static List<CommitLink> Get(List<Remote> remotes)
2727
trimmedUrl = url.AsSpan(0, url.Length - 4);
2828

2929
if (url.StartsWith("https://github.com/", StringComparison.Ordinal))
30-
outs.Add(new($"Github ({trimmedUrl.Slice(19)})", $"{url}/commit/"));
30+
outs.Add(new($"Github ({trimmedUrl[19..]})", $"{url}/commit/"));
3131
else if (url.StartsWith("https://gitlab.", StringComparison.Ordinal))
32-
outs.Add(new($"GitLab ({trimmedUrl.Slice(trimmedUrl.Slice(15).IndexOf('/') + 16)})", $"{url}/-/commit/"));
32+
outs.Add(new($"GitLab ({trimmedUrl[(trimmedUrl[15..].IndexOf('/') + 16)..]})", $"{url}/-/commit/"));
3333
else if (url.StartsWith("https://gitee.com/", StringComparison.Ordinal))
34-
outs.Add(new($"Gitee ({trimmedUrl.Slice(18)})", $"{url}/commit/"));
34+
outs.Add(new($"Gitee ({trimmedUrl[18..]})", $"{url}/commit/"));
3535
else if (url.StartsWith("https://bitbucket.org/", StringComparison.Ordinal))
36-
outs.Add(new($"BitBucket ({trimmedUrl.Slice(22)})", $"{url}/commits/"));
36+
outs.Add(new($"BitBucket ({trimmedUrl[22..]})", $"{url}/commits/"));
3737
else if (url.StartsWith("https://codeberg.org/", StringComparison.Ordinal))
38-
outs.Add(new($"Codeberg ({trimmedUrl.Slice(21)})", $"{url}/commit/"));
38+
outs.Add(new($"Codeberg ({trimmedUrl[21..]})", $"{url}/commit/"));
3939
else if (url.StartsWith("https://gitea.org/", StringComparison.Ordinal))
40-
outs.Add(new($"Gitea ({trimmedUrl.Slice(18)})", $"{url}/commit/"));
40+
outs.Add(new($"Gitea ({trimmedUrl[18..]})", $"{url}/commit/"));
4141
else if (url.StartsWith("https://git.sr.ht/", StringComparison.Ordinal))
42-
outs.Add(new($"sourcehut ({trimmedUrl.Slice(18)})", $"{url}/commit/"));
42+
outs.Add(new($"sourcehut ({trimmedUrl[18..]})", $"{url}/commit/"));
4343
}
4444
}
4545

src/Models/DealWithChangesAfterStashing.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,15 @@
22

33
namespace SourceGit.Models
44
{
5-
public class DealWithChangesAfterStashing
5+
public class DealWithChangesAfterStashing(string label, string desc)
66
{
7-
public string Label { get; set; }
8-
public string Desc { get; set; }
7+
public string Label { get; set; } = label;
8+
public string Desc { get; set; } = desc;
99

1010
public static readonly List<DealWithChangesAfterStashing> Supported = [
1111
new ("Discard", "All (or selected) changes will be discarded"),
1212
new ("Keep Index", "Staged changes are left intact"),
1313
new ("Keep All", "All (or selected) changes are left intact"),
1414
];
15-
16-
public DealWithChangesAfterStashing(string label, string desc)
17-
{
18-
Label = label;
19-
Desc = desc;
20-
}
2115
}
2216
}

src/Models/DiffResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public void GeneratePatchFromSelection(Change change, string fileTreeGuid, TextD
199199
writer.WriteLine($"+++ b/{change.Path}");
200200

201201
// If last line of selection is a change. Find one more line.
202-
var tail = null as string;
202+
string tail = null;
203203
if (selection.EndLine < Lines.Count)
204204
{
205205
var lastLine = Lines[selection.EndLine - 1];
@@ -323,7 +323,7 @@ public void GeneratePatchFromSelectionSingleSide(Change change, string fileTreeG
323323
writer.WriteLine($"+++ b/{change.Path}");
324324

325325
// If last line of selection is a change. Find one more line.
326-
var tail = null as string;
326+
string tail = null;
327327
if (selection.EndLine < Lines.Count)
328328
{
329329
var lastLine = Lines[selection.EndLine - 1];

src/Models/ExternalMerger.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,9 @@ public string[] GetPatterns()
9696
{
9797
return Exec.Split(';');
9898
}
99-
else
100-
{
101-
var patterns = new List<string>();
102-
var choices = Exec.Split(';', StringSplitOptions.RemoveEmptyEntries);
103-
foreach (var c in choices)
104-
{
105-
patterns.Add(Path.GetFileName(c));
106-
}
107-
return patterns.ToArray();
108-
}
99+
100+
var choices = Exec.Split(';', StringSplitOptions.RemoveEmptyEntries);
101+
return Array.ConvertAll(choices, Path.GetFileName);
109102
}
110103
}
111104
}

src/Models/TextInlineChange.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static List<TextInlineChange> Compare(string oldValue, string newValue)
5050
var ret = new List<TextInlineChange>();
5151
var posOld = 0;
5252
var posNew = 0;
53-
var last = null as TextInlineChange;
53+
TextInlineChange last = null;
5454
do
5555
{
5656
while (posOld < sizeOld && posNew < sizeNew && !chunksOld[posOld].Modified && !chunksNew[posNew].Modified)

src/Native/Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ private void FixWindowFrameOnWin10(Window w)
257257
{
258258
// Schedule the DWM frame extension to run in the next render frame
259259
// to ensure proper timing with the window initialization sequence
260-
Dispatcher.UIThread.InvokeAsync(() =>
260+
Dispatcher.UIThread.Post(() =>
261261
{
262262
var platformHandle = w.TryGetPlatformHandle();
263263
if (platformHandle == null)

src/ViewModels/Blame.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private set
3434

3535
public bool IsBinary
3636
{
37-
get => _data != null && _data.IsBinary;
37+
get => _data?.IsBinary ?? false;
3838
}
3939

4040
public bool CanBack

src/ViewModels/BranchCompare.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public List<Models.Change> SelectedChanges
4949
{
5050
if (SetProperty(ref _selectedChanges, value))
5151
{
52-
if (value != null && value.Count == 1)
52+
if (value?.Count == 1)
5353
DiffContext = new DiffContext(_repo, new Models.DiffOption(_based.Head, _to.Head, value[0]), _diffContext);
5454
else
5555
DiffContext = null;

src/ViewModels/BranchTreeNode.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using Avalonia;
4+
using AvaloniaEdit.Utils;
45
using CommunityToolkit.Mvvm.ComponentModel;
56

67
namespace SourceGit.ViewModels
@@ -86,8 +87,7 @@ public Builder(Models.BranchSortMode localSortMode, Models.BranchSortMode remote
8687

8788
public void SetExpandedNodes(List<string> expanded)
8889
{
89-
foreach (var node in expanded)
90-
_expanded.Add(node);
90+
_expanded.AddRange(expanded);
9191
}
9292

9393
public void Run(List<Models.Branch> branches, List<Models.Remote> remotes, bool bForceExpanded)
@@ -165,7 +165,7 @@ private void MakeBranchNode(Models.Branch branch, List<BranchTreeNode> roots, Di
165165
return;
166166
}
167167

168-
var lastFolder = null as BranchTreeNode;
168+
BranchTreeNode lastFolder = null;
169169
var start = 0;
170170

171171
while (sepIdx != -1)
@@ -250,8 +250,7 @@ private void SortNodesByTime(List<BranchTreeNode> nodes)
250250
{
251251
if (r.Backend is Models.Branch)
252252
return r.TimeToSort == l.TimeToSort ? Models.NumericSort.Compare(l.Name, r.Name) : r.TimeToSort.CompareTo(l.TimeToSort);
253-
else
254-
return 1;
253+
return 1;
255254
}
256255

257256
if (r.Backend is Models.Branch)

src/ViewModels/CheckoutAndFastForward.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public override Task<bool> Sure()
4949
var updateSubmodules = IsRecurseSubmoduleVisible && RecurseSubmodules;
5050
return Task.Run(() =>
5151
{
52-
var succ = false;
52+
bool succ;
5353
var needPopStash = false;
5454

5555
if (!_repo.ConfirmCheckoutBranch())

src/ViewModels/Clone.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public override Task<bool> Sure()
151151
{
152152
var node = Preferences.Instance.FindOrAddNodeByRepositoryPath(path, null, true);
153153
var launcher = App.GetLauncher();
154-
var page = null as LauncherPage;
154+
LauncherPage page = null;
155155
foreach (var one in launcher.Pages)
156156
{
157157
if (one.Node.Id == _pageId)

src/ViewModels/ConfigureWorkspace.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public Workspace Selected
1717
set
1818
{
1919
if (SetProperty(ref _selected, value))
20-
CanDeleteSelected = value != null && !value.IsActive;
20+
CanDeleteSelected = value is { IsActive: false };
2121
}
2222
}
2323

src/ViewModels/ConventionalCommitMessageBuilder.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,9 @@ public bool Apply()
7272
builder.Append(")");
7373
}
7474

75-
if (string.IsNullOrEmpty(_breakingChanges))
76-
builder.Append(": ");
77-
else
78-
builder.Append("!: ");
75+
if (!string.IsNullOrEmpty(_breakingChanges))
76+
builder.Append("!");
77+
builder.Append(": ");
7978

8079
builder.Append(_description);
8180
builder.Append("\n\n");

0 commit comments

Comments
 (0)