Skip to content

'$' character in snippet body causes error in 'vsnip#to_string' #245

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

Closed
Kamholtz opened this issue Mar 15, 2022 · 2 comments
Closed

'$' character in snippet body causes error in 'vsnip#to_string' #245

Kamholtz opened this issue Mar 15, 2022 · 2 comments

Comments

@Kamholtz
Copy link

The presence of '$' in snippets seems to result in an error and does not expand:

Error executing vim.schedule lua callback: {"result": [false, null, 0], "text": "$('

')"}
stack traceback:
[C]: in function 'vsnip#to_string'
...\site\pack\packer\start\cmp-vsnip/lua/cmp_vsnip/init.lua:40: in function 'resolve'
...-data\site\pack\packer\start\nvim-cmp/lua/cmp/source.lua:348: in function 'resolve'
...m-data\site\pack\packer\start\nvim-cmp/lua/cmp/entry.lua:452: in function 'resolve'
...im-data\site\pack\packer\start\nvim-cmp/lua/cmp/view.lua:225: in function 'fn'
...\site\pack\packer\start\nvim-cmp/lua/cmp/utils/async.lua:41: in function <...\site\pack\packer\start\nvim-cmp/lua/cmp/utils/async.lua:39>

The snippet causing this error:

{
	"JQuery div": {
		"prefix": "jdiv",
		"body": [
			"$('<div />')"
		]
	}
}

Removing the '$' in the body array allows the snippet to expand without error. The snippet as is works in VSCode, so not sure if this is considered a bug or just a difference between VSCode snippets and this plugin. I suspect it is trying to expand the '$' and following characters as if it were a placeholder. Is there a way to escape the '$' in the meantime?

@hrsh7th
Copy link
Owner

hrsh7th commented Mar 15, 2022

You should escape $ with \\

@hrsh7th hrsh7th closed this as completed Mar 15, 2022
@dezza
Copy link

dezza commented Feb 26, 2025

@hrsh7th hey I saw this and I was wondering why some snippets in bash-language-server like
if-unset-or-null
https://github.com/bash-lsp/bash-language-server/blob/main/server/src/snippets.ts#L527 is escaped

  { # Works
    documentation: 'if unset or null',
    label: 'if-unset-or-null',
    insertText: '"\\${${1:variable}:-${2:default}}"',
  },

while others are not
while-defined

  { # Does not work
    label: 'while-defined',
    documentation: 'while with variable existence check',
    insertText: [
      'while [[ -n "${${1:variable}+x}" ]]',
      '\t${2:command ...}',
      'done',
    ].join('\n'),
  },

The effect of course is that only

while [[ -n "

Is returned, it breaks the snippet expansion completely as well as jumps.

I tested in (reference) vscode just to check, as well as ask my friend who uses coc-lsp (I use yegappan/lsp) how it behaved, but to my surprise, it worked.

These other clients must use some kind of magic interpretation of the tokens to conclude that the outer ${...+x} is not a placeholder, but actual text to be expanded - where vsnip assumes $ is placeholder due to lack of escaping and breaks

Still raises the question why the first snippet if-unset-or-null is properly escaped in that file.. it makes little sense to me, but I trust the bashls maintainers enough not to have overlooked it.

I did this PR, hope they will understand.
bash-lsp/bash-language-server#1258

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

No branches or pull requests

3 participants