Use faster get_complete_context in nvim-0.5#390
Use faster get_complete_context in nvim-0.5#390SevereOverfl0w wants to merge 1 commit intotpope:masterfrom
Conversation
|
Apparently I could, but for some reason there's no upload button! Uploaded here anyway. The benchmark script I used was: And to run it I opened vim and did: The function I tested this within is 54 lines in total, within a file that is 346 lines long. I originally noticed this problem in a file that was ~500 lines long when editing a function of around ~100 lines towards the end of the file. |
02464fb to
3d30e86
Compare
|
First question: Why |
|
There's no way to mark code "private" in lua, _ is a convention for that (similar to python). |
|
That answers half the question. Why |
|
@tpope It is in lua, I assumed you typo'd autoload. Apologies. |
|
My bad, could have sworn I saw If the function changes, how do I reload it? I think ideally, reloading Can we go ahead and structure this in a way that isn't limited to a single function? |
|
I can make that change, no problem! https://github.com/nanotee/nvim-lua-guide has tips for developing against nvim + lua. https://github.com/nanotee/nvim-lua-guide#reloading-cached-modules explains reloading. I was just restarting nvim personally, it's fast! |
3d30e86 to
2fc7bbd
Compare
|
I've made that change. I've also fixed a bug I noticed where if you started a new form the treesitter AST needs to be updated. I've re-run the benchmark, and the numbers are approximately the same. |
66172c9 to
034e39f
Compare
034e39f to
90d0941
Compare
autoload/fireplace.vim
Outdated
| " Find toplevel form | ||
| " If cursor is on start parenthesis we don't want to find the form | ||
| " If cursor is on end parenthesis we want to find the form | ||
| let [line1, col1] = searchpairpos('(', '', ')', 'Wrnb', g:fireplace#skip) | ||
| let [line2, col2] = searchpairpos('(', '', ')', 'Wrnc', g:fireplace#skip) | ||
|
|
||
| if (line1 == 0 && col1 == 0) || (line2 == 0 && col2 == 0) | ||
| return "" | ||
| endif | ||
| if (line1 == 0 && col1 == 0) || (line2 == 0 && col2 == 0) | ||
| return "" | ||
| endif | ||
|
|
||
| if line1 == line2 | ||
| let expr = getline(line1)[col1-1 : col2-1] | ||
| else | ||
| let expr = getline(line1)[col1-1 : -1] . ' ' | ||
| \ . join(getline(line1+1, line2-1), ' ') | ||
| \ . getline(line2)[0 : col2-1] | ||
| endif | ||
| if line1 == line2 | ||
| let expr = getline(line1)[col1-1 : col2-1] | ||
| else | ||
| let expr = getline(line1)[col1-1 : -1] . ' ' | ||
| \ . join(getline(line1+1, line2-1), ' ') | ||
| \ . getline(line2)[0 : col2-1] | ||
| endif | ||
|
|
||
| " Calculate the position of cursor inside the expr | ||
| if line1 == line('.') | ||
| let p = col('.') - col1 | ||
| else | ||
| let p = strlen(getline(line1)[col1-1 : -1]) | ||
| \ + strlen(join(getline(line1 + 1, line('.') - 1), ' ')) | ||
| \ + col('.') | ||
| endif | ||
| " Calculate the position of cursor inside the expr | ||
| if line1 == line('.') | ||
| let p = col('.') - col1 | ||
| else | ||
| let p = strlen(getline(line1)[col1-1 : -1]) | ||
| \ + strlen(join(getline(line1 + 1, line('.') - 1), ' ')) | ||
| \ + col('.') | ||
| endif | ||
|
|
||
| return strpart(expr, 0, p) . ' __prefix__ ' . strpart(expr, p) | ||
| return strpart(expr, 0, p) . ' __prefix__ ' . strpart(expr, p) |
There was a problem hiding this comment.
Looks like you botched the indent here.
lua/_fireplace.lua
Outdated
| local nrow1,ncol1,nline2,ncol2 = root_node:range() | ||
| local crow,ccol = unpack(vim.api.nvim_win_get_cursor(0)) |
There was a problem hiding this comment.
Can you give these commas some breathing room, along with the = up top?
For users using an "autocomplete" package of some kind (so completion on every key press), a slow completion function adds noticeable lag to typing. The use of searchpairpos really slows down the current context implementation. Since nvim-0.5, there's a treesitter package included. Treesitter can incrementally parse the tree, and offers a very fast way to find the root node.
3cf7167 to
0c817aa
Compare
For users using an "autocomplete" package of some kind (so completion on
every key press), a slow completion function adds noticeable lag to
typing. The use of searchpairpos really slows down the current context
implementation.
Since nvim-0.5, there's a treesitter package included. Treesitter can
incrementally parse the tree, and offers a very fast way to find the
root node.
I've benchmarked before & after using the clip library and get_complete_context goes from 6.244431 to 0.027365 self time (I will include the profile in a comment, as I cannot upload a file to a PR).
I've implemented this such that existing vim users will experience no degradation, that's required gymnastics in defining s:get_complete_context. To keep s:get_complete_context very fast, I've avoided conditionals which won't change while vim is running.