Skip to content

Commit

Permalink
fix(nvim): avoid annoying unwanted jumps in LuaSnip
Browse files Browse the repository at this point in the history
This one has been driving me up the wall. If you type something like
this:

    log<tab>

that expands to:

    console.log(<stuff>);

and if later on you type tab somewhere else, like here, where you want
an indent:

    function foo() {
    <tab>
    }

then LuaSnip will jump back inside the `console.log()`.

I thought that setting `history = false` in the config would stop this,
because the docs say:

> - `history`: If true, Snippets that were exited can still be jumped back into.

and it seems I wasn't alone in thinking this:

    L3MON4D3/LuaSnip#213

That issue lead to this PR:

    L3MON4D3/LuaSnip#215

which adds an `expand_or_locally_jumpable()` function.

So, this commit pulls that in. Two things to note:

(1) The LuaSnip update changes the API in a way that produced these errors
    trying to complete a snippet:

        E5108: Error executing lua ...vim/pack/bundle/opt/LuaSnip/lua/luasnip/util/environ.lua:16:
        attempt to index local 'pos' (a nil value)

    I updated nvim-cmp to see if that would fix it (it didn't), before
    finding the actual remedy, which was updating cmp_luasnip.

(2) `expand_or_locally_jumpable()` only works for forward jumps, not
    backwards ones like `jumpable(-1)`, so I grabbed the `in_snippet()`
    function (private to LuaSnip) and adapted it so I could use it
    for the backwards case as well. I am not actually sure if this is
    required, strictly speaking, but it doesn't seem to do any harm
    either, so I'm keeping it.

Submodule updates listed below:

* aspects/nvim/files/.config/nvim/pack/bundle/opt/LuaSnip b7d520e...89f8712 (59):
  > fix: Prevent error when snip is deleted and updated from same event.
  > Format with stylua
  > perf: Optimize out unnecessary calls to pos_begin_end_raw.
  > perf: use pos_begin_raw for comparing positions.
  > perf: improve primitive algorithm for adjusting marks inside snippet.
  > Auto generate docs
  > Format with stylua
  > fix(docstrings): only wrap env-value in table if it is usually in one.
  > doc: mention `snippet.trigger`.
  > doc: specify that the immediate parent is passed to the d/fNode-function.
  > BREAKING CHANGE: don't pass env to snippetNodes.
  > fix: handle end-mark being one line below after full delete.
  > fix: Perform snip-delete-check before updating.
  > Format with stylua
  > fix: Unlink snippet on out-of-range-errors.
  > fix: make check for node vs table a bit better.
  > Auto generate docs
  > Format with stylua
  > Merge pull request #226 from ner0-m/feat/fs-for-language-injection
  > Auto generate docs
  > doc: use simple ordered list instead of indexing an unordered one.
  > doc: clarify expected position-format.
  > Auto generate docs
  > docs: update lsp_expand-doc.
  > Auto generate docs
  > Format with stylua
  > fix: default captures to empty table.
  > feat: allow expanding at any position.
  > Merge pull request #236 from L3MON4D3/refactor_expand
  > docs: also update link to snippets-table in readme.
  > docs: recommend skipping auxiliary functions.
  > fix: fix+improve update-behaviour of restore/dynamic/snippetNode.
  > Auto generate docs
  > perf: don't copy restoreNode-snippet in put_initial. Closes #234.
  > Auto generate docs
  > feat(choice): Allow passing a table of nodes as choice.
  > Auto generate docs
  > docs: misc fixes, rewords, corrections.
  > Merge pull request #233 from L3MON4D3/restore_position
  > fix(dyn): prevent querying cleared extmarks.
  > Auto generate docs
  > docs: whoops.
  > Merge pull request #228 from L3MON4D3/restoreNode
  > Auto generate docs
  > Merge pull request #229 from tiagovla/fix_typo
  > fix(dynamicNode): Correctly init subsnips for static text/docstring.
  > Auto generate docs
  > doc: recommend `lambda` in text on `functionNode`.
  > Auto generate docs
  > doc(luasnip.extras): add it.
  > Format with stylua
  > fix(fmt): prevent the same node being reused (close #221).
  > Merge pull request #216 from rhcher/master
  > Auto generate docs
  > Merge pull request #215 from dvdsk/master
  > Format with stylua
  > Format with stylua
  > fix(select): if select ends on a multibyte-char, it's cut off (close #214).
  > fix(insertNode): node ending with \n leads to wrong selection.

* aspects/nvim/files/.config/nvim/pack/bundle/opt/cmp_luasnip 16832bb...7bd2612 (2):
  > fix(cmp_luasnip): remove unused parameters
  > Merge pull request #29 from L3MON4D3/master

* aspects/nvim/files/.config/nvim/pack/bundle/opt/nvim-cmp 669803b...4efecf7 (103):
  > Correct vim doc reference for getcmdtype() (#650)
  > Fix #648
  > Fix #582
  > Fix #603
  > Fix #640
  > Fix #646 Resolve all properties by default
  > Fix #637
  > Update issue template
  > Update issue template
  > Update issue template
  > Fix rendering of wide characters in the custom completion window (#641)
  > docs: clarify behavior of `select` in confirm mapping (#621)
  >  Readme: improve readability of lua code block in setup section (#622)
  > Fix #610
  > feat: add issue template (#604)
  > Fix docs
  > fmt
  > Implement #349
  > Fix #595
  > fmt
  > Use vim.lsp.util.apply_text_edits instead of vim-vital-vs
  > fix: default highlights overriding existing highlights (#591)
  > Fix duplicate doc tags (#586)
  > Implement kind highlights (#584)
  > Fix multibyte chars
  > Fire change events when selecting different items with native menu completion (#514)
  > Add documents (#460)
  > Fix README.md
  > Improve incomplete handling
  > Remove misc.readonly Fix #571
  > Fix #321
  > Allow to take the full SourceConfig from source API (#561)
  > Normalize keymapping configuration Fix #475 Fix #570 Fix #533
  > Fix #567
  > Fix #563
  > Fix #559
  > Improve first display
  > Fix possible nil argument to string.byte (#550)
  > Add some wiki info in README (#549)
  > Rename type as cmdtype
  > Improve completion reset handling
  > Fix document about scrolling docs backwards (#538)
  > Refactor get_active_entry and core.filter (#535)
  > Add reason to complete function (#529)
  > Fix cmdline screen cursor
  > Don't feed <C-f> manually but check auto-indentation
  > Fix #526
  > Fix #520
  > cosmetic changes
  > fmt
  > Add cmdline expansion test
  > Add tests
  > Fix #516 (#517)
  > Fix #513
  > Fixed typo in README.md (#515)
  > Link to menu appearance page in wiki (#493)
  > Fix cmdline confirmation bug
  > Fix #504
  > Handle block visual/select modes, refactor mode handling (#498)
  > Remove unused methods
  > Improve regex handling
  > Handle InsertEnter and CmdlineEnter seperately
  > Remove triggering insert enter event
  > Improve avoiding InsertEnter's invalid mode detection
  > Fixes the bug that the `cmp.close` does not close the menu when using native_menu.
  > Use key sequence to avoid the invalid mode detection
  > Add textwidth tests
  > Re-fix autoindent with tests
  > Fix #494
  > Fix #473
  > strict mapping handling
  > Fix tests
  > Fix #480
  > Fix #481
  > Add linebreak option to cmp documentation window (#486)
  > Fix cmdline confirmation
  > Improve async.step
  > Save undopoint
  > Use apply_text_edits to avoid making the unexpected indentation by '<CR>'
  > fix(ghost_text): generate it using chars instead of bytes (#471)
  > Escape <Bar> when setting indentkeys (#474)
  > Fix expansion bug
  > Allow the callback invoking multiple times if it's the recent one
  > Support session
  > Fix native menu bug
  > tmp fix for serveral bugs
  > Fix #421
  > Fix #463
  > Fix and improve README.md (#462)
  > Fix
  > Support isIncomplete only response
  > Fix #458
  > Fix #449
  > Fix #456
  > Organize keymap implementation (#454)
  > Fix keymap bug
  > Fix #443
  > Rewrite keymap.lua (#452)
  > Fix #436
  > Fix native menu not respecting cmp.PreselectMode.None sometimes (#447)
  > misc.merge: ensure that `misc.none` is never returned (#441)
  > README: clarify that a snippet plugin is required (#439)
  > Add mappings for visual mode (#438)
  • Loading branch information
wincent committed Dec 15, 2021
1 parent fe159cb commit c2e7615
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
19 changes: 17 additions & 2 deletions aspects/nvim/files/.config/nvim/after/plugin/nvim-cmp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ if has_cmp then
return col
end

-- Based on (private) function in LuaSnip/lua/luasnip/init.lua.
local in_snippet = function()
local session = require('luasnip.session')
local node = session.current_nodes[vim.api.nvim_get_current_buf()]
if not node then
return false
end
local snippet = node.parent.snippet
local snip_begin_pos, snip_end_pos = snippet.mark:pos_begin_end()
local pos = vim.api.nvim_win_get_cursor(0)
if pos[1] - 1 >= snip_begin_pos[1] and pos[1] - 1 <= snip_end_pos[1] then
return true
end
end

-- Returns true if the cursor is in leftmost column or at a whitespace
-- character.
local in_whitespace = function()
Expand Down Expand Up @@ -133,7 +148,7 @@ if has_cmp then
['<S-Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif has_luasnip and luasnip.jumpable(-1) then
elseif has_luasnip and in_snippet() and luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
Expand All @@ -150,7 +165,7 @@ if has_cmp then
else
cmp.select_next_item()
end
elseif has_luasnip and luasnip.expand_or_jumpable() then
elseif has_luasnip and luasnip.expand_or_locally_jumpable() then
luasnip.expand_or_jump()
elseif in_whitespace() then
smart_tab()
Expand Down
2 changes: 1 addition & 1 deletion aspects/nvim/files/.config/nvim/pack/bundle/opt/nvim-cmp
Submodule nvim-cmp updated 45 files
+0 βˆ’31 .github/ISSUE_TEMPLATE/bug_report.md
+66 βˆ’0 .github/ISSUE_TEMPLATE/bug_report.yml
+2 βˆ’0 .gitignore
+123 βˆ’74 README.md
+0 βˆ’12 autoload/cmp.vim
+0 βˆ’9 autoload/vital/_cmp.vim
+0 βˆ’62 autoload/vital/_cmp/VS/LSP/Position.vim
+0 βˆ’23 autoload/vital/_cmp/VS/LSP/Text.vim
+0 βˆ’185 autoload/vital/_cmp/VS/LSP/TextEdit.vim
+0 βˆ’126 autoload/vital/_cmp/VS/Vim/Buffer.vim
+0 βˆ’21 autoload/vital/_cmp/VS/Vim/Option.vim
+0 βˆ’330 autoload/vital/cmp.vim
+0 βˆ’4 autoload/vital/cmp.vital
+483 βˆ’0 doc/cmp.txt
+57 βˆ’25 lua/cmp/config.lua
+2 βˆ’2 lua/cmp/config/compare.lua
+2 βˆ’5 lua/cmp/config/default.lua
+8 βˆ’26 lua/cmp/config/mapping.lua
+1 βˆ’29 lua/cmp/context.lua
+193 βˆ’180 lua/cmp/core.lua
+158 βˆ’0 lua/cmp/core_spec.lua
+28 βˆ’21 lua/cmp/entry.lua
+29 βˆ’16 lua/cmp/init.lua
+61 βˆ’67 lua/cmp/source.lua
+21 βˆ’10 lua/cmp/types/cmp.lua
+28 βˆ’17 lua/cmp/utils/api.lua
+46 βˆ’0 lua/cmp/utils/api_spec.lua
+12 βˆ’0 lua/cmp/utils/async.lua
+26 βˆ’0 lua/cmp/utils/async_spec.lua
+26 βˆ’13 lua/cmp/utils/buffer.lua
+0 βˆ’0 lua/cmp/utils/cache_spec.lua
+110 βˆ’0 lua/cmp/utils/feedkeys.lua
+47 βˆ’0 lua/cmp/utils/feedkeys_spec.lua
+1 βˆ’1 lua/cmp/utils/highlight.lua
+123 βˆ’227 lua/cmp/utils/keymap.lua
+5 βˆ’27 lua/cmp/utils/keymap_spec.lua
+9 βˆ’3 lua/cmp/utils/misc.lua
+14 βˆ’0 lua/cmp/utils/misc_spec.lua
+39 βˆ’0 lua/cmp/utils/spec.lua
+38 βˆ’5 lua/cmp/utils/window.lua
+86 βˆ’76 lua/cmp/view/custom_entries_view.lua
+1 βˆ’0 lua/cmp/view/docs_view.lua
+29 βˆ’9 lua/cmp/view/ghost_text_view.lua
+20 βˆ’6 lua/cmp/view/native_entries_view.lua
+36 βˆ’24 plugin/cmp.lua

0 comments on commit c2e7615

Please sign in to comment.