Skip to content

Commit

Permalink
bugfix for #266
Browse files Browse the repository at this point in the history
  • Loading branch information
jayli committed Dec 6, 2023
1 parent 05d5bc8 commit d86d6cb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ noremap rn :EasyCompleteRename<CR>
noremap gb :BackToOriginalBuffer<CR>
```

*All configurations*
Global configurations

| Global Configure | Default | Description |
|--------------------------------------|---------------|---------------------------------------------------------------|
Expand Down Expand Up @@ -242,6 +242,8 @@ Vim-EasyComplete does not support snippets by default. If you want snippet integ

Install TabNine: `:InstallLspServer tabnine`. Then restart your vim/nvim.

<src img="https://github.com/jayli/vim-easycomplete/assets/188244/c6688d37-604f-456b-a030-13c21e9df1f3" width="500px" />

Set `let g:easycomplete_tabnine_enable = 0` to disable TabNine. You can config TabNine by `g:easycomplete_tabnine_config` witch contains two properties:

- *line_limit*: The number of lines before and after the cursor to send to TabNine. If the option is smaller, the performance may be improved. (default: 1000)
Expand Down
39 changes: 23 additions & 16 deletions autoload/easycomplete/tabnine.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let s:tabnine_toolkit = v:lua.require("easycomplete.tabnine")
" 临时存放 suggest 或者 complete
let b:tabnine_typing_type = ""
let s:tabnine_hint_snippet = ""
let s:tabnine_hint_snippet = []

function easycomplete#tabnine#ready()
if g:env_is_vim
Expand Down Expand Up @@ -61,12 +61,12 @@ function! easycomplete#tabnine#SuggestFlagCheck()
endfunction

function! s:flush()
if exists("s:tabnine_hint_snippet") && s:tabnine_hint_snippet == ""
if exists("s:tabnine_hint_snippet") && empty(s:tabnine_hint_snippet)
return
endif
call s:tabnine_toolkit.delete_hint()
call easycomplete#tabnine#SuggestFlagClear()
let s:tabnine_hint_snippet = ""
let s:tabnine_hint_snippet = []
endfunction

function! easycomplete#tabnine#flush()
Expand All @@ -78,13 +78,20 @@ function! easycomplete#tabnine#Callback(res_array)
call s:flush()
return
endif
let snippet = s:GetSnippets(a:res_array)
call s:tabnine_toolkit.show_hint(snippet)
let s:tabnine_hint_snippet = snippet
let l:snippet = s:GetSnippets(a:res_array)
let l:snippet_array = easycomplete#tabnine#ParseSnippets2Array(l:snippet)
call s:tabnine_toolkit.show_hint(l:snippet_array)
let s:tabnine_hint_snippet = deepcopy(l:snippet_array)
endfunction

function! easycomplete#tabnine#SnippetReady()
return s:tabnine_hint_snippet != ""
return exists("s:tabnine_hint_snippet") && !empty(s:tabnine_hint_snippet)
endfunction

" 返回一个标准数组,提示和显示都用这一份数据
function! easycomplete#tabnine#ParseSnippets2Array(code_block)
let l:lines = split(a:code_block, "\n")
return l:lines
endfunction

function! s:nr()
Expand All @@ -96,27 +103,27 @@ function! easycomplete#tabnine#insert()
let l:tabnine_hint_snippet = s:tabnine_hint_snippet
call s:flush()
try
let lines = split(l:tabnine_hint_snippet, "\n")
if len(lines) == 1 " 单行插入
let l:lines = l:tabnine_hint_snippet
if len(l:lines) == 1 " 单行插入
if getline('.') == ""
call execute("normal! \<Esc>")
call setbufline(bufnr(""), line("."), lines[0])
call cursor(line('.'), len(lines[0]))
call setbufline(bufnr(""), line("."), l:lines[0])
call cursor(line('.'), len(l:lines[0]))
call s:RemainInsertMode()
else
call feedkeys(l:tabnine_hint_snippet, 'i')
call feedkeys(l:lines[0], 'i')
endif
elseif len(lines) > 1 " 多行插入
elseif len(l:lines) > 1 " 多行插入
call execute("normal! \<Esc>")
let curr_line_nr = line('.')
let curr_line_str = getline(".")
call setbufline(bufnr(""), line("."), curr_line_str . lines[0])
for line in lines[1:]
call setbufline(bufnr(""), line("."), curr_line_str . l:lines[0])
for line in l:lines[1:]
call s:nr()
call setbufline(bufnr(""), line("."), line)
endfor

let end_line_nr = curr_line_nr + len(lines) - 1
let end_line_nr = curr_line_nr + len(l:lines) - 1
call cursor(end_line_nr, len(getline(end_line_nr)))
call s:RemainInsertMode()
redraw
Expand Down
7 changes: 5 additions & 2 deletions lua/easycomplete/tabnine.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local Util = require "easycomplete.util"
local log = Util.log
local Export = {}
local tabnine_ns = vim.api.nvim_create_namespace('tabnine_ns')

Expand All @@ -24,10 +26,12 @@ end

-- code_block 是一个字符串,有可能包含回车符
-- call v:lua.require("easycomplete.tabnine").show_hint()
-- code_block 是数组类型
function Export.show_hint(code_block)
local lines = {}
local count = 1
for line in code_block:gmatch("[^\r\n]+") do
local code_lines = code_block
for key, line in pairs(code_lines) do
local highlight_group = ""
if count == 1 then
highlight_group = "TabNineSuggestionFirstLine"
Expand All @@ -41,7 +45,6 @@ function Export.show_hint(code_block)
local virt_text = lines[1]
local virt_lines

-- print(#lines)
if #lines >= 2 then
table.remove(lines, 1)
virt_lines = lines
Expand Down

0 comments on commit d86d6cb

Please sign in to comment.