Skip to content

Commit 8235c0d

Browse files
committed
refactor: better way to calculate length of commit subject
- All empty line before valid commit message content will be ignored by git - Whitespace at end-of-line will be ignored by git - Whitespace at begining-of-line will be used by git - For multiple-lines in subject, it will be replaced by single space by git when querying commit's subject Signed-off-by: leo <longshuang@msn.cn>
1 parent 52ade74 commit 8235c0d

File tree

1 file changed

+29
-36
lines changed

1 file changed

+29
-36
lines changed

src/Views/CommitMessageToolBox.axaml.cs

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,16 @@ public override void Render(DrawingContext context)
143143

144144
lines.Sort((l, r) => l.StartOffset - r.StartOffset);
145145

146-
var lastSubjectLine = lines[0];
147-
if (lastSubjectLine.StartOffset > SubjectLength)
148-
return;
149-
150-
for (var i = 1; i < lines.Count; i++)
146+
for (var i = 0; i < lines.Count; i++)
151147
{
152-
if (lines[i].StartOffset > SubjectLength)
153-
break;
154-
155-
lastSubjectLine = lines[i];
148+
var line = lines[i];
149+
if (line.FirstDocumentLine.LineNumber == _subjectEndLine)
150+
{
151+
var y = line.GetTextLineVisualYPosition(line.TextLines[^1], VisualYPosition.LineBottom) - view.VerticalOffset + 4;
152+
context.DrawLine(pen, new Point(0, y), new Point(w, y));
153+
return;
154+
}
156155
}
157-
158-
var endY = lastSubjectLine.GetTextLineVisualYPosition(lastSubjectLine.TextLines[^1], VisualYPosition.LineBottom) - view.VerticalOffset + 4;
159-
context.DrawLine(pen, new Point(0, endY), new Point(w, endY));
160156
}
161157

162158
protected override void OnLoaded(RoutedEventArgs e)
@@ -184,38 +180,34 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
184180
if (!_isEditing)
185181
Text = CommitMessage;
186182

187-
var chars = CommitMessage.ToCharArray();
188-
var lastLinebreakIndex = 0;
189-
var lastLinebreakCount = 0;
183+
var lines = CommitMessage.ReplaceLineEndings("\n").Split('\n');
184+
var subjectLen = 0;
190185
var foundSubjectEnd = false;
191-
for (var i = 0; i < chars.Length; i++)
192-
{
193-
var ch = chars[i];
194-
if (ch == '\r')
195-
continue;
196186

197-
if (ch == '\n')
187+
for (var i = 0; i < lines.Length; i++)
188+
{
189+
var line = lines[i].Trim();
190+
if (line.Length == 0)
198191
{
199-
if (lastLinebreakCount > 0)
200-
{
201-
SetCurrentValue(SubjectLengthProperty, lastLinebreakIndex);
202-
foundSubjectEnd = true;
203-
break;
204-
}
205-
else
206-
{
207-
lastLinebreakIndex = i;
208-
lastLinebreakCount = 1;
209-
}
192+
if (subjectLen == 0)
193+
continue;
194+
195+
_subjectEndLine = i;
196+
foundSubjectEnd = true;
197+
break;
210198
}
199+
200+
var validCharLen = lines[i].TrimEnd().Length;
201+
if (subjectLen > 0)
202+
subjectLen += (validCharLen + 1);
211203
else
212-
{
213-
lastLinebreakCount = 0;
214-
}
204+
subjectLen = validCharLen;
215205
}
216206

217207
if (!foundSubjectEnd)
218-
SetCurrentValue(SubjectLengthProperty, CommitMessage?.Length ?? 0);
208+
_subjectEndLine = lines.Length;
209+
210+
SetCurrentValue(SubjectLengthProperty, subjectLen);
219211
}
220212
}
221213

@@ -325,6 +317,7 @@ private void OnTextViewVisualLinesChanged(object sender, EventArgs e)
325317

326318
private readonly List<string> _keywords = ["Acked-by: ", "Co-authored-by: ", "Reviewed-by: ", "Signed-off-by: ", "BREAKING CHANGE: "];
327319
private bool _isEditing = false;
320+
private int _subjectEndLine = 0;
328321
private CompletionWindow _completionWnd = null;
329322
}
330323

0 commit comments

Comments
 (0)