From eb1f0e8fcca756a00d287e23bf87554e0e7f6dfd Mon Sep 17 00:00:00 2001 From: Maria José Solano Date: Sun, 1 Oct 2023 09:54:04 -0700 Subject: feat(lsp)!: replace snippet parser by lpeg grammar --- test/functional/plugin/lsp/snippet_spec.lua | 243 +++++++++------------------- 1 file changed, 80 insertions(+), 163 deletions(-) (limited to 'test/functional/plugin/lsp/snippet_spec.lua') diff --git a/test/functional/plugin/lsp/snippet_spec.lua b/test/functional/plugin/lsp/snippet_spec.lua index 7903885420..13df861b91 100644 --- a/test/functional/plugin/lsp/snippet_spec.lua +++ b/test/functional/plugin/lsp/snippet_spec.lua @@ -1,130 +1,70 @@ local helpers = require('test.functional.helpers')(after_each) -local snippet = require('vim.lsp._snippet') +local snippet = require('vim.lsp._snippet_grammar') local eq = helpers.eq local exec_lua = helpers.exec_lua -describe('vim.lsp._snippet', function() +describe('vim.lsp._snippet_grammar', function() before_each(helpers.clear) after_each(helpers.clear) local parse = function(...) - return exec_lua('return require("vim.lsp._snippet").parse(...)', ...) + local res = exec_lua('return require("vim.lsp._snippet_grammar").parse(...)', ...) + return res.data.children end - it('should parse only text', function() + it('parses only text', function() eq({ - type = snippet.NodeType.SNIPPET, - children = { - { - type = snippet.NodeType.TEXT, - raw = 'TE\\$\\}XT', - esc = 'TE$}XT', - }, - }, + { type = snippet.NodeType.Text, data = { text = 'TE$}XT' } }, }, parse('TE\\$\\}XT')) end) - it('should parse tabstop', function() + it('parses tabstops', function() eq({ - type = snippet.NodeType.SNIPPET, - children = { - { - type = snippet.NodeType.TABSTOP, - tabstop = 1, - }, - { - type = snippet.NodeType.TABSTOP, - tabstop = 2, - }, - }, + { type = snippet.NodeType.Tabstop, data = { tabstop = 1 } }, + { type = snippet.NodeType.Tabstop, data = { tabstop = 2 } }, }, parse('$1${2}')) end) - it('should parse placeholders', function() + it('parses nested placeholders', function() eq({ - type = snippet.NodeType.SNIPPET, - children = { - { - type = snippet.NodeType.PLACEHOLDER, + { + type = snippet.NodeType.Placeholder, + data = { tabstop = 1, - children = { - { - type = snippet.NodeType.PLACEHOLDER, + value = { + type = snippet.NodeType.Placeholder, + data = { tabstop = 2, - children = { - { - type = snippet.NodeType.TEXT, - raw = 'TE\\$\\}XT', - esc = 'TE$}XT', - }, - { - type = snippet.NodeType.TABSTOP, - tabstop = 3, - }, - { - type = snippet.NodeType.TABSTOP, - tabstop = 1, - transform = { - type = snippet.NodeType.TRANSFORM, - pattern = 'regex', - option = 'i', - format = { - { - type = snippet.NodeType.FORMAT, - capture_index = 1, - modifier = 'upcase', - }, - }, - }, - }, - { - type = snippet.NodeType.TEXT, - raw = 'TE\\$\\}XT', - esc = 'TE$}XT', - }, - }, + value = { type = snippet.NodeType.Tabstop, data = { tabstop = 3 } }, }, }, }, }, - }, parse('${1:${2:TE\\$\\}XT$3${1/regex/${1:/upcase}/i}TE\\$\\}XT}}')) + }, parse('${1:${2:${3}}}')) end) - it('should parse variables', function() + it('parses variables', function() eq({ - type = snippet.NodeType.SNIPPET, - children = { - { - type = snippet.NodeType.VARIABLE, - name = 'VAR', - }, - { - type = snippet.NodeType.VARIABLE, + { type = snippet.NodeType.Variable, data = { name = 'VAR' } }, + { type = snippet.NodeType.Variable, data = { name = 'VAR' } }, + { + type = snippet.NodeType.Variable, + data = { name = 'VAR', + default = { type = snippet.NodeType.Tabstop, data = { tabstop = 1 } }, }, - { - type = snippet.NodeType.VARIABLE, + }, + { + type = snippet.NodeType.Variable, + data = { name = 'VAR', - children = { + regex = 'regex', + options = '', + format = { { - type = snippet.NodeType.TABSTOP, - tabstop = 1, - }, - }, - }, - { - type = snippet.NodeType.VARIABLE, - name = 'VAR', - transform = { - type = snippet.NodeType.TRANSFORM, - pattern = 'regex', - format = { - { - type = snippet.NodeType.FORMAT, - capture_index = 1, - modifier = 'upcase', - }, + type = snippet.NodeType.Format, + data = { capture = 1, modifier = 'upcase' }, }, }, }, @@ -132,105 +72,82 @@ describe('vim.lsp._snippet', function() }, parse('$VAR${VAR}${VAR:$1}${VAR/regex/${1:/upcase}/}')) end) - it('should parse choice', function() + it('parses choice', function() eq({ - type = snippet.NodeType.SNIPPET, - children = { - { - type = snippet.NodeType.CHOICE, - tabstop = 1, - items = { - ',', - '|', - }, - }, + { + type = snippet.NodeType.Choice, + data = { tabstop = 1, values = { ',', '|' } }, }, }, parse('${1|\\,,\\||}')) end) - it('should parse format', function() - eq({ - type = snippet.NodeType.SNIPPET, - children = { + it('parses format', function() + eq( + { { - type = snippet.NodeType.VARIABLE, - name = 'VAR', - transform = { - type = snippet.NodeType.TRANSFORM, - pattern = 'regex', + type = snippet.NodeType.Variable, + data = { + name = 'VAR', + regex = 'regex', + options = '', format = { { - type = snippet.NodeType.FORMAT, - capture_index = 1, - modifier = 'upcase', + type = snippet.NodeType.Format, + data = { capture = 1, modifier = 'upcase' }, }, { - type = snippet.NodeType.FORMAT, - capture_index = 1, - if_text = 'if_text', - else_text = '', + type = snippet.NodeType.Format, + data = { capture = 1, if_text = 'if_text' }, }, { - type = snippet.NodeType.FORMAT, - capture_index = 1, - if_text = '', - else_text = 'else_text', + type = snippet.NodeType.Format, + data = { capture = 1, else_text = 'else_text' }, }, { - type = snippet.NodeType.FORMAT, - capture_index = 1, - else_text = 'else_text', - if_text = 'if_text', + type = snippet.NodeType.Format, + data = { capture = 1, if_text = 'if_text', else_text = 'else_text' }, }, { - type = snippet.NodeType.FORMAT, - capture_index = 1, - if_text = '', - else_text = 'else_text', + type = snippet.NodeType.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}/}')) + parse( + '${VAR/regex/${1:/upcase}${1:+if_text}${1:-else_text}${1:?if_text:else_text}${1:else_text}/}' + ) + ) end) - it('should parse empty strings', function() + it('parses empty strings', function() eq({ - children = { - { - children = { { - esc = '', - raw = '', - type = 7, - } }, + { + type = snippet.NodeType.Placeholder, + data = { tabstop = 1, - type = 2, - }, - { - esc = ' ', - raw = ' ', - type = 7, + value = { type = snippet.NodeType.Text, data = { text = '' } }, }, - { + }, + { + type = snippet.NodeType.Text, + data = { text = ' ' }, + }, + { + type = snippet.NodeType.Variable, + data = { name = 'VAR', - transform = { - format = { - { - capture_index = 1, - else_text = '', - if_text = '', - type = 6, - }, + regex = 'erg', + format = { + { + type = snippet.NodeType.Format, + data = { capture = 1, if_text = '' }, }, - option = 'g', - pattern = 'erg', - type = 5, }, - type = 3, + options = 'g', }, }, - type = 0, - }, parse('${1:} ${VAR/erg/${1:?:}/g}')) + }, parse('${1:} ${VAR/erg/${1:+}/g}')) end) end) -- cgit From ee156ca60ede95c95160cb8dff0197d40cb1a491 Mon Sep 17 00:00:00 2001 From: Maria José Solano Date: Sat, 14 Oct 2023 00:06:40 -0700 Subject: fix(lsp): refactor escaping snippet text (#25611) --- test/functional/plugin/lsp/snippet_spec.lua | 66 ++++++++++++++++++----------- 1 file changed, 42 insertions(+), 24 deletions(-) (limited to 'test/functional/plugin/lsp/snippet_spec.lua') diff --git a/test/functional/plugin/lsp/snippet_spec.lua b/test/functional/plugin/lsp/snippet_spec.lua index 13df861b91..ba8bc7fe04 100644 --- a/test/functional/plugin/lsp/snippet_spec.lua +++ b/test/functional/plugin/lsp/snippet_spec.lua @@ -1,5 +1,6 @@ local helpers = require('test.functional.helpers')(after_each) local snippet = require('vim.lsp._snippet_grammar') +local type = snippet.NodeType local eq = helpers.eq local exec_lua = helpers.exec_lua @@ -15,28 +16,28 @@ describe('vim.lsp._snippet_grammar', function() it('parses only text', function() eq({ - { type = snippet.NodeType.Text, data = { text = 'TE$}XT' } }, + { type = type.Text, data = { text = 'TE$}XT' } }, }, parse('TE\\$\\}XT')) end) it('parses tabstops', function() eq({ - { type = snippet.NodeType.Tabstop, data = { tabstop = 1 } }, - { type = snippet.NodeType.Tabstop, data = { tabstop = 2 } }, + { type = type.Tabstop, data = { tabstop = 1 } }, + { type = type.Tabstop, data = { tabstop = 2 } }, }, parse('$1${2}')) end) it('parses nested placeholders', function() eq({ { - type = snippet.NodeType.Placeholder, + type = type.Placeholder, data = { tabstop = 1, value = { - type = snippet.NodeType.Placeholder, + type = type.Placeholder, data = { tabstop = 2, - value = { type = snippet.NodeType.Tabstop, data = { tabstop = 3 } }, + value = { type = type.Tabstop, data = { tabstop = 3 } }, }, }, }, @@ -46,24 +47,24 @@ describe('vim.lsp._snippet_grammar', function() it('parses variables', function() eq({ - { type = snippet.NodeType.Variable, data = { name = 'VAR' } }, - { type = snippet.NodeType.Variable, data = { name = 'VAR' } }, + { type = type.Variable, data = { name = 'VAR' } }, + { type = type.Variable, data = { name = 'VAR' } }, { - type = snippet.NodeType.Variable, + type = type.Variable, data = { name = 'VAR', - default = { type = snippet.NodeType.Tabstop, data = { tabstop = 1 } }, + default = { type = type.Tabstop, data = { tabstop = 1 } }, }, }, { - type = snippet.NodeType.Variable, + type = type.Variable, data = { name = 'VAR', regex = 'regex', options = '', format = { { - type = snippet.NodeType.Format, + type = type.Format, data = { capture = 1, modifier = 'upcase' }, }, }, @@ -75,7 +76,7 @@ describe('vim.lsp._snippet_grammar', function() it('parses choice', function() eq({ { - type = snippet.NodeType.Choice, + type = type.Choice, data = { tabstop = 1, values = { ',', '|' } }, }, }, parse('${1|\\,,\\||}')) @@ -85,30 +86,30 @@ describe('vim.lsp._snippet_grammar', function() eq( { { - type = snippet.NodeType.Variable, + type = type.Variable, data = { name = 'VAR', regex = 'regex', options = '', format = { { - type = snippet.NodeType.Format, + type = type.Format, data = { capture = 1, modifier = 'upcase' }, }, { - type = snippet.NodeType.Format, + type = type.Format, data = { capture = 1, if_text = 'if_text' }, }, { - type = snippet.NodeType.Format, + type = type.Format, data = { capture = 1, else_text = 'else_text' }, }, { - type = snippet.NodeType.Format, + type = type.Format, data = { capture = 1, if_text = 'if_text', else_text = 'else_text' }, }, { - type = snippet.NodeType.Format, + type = type.Format, data = { capture = 1, else_text = 'else_text' }, }, }, @@ -124,24 +125,24 @@ describe('vim.lsp._snippet_grammar', function() it('parses empty strings', function() eq({ { - type = snippet.NodeType.Placeholder, + type = type.Placeholder, data = { tabstop = 1, - value = { type = snippet.NodeType.Text, data = { text = '' } }, + value = { type = type.Text, data = { text = '' } }, }, }, { - type = snippet.NodeType.Text, + type = type.Text, data = { text = ' ' }, }, { - type = snippet.NodeType.Variable, + type = type.Variable, data = { name = 'VAR', regex = 'erg', format = { { - type = snippet.NodeType.Format, + type = type.Format, data = { capture = 1, if_text = '' }, }, }, @@ -150,4 +151,21 @@ describe('vim.lsp._snippet_grammar', function() }, }, 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) -- cgit