robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Artem Krinitsynfix(text): indent() ignores `expandtab` when indent unchanged #40932dec3d8215d

master
4.4 KiB172 linesraw
1local t = require('test.testutil')
2
3local describe, it = t.describe, t.it
4local eq = t.eq
5local pcall_err = t.pcall_err
6
7describe('vim.text', function()
8  describe('indent()', function()
9    it('validation', function()
10      t.matches('size%: expected number, got string', t.pcall_err(vim.text.indent, 'x', 'x'))
11      t.matches('size%: expected number, got nil', t.pcall_err(vim.text.indent, nil, 'x'))
12      t.matches('opts%: expected table, got string', t.pcall_err(vim.text.indent, 0, 'x', 'z'))
13    end)
14
15    it('basic cases', function()
16      -- Basic cases.
17      eq({ '', 0 }, { vim.text.indent(0, '') })
18      eq({ '', 0 }, { vim.text.indent(2, '') })
19      eq({ '  a', 4 }, { vim.text.indent(2, '    a') })
20      eq({ '  a\n  b', 4 }, { vim.text.indent(2, '    a\n    b') })
21      eq({ '\t\ta', 1 }, { vim.text.indent(2, '\ta') })
22      eq({ ' a\n\n', 5 }, { vim.text.indent(1, '     a\n\n') })
23      -- Indent 1 (tab) => 0. Starting with empty + blank lines.
24      eq({ '\n\naa a aa', 1 }, { vim.text.indent(0, '\n	\n	aa a aa') })
25      -- Indent 1 (tab) => 2 (tabs). Starting with empty + blank lines, 1-tab indent.
26      eq({ '\n\t\t\n\t\taa a aa', 1 }, { vim.text.indent(2, '\n\t\n\taa a aa') })
27
28      -- Indent 4 => 2, expandtab=false preserves tabs after the common indent.
29      eq(
30        { '  foo\n    bar\n  \tbaz\n', 4 },
31        { vim.text.indent(2, '    foo\n      bar\n    \tbaz\n') }
32      )
33      -- Indent 9 => 3, expandtab=true.
34      eq(
35        { '    foo\n\n   bar \t baz\n', 9 },
36        { vim.text.indent(3, '\t  foo\n\n         bar \t baz\n', { expandtab = 8 }) }
37      )
38      -- Indent 9 => 8, expandtab=true.
39      eq(
40        { '         foo\n\n        bar\n', 9 },
41        { vim.text.indent(8, '\t  foo\n\n         bar\n', { expandtab = 8 }) }
42      )
43      -- Dedent: 5 => 0.
44      eq({ '  foo\n\nbar\n', 5 }, { vim.text.indent(0, '       foo\n\n     bar\n') })
45      -- Dedent: 1 => 0. Empty lines are ignored when deciding "common indent".
46      eq(
47        { ' \n  \nfoo\n\nbar\nbaz\n    \n', 1 },
48        { vim.text.indent(0, '  \n   \n foo\n\n bar\n baz\n     \n') }
49      )
50      -- Tabs expand when old indent matches new indent #40932.
51      eq({ '  a', 2 }, { vim.text.indent(2, '\t a', { expandtab = 1 }) })
52    end)
53
54    it('real-world cases', function()
55      -- Dedent.
56      eq({
57        [[
58bufs:
59nvim args: 3
60lua args: {
61  [0] = "foo.lua"
62}
63]],
64        10,
65      }, {
66        vim.text.indent(
67          0,
68          [[
69          bufs:
70          nvim args: 3
71          lua args: {
72            [0] = "foo.lua"
73          }
74          ]]
75        ),
76      })
77
78      -- Indent 0 => 2.
79      eq({
80        [[
81  # yay
82
83  local function foo()
84    if true then
85      # yay
86    end
87  end
88
89  return
90]],
91        0,
92      }, {
93        vim.text.indent(
94          2,
95          [[
96# yay
97
98local function foo()
99  if true then
100    # yay
101  end
102end
103
104return
105]]
106        ),
107      })
108
109      -- 1-tab indent, last line spaces < tabsize.
110      -- Preserves tab char immediately following the indent.
111      eq({ 'text\n\tmatch\nmatch\ntext\n', 1 }, {
112        vim.text.indent(0, (([[
113	text
114		match
115	match
116	text
117]]):gsub('\n%s-([\n]?)$', '\n%1'))),
118      })
119
120      -- 1-tab indent, last line spaces=tabsize.
121      eq({ 'text\n      match\nmatch\ntext\n', 6 }, {
122        vim.text.indent(
123          0,
124          [[
125	text
126		match
127	match
128	text
129      ]],
130          { expandtab = 6 }
131        ),
132      })
133    end)
134  end)
135
136  describe('hexencode(), hexdecode()', function()
137    it('works', function()
138      local cases = {
139        { 'Hello world!', '48656C6C6F20776F726C6421' },
140        { '😂', 'F09F9882' },
141      }
142
143      for _, v in ipairs(cases) do
144        local input, output = unpack(v)
145        eq(output, vim.text.hexencode(input))
146        eq(input, vim.text.hexdecode(output))
147      end
148    end)
149
150    it('with very large strings', function()
151      local input, output = string.rep('😂', 2 ^ 16), string.rep('F09F9882', 2 ^ 16)
152      eq(output, vim.text.hexencode(input))
153      eq(input, vim.text.hexdecode(output))
154    end)
155
156    it('invalid input', function()
157      -- Odd number of hex characters
158      do
159        local res, err = vim.text.hexdecode('ABC')
160        eq(nil, res)
161        eq('string must have an even number of hex characters', err)
162      end
163
164      -- Non-hexadecimal input
165      do
166        local res, err = vim.text.hexdecode('nothex')
167        eq(nil, res)
168        eq('string must contain only hex characters', err)
169      end
170    end)
171  end)
172end)