Skip to content

Commit ab080b5

Browse files
committed
enhance: exclude indicators or empty blocks in diff text view while copying text (#924)
1 parent cc111ba commit ab080b5

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

src/Views/TextDiffView.axaml.cs

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,8 @@ protected override void OnLoaded(RoutedEventArgs e)
669669
TextArea.TextView.PointerWheelChanged += OnTextViewPointerWheelChanged;
670670
TextArea.TextView.VisualLinesChanged += OnTextViewVisualLinesChanged;
671671

672+
TextArea.AddHandler(KeyDownEvent, OnTextAreaKeyDown, RoutingStrategies.Tunnel);
673+
672674
UpdateTextMate();
673675
OnTextViewVisualLinesChanged(null, null);
674676
}
@@ -677,6 +679,8 @@ protected override void OnUnloaded(RoutedEventArgs e)
677679
{
678680
base.OnUnloaded(e);
679681

682+
TextArea.RemoveHandler(KeyDownEvent, OnTextAreaKeyDown);
683+
680684
TextArea.TextView.ContextRequested -= OnTextViewContextRequested;
681685
TextArea.TextView.PointerEntered -= OnTextViewPointerChanged;
682686
TextArea.TextView.PointerMoved -= OnTextViewPointerChanged;
@@ -732,6 +736,21 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
732736
}
733737
}
734738

739+
private void OnTextAreaKeyDown(object sender, KeyEventArgs e)
740+
{
741+
if (e.KeyModifiers.Equals(OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control))
742+
{
743+
if (e.Key == Key.C)
744+
{
745+
CopyWithoutIndicators();
746+
e.Handled = true;
747+
}
748+
}
749+
750+
if (!e.Handled)
751+
base.OnKeyDown(e);
752+
}
753+
735754
private void OnBlockNavigationPropertyChanged(object _1, PropertyChangedEventArgs _2)
736755
{
737756
TextArea?.TextView?.Redraw();
@@ -748,7 +767,7 @@ private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs
748767
copy.Icon = App.CreateMenuIcon("Icons.Copy");
749768
copy.Click += (_, ev) =>
750769
{
751-
App.CopyText(SelectedText);
770+
CopyWithoutIndicators();
752771
ev.Handled = true;
753772
};
754773

@@ -941,6 +960,59 @@ private void UpdateTextMate()
941960
}
942961
}
943962

963+
private void CopyWithoutIndicators()
964+
{
965+
var selection = TextArea.Selection;
966+
if (selection.IsEmpty)
967+
{
968+
App.CopyText(string.Empty);
969+
return;
970+
}
971+
972+
var lines = GetLines();
973+
var startIdx = Math.Min(selection.StartPosition.Line - 1, lines.Count - 1);
974+
var endIdx = Math.Min(selection.EndPosition.Line - 1, lines.Count - 1);
975+
976+
if (startIdx == endIdx)
977+
{
978+
var line = lines[startIdx];
979+
if (line.Type == Models.TextDiffLineType.Indicator ||
980+
line.Type == Models.TextDiffLineType.None)
981+
{
982+
App.CopyText(string.Empty);
983+
return;
984+
}
985+
986+
App.CopyText(SelectedText);
987+
return;
988+
}
989+
990+
var builder = new StringBuilder();
991+
for (var i = startIdx; i <= endIdx; i++)
992+
{
993+
var line = lines[i];
994+
if (line.Type == Models.TextDiffLineType.Indicator ||
995+
line.Type == Models.TextDiffLineType.None)
996+
continue;
997+
998+
if (i == startIdx && selection.StartPosition.Column > 1)
999+
{
1000+
builder.AppendLine(line.Content.Substring(selection.StartPosition.Column - 1));
1001+
continue;
1002+
}
1003+
1004+
if (i == endIdx && selection.EndPosition.Column < line.Content.Length)
1005+
{
1006+
builder.AppendLine(line.Content.Substring(0, selection.EndPosition.Column));
1007+
continue;
1008+
}
1009+
1010+
builder.AppendLine(line.Content);
1011+
}
1012+
1013+
App.CopyText(builder.ToString());
1014+
}
1015+
9441016
private TextMate.Installation _textMate = null;
9451017
private TextLocation _lastSelectStart = TextLocation.Empty;
9461018
private TextLocation _lastSelectEnd = TextLocation.Empty;

0 commit comments

Comments
 (0)