-
Notifications
You must be signed in to change notification settings - Fork 37
/
session.vim
273 lines (239 loc) · 6.81 KB
/
session.vim
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
let s:Snippet = vsnip#snippet#import()
let s:TextEdit = vital#vsnip#import('VS.LSP.TextEdit')
let s:Position = vital#vsnip#import('VS.LSP.Position')
let s:Diff = vital#vsnip#import('VS.LSP.Diff')
"
" import.
"
function! vsnip#session#import() abort
return s:Session
endfunction
let s:Session = {}
"
" new.
"
function! s:Session.new(bufnr, position, text) abort
return extend(deepcopy(s:Session), {
\ 'bufnr': a:bufnr,
\ 'buffer': getbufline(a:bufnr, '^', '$'),
\ 'timer_id': -1,
\ 'changedtick': getbufvar(a:bufnr, 'changedtick', 0),
\ 'snippet': s:Snippet.new(a:position, vsnip#indent#adjust_snippet_body(getline('.'), a:text)),
\ 'tabstop': -1,
\ 'changenr': changenr(),
\ 'changenrs': {},
\ })
endfunction
"
" expand.
"
function! s:Session.expand() abort
" insert snippet.
call s:TextEdit.apply(self.bufnr, [{
\ 'range': {
\ 'start': self.snippet.position,
\ 'end': self.snippet.position
\ },
\ 'newText': self.snippet.text()
\ }])
call self.store(changenr())
endfunction
"
" merge.
"
function! s:Session.merge(session) abort
call s:TextEdit.apply(self.bufnr, self.snippet.sync())
call self.store(self.changenr)
call a:session.expand()
call self.snippet.merge(self.tabstop, a:session.snippet)
call self.snippet.insert(deepcopy(a:session.snippet.position), a:session.snippet.children)
call s:TextEdit.apply(self.bufnr, self.snippet.sync())
call self.store(changenr())
endfunction
"
" jumpable.
"
function! s:Session.jumpable(direction) abort
if a:direction == 1
let l:jumpable = !empty(self.snippet.get_next_jump_point(self.tabstop))
else
let l:jumpable = !empty(self.snippet.get_prev_jump_point(self.tabstop))
endif
return l:jumpable
endfunction
"
" jump.
"
function! s:Session.jump(direction) abort
call self.flush_changes()
if a:direction == 1
let l:jump_point = self.snippet.get_next_jump_point(self.tabstop)
else
let l:jump_point = self.snippet.get_prev_jump_point(self.tabstop)
endif
if empty(l:jump_point)
return
endif
let self.tabstop = l:jump_point.placeholder.id
" choice.
if len(l:jump_point.placeholder.choice) > 0
call self.choice(l:jump_point)
" select.
elseif l:jump_point.range.start.character != l:jump_point.range.end.character
call self.select(l:jump_point)
" move.
else
call self.move(l:jump_point)
endif
doautocmd <nomodeline> User vsnip#jump
endfunction
"
" choice.
"
function! s:Session.choice(jump_point) abort
call self.move(a:jump_point)
let l:fn = {}
let l:fn.jump_point = a:jump_point
function! l:fn.next_tick() abort
if mode()[0] ==# 'i'
let l:pos = s:Position.lsp_to_vim('%', self.jump_point.range.start)
call complete(l:pos[1], map(copy(self.jump_point.placeholder.choice), { k, v -> {
\ 'word': v.escaped,
\ 'abbr': v.escaped,
\ 'menu': '[vsnip]',
\ 'kind': 'Choice'
\ } }))
endif
endfunction
call timer_start(g:vsnip_choice_delay, { -> l:fn.next_tick() })
endfunction
"
" select.
"
" @NOTE: Must work even if virtualedit=all/onmore or not.
"
function! s:Session.select(jump_point) abort
let l:pos = s:Position.lsp_to_vim('%', a:jump_point.range.end)
call cursor([l:pos[0], l:pos[1] - (&selection !~# 'exclusive')])
let l:select_length = strchars(a:jump_point.placeholder.text()) - (&selection !~# 'exclusive')
let l:cmd = ''
let l:cmd .= mode()[0] ==# 'i' ? "\<Esc>l" : ''
let l:cmd .= printf('v%s', l:select_length > 0 ? l:select_length . 'h' : '')
if get(g:, 'vsnip_test_mode', v:false)
let l:cmd .= "\<Esc>gvo\<C-g>" " Update `last visual selection` for getting it in test.
execute printf('normal! %s', l:cmd)
else
let l:cmd .= "o\<C-g>"
call feedkeys(l:cmd, 'n')
endif
endfunction
"
" move.
"
" @NOTE: Must work even if virtualedit=all/onmore or not.
"
function! s:Session.move(jump_point) abort
let l:pos = s:Position.lsp_to_vim('%', a:jump_point.range.end)
call cursor(l:pos)
if l:pos[1] > strlen(getline(l:pos[0]))
startinsert!
else
startinsert
endif
endfunction
"
" refresh
"
function! s:Session.refresh() abort
let self.buffer = getbufline(self.bufnr, '^', '$')
let self.changedtick = getbufvar(self.bufnr, 'changedtick', 0)
endfunction
"
" on_insert_leave
"
function! s:Session.on_insert_leave() abort
call self.flush_changes()
endfunction
"
" on_insert_char_pre
"
function! s:Session.on_insert_char_pre(char) abort
if a:char =~# '^[:print:]\+$'
let l:range = self.snippet.range()
let l:position = s:Position.cursor()
let l:is_out_of_range = v:false
let l:is_out_of_range = l:is_out_of_range || l:position.line < l:range.start.line || (l:position.line == l:range.start.line && l:position.character < l:range.start.character)
let l:is_out_of_range = l:is_out_of_range || l:position.line > l:range.end.line || (l:position.line == l:range.end.line && l:position.character > l:range.end.character)
if l:is_out_of_range
call vsnip#deactivate()
endif
endif
endfunction
"
" on_text_changed
"
function! s:Session.on_text_changed() abort
if self.bufnr != bufnr('%')
return vsnip#deactivate()
endif
let l:changenr = changenr()
" save state.
if self.changenr != l:changenr
call self.store(self.changenr)
if has_key(self.changenrs, l:changenr)
let self.tabstop = self.changenrs[l:changenr].tabstop
let self.snippet = self.changenrs[l:changenr].snippet
let self.changenr = l:changenr
let self.buffer = getbufline(self.bufnr, '^', '$')
return
endif
endif
if g:vsnip_sync_delay == 0
call self.flush_changes()
elseif g:vsnip_sync_delay > 0
call timer_stop(self.timer_id)
let self.timer_id = timer_start(g:vsnip_sync_delay, { -> self.flush_changes() }, { 'repeat': 1 })
endif
endfunction
"
" flush_changes
"
function! s:Session.flush_changes() abort
let l:changedtick = getbufvar(self.bufnr, 'changedtick', 0)
if self.changedtick == l:changedtick
return
endif
let self.changedtick = l:changedtick
" compute diff.
let l:buffer = getbufline(self.bufnr, '^', '$')
let l:diff = s:Diff.compute(self.buffer, l:buffer)
let self.buffer = l:buffer
if l:diff.rangeLength == 0 && l:diff.text ==# ''
return
endif
" if follow succeeded, sync placeholders and write back to the buffer.
if self.snippet.follow(self.tabstop, l:diff)
try
let l:text_edits = self.snippet.sync()
if len(l:text_edits) > 0
undojoin | call s:TextEdit.apply(self.bufnr, l:text_edits)
endif
call self.refresh()
catch /.*/
" TODO: More strict changenrs mangement.
call vsnip#deactivate()
endtry
else
call vsnip#deactivate()
endif
endfunction
"
" save.
"
function! s:Session.store(changenr) abort
let self.changenrs[a:changenr] = {
\ 'tabstop': self.tabstop,
\ 'snippet': deepcopy(self.snippet)
\ }
let self.changenr = a:changenr
endfunction