HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //home/ubuntu/neovim/test/functional/plugin/lsp/snippet_spec.lua
local t = require('test.testutil')
local n = require('test.functional.testnvim')()

local snippet = require('vim.lsp._snippet_grammar')
local type = snippet.NodeType

local eq = t.eq
local exec_lua = n.exec_lua

describe('vim.lsp._snippet_grammar', function()
  before_each(n.clear)
  after_each(n.clear)

  local parse = function(...)
    local res = exec_lua('return require("vim.lsp._snippet_grammar").parse(...)', ...)
    return res.data.children
  end

  it('parses only text', function()
    eq({
      { type = type.Text, data = { text = 'TE$}XT' } },
    }, parse('TE\\$\\}XT'))
  end)

  it('parses tabstops', function()
    eq({
      { type = type.Tabstop, data = { tabstop = 1 } },
      { type = type.Tabstop, data = { tabstop = 2 } },
    }, parse('$1${2}'))
  end)

  it('parses nested placeholders', function()
    eq({
      {
        type = type.Placeholder,
        data = {
          tabstop = 1,
          value = {
            type = type.Placeholder,
            data = {
              tabstop = 2,
              value = { type = type.Tabstop, data = { tabstop = 3 } },
            },
          },
        },
      },
    }, parse('${1:${2:${3}}}'))
  end)

  it('parses variables', function()
    eq({
      { type = type.Variable, data = { name = 'VAR' } },
      { type = type.Variable, data = { name = 'VAR' } },
      {
        type = type.Variable,
        data = {
          name = 'VAR',
          default = { type = type.Tabstop, data = { tabstop = 1 } },
        },
      },
      {
        type = type.Variable,
        data = {
          name = 'VAR',
          regex = 'regex',
          options = '',
          format = {
            {
              type = type.Format,
              data = { capture = 1, modifier = 'upcase' },
            },
          },
        },
      },
    }, parse('$VAR${VAR}${VAR:$1}${VAR/regex/${1:/upcase}/}'))
  end)

  it('parses choice', function()
    eq({
      {
        type = type.Choice,
        data = { tabstop = 1, values = { ',', '|' } },
      },
    }, parse('${1|\\,,\\||}'))
  end)

  it('parses format', function()
    eq(
      {
        {
          type = type.Variable,
          data = {
            name = 'VAR',
            regex = 'regex',
            options = '',
            format = {
              {
                type = type.Format,
                data = { capture = 1, modifier = 'upcase' },
              },
              {
                type = type.Format,
                data = { capture = 1, if_text = 'if_text' },
              },
              {
                type = type.Format,
                data = { capture = 1, else_text = 'else_text' },
              },
              {
                type = type.Format,
                data = { capture = 1, if_text = 'if_text', else_text = 'else_text' },
              },
              {
                type = type.Format,
                data = { capture = 1, else_text = 'else_text' },
              },
            },
          },
        },
      },
      parse(
        '${VAR/regex/${1:/upcase}${1:+if_text}${1:-else_text}${1:?if_text:else_text}${1:else_text}/}'
      )
    )
  end)

  it('parses empty strings', function()
    eq({
      {
        type = type.Placeholder,
        data = {
          tabstop = 1,
          value = { type = type.Text, data = { text = '' } },
        },
      },
      {
        type = type.Text,
        data = { text = ' ' },
      },
      {
        type = type.Variable,
        data = {
          name = 'VAR',
          regex = 'erg',
          format = {
            {
              type = type.Format,
              data = { capture = 1, if_text = '' },
            },
          },
          options = 'g',
        },
      },
    }, parse('${1:} ${VAR/erg/${1:+}/g}'))
  end)

  it('parses closing curly brace as text', function()
    eq(
      {
        { type = type.Text, data = { text = 'function ' } },
        { type = type.Tabstop, data = { tabstop = 1 } },
        { type = type.Text, data = { text = '() {\n  ' } },
        { type = type.Tabstop, data = { tabstop = 0 } },
        { type = type.Text, data = { text = '\n}' } },
      },
      parse(table.concat({
        'function $1() {',
        '  $0',
        '}',
      }, '\n'))
    )
  end)
end)