snippet_spec.lua (4248B)
1 local t = require('test.testutil') 2 local n = require('test.functional.testnvim')() 3 4 local snippet = require('vim.lsp._snippet_grammar') 5 local type = snippet.NodeType 6 7 local eq = t.eq 8 local exec_lua = n.exec_lua 9 10 describe('vim.lsp._snippet_grammar', function() 11 before_each(n.clear) 12 13 local parse = function(...) 14 local res = exec_lua('return require("vim.lsp._snippet_grammar").parse(...)', ...) 15 return res.data.children 16 end 17 18 it('parses only text', function() 19 eq({ 20 { type = type.Text, data = { text = 'TE$}XT' } }, 21 }, parse('TE\\$\\}XT')) 22 end) 23 24 it('parses tabstops', function() 25 eq({ 26 { type = type.Tabstop, data = { tabstop = 1 } }, 27 { type = type.Tabstop, data = { tabstop = 2 } }, 28 }, parse('$1${2}')) 29 end) 30 31 it('parses nested placeholders', function() 32 eq({ 33 { 34 type = type.Placeholder, 35 data = { 36 tabstop = 1, 37 value = { 38 type = type.Placeholder, 39 data = { 40 tabstop = 2, 41 value = { type = type.Tabstop, data = { tabstop = 3 } }, 42 }, 43 }, 44 }, 45 }, 46 }, parse('${1:${2:${3}}}')) 47 end) 48 49 it('parses variables', function() 50 eq({ 51 { type = type.Variable, data = { name = 'VAR' } }, 52 { type = type.Variable, data = { name = 'VAR' } }, 53 { 54 type = type.Variable, 55 data = { 56 name = 'VAR', 57 default = { type = type.Tabstop, data = { tabstop = 1 } }, 58 }, 59 }, 60 { 61 type = type.Variable, 62 data = { 63 name = 'VAR', 64 regex = 'regex', 65 options = '', 66 format = { 67 { 68 type = type.Format, 69 data = { capture = 1, modifier = 'upcase' }, 70 }, 71 }, 72 }, 73 }, 74 }, parse('$VAR${VAR}${VAR:$1}${VAR/regex/${1:/upcase}/}')) 75 end) 76 77 it('parses choice', function() 78 eq({ 79 { 80 type = type.Choice, 81 data = { tabstop = 1, values = { ',', '|' } }, 82 }, 83 }, parse('${1|\\,,\\||}')) 84 end) 85 86 it('parses format', function() 87 eq( 88 { 89 { 90 type = type.Variable, 91 data = { 92 name = 'VAR', 93 regex = 'regex', 94 options = '', 95 format = { 96 { 97 type = type.Format, 98 data = { capture = 1, modifier = 'upcase' }, 99 }, 100 { 101 type = type.Format, 102 data = { capture = 1, if_text = 'if_text' }, 103 }, 104 { 105 type = type.Format, 106 data = { capture = 1, else_text = 'else_text' }, 107 }, 108 { 109 type = type.Format, 110 data = { capture = 1, if_text = 'if_text', else_text = 'else_text' }, 111 }, 112 { 113 type = type.Format, 114 data = { capture = 1, else_text = 'else_text' }, 115 }, 116 }, 117 }, 118 }, 119 }, 120 parse( 121 '${VAR/regex/${1:/upcase}${1:+if_text}${1:-else_text}${1:?if_text:else_text}${1:else_text}/}' 122 ) 123 ) 124 end) 125 126 it('parses empty strings', function() 127 eq({ 128 { 129 type = type.Placeholder, 130 data = { 131 tabstop = 1, 132 value = { type = type.Text, data = { text = '' } }, 133 }, 134 }, 135 { 136 type = type.Text, 137 data = { text = ' ' }, 138 }, 139 { 140 type = type.Variable, 141 data = { 142 name = 'VAR', 143 regex = 'erg', 144 format = { 145 { 146 type = type.Format, 147 data = { capture = 1, if_text = '' }, 148 }, 149 }, 150 options = 'g', 151 }, 152 }, 153 }, parse('${1:} ${VAR/erg/${1:+}/g}')) 154 end) 155 156 it('parses closing curly brace as text', function() 157 eq( 158 { 159 { type = type.Text, data = { text = 'function ' } }, 160 { type = type.Tabstop, data = { tabstop = 1 } }, 161 { type = type.Text, data = { text = '() {\n ' } }, 162 { type = type.Tabstop, data = { tabstop = 0 } }, 163 { type = type.Text, data = { text = '\n}' } }, 164 }, 165 parse(table.concat({ 166 'function $1() {', 167 ' $0', 168 '}', 169 }, '\n')) 170 ) 171 end) 172 end)