Skip to content

Commit f968bec

Browse files
committed
fix(snippets): make linewise $TM_SELECTED_TEXT not insert extra new line
This seems to be more useful in practice.
1 parent 406ca4a commit f968bec

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

doc/mini-snippets.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,9 @@ Create environment variables and `config.expand.insert` wrapper: >lua
454454

455455
-- Compute custom lookup for variables with dynamic values
456456
local insert_with_lookup = function(snippet)
457-
local lookup = { TM_SELECTED_TEXT = vim.fn.getreg('*') }
457+
local lookup = {
458+
TM_SELECTED_TEXT = table.concat(vim.fn.getreg('a', true, true), '\n'),
459+
}
458460
return MiniSnippets.default_insert(snippet, { lookup = lookup })
459461
end
460462

lua/mini/snippets.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,9 @@
451451
---
452452
--- -- Compute custom lookup for variables with dynamic values
453453
--- local insert_with_lookup = function(snippet)
454-
--- local lookup = { TM_SELECTED_TEXT = vim.fn.getreg('*') }
454+
--- local lookup = {
455+
--- TM_SELECTED_TEXT = table.concat(vim.fn.getreg('a', true, true), '\n'),
456+
--- }
455457
--- return MiniSnippets.default_insert(snippet, { lookup = lookup })
456458
--- end
457459
---
@@ -1940,7 +1942,7 @@ end
19401942
--stylua: ignore
19411943
H.var_evaluators = {
19421944
-- LSP
1943-
TM_SELECTED_TEXT = function() return vim.fn.getreg('"') end,
1945+
TM_SELECTED_TEXT = function() return table.concat(vim.fn.getreg('"', true, true), '\n') end,
19441946
TM_CURRENT_LINE = function() return vim.api.nvim_get_current_line() end,
19451947
TM_CURRENT_WORD = function() return vim.fn.expand('<cword>') end,
19461948
TM_LINE_INDEX = function() return tostring(vim.fn.line('.') - 1) end,

tests/test_snippets.lua

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,12 +1918,12 @@ T['default_insert()']['treats any digit sequence as unique tabstop'] = function(
19181918
end
19191919

19201920
T['default_insert()']['can work with special variables'] = function()
1921-
-- Prepare linewise selected text which ends with "\n" and adds extra line
1922-
set_lines({ 'sel' })
1923-
type_keys('dd')
1921+
-- Prepare linewise selected text which should not end add extra line
1922+
set_lines({ 'sel', 'text' })
1923+
type_keys('dip')
19241924

19251925
default_insert({ body = 'Selected=$TM_SELECTED_TEXT\n$TM_LINE_NUMBER\n$WORKSPACE_FOLDER\n$1' })
1926-
validate_state('i', { 'Selected=sel', '', '1', child.fn.getcwd(), '' }, { 5, 0 })
1926+
validate_state('i', { 'Selected=sel', 'text', '1', child.fn.getcwd(), '' }, { 5, 0 })
19271927
end
19281928

19291929
T['default_insert()']['respects `opts.empty_tabstop` and `opts.empty_tabstop_final`'] = function()
@@ -4355,7 +4355,7 @@ T['Various snippets']['var'] = function()
43554355
validate('$RANDOM ${RANDOM}', { '491985 873024' }, { 1, 13 })
43564356

43574357
child.fn.setreg('"', 'abc\n')
4358-
validate('<tag>\n\t$TM_SELECTED_TEXT\n</tag>', { '<tag>', '\tabc', '\t', '</tag>' }, { 4, 6 })
4358+
validate('<tag>\n\t$TM_SELECTED_TEXT\n</tag>', { '<tag>', '\tabc', '</tag>' }, { 3, 6 })
43594359

43604360
-- Placeholders
43614361
validate('var=$AAA', { 'var=' }, { 1, 4 })
@@ -4398,7 +4398,7 @@ T['Various snippets']['transform'] = function()
43984398
-- Should ignore present transform (for now) in both variables and tabstops
43994399
child.fn.setreg('"', 'abc\n')
44004400
start_session('Upcase=${TM_SELECTED_TEXT/.*/upcase/}')
4401-
validate_state('i', { 'Upcase=abc', '' }, { 2, 0 })
4401+
validate_state('i', { 'Upcase=abc' }, { 1, 10 })
44024402
ensure_clean_state()
44034403

44044404
start_session('Upcase=${1/.*/upcase/};')
@@ -4800,7 +4800,9 @@ T['Examples']['customize variable evaluation'] = function()
48004800
child.lua([[
48014801
vim.loop.os_setenv('USERNAME', 'user')
48024802
local insert_with_lookup = function(snippet)
4803-
local lookup = { TM_SELECTED_TEXT = vim.fn.getreg('a') }
4803+
local lookup = {
4804+
TM_SELECTED_TEXT = table.concat(vim.fn.getreg('a', true, true), '\n'),
4805+
}
48044806
return MiniSnippets.default_insert(snippet, { lookup = lookup })
48054807
end
48064808
@@ -4810,11 +4812,11 @@ T['Examples']['customize variable evaluation'] = function()
48104812
})
48114813
]])
48124814

4813-
child.fn.setreg('a', 'aaa')
4814-
child.fn.setreg('"', 'xxx')
4815+
type_keys('i', 'aa<CR>bb', '<Esc>', '"adip')
4816+
type_keys('i', 'xx', '<Esc>', 'dip')
48154817

48164818
type_keys('i', 't', '<C-j>')
4817-
validate_state('i', { 'user aaa' }, { 1, 8 })
4819+
validate_state('i', { 'user aa', 'bb' }, { 2, 2 })
48184820
end
48194821

48204822
T['Examples']['<Tab>/<S-Tab> mappings'] = function()

0 commit comments

Comments
 (0)