Skip to content

Commit ea51c41

Browse files
committed
enhance: disable copying when user only selected hunk indicator line (#2391)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 0938fd4 commit ea51c41

1 file changed

Lines changed: 24 additions & 11 deletions

File tree

‎src/Views/TextDiffView.axaml.cs‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -619,8 +619,10 @@ private async void OnTextAreaKeyDown(object sender, KeyEventArgs e)
619619
{
620620
if (e.Key == Key.C)
621621
{
622-
await CopyWithoutIndicatorsAsync();
623622
e.Handled = true;
623+
624+
if (CanCopyText())
625+
await CopyWithoutIndicatorsAsync();
624626
}
625627
}
626628

@@ -635,8 +637,7 @@ private void OnTextAreaCaretPositionChanged(object sender, EventArgs e)
635637

636638
private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e)
637639
{
638-
var selection = TextArea.Selection;
639-
if (selection.IsEmpty)
640+
if (!CanCopyText())
640641
return;
641642

642643
var copy = new MenuItem();
@@ -815,17 +816,31 @@ private void AutoScrollToFirstChange()
815816
}
816817
}
817818

818-
private async Task CopyWithoutIndicatorsAsync()
819+
private bool CanCopyText()
819820
{
820821
var selection = TextArea.Selection;
821822
if (selection.IsEmpty)
823+
return false;
824+
825+
var startPosition = selection.StartPosition;
826+
var endPosition = selection.EndPosition;
827+
if (startPosition.Line == endPosition.Line)
822828
{
823-
await this.CopyTextAsync(string.Empty);
824-
return;
829+
var lines = GetLines();
830+
var idx = startPosition.Line - 1;
831+
if (idx >= lines.Count)
832+
return false;
833+
834+
var line = lines[idx];
835+
return line.Type != Models.TextDiffLineType.Indicator && line.Type != Models.TextDiffLineType.None;
825836
}
826837

827-
var lines = GetLines();
838+
return true;
839+
}
828840

841+
private async Task CopyWithoutIndicatorsAsync()
842+
{
843+
var selection = TextArea.Selection;
829844
var startPosition = selection.StartPosition;
830845
var endPosition = selection.EndPosition;
831846

@@ -837,13 +852,11 @@ private async Task CopyWithoutIndicatorsAsync()
837852

838853
if (startIdx == endIdx)
839854
{
840-
if (lines[startIdx].Type is Models.TextDiffLineType.Indicator or Models.TextDiffLineType.None)
841-
await this.CopyTextAsync(string.Empty);
842-
else
843-
await this.CopyTextAsync(SelectedText);
855+
await this.CopyTextAsync(SelectedText);
844856
return;
845857
}
846858

859+
var lines = GetLines();
847860
var builder = new StringBuilder();
848861
for (var i = startIdx; i <= endIdx && i <= lines.Count - 1; i++)
849862
{

0 commit comments

Comments
 (0)