forked from limetext/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.go
59 lines (51 loc) · 1.33 KB
/
edit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2013 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package backend
import (
"fmt"
"github.com/limetext/text"
)
type (
// The Edit object is an internal type passed as an argument
// to a TextCommand. All text operations need to be associated
// with a valid Edit object.
//
// Think of it a bit like an SQL transaction.
Edit struct {
invalid bool
composite text.CompositeAction
savedSel text.RegionSet
savedCount int
command string
args Args
v *View
bypassUndo bool
}
)
func newEdit(v *View) *Edit {
ret := &Edit{
v: v,
savedCount: v.ChangeCount(),
}
for _, r := range v.Sel().Regions() {
ret.savedSel.Add(r)
}
return ret
}
// Returns a string describing this Edit object. Should typically not be manually called.
func (e *Edit) String() string {
return fmt.Sprintf("%s: %v, %v, %v", e.command, e.args, e.bypassUndo, e.composite)
}
// Applies the action of this Edit object. Should typically not be manually called.
func (e *Edit) Apply() {
e.composite.Apply()
}
// Reverses the application of this Edit object. Should typically not be manually called.
func (e *Edit) Undo() {
e.composite.Undo()
e.v.Sel().Clear()
for _, r := range e.savedSel.Regions() {
e.v.Sel().Add(r)
}
}