4
4
function ! s: _SID () abort
5
5
return matchstr (expand (' <sfile>' ), ' <SNR>\zs\d\+\ze__SID$' )
6
6
endfunction
7
- execute join ([' function! vital#_lsp#VS#LSP#TextEdit#import() abort' , printf (" return map({'set_method': '', ' _vital_depends': '', 'get_method': '', 'is_text_mark_preserved': '', ' apply': '', 'get_methods ': '', 'delete': '', '_vital_loaded': ''}, \" vital#_lsp#function('<SNR>%s_' . v:key)\" )" , s: _SID ()), ' endfunction' ], " \n " )
7
+ execute join ([' function! vital#_lsp#VS#LSP#TextEdit#import() abort' , printf (" return map({'_vital_depends': '', 'apply': '', 'delete': '', '_vital_loaded': ''}, \" vital#_lsp#function('<SNR>%s_' . v:key)\" )" , s: _SID ()), ' endfunction' ], " \n " )
8
8
delfunction s: _SID
9
9
" ___vital___
10
10
"
@@ -23,61 +23,6 @@ function! s:_vital_depends() abort
23
23
return [' VS.LSP.Text' , ' VS.LSP.Position' , ' VS.Vim.Option' ]
24
24
endfunction
25
25
26
- "
27
- " Current selected method.
28
- "
29
- let s: _method = ' auto'
30
-
31
- "
32
- " This dict contains some logics for patching text.
33
- "
34
- let s: _methods = {}
35
-
36
- "
37
- " set_method
38
- "
39
- function ! s: set_method (method) abort
40
- if ! has_key (s: _methods , a: method )
41
- let s: _method = ' auto'
42
- elseif a: method == # ' nvim_buf_set_text' && ! exists (' *nvim_buf_set_text' )
43
- let s: _method = ' auto'
44
- elseif a: method == # ' normal' && has (' nvim' )
45
- let s: _method = ' auto'
46
- else
47
- let s: _method = a: method
48
- endif
49
- endfunction
50
-
51
- "
52
- " get_method
53
- "
54
- function ! s: get_method () abort
55
- if s: _method == # ' auto'
56
- if exists (' *nvim_buf_set_text' )
57
- return ' nvim_buf_set_text'
58
- elseif ! has (' nvim' )
59
- return ' normal'
60
- else
61
- return ' function'
62
- endif
63
- endif
64
- return s: _method
65
- endfunction
66
-
67
- "
68
- " get_methods
69
- "
70
- function ! s: get_methods () abort
71
- return [' nvim_buf_set_text' , ' normal' , ' function' ]
72
- endfunction
73
-
74
- "
75
- " is_text_mark_preserved
76
- "
77
- function ! s: is_text_mark_preserved () abort
78
- return index ([' nvim_buf_set_text' ], s: get_method ()) >= 0
79
- endfunction
80
-
81
26
"
82
27
" apply
83
28
"
@@ -89,7 +34,7 @@ function! s:apply(path, text_edits) abort
89
34
try
90
35
call s: _switch (a: path )
91
36
let [l: has_overflowed , l: text_edits ] = s: _normalize (bufnr (l: target_bufname ), a: text_edits )
92
- let l: fix_cursor = s: _methods [ s: get_method ()] (bufnr (l: target_bufname ), l: text_edits , l: cursor_position )
37
+ let l: fix_cursor = s: _substitute (bufnr (l: target_bufname ), l: text_edits , l: cursor_position )
93
38
if l: has_overflowed && getline (' $' ) == # ' '
94
39
call s: delete (bufnr (l: target_bufname ), ' $' , ' $' )
95
40
endif
@@ -103,63 +48,34 @@ function! s:apply(path, text_edits) abort
103
48
endif
104
49
endfunction
105
50
106
- let s: _methods = {}
107
-
108
51
"
109
- " nvim_buf_set_text
52
+ " _substitute
110
53
"
111
- function ! s: _methods .nvim_buf_set_text (bufnr , text_edits, cursor_position) abort
112
- let l: fix_cursor = v: false
113
-
114
- for l: text_edit in a: text_edits
115
- let l: start = s: Position .lsp_to_vim (a: bufnr , l: text_edit .range .start )
116
- let l: end = s: Position .lsp_to_vim (a: bufnr , l: text_edit .range .end )
117
- let l: lines = s: Text .split_by_eol (l: text_edit .newText)
118
- call nvim_buf_set_text (
119
- \ a: bufnr ,
120
- \ l: start [0 ] - 1 ,
121
- \ l: start [1 ] - 1 ,
122
- \ l: end [0 ] - 1 ,
123
- \ l: end [1 ] - 1 ,
124
- \ l: lines
125
- \ )
126
- let l: fix_cursor = s: _fix_cursor (a: cursor_position , l: text_edit , l: lines ) || l: fix_cursor
127
- endfor
128
-
129
- return l: fix_cursor
130
- endfunction
131
-
132
- "
133
- " normal
134
- "
135
- function ! s: _methods .normal (bufnr , text_edits, cursor_position) abort
54
+ function ! s: _substitute (bufnr , text_edits, cursor_position) abort
136
55
let l: fix_cursor = v: false
137
56
138
57
try
139
58
let l: Restore = s: Option .define ({
140
59
\ ' foldenable' : ' 0' ,
141
- \ ' virtualedit' : ' onemore' ,
142
- \ ' whichwrap' : ' h' ,
143
- \ ' selection' : ' exclusive' ,
144
60
\ })
145
61
let l: view = winsaveview ()
146
62
let l: regx = getreg (' x' )
147
63
148
64
for l: text_edit in a: text_edits
149
65
let l: start = s: Position .lsp_to_vim (a: bufnr , l: text_edit .range .start )
150
66
let l: end = s: Position .lsp_to_vim (a: bufnr , l: text_edit .range .end )
151
- if l: start [0 ] != l: end [0 ] || l: start [1 ] != l: end [1 ]
152
- let l: command = printf (' %sG%s|v%sG%s|"_d' , l: start [0 ], l: start [1 ], l: end [0 ], l: end [1 ])
153
- else
154
- let l: command = printf (' %sG%s|' , l: start [0 ], l: start [1 ])
155
- endif
156
67
call setreg (' x' , s: Text .normalize_eol (l: text_edit .newText), ' c' )
157
- execute printf (' noautocmd keepjumps normal! %s"xP' , l: command )
158
-
68
+ execute printf (' noautocmd keepjumps %ssubstitute/\%%%sl\%%%sc\zs\_.\{-}\ze\%%%sl\%%%sc/\=getreg("x")/' ,
69
+ \ l: start [0 ],
70
+ \ l: start [0 ],
71
+ \ l: start [1 ],
72
+ \ l: end [0 ],
73
+ \ l: end [1 ]
74
+ \ )
159
75
let l: fix_cursor = s: _fix_cursor (a: cursor_position , l: text_edit , s: Text .split_by_eol (l: text_edit .newText)) || l: fix_cursor
160
76
endfor
161
77
catch /.*/
162
- echomsg string ({ ' exception' : v: exception , ' throwpoint' : v: throwpoint })
78
+ call themis#log ( string ({ ' exception' : v: exception , ' throwpoint' : v: throwpoint }) )
163
79
finally
164
80
call l: Restore ()
165
81
call winrestview (l: view )
@@ -169,50 +85,6 @@ function! s:_methods.normal(bufnr, text_edits, cursor_position) abort
169
85
return l: fix_cursor
170
86
endfunction
171
87
172
- "
173
- " function
174
- "
175
- function ! s: _methods .function (bufnr , text_edits, cursor_position) abort
176
- let l: fix_cursor = v: false
177
-
178
- for l: text_edit in a: text_edits
179
- let l: start_line = getline (l: text_edit .range .start .line + 1 )
180
- let l: end_line = getline (l: text_edit .range .end .line + 1 )
181
- let l: before_line = strcharpart (l: start_line , 0 , l: text_edit .range .start .character )
182
- let l: after_line = strcharpart (l: end_line , l: text_edit .range .end .character , strchars (l: end_line ) - l: text_edit .range .end .character )
183
-
184
- " create lines.
185
- let l: lines = s: Text .split_by_eol (l: text_edit .newText)
186
- let l: lines [0 ] = l: before_line . l: lines [0 ]
187
- let l: lines [-1 ] = l: lines [-1 ] . l: after_line
188
-
189
- " save length.
190
- let l: lines_len = len (l: lines )
191
- let l: range_len = (l: text_edit .range .end .line - l: text_edit .range .start .line ) + 1
192
-
193
- " append or delete lines.
194
- if l: lines_len > l: range_len
195
- call append (l: text_edit .range .end .line , repeat ([' ' ], l: lines_len - l: range_len ))
196
- elseif l: lines_len < l: range_len
197
- call s: delete (a: bufnr , l: text_edit .range .start .line + l: lines_len , l: text_edit .range .end .line )
198
- endif
199
-
200
- " set lines.
201
- let l: i = 0
202
- while l: i < len (l: lines )
203
- let l: lnum = l: text_edit .range .start .line + l: i + 1
204
- if get (getbufline (a: bufnr , l: lnum ), 0 , v: null ) !=# l: lines [l: i ]
205
- call setline (l: lnum , l: lines [l: i ])
206
- endif
207
- let l: i += 1
208
- endwhile
209
-
210
- let l: fix_cursor = s: _fix_cursor (a: cursor_position , l: text_edit , s: Text .split_by_eol (l: text_edit .newText))
211
- endfor
212
-
213
- return l: fix_cursor
214
- endfunction
215
-
216
88
"
217
89
" _fix_cursor
218
90
"
0 commit comments