Browse Source

Fix #398 user_data support

Shougo Matsushita 1 year ago
parent
commit
7121b95c9e
3 changed files with 42 additions and 48 deletions
  1. 24
    30
      autoload/neosnippet/mappings.vim
  2. 16
    11
      autoload/neosnippet/parser.vim
  3. 2
    7
      test/functions.vim

+ 24
- 30
autoload/neosnippet/mappings.vim View File

@@ -20,19 +20,12 @@ function! neosnippet#mappings#jumpable() abort
20 20
             \ .neosnippet#get_sync_placeholder_marker_pattern(), 'nw') > 0
21 21
 endfunction
22 22
 function! neosnippet#mappings#completed_expandable() abort
23
-  if !s:enabled_completed_snippet()
23
+  if empty(get(v:, 'completed_item', {}))
24 24
     return 0
25 25
   endif
26 26
 
27
-  let snippet = neosnippet#parser#_get_completed_snippet(
28
-        \ v:completed_item, neosnippet#util#get_cur_text(),
29
-        \ neosnippet#util#get_next_text())
30
-  return snippet != ''
31
-endfunction
32
-function! s:enabled_completed_snippet() abort
33
-  return exists('v:completed_item')
34
-        \ && !empty(v:completed_item)
35
-        \ && g:neosnippet#enable_completed_snippet
27
+  return !empty(s:get_completed_snippets(
28
+        \ neosnippet#util#get_cur_text(), col('.')))
36 29
 endfunction
37 30
 
38 31
 function! neosnippet#mappings#_clear_select_mode_mappings() abort
@@ -163,35 +156,36 @@ function! s:snippets_expand(cur_text, col) abort
163 156
 
164 157
   return 1
165 158
 endfunction
166
-function! s:expand_completed_snippets(cur_text, col) abort
167
-  if !s:enabled_completed_snippet()
168
-    return 0
159
+function! s:get_completed_snippets(cur_text, col) abort
160
+  if empty(get(v:, 'completed_item', {}))
161
+    return []
169 162
   endif
170 163
 
171 164
   let cur_text = a:cur_text
172 165
 
173
-  if !empty(get(g:, 'deoplete#_context', []))
174
-        \ && has_key(v:completed_item, 'word')
175
-    let completed_candidates = filter(copy(g:deoplete#_context),
176
-          \ "has_key(v:val, 'snippet') && has_key(v:val, 'snippet_trigger')
177
-          \  && v:val.word ==# v:completed_item.word")
178
-    if !empty(completed_candidates)
179
-      let v:completed_item.snippet = completed_candidates[0].snippet
180
-      let v:completed_item.snippet_trigger =
181
-            \ completed_candidates[0].snippet_trigger
166
+  if !empty(get(v:completed_item, 'user_data', '{}'))
167
+    let user_data = json_decode(v:completed_item.user_data)
168
+    if has_key(user_data, 'snippet')
169
+      let snippet = user_data.snippet
170
+      if has_key(user_data, 'snippet_trigger')
171
+        let cur_text = cur_text[: -1-len(user_data.snippet_trigger)]
172
+      endif
173
+      return [cur_text, snippet]
182 174
     endif
183 175
   endif
184 176
 
185
-  let snippet = neosnippet#parser#_get_completed_snippet(
186
-        \ v:completed_item, cur_text, neosnippet#util#get_next_text())
187
-  if snippet == ''
188
-    return 0
189
-  endif
190
-
191
-  if has_key(v:completed_item, 'snippet_trigger')
192
-    let cur_text = cur_text[: -1-len(v:completed_item.snippet_trigger)]
177
+  if g:neosnippet#enable_completed_snippet
178
+    let snippet = neosnippet#parser#_get_completed_snippet(
179
+          \ v:completed_item, cur_text, neosnippet#util#get_next_text())
180
+    if snippet != ''
181
+      return [cur_text, snippet]
182
+    endif
193 183
   endif
194 184
 
185
+  return []
186
+endfunction
187
+function! s:expand_completed_snippets(cur_text, col) abort
188
+  let [cur_text, snippet] = s:get_completed_snippets(a:cur_text, a:col)
195 189
   call neosnippet#view#_insert(snippet, {}, cur_text, a:col)
196 190
   return 1
197 191
 endfunction

+ 16
- 11
autoload/neosnippet/parser.vim View File

@@ -259,15 +259,24 @@ function! neosnippet#parser#_initialize_snippet(dict, path, line, pattern, name)
259 259
   endif
260 260
 
261 261
   let snippet = {
262
-        \ 'word' : a:dict.name, 'snip' : a:dict.word,
263
-        \ 'description' : a:dict.word,
264
-        \ 'menu_template' : abbr,
265
-        \ 'menu_abbr' : abbr,
266
-        \ 'options' : a:dict.options,
267
-        \ 'action__path' : a:path, 'action__line' : a:line,
268
-        \ 'action__pattern' : a:pattern, 'real_name' : a:name,
262
+        \ 'word': a:dict.name, 'snip': a:dict.word,
263
+        \ 'description': a:dict.word,
264
+        \ 'menu_template': abbr,
265
+        \ 'menu_abbr': abbr,
266
+        \ 'options': a:dict.options,
267
+        \ 'real_name': a:name,
268
+        \ 'action__path': a:path,
269
+        \ 'action__line': a:line,
270
+        \ 'action__pattern': a:pattern,
269 271
         \}
270 272
 
273
+  if exists('*json_encode')
274
+    let snippet.user_data = json_encode({
275
+          \   'snippet': a:dict.word,
276
+          \   'snippet_trigger': a:dict.name
277
+          \ })
278
+  endif
279
+
271 280
   if has_key(a:dict, 'regexp')
272 281
     let snippet.regexp = a:dict.regexp
273 282
   endif
@@ -292,10 +301,6 @@ function! neosnippet#parser#_get_completed_snippet(completed_item, cur_text, nex
292 301
     return ''
293 302
   endif
294 303
 
295
-  if has_key(item, 'snippet')
296
-    return item.snippet
297
-  endif
298
-
299 304
   " Set abbr
300 305
   let abbrs = []
301 306
   if get(item, 'info', '') =~# '^.\+('

+ 2
- 7
test/functions.vim View File

@@ -125,12 +125,7 @@ function! s:suite.get_completed_snippet() abort
125 125
 
126 126
   call s:assert.equals(neosnippet#parser#_get_completed_snippet({
127 127
         \ 'word' : 'something', 'abbr' : 'something(else)',
128
-        \ 'menu' : '', 'info' : '', 'snippet' : '(${1:custom})${2}'
129
-        \ }, 'something', ''), '(${1:custom})${2}')
130
-
131
-  call s:assert.equals(neosnippet#parser#_get_completed_snippet({
132
-        \ 'word' : 'something', 'abbr' : 'something(else)',
133
-        \ 'menu' : '', 'info' : 'func()', 'snippet' : '(${1:custom})${2}'
134
-        \ }, 'something', ''), '(${1:custom})${2}')
128
+        \ 'menu' : '', 'info' : 'func()',
129
+        \ }, 'something', ''), '(${1:#:else})${2}')
135 130
 endfunction
136 131