Skip to content

Commit aca92dd

Browse files
authored
Remove suffix (#723)
* Remove suffix * Get default value in placeholder * Fix make_valid_word * Fix test
1 parent 415f6e6 commit aca92dd

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

autoload/lsp/omni.vim

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ function! s:contains_filter(item, last_typed_word) abort
120120
endif
121121
endfunction
122122

123+
let s:pair = {
124+
\ '"': '"',
125+
\ '''': '''',
126+
\ '{': '}',
127+
\ '(': ')',
128+
\ '[': ']',
129+
\}
130+
123131
function! s:display_completions(timer, info) abort
124132
" TODO: Allow multiple servers
125133
let l:server_name = a:info['server_names'][0]
@@ -131,6 +139,15 @@ function! s:display_completions(timer, info) abort
131139

132140
if l:filter['name'] ==? 'prefix'
133141
let s:completion['matches'] = filter(s:completion['matches'], {_, item -> s:prefix_filter(item, l:last_typed_word)})
142+
if has_key(s:pair, l:last_typed_word[0])
143+
let [l:lhs, l:rhs] = [l:last_typed_word[0], s:pair[l:last_typed_word[0]]]
144+
for l:item in s:completion['matches']
145+
let l:str = l:item['word']
146+
if len(l:str) > 1 && l:str[0] ==# l:lhs && l:str[-1:] ==# l:rhs
147+
let l:item['word'] = l:str[:-2]
148+
endif
149+
endfor
150+
endif
134151
elseif l:filter['name'] ==? 'contains'
135152
let s:completion['matches'] = filter(s:completion['matches'], {_, item -> s:contains_filter(item, l:last_typed_word)})
136153
endif
@@ -254,7 +271,7 @@ function! lsp#omni#default_get_vim_completion_item(item, ...) abort
254271
let l:abbr = a:item['label']
255272

256273
if has_key(a:item, 'insertTextFormat') && a:item['insertTextFormat'] == 2
257-
let l:word = substitute(l:word, '\<\$[0-9]\+\|\${[^}]\+}\>', '', 'g')
274+
let l:word = substitute(l:word, '\$[0-9]\+\|\${\%(\\.\|[^}]\)\+}', '', 'g')
258275
endif
259276

260277
let l:completion = {

autoload/lsp/ui/vim/completion.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ function! s:simple_expand_text(text) abort
233233

234234
" Remove placeholders and get first placeholder position that use to cursor position.
235235
" e.g. `|getbufline(${1:expr}, ${2:lnum})${0}` to getbufline(|,)
236-
let l:text = substitute(a:text, '\$\%({[0-9]*[^}]*}\|[0-9]*\)', '', 'g')
237-
let l:offset = match(a:text, '\$\%({[0-9]*[^}]*}\|[0-9]*\)')
236+
let l:text = substitute(a:text, '\$\%({[0-9]\+\%(:\(\\.\|[^}]\+\)*\)}\|[0-9]\+\)', '\=substitute(submatch(1), "\\", "", "g")', 'g')
237+
let l:offset = match(a:text, '\$\%({[0-9]\+\%(:\(\\.\|[^}]\+\)*\)}\|[0-9]\+\)')
238238
if l:offset == -1
239239
let l:offset = strchars(l:text)
240240
endif

autoload/lsp/utils.vim

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,16 @@ function! lsp#utils#base64_decode(data) abort
313313
endfunction
314314

315315
function! lsp#utils#make_valid_word(str) abort
316-
let l:str = matchstr(a:str, '^[^ (<{\[\t\r\n]\+')
317-
if l:str =~# ':$'
318-
return l:str[:-2]
316+
let l:str = substitute(a:str, '\$[0-9]\+\|\${\%(\\.\|[^}]\)\+}', '', 'g')
317+
let l:str = substitute(l:str, '\\\(.\)', '\1', 'g')
318+
let l:valid = matchstr(l:str, '^[^"'' (<{\[\t\r\n]\+')
319+
if empty(l:valid)
320+
return l:str
319321
endif
320-
return l:str
322+
if l:valid =~# ':$'
323+
return l:valid[:-2]
324+
endif
325+
return l:valid
321326
endfunction
322327

323328
function! lsp#utils#_split_by_eol(text) abort

test/lsp/utils.vimspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ Describe lsp#utils
176176
Describe lsp#utils#make_valid_word
177177
It should make valid word
178178
Assert Equals(lsp#utils#make_valid_word('my-word'), 'my-word')
179-
Assert Equals(lsp#utils#make_valid_word(' my-word'), '')
180179
Assert Equals(lsp#utils#make_valid_word("my\nword"), 'my')
181180
Assert Equals(lsp#utils#make_valid_word('my-word: description'), 'my-word')
182181
Assert Equals(lsp#utils#make_valid_word('my-word : description'), 'my-word')

0 commit comments

Comments
 (0)