aboutsummaryrefslogtreecommitdiff
path: root/test/functional/ui/cmdline_spec.lua
blob: 8d694052ee819231dcac44a439d547c982b668f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
local helpers = require('test.functional.helpers')(after_each)
local Screen = require('test.functional.ui.screen')
local clear, feed, eq = helpers.clear, helpers.feed, helpers.eq
local source = helpers.source

if helpers.pending_win32(pending) then return end

describe('External command line completion', function()
  local screen
  local shown = false
  local firstc, prompt, content, pos, char, shift, indent, level, current_hide_level, in_function

  before_each(function()
    clear()
    screen = Screen.new(25, 5)
    screen:attach({rgb=true, ext_cmdline=true})
    screen:set_on_event_handler(function(name, data)
      if name == "cmdline_hide" then
        shown = false
        current_hide_level = data[1]
      elseif name == "cmdline_show" then
        shown = true
        content, pos, firstc, prompt, indent, level = unpack(data)
      elseif name == "cmdline_char" then
        char, shift = unpack(data)
      elseif name == "cmdline_pos" then
        pos = data[1]
      elseif name == "cmdline_function_show" then
        in_function = true
      elseif name == "cmdline_function_hide" then
        in_function = false
      end
    end)
  end)

  after_each(function()
    screen:detach()
  end)

  function expect_cmdline(expected)
    local attr_ids = screen._default_attr_ids
    local attr_ignore = screen._default_attr_ignore
    local actual = ''
    for _, chunk in ipairs(content or {}) do
      local attrs, text = chunk[1], chunk[2]
      if screen:_equal_attrs(attrs, {}) then
        actual = actual..text
      else
        local attr_id = screen:_get_attr_id(attr_ids, attr_ignore, attrs)
        actual =  actual..'{' .. attr_id .. ':' .. text .. '}'
      end
    end
    eq(expected, actual)
  end

  describe("'cmdline'", function()
    it(':sign', function()
      feed(':')
      screen:expect([[
        ^                         |
        ~                        |
        ~                        |
        ~                        |
                                 |
      ]], nil, nil, function()
        eq(true, shown)
        eq(':', firstc)
      end)

      feed('sign')
      screen:expect([[
        ^                         |
        ~                        |
        ~                        |
        ~                        |
                                 |
      ]], nil, nil, function()
        eq({{{}, 'sign'}}, content)
        eq(4, pos)
      end)

      feed('<Left>')
      screen:expect([[
        ^                         |
        ~                        |
        ~                        |
        ~                        |
                                 |
      ]], nil, nil, function()
        eq({{{}, 'sign'}}, content)
        eq(true, shown)
        eq(3, pos)
      end)

      feed('<bs>')
      screen:expect([[
        ^                         |
        ~                        |
        ~                        |
        ~                        |
                                 |
      ]], nil, nil, function()
        eq({{{}, 'sin'}}, content)
        eq(true, shown)
        eq(2, pos)
      end)

      feed('<Esc>')
      screen:expect([[
        ^                         |
        ~                        |
        ~                        |
        ~                        |
                                 |
      ]], nil, nil, function()
        eq(false, shown)
      end)

      feed(':call input("input", "default")<cr>')
      screen:expect([[
        ^                         |
        ~                        |
        ~                        |
        ~                        |
                                 |
      ]], nil, nil, function()
        eq(true, shown)
        eq("input", prompt)
        eq({{{}, 'default'}}, content)
      end)
      feed('<cr>')

      feed(':')
      screen:expect([[
        ^                         |
        ~                        |
        ~                        |
        ~                        |
                                 |
      ]], nil, nil, function()
        eq(1, level)
      end)

      feed('<C-R>=1+2')
      screen:expect([[
        ^                         |
        ~                        |
        ~                        |
        ~                        |
                                 |
      ]], nil, nil, function()
        eq({{{}, '1+2'}}, content)
        eq("\"", char)
        eq(1, shift)
        eq(2, level)
      end)

      feed('<cr>')
      screen:expect([[
        ^                         |
        ~                        |
        ~                        |
        ~                        |
                                 |
      ]], nil, nil, function()
        eq({{{}, '3'}}, content)
        eq(2, current_hide_level)
        eq(1, level)
      end)

      feed('<esc>')
      screen:expect([[
        ^                         |
        ~                        |
        ~                        |
        ~                        |
                                 |
      ]], nil, nil, function()
        eq(1, current_hide_level)
      end)

      feed(':function Foo()<cr>')
      screen:expect([[
        ^                         |
        ~                        |
        ~                        |
        ~                        |
                                 |
      ]], nil, nil, function()
        eq(true, in_function)
        eq(2, indent)
      end)

      feed('line1<cr>')
      screen:expect([[
        ^                         |
        ~                        |
        ~                        |
        ~                        |
                                 |
      ]], nil, nil, function()
        eq(true, in_function)
        eq(2, indent)
      end)

      feed('endfunction<cr>')
      screen:expect([[
        ^                         |
        ~                        |
        ~                        |
        ~                        |
                                 |
      ]], nil, nil, function()
        eq(false, in_function)
      end)

      feed(':sign<c-f>')
      screen:expect([[
                                 |
        [No Name]                |
        :sign^                    |
        [Command Line]           |
                                 |
      ]], nil, nil, function()
        eq(false, in_function)
      end)

    end)
  end)

  it('works with highlighted cmdline', function()
      source([[
        highlight RBP1 guibg=Red
        highlight RBP2 guibg=Yellow
        highlight RBP3 guibg=Green
        highlight RBP4 guibg=Blue
        let g:NUM_LVLS = 4
        function RainBowParens(cmdline)
          let ret = []
          let i = 0
          let lvl = 0
          while i < len(a:cmdline)
            if a:cmdline[i] is# '('
              call add(ret, [i, i + 1, 'RBP' . ((lvl % g:NUM_LVLS) + 1)])
              let lvl += 1
            elseif a:cmdline[i] is# ')'
              let lvl -= 1
              call add(ret, [i, i + 1, 'RBP' . ((lvl % g:NUM_LVLS) + 1)])
            endif
            let i += 1
          endwhile
          return ret
        endfunction
        map <f5>  :let x = input({'prompt':'>','highlight':'RainBowParens'})<cr>
        "map <f5>  :let x = input({'prompt':'>'})<cr>
      ]])
      screen:set_default_attr_ids({
        RBP1={background = Screen.colors.Red},
        RBP2={background = Screen.colors.Yellow},
        RBP3={background = Screen.colors.Green},
        RBP4={background = Screen.colors.Blue},
        EOB={bold = true, foreground = Screen.colors.Blue1},
        ERR={foreground = Screen.colors.Grey100, background = Screen.colors.Red},
        SK={foreground = Screen.colors.Blue},
        PE={bold = true, foreground = Screen.colors.SeaGreen4}
      })
      feed('<f5>(a(b)a)')
      screen:expect([[
        ^                         |
        {EOB:~                        }|
        {EOB:~                        }|
        {EOB:~                        }|
                                 |
      ]], nil, nil, function()
        expect_cmdline('{RBP1:(}a{RBP2:(}b{RBP2:)}a{RBP1:)}')
      end)
  end)
end)