Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use luasnip api for getting choice-docstrings #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

L3MON4D3
Copy link

@L3MON4D3 L3MON4D3 commented Mar 6, 2023

Hey!
I noticed (due to an issue in luasnip) that first of all, this plugin exists, nice work! and that it's using some not-so-safe api of luasnip.

This change mostly uses ls.get_current_choices to get the string-descriptions of the choices, which should be more stable than the current approach.

Also, I've set the text inserted by nvim-cmp when hovering over any of the choices to "", I've encountered some issues when this was long text (for example with this snippet) (feel free to have me drop this one if you don't like it though :) )

@hinell
Copy link

hinell commented Mar 7, 2023

@doxnit Please merge asap!

@L3MON4D3
Copy link
Author

L3MON4D3 commented Mar 7, 2023

@doxnit Please merge asap!

You can also get my changes right away by using L3MON4D3/cmp-luasnip-choice in you config

@hinell
Copy link

hinell commented Mar 7, 2023

@L3MON4D3 I know but it would be best to have it in the mainstream branch instead.

@L3MON4D3
Copy link
Author

L3MON4D3 commented Mar 7, 2023

Ah okay, I misunderstood you😅

@hinell
Copy link

hinell commented Mar 24, 2023

So far, so good... I think we need a small fix: the choice has to be rendered when user choses it..

lua-snip-choice-fix-by-L3MON4D3.mov

@hinell
Copy link

hinell commented Mar 24, 2023

@L3MON4D3 I think previous, OOP-oriented api of the LuaSnip was much better.... I wonder when this has changed.

@L3MON4D3
Copy link
Author

the choice has to be rendered when user choses it..

I removed that, because it seemed to mess up the snippet a bit. I'll see if I can add it back in while avoiding those issues

I think previous, OOP-oriented api of the LuaSnip was much better.... I wonder when this has changed.

Unfortunately, those methods were never public api, and I don't think they even used to work in all cases :/
Having an api like that would be pretty cool, but that would require a lot of work, and I would instead like to support just the operations that are actually useful

@hinell
Copy link

hinell commented Mar 25, 2023

@L3MON4D3 Totally understandable. Thanks for all the hardwork. Looking forward for a merge!
UPD: I think we will have to switch to yours fork instead as this repo is basically dead. User isn't active.

@chrisgrieser
Copy link

chrisgrieser commented Apr 2, 2023

Hi,
sorry to use this PR for asking a question, but the owner does not seem to be active, and the fork does not have issues enabled... 🙈

For some reason, I cannot get this plugin to work? Choice selection works fine when using require("luasnip").change_choice(1), and :CmpStatus also reports that luasnip_choice is active. :lua= require"luasnip".get_current_choices() also shows me an array of all choices. However, the choices never show up in a cmp window for me?

I am using using snippets loaded from VS-code style snippet files if that matters.

@hinell
Copy link

hinell commented Apr 4, 2023

@chrisgrieser Your question is compeletly unrelated and should be referred to the nvim-cmp repo.

You have to properly configure nvim-cmp first. Here is a little trick I use to configure arrow shortcuts via require("cmp").setup(...) for instance:

    mapping = {
		["<Up>"] = cmp.mapping(function(fallback) 
			if luasnip.choice_active() then
				luasnip.change_choice(-1)
				if cmp.visible() then
					-- Instead of inserting, just focus a visual dropdown item
					cmp.select_prev_item({ behavior = cmp.SelectBehavior.Select })
					return
				end
			end
			if cmp.visible() then
				cmp.select_prev_item({ behavior = cmp.SelectBehavior.Insert })
			else
				fallback()
			end	
		end, { "i", "s", "c" }),
		["<Down>"] = cmp.mapping(function(fallback) 
			if luasnip.choice_active() then
				luasnip.change_choice(1)
				if cmp.visible() then
					-- Instead of inserting, just select a visual dropdown item
					cmp.select_next_item({ behavior = cmp.SelectBehavior.Select })
					return
				end
			end
			if cmp.visible() then
				cmp.select_next_item({ behavior = cmp.SelectBehavior.Insert })
			else
				fallback()
			end
		end, { "i", "s", "c" }),
      }

@chrisgrieser
Copy link

chrisgrieser commented Apr 4, 2023

I have cmp working correctly for 10+ sources – cmp-luasnip-choice is the only one that does not work.
I only mentioned the choice selection as it indicates that the luasnip part is working as well, meaning the problem seems to be with this plugin.

@hinell
Copy link

hinell commented Apr 5, 2023

@chrisgrieser It works for me as it's properly configured.

@Aumnescio
Copy link

I'm also having issues with both versions of this plugin, and agree with Chris in that the problem seems to be with this plugin.

  • Using a binding to change the choices does work, but I find it practically useless.
  • In L3MON4D3's fork, the results do not get sorted at all based on currently entered text.
    • Presumably this might be related to the word field being assigned to an empty string.
    • In doxnit's version, results are sorted based on the text before cursor. Which is how basically every other source also works.
  • Auto-open does not seem to work for me on either version.
    • If I start typing something when a choice node is active, the completion menu does not include the results from the cmp-luasnip-choice source.
    • Only when I explicitly call the cmp.complete() function, I will get the results, but if I then type any character, the results are gone again.

For example, if I'm on a choice-node with the choices "black", "white", "blue", "orange", and type the character 'b', I would expect to get the results: "blue", "black", "white", and "orange", in this order, in the nvim-cmp completion popup menu. (without explicitly having to call cmp.complete())

@L3MON4D3
Copy link
Author

Sorry, I'm really not interested in maintaining this :/
If anybody wants to do so, feel free to, I'll happily assist on the luasnip-api-side of things, but I don't have a good-enough understanding of cmp to help there.
For what it's worth, this source is working for me if its triggered manually, consider setting up a keybinding exclusively for triggering this source, and exclude it from autocompletion

@Aumnescio
Copy link

Fixed the auto-open/autocompletion issue by changing this:

function M.source:get_keyword_pattern()
    return ''
end

to this:

function M.source:get_keyword_pattern()
    return "\\%([^[:alnum:][:blank:]]\\|\\w\\+\\)"
end

which I fetched from the cmp_luasnip source plugin.

Some other pattern might work there too I suppose, but I'm not too good at this regex stuff, so I'll take what works.

@weilbith
Copy link

weilbith commented Nov 16, 2023

For some reason, there is never a choice node active. Tried to do some minor debugging. But even while the ext_opts show me visually that there is a choice node active, there is none. At least not according to the API calls within this plugin.

EDIT:
Okay, so somehow it works for me when I call this manually with a keymap, but not via the auto-command. Though, not in select mode, just in insert mode.

EDIT2:
Okay, so after all the only adjustment necessary for me is the plural s for the active_choice_nodes in the is_available function.
Despite that, nvim-cmp does not seem to work with select mode. Having insert nodes that have no default where you end up in insert mode after the choice node is entered, the completion menu pops-up.
And for some reason each item is twice in the list.

@weilbith
Copy link

weilbith commented Nov 17, 2023

@Aumnescio your trigger should not be necessary as completion gets called manually when entering a choice node. So this simply doesn't matter. It is also not necessary to register the luasnip_choice source in the setup.

@weilbith
Copy link

@L3MON4D3 is it possible to have snippet descriptions for choice nodes? Like you can provide the descr for a top level snippet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants