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

Merge multi-snippet common field, fix #921 #924

Merged
merged 3 commits into from
Jun 15, 2023
Merged

Merge multi-snippet common field, fix #921 #924

merged 3 commits into from
Jun 15, 2023

Conversation

Bekaboo
Copy link
Contributor

@Bekaboo Bekaboo commented Jun 15, 2023

Should fix #921

Currently the common field is replaced as a whole when the passed arg has this field set instead of merging the passed arg and the extend arg, which is not expected.

To clarify, consider the following cases:

I create some luasnip conds and define a new multi-snippet that only expand if ts is not active or ts and active and the cursor is not in a comment or string:

local msc = ls.extend_decorator.apply(ms, {
  common = {
    condition = -conds.ts_active
      + -conds.in_tsnode('comment') * -conds.in_tsnode('string'),
    show_condition = -conds.ts_active
      + -conds.in_tsnode('comment') * -conds.in_tsnode('string'),
  },
})

Then I want to create a multi-snippet with regex trigger and only expand when the cursor is not in a comment or string:

msc(
  {
    common = { regTrig = true },
    { trig = '(%S?)(%s*)%.%.%s*ck' },
    { trig = '(%S?)(%s*)%.%.%s*check' },
  },
  fmta('<spc>.. ", <v>: " .. vim.inspect(<v>)', {
    spc = f(function(_, snip, _)
      return snip.captures[1] == '' and snip.captures[2]
        or snip.captures[1] .. ' '
    end, {}, {}),
    v = i(1),
  }, { repeat_duplicates = true })
)

the original condition field is dropped because I provide the common field in the new snippet, making the original common table completely replaced by the new one i.e. { regTrig = true, condition = nil, show_condition = nil, ... }

I would suggest using vim.tbl_deep_extend to merge the common table, this fixes the issue (in function extend_multisnippet_contexts:

	-- extend ("keep") valid keyword-arguments.
	passed_arg.common = vim.tbl_deep_extend(
		"keep",
		passed_arg.common or {},
		extend_arg.common or {}
	)

@L3MON4D3
Copy link
Owner

Nice!
common can also be a string, could you take care of that? (node_util.wrap_context handles the conversion iirc)

@Bekaboo
Copy link
Contributor Author

Bekaboo commented Jun 15, 2023

Sure, will work on this later. Dont know why the test fails, though.

@Bekaboo
Copy link
Contributor Author

Bekaboo commented Jun 15, 2023

@L3MON4D3 Wrapped common in node_util.wrap_context.

@L3MON4D3
Copy link
Owner

Looks good 👍
Thank you!

@L3MON4D3 L3MON4D3 merged commit 65ff66d into L3MON4D3:master Jun 15, 2023
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.

[Bug] Cannot extend ls.multi_snippet
2 participants