Skip to content

Commit 717a7df

Browse files
committed
refactor: rewrite the code to calculate highlighting state of a commit
Signed-off-by: leo <longshuang@msn.cn>
1 parent 57c494a commit 717a7df

1 file changed

Lines changed: 29 additions & 21 deletions

File tree

src/Models/CommitGraph.cs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public static CommitGraph Generate(List<Commit> commits, bool firstParentOnlyEna
8383
var ended = new List<PathHelper>();
8484
var offsetY = -halfHeight;
8585
var colorPicker = new ColorPicker();
86+
var defHighlighting = highlighting == CommitGraphHighlighting.All;
8687

8788
foreach (var commit in commits)
8889
{
@@ -94,7 +95,7 @@ public static CommitGraph Generate(List<Commit> commits, bool firstParentOnlyEna
9495
// Find first curves that links to this commit and marks others that links to this commit ended.
9596
var offsetX = 4 - halfWidth;
9697
var maxOffsetOld = unsolved.Count > 0 ? unsolved[^1].LastX : offsetX + unitWidth;
97-
var isHighlighted = false;
98+
var isHighlighted = defHighlighting;
9899
foreach (var l in unsolved)
99100
{
100101
if (l.Next.Equals(commit.SHA, StringComparison.Ordinal))
@@ -143,27 +144,34 @@ public static CommitGraph Generate(List<Commit> commits, bool firstParentOnlyEna
143144
// Calculate highlighted state
144145
if (!isHighlighted)
145146
{
146-
if (highlighting == CommitGraphHighlighting.All)
147+
switch (highlighting)
147148
{
148-
isHighlighted = true;
149-
}
150-
151-
if (!isHighlighted &&
152-
(highlighting == CommitGraphHighlighting.CurrentBranchOnly ||
153-
highlighting == CommitGraphHighlighting.CurrentBranchAndSelectedCommits))
154-
{
155-
isHighlighted = commit.IsMerged;
156-
}
157-
158-
if (!isHighlighted &&
159-
(highlighting == CommitGraphHighlighting.SelectedCommitsOnly ||
160-
highlighting == CommitGraphHighlighting.SelectedCommitsOnlyFirstParent ||
161-
highlighting == CommitGraphHighlighting.CurrentBranchAndSelectedCommits))
162-
{
163-
isHighlighted = highlightExtraCommits.Remove(commit.SHA);
164-
// Highlight first parent, other parents are dealt with later
165-
if (isHighlighted && commit.Parents.Count > 0)
166-
highlightExtraCommits.Add(commit.Parents[0]);
149+
case CommitGraphHighlighting.CurrentBranchOnly:
150+
isHighlighted = commit.IsMerged;
151+
break;
152+
case CommitGraphHighlighting.SelectedCommitsOnly:
153+
case CommitGraphHighlighting.SelectedCommitsOnlyFirstParent:
154+
if (highlightExtraCommits.Remove(commit.SHA))
155+
{
156+
isHighlighted = true;
157+
// Highlight first parent, other parents are dealt with later
158+
if (commit.Parents.Count > 0)
159+
highlightExtraCommits.Add(commit.Parents[0]);
160+
}
161+
break;
162+
default: // CommitGraphHighlighting.CurrentBranchAndSelectedCommits
163+
if (commit.IsMerged)
164+
{
165+
isHighlighted = true;
166+
}
167+
else if (highlightExtraCommits.Remove(commit.SHA))
168+
{
169+
isHighlighted = true;
170+
// Highlight first parent, other parents are dealt with later
171+
if (commit.Parents.Count > 0)
172+
highlightExtraCommits.Add(commit.Parents[0]);
173+
}
174+
break;
167175
}
168176
}
169177
commit.IsHighlightedInGraph = isHighlighted;

0 commit comments

Comments
 (0)