-
Notifications
You must be signed in to change notification settings - Fork 8
/
glue.go
85 lines (72 loc) · 2.34 KB
/
glue.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// 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 commands
import (
"fmt"
"github.com/limetext/backend"
)
const limeCmdMark = "lime.cmd.mark"
type (
// MarkUndoGroupsForGluing Command marks the current position
// in the undo stack as the start of commands to glue, potentially
// overwriting any existing marks.
MarkUndoGroupsForGluing struct {
backend.BypassUndoCommand
}
// GlueMarkedUndoGroups Command merges commands from the previously
// marked undo stack location to the current location into a single
// entry in the undo stack.
GlueMarkedUndoGroups struct {
backend.BypassUndoCommand
}
// MaybeMarkUndoGroupsForGluing Command is similar to
// MarkUndoGroupsForGluingCommand with the exception that if there
// is already a mark set, it is not overwritten.
MaybeMarkUndoGroupsForGluing struct {
backend.BypassUndoCommand
}
// UnmarkUndoGroupsForGluing Command removes the glue mark set by
// either MarkUndoGroupsForGluingCommand or MaybeMarkUndoGroupsForGluingCommand
// if it was set.
UnmarkUndoGroupsForGluing struct {
backend.BypassUndoCommand
}
)
// Run executes the MarkUndoGroupsForGluing command.
func (c *MarkUndoGroupsForGluing) Run(v *backend.View, e *backend.Edit) error {
v.Settings().Set(limeCmdMark, v.UndoStack().Position())
return nil
}
// Run executes the UnmarkUndoGroupsForGluing command.
func (c *UnmarkUndoGroupsForGluing) Run(v *backend.View, e *backend.Edit) error {
v.Settings().Erase(limeCmdMark)
return nil
}
// Run executes the GlueMarkedUndoGroups command.
func (c *GlueMarkedUndoGroups) Run(v *backend.View, e *backend.Edit) error {
pos := v.UndoStack().Position()
mark, ok := v.Settings().Get(limeCmdMark).(int)
if !ok {
return fmt.Errorf("No mark in the current view")
}
if l, p := pos-mark, mark; p != -1 && (l-p) > 1 {
v.UndoStack().GlueFrom(mark)
}
return nil
}
// Run executes the MaybeMarkUndoGroupsForGluing command.
func (c *MaybeMarkUndoGroupsForGluing) Run(v *backend.View, e *backend.Edit) error {
if !v.Settings().Has(limeCmdMark) {
v.Settings().Set(limeCmdMark, v.UndoStack().Position())
}
return nil
}
func init() {
register([]backend.Command{
&MarkUndoGroupsForGluing{},
&GlueMarkedUndoGroups{},
&MaybeMarkUndoGroupsForGluing{},
&UnmarkUndoGroupsForGluing{},
})
}