Skip to content

Commit

Permalink
added delete_word command closes limetext#71
Browse files Browse the repository at this point in the history
  • Loading branch information
zoli committed Jul 28, 2014
1 parent 8d1b46d commit 6de2329
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
73 changes: 73 additions & 0 deletions backend/commands/insdel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package commands
import (
. "github.com/limetext/lime/backend"
"github.com/quarnster/util/text"
"strings"
)

type (
Expand All @@ -30,6 +31,13 @@ type (
RightDeleteCommand struct {
DefaultCommand
}

// The DeleteWordCommand deletes one word to right or left
// depending on forward variable
DeleteWordCommand struct {
DefaultCommand
Forward bool
}
)

func (c *InsertCommand) Run(v *View, e *Edit) error {
Expand Down Expand Up @@ -116,10 +124,75 @@ func (c *RightDeleteCommand) Run(v *View, e *Edit) error {
return nil
}

func (c *DeleteWordCommand) Run(v *View, e *Edit) error {
var class int
if c.Forward {
class = CLASS_WORD_END | CLASS_PUNCTUATION_END | CLASS_LINE_START
} else {
class = CLASS_WORD_START | CLASS_PUNCTUATION_START | CLASS_LINE_END | CLASS_LINE_START
}

sel := v.Sel()
var rs []text.Region
for i := 0; i < sel.Len(); i++ {
r := sel.Get(i)
if r.Empty() {
p := c.findByClass(r.A, class, v)
if c.Forward {
r = text.Region{r.A, p}
} else {
r = text.Region{p, r.A}
}
}
rs = append(rs, r)
}
sel.Clear()
sel.AddAll(rs)
if c.Forward {
GetEditor().CommandHandler().RunTextCommand(v, "right_delete", nil)
} else {
GetEditor().CommandHandler().RunTextCommand(v, "left_delete", nil)
}
return nil
}

func (c *DeleteWordCommand) findByClass(point int, class int, v *View) int {
var end, d int
if c.Forward {
d = 1
end = v.Buffer().Size()
if point > end {
point = end
}
s := v.Buffer().Substr(text.Region{point, point + 2})
if strings.Contains(s, "\t") && strings.Contains(s, " ") {
class = CLASS_WORD_START | CLASS_PUNCTUATION_START | CLASS_LINE_END
}
} else {
d = -1
end = 0
if point < end {
point = end
}
s := v.Buffer().Substr(text.Region{point - 2, point})
if strings.Contains(s, "\t") && strings.Contains(s, " ") {
class = CLASS_WORD_END | CLASS_PUNCTUATION_END | CLASS_LINE_START
}
}
point += d
for ; point != end; point += d {
if v.Classify(point)&class != 0 {
return point
}
}
return point
}

func init() {
register([]cmd{
{"insert", &InsertCommand{}},
{"left_delete", &LeftDeleteCommand{}},
{"right_delete", &RightDeleteCommand{}},
{"delete_word", &DeleteWordCommand{}},
})
}
52 changes: 52 additions & 0 deletions backend/commands/insdel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,55 @@ func TestInsert(t *testing.T) {
ch.RunTextCommand(v, "undo", nil)
}
}

func TestDeleteWord(t *testing.T) {
tests := []struct {
text string
sel []Region
forward bool
expect string
}{
{
"word",
[]Region{{4, 4}},
false,
"",
},
{
"'(}[word",
[]Region{{7, 7}, {4, 4}},
false,
"d",
},
{
"testing forwar|d\ndelete word",
[]Region{{0, 2}, {11, 11}, {16, 16}},
true,
"sting for|ddelete word",
},
{
"simple test on outside",
[]Region{{-1, -1}, {6, 6}, {13, 13}, {54, 33}, {31, 31}},
true,
"simpletest outside",
},
}

ed := GetEditor()
w := ed.NewWindow()
for i, test := range tests {
v := w.NewFile()
e := v.BeginEdit()

v.Insert(e, 0, test.text)
v.EndEdit(e)

v.Sel().Clear()
v.Sel().AddAll(test.sel)

ed.CommandHandler().RunTextCommand(v, "delete_word", Args{"forward": test.forward})
if d := v.Buffer().Substr(Region{0, v.Buffer().Size()}); d != test.expect {
t.Errorf("Test %d:\nExcepted: '%s' but got: '%s'", i, test.expect, d)
}
}
}

0 comments on commit 6de2329

Please sign in to comment.