From bbf261b0d07bca49ea93d45c97d47ec7d4d60347 Mon Sep 17 00:00:00 2001 From: Andrew Pam Date: Sat, 5 Oct 2024 18:07:09 +1000 Subject: [PATCH] Implement disjoint cuts as documented --- edits/actions.go | 12 ++++++++++-- edits/actions_test.go | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/edits/actions.go b/edits/actions.go index ce2aed1..a0c043d 100644 --- a/edits/actions.go +++ b/edits/actions.go @@ -339,6 +339,10 @@ func scopeSpan() (int, int) { } func Copy() { + if len(mark) > 2 && secondary.oend > secondary.obegin { + ps.CopyText(markPara, secondary.obegin, secondary.oend) + } + if len(mark) > 0 && primary.oend > primary.obegin { currentCut = ps.CopyText(markPara, primary.obegin, primary.oend) @@ -353,6 +357,10 @@ func Copy() { } func cutPrimary() { + if primary.oend <= primary.obegin { + return + } + currentCut = ps.CutText(markPara, primary.obegin, primary.oend) cursor = counts{Char: primary.cbegin, Para: markPara} @@ -365,8 +373,8 @@ func cutPrimary() { } func cut() { - if len(mark) > 2 { - ps.DeleteText(markPara, secondary.obegin, secondary.oend) + if len(mark) > 2 && secondary.oend > secondary.obegin { + ps.CutText(markPara, secondary.obegin, secondary.oend) } cutPrimary() diff --git a/edits/actions_test.go b/edits/actions_test.go index fa8e5cb..aae263c 100644 --- a/edits/actions_test.go +++ b/edits/actions_test.go @@ -416,6 +416,20 @@ func TestCopy(t *testing.T) { Copy() text, _ = ps.GetCut(currentCut) assert.Equal("e", text) + + ps.AppendText(1, " more") + markPara, mark, primary, secondary = 1, []int{0, 4, 9}, selection{0, 4, 0, 4}, selection{9, 9, 9, 9} + Copy() + assert.Equal(3, ps.Cuts()) + text, _ = ps.GetCut(currentCut) + assert.Equal("Test", text) + + markPara, mark, primary, secondary = 1, []int{1, 3, 7, 8}, selection{1, 3, 1, 3}, selection{7, 8, 7, 8} + Copy() + text, _ = ps.GetCut(currentCut) + assert.Equal("es", text, "primary cut") + text, _ = ps.GetCut(currentCut - 1) + assert.Equal("r", text, "secondary cut") } func TestCut(t *testing.T) { @@ -426,12 +440,22 @@ func TestCut(t *testing.T) { ps.AppendText(1, "Test") markPara, mark, primary = 1, []int{1, 2}, selection{1, 2, 1, 2} cut() + text, _ := ps.GetCut(currentCut) + assert.Equal("e", text) assert.Equal("Tst", ps.GetText(1)) ps.AppendText(1, " more") - markPara, mark, primary, secondary = 1, []int{1, 3, 6, 7}, selection{1, 3, 1, 3}, selection{6, 7, 6, 7} + markPara, mark, primary, secondary = 1, []int{5, 6, 9}, selection{5, 6, 5, 6}, selection{9, 9, 9, 9} + cut() + assert.Equal(2, ps.Cuts()) + + markPara, mark, primary, secondary = 1, []int{1, 3, 5, 6}, selection{1, 3, 1, 3}, selection{5, 6, 5, 6} cut() - assert.Equal("T moe", ps.GetText(1)) + text, _ = ps.GetCut(currentCut) + assert.Equal("st", text, "primary cut") + text, _ = ps.GetCut(currentCut - 1) + assert.Equal("r", text, "secondary cut") + assert.Equal("T me", ps.GetText(1)) } func TestCutPrimary(t *testing.T) { @@ -440,7 +464,11 @@ func TestCutPrimary(t *testing.T) { ResizeScreen(margin+4, 3) ps.AppendText(1, "Test") - markPara, primary = 1, selection{1, 2, 1, 2} + markPara = 1 + cutPrimary() + assert.Zero(currentCut) + + primary = selection{1, 2, 1, 2} cutPrimary() assert.Equal(para{3, []int{0}, []int{0}, []string{"T_st"}}, cache[0]) assert.Nil(mark)