-
-
Notifications
You must be signed in to change notification settings - Fork 247
/
Copy pathinsertNode.lua
222 lines (201 loc) · 5.15 KB
/
insertNode.lua
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
local InsertNode = require("luasnip.nodes.node").Node:new()
local ExitNode = InsertNode:new()
local util = require("luasnip.util.util")
local types = require("luasnip.util.types")
local events = require("luasnip.util.events")
local session = require("luasnip.session")
local extend_decorator = require("luasnip.util.extend_decorator")
local function I(pos, static_text, opts)
static_text = util.to_string_table(static_text)
if pos == 0 then
return ExitNode:new({
pos = pos,
static_text = static_text,
mark = nil,
dependents = {},
type = types.exitNode,
-- will only be needed for 0-node, -1-node isn't set with this.
ext_gravities_active = { false, false },
}, opts)
else
return InsertNode:new({
pos = pos,
static_text = static_text,
mark = nil,
dependents = {},
type = types.insertNode,
inner_active = false,
}, opts)
end
end
extend_decorator.register(I, { arg_indx = 3 })
function ExitNode:input_enter(no_move)
-- Don't enter node for -1-node, it isn't in the node-table.
if self.pos == 0 then
InsertNode.input_enter(self, no_move)
else
-- -1-node:
self:set_mark_rgrav(true, true)
if not no_move then
local mark_begin_pos = self.mark:pos_begin_raw()
if vim.fn.mode() == "i" then
util.insert_move_on(mark_begin_pos)
else
vim.api.nvim_feedkeys(
vim.api.nvim_replace_termcodes("<Esc>", true, false, true),
"n",
true
)
util.normal_move_on_insert(mark_begin_pos)
end
end
self:event(events.enter)
end
end
function ExitNode:input_leave()
if self.pos == 0 then
InsertNode.input_leave(self)
else
self:event(events.leave)
end
end
function ExitNode:jump_into(dir, no_move)
if not session.config.history then
self:input_enter(no_move)
if (dir == 1 and not self.next) or (dir == -1 and not self.prev) then
if self.pos == 0 then
-- leave instantly, self won't be active snippet.
self:input_leave()
end
return nil
else
return self
end
else
-- if no next node, return self as next current node.
return InsertNode.jump_into(self, dir, no_move) or self
end
end
function ExitNode:_update_dependents() end
function ExitNode:update_dependents() end
function ExitNode:update_all_dependents() end
function ExitNode:_update_dependents_static() end
function ExitNode:update_dependents_static() end
function ExitNode:update_all_dependents_static() end
function ExitNode:is_interactive()
return true
end
function InsertNode:input_enter(no_move)
self.visited = true
self.mark:update_opts(self.ext_opts.active)
if not no_move then
self.parent:enter_node(self.indx)
-- SELECT snippet text only when there is text to select (more oft than not there isnt).
local mark_begin_pos, mark_end_pos = self.mark:pos_begin_end_raw()
if not util.pos_equal(mark_begin_pos, mark_end_pos) then
util.any_select(mark_begin_pos, mark_end_pos)
else
-- if current and target mode is INSERT, there's no reason to leave it.
if vim.fn.mode() == "i" then
util.insert_move_on(mark_begin_pos)
else
-- mode might be VISUAL or something else, but <Esc> always leads to normal.
vim.api.nvim_feedkeys(
vim.api.nvim_replace_termcodes("<Esc>", true, false, true),
"n",
true
)
util.normal_move_on_insert(mark_begin_pos)
end
end
else
self.parent:enter_node(self.indx)
end
self:event(events.enter)
end
function InsertNode:jump_into(dir, no_move)
if self.inner_active then
if dir == 1 then
if self.next then
self.inner_active = false
if not session.config.history then
self.inner_first = nil
self.inner_last = nil
end
self:input_leave()
return self.next:jump_into(dir, no_move)
else
return false
end
else
if self.prev then
self.inner_active = false
if not session.config.history then
self.inner_first = nil
self.inner_last = nil
end
self:input_leave()
return self.prev:jump_into(dir, no_move)
else
return false
end
end
else
self:input_enter(no_move)
return self
end
end
function InsertNode:jump_from(dir, no_move)
if dir == 1 then
if self.inner_first then
self.inner_active = true
return self.inner_first:jump_into(dir, no_move)
else
if self.next then
self:input_leave()
return self.next:jump_into(dir, no_move)
else
-- only happens for exitNodes, but easier to include here
-- and reuse this impl for them.
return self
end
end
else
if self.inner_last then
self.inner_active = true
return self.inner_last:jump_into(dir, no_move)
else
if self.prev then
self:input_leave()
return self.prev:jump_into(dir, no_move)
else
return self
end
end
end
end
function InsertNode:input_leave()
self:event(events.leave)
self:update_dependents()
self.mark:update_opts(self:get_passive_ext_opts())
end
function InsertNode:exit()
if self.inner_first then
self.inner_first:exit()
end
self.visible = false
self.inner_first = nil
self.inner_last = nil
self.inner_active = false
self.mark:clear()
end
function InsertNode:get_docstring()
-- copy as to not in-place-modify static text.
return util.string_wrap(self.static_text, rawget(self, "pos"))
end
function InsertNode:is_interactive()
return true
end
return {
I = I,
}