Skip to content

Commit

Permalink
Fix formatting of transpose sources (gofmt)
Browse files Browse the repository at this point in the history
  • Loading branch information
trapgate committed Nov 7, 2014
1 parent 789c21f commit e5b67a0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 75 deletions.
70 changes: 35 additions & 35 deletions backend/commands/transpose.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,48 @@
package commands

import (
. "github.com/limetext/lime/backend"
. "github.com/limetext/text"
. "github.com/limetext/lime/backend"
. "github.com/limetext/text"
)

type (
// Transpose: Swap the characters on either side of the cursor,
// then move the cursor forward one character.
TransposeCommand struct {
DefaultCommand
}
// Transpose: Swap the characters on either side of the cursor,
// then move the cursor forward one character.
TransposeCommand struct {
DefaultCommand
}
)

func (c *TransposeCommand) Run(v *View, e *Edit) error {
/*
Correct behavior of Transpose:
- Swap the characters on either side of the cursor(s), then move
forward one character. If a region is selected, do nothing.
*/

if v.Sel().HasNonEmpty() {
return nil
}

rs := v.Sel().Regions()
for i := range rs {
r := rs[i]
if r.A == 0 || r.A >= v.Buffer().Size() {
continue
}
s := Region{r.A - 1, r.A + 1}
rns := v.Buffer().SubstrR(s)
rnd := make([]rune, len(rns))
rnd[0] = rns[1]
rnd[1] = rns[0]
v.Replace(e, s, string(rnd))
}

return nil
/*
Correct behavior of Transpose:
- Swap the characters on either side of the cursor(s), then move
forward one character. If a region is selected, do nothing.
*/

if v.Sel().HasNonEmpty() {
return nil
}

rs := v.Sel().Regions()
for i := range rs {
r := rs[i]
if r.A == 0 || r.A >= v.Buffer().Size() {
continue
}
s := Region{r.A - 1, r.A + 1}
rns := v.Buffer().SubstrR(s)
rnd := make([]rune, len(rns))
rnd[0] = rns[1]
rnd[1] = rns[0]
v.Replace(e, s, string(rnd))
}

return nil
}

func init() {
register([]Command{
&TransposeCommand{},
})
register([]Command{
&TransposeCommand{},
})
}
80 changes: 40 additions & 40 deletions backend/commands/transpose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,53 @@
package commands

import (
. "github.com/limetext/lime/backend"
. "github.com/limetext/text"
"testing"
. "github.com/limetext/lime/backend"
. "github.com/limetext/text"
"testing"
)

func TestTranspose(t *testing.T) {
ed := GetEditor()

w := ed.NewWindow()
defer w.Close()

v := w.NewFile()
defer func() {
v.SetScratch(true)
v.Close()
}()

e := v.BeginEdit()
v.Insert(e, 0, "First&Second")
v.EndEdit(e)

// Test with regions at the beginning and end of the buffer,
// and one beyond the buffer limits.
rs1 := []Region{{0, 0}, {1, 2}, {3, 3}, {5, 5}, {12, 12}, {20, 20}}
rs2 := []Region{{0, 0}, {3, 3}, {5, 5}, {12, 12}, {20, 20}}

// This first should change nothing, because there's a selection with size
// > 0.
selectRegions(v, rs1)
ed.CommandHandler().RunTextCommand(v, "transpose", nil)
checkContents(t, v, 1, "First&Second")

selectRegions(v, rs2)
ed.CommandHandler().RunTextCommand(v, "transpose", nil)
checkContents(t, v, 2, "Fisr&tSecond")
ed := GetEditor()

w := ed.NewWindow()
defer w.Close()

v := w.NewFile()
defer func() {
v.SetScratch(true)
v.Close()
}()

e := v.BeginEdit()
v.Insert(e, 0, "First&Second")
v.EndEdit(e)

// Test with regions at the beginning and end of the buffer,
// and one beyond the buffer limits.
rs1 := []Region{{0, 0}, {1, 2}, {3, 3}, {5, 5}, {12, 12}, {20, 20}}
rs2 := []Region{{0, 0}, {3, 3}, {5, 5}, {12, 12}, {20, 20}}

// This first should change nothing, because there's a selection with size
// > 0.
selectRegions(v, rs1)
ed.CommandHandler().RunTextCommand(v, "transpose", nil)
checkContents(t, v, 1, "First&Second")

selectRegions(v, rs2)
ed.CommandHandler().RunTextCommand(v, "transpose", nil)
checkContents(t, v, 2, "Fisr&tSecond")
}

func selectRegions(v *View, rs []Region) {
v.Sel().Clear()
for _, r := range rs {
v.Sel().Add(r)
}
v.Sel().Clear()
for _, r := range rs {
v.Sel().Add(r)
}
}

func checkContents(t *testing.T, v *View, tnum int, expected string) {
b := v.Buffer().Substr(Region{0, v.Buffer().Size()})
if b != expected {
t.Error("Test %d: Expected %q but got %q", tnum, expected, b)
}
b := v.Buffer().Substr(Region{0, v.Buffer().Size()})
if b != expected {
t.Error("Test %d: Expected %q but got %q", tnum, expected, b)
}
}

0 comments on commit e5b67a0

Please sign in to comment.