Skip to content

Commit

Permalink
SingleSelectionCommand, FindUnderExpandCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
quarnster committed Apr 17, 2013
1 parent e478969 commit 372fcc9
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ out.txt
backend/sublime/*.go
!backend/sublime/*_test.go
!backend/sublime/*_manual.go
*.log
*.log.0*
72 changes: 72 additions & 0 deletions backend/commands/find.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package commands

import (
. "lime/backend"
. "lime/backend/primitives"
)

type (
FindUnderExpandCommand struct {
DefaultCommand
}
SingleSelectionCommand struct {
DefaultCommand
}
)

func (c *SingleSelectionCommand) Run(v *View, e *Edit, args Args) error {
r := v.Sel().Get(0)
v.Sel().Clear()
v.Sel().Add(r)
return nil
}

func (c *FindUnderExpandCommand) Run(v *View, e *Edit, args Args) error {
sel := v.Sel()
rs := sel.Regions()

he := sel.HasEmpty()
if he {
for i, r := range rs {
if r2 := v.Buffer().Word(r.A); r2.Size() > r.Size() {
rs[i] = r2
}
}
sel.Clear()
sel.AddAll(rs)
} else {
last := rs[len(rs)-1]
b := v.Buffer()
data := b.SubstrR(last)
next := last
size := last.Size()
next.A += size
next.B += size
buf := b.SubstrR(Region{next.A, next.B})
for next.End() < b.Size() {
buf[size-1] = b.Index(next.B - 1)
found := true
for j, r := range buf {
if r != data[j] {
found = false
break
}
}
if found {
sel.Add(next)
break
}
copy(buf, buf[1:])
next.A += 1
next.B += 1
}
}
return nil
}

func init() {
register([]cmd{
{"find_under_expand", &FindUnderExpandCommand{}},
{"single_selection", &SingleSelectionCommand{}},
})
}
16 changes: 2 additions & 14 deletions backend/commands/insdel.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,7 @@ func (c *LeftDeleteCommand) Run(v *View, e *Edit, args Args) error {
}

sel := v.Sel()
hasNonEmpty := false
for _, r := range sel.Regions() {
if !r.Empty() {
hasNonEmpty = true
break
}
}
hasNonEmpty := sel.HasNonEmpty()
i := 0
for {
l := sel.Len()
Expand Down Expand Up @@ -94,13 +88,7 @@ func (c *LeftDeleteCommand) Run(v *View, e *Edit, args Args) error {

func (c *RightDeleteCommand) Run(v *View, e *Edit, args Args) error {
sel := v.Sel()
hasNonEmpty := false
for _, r := range sel.Regions() {
if !r.Empty() {
hasNonEmpty = true
break
}
}
hasNonEmpty := sel.HasNonEmpty()
i := 0
for {
l := sel.Len()
Expand Down
14 changes: 14 additions & 0 deletions backend/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ func init() {
return True
}
return False
} else if key == "num_selections" {
op, _ := operand.(int)
switch operator {
case OpEqual:
if op == v.Sel().Len() {
return True
}
return False
case OpNotEqual:
if op != v.Sel().Len() {
return True
}
return False
}
}
return Unknown
})
Expand Down
3 changes: 2 additions & 1 deletion backend/packages/Default/Default.sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
{ "keys": ["ctrl+z"], "command": "undo"},
{ "keys": ["ctrl+r"], "command": "redo"},
{ "keys": ["enter"], "command": "insert", "args": {"characters": "\n"}},
{ "keys": ["escape"], "command": "single_selection", "context": [ { "key": "num_selections", "operator": "not_equal", "operand": 1 } ] }
{ "keys": ["escape"], "command": "single_selection", "context": [ { "key": "num_selections", "operator": "not_equal", "operand": 1 } ] },
{ "keys": ["ctrl+d"], "command": "find_under_expand"},
]
22 changes: 22 additions & 0 deletions backend/primitives/regionset.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,25 @@ func (r *RegionSet) Regions() (ret []Region) {
copy(ret, r.regions)
return
}

func (r *RegionSet) HasNonEmpty() bool {
r.lock.Lock()
defer r.lock.Unlock()
for _, r := range r.regions {
if !r.Empty() {
return true
}
}
return false
}

func (r *RegionSet) HasEmpty() bool {
r.lock.Lock()
defer r.lock.Unlock()
for _, r := range r.regions {
if r.Empty() {
return true
}
}
return false
}
2 changes: 1 addition & 1 deletion build/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ func main() {
}
data := [][]string{
{"../backend/sublime/region.go", generateWrapper(reflect.TypeOf(primitives.Region{}), true, nil)},
{"../backend/sublime/regionset.go", generateWrapper(reflect.TypeOf(&primitives.RegionSet{}), false, regexp.MustCompile("Less|Swap|Adjust").MatchString)},
{"../backend/sublime/regionset.go", generateWrapper(reflect.TypeOf(&primitives.RegionSet{}), false, regexp.MustCompile("Less|Swap|Adjust|Has").MatchString)},
{"../backend/sublime/edit.go", generateWrapper(reflect.TypeOf(&backend.Edit{}), false, regexp.MustCompile("Apply|Undo").MatchString)},
{"../backend/sublime/view.go", generateWrapper(reflect.TypeOf(&backend.View{}), false, regexp.MustCompile("Buffer|Syntax|CommandHistory|Show|AddRegions|UndoStack").MatchString)},
{"../backend/sublime/window.go", generateWrapper(reflect.TypeOf(&backend.Window{}), false, regexp.MustCompile("OpenFile|SetActiveView").MatchString)},
Expand Down

0 comments on commit 372fcc9

Please sign in to comment.