summaryrefslogtreecommitdiffstatshomepage
path: root/test/functional/plugin/lsp/selection_range_spec.lua
blob: 074b1a299231450651de733bfa487d0c64d990c2 (plain)
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
local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local t_lsp = require('test.functional.plugin.lsp.testutil')
local Screen = require('test.functional.ui.screen')

local dedent = t.dedent
local exec_lua = n.exec_lua
local insert = n.insert

local clear_notrace = t_lsp.clear_notrace
local create_server_definition = t_lsp.create_server_definition

describe('vim.lsp.selection_range', function()
  local text = dedent([[
  hello
  hello
  hello
  hello
  hello]])

  --- @type test.functional.ui.screen
  local screen

  before_each(function()
    clear_notrace()
    screen = Screen.new(50, 9)

    exec_lua(create_server_definition)
    exec_lua(function()
      _G.server = _G._create_server({
        capabilities = {
          selectionRangeProvider = true,
        },
        handlers = {
          ['textDocument/selectionRange'] = function(_, _, callback)
            callback(nil, {
              {
                range = {
                  start = { line = 2, character = 0 },
                  ['end'] = { line = 2, character = 5 },
                },
                parent = {
                  range = {
                    start = { line = 1, character = 0 },
                    ['end'] = { line = 3, character = 5 },
                  },
                  parent = {
                    range = {
                      start = { line = 0, character = 0 },
                      ['end'] = { line = 5, character = 5 },
                    },
                    parent = nil,
                  },
                },
              },
            })
          end,
        },
      })

      return vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
    end)

    insert(text)
  end)

  it('selects ranges', function()
    -- Initial range
    exec_lua(function()
      local win = vim.api.nvim_get_current_win()
      vim.api.nvim_win_set_cursor(win, { 3, 0 })
      vim.lsp.buf.selection_range(1)
    end)

    screen:expect([[
      hello                                             |*2
      {17:hell}^o                                             |
      hello                                             |*2
      {1:~                                                 }|*3
      {5:-- VISUAL --}                                      |
    ]])

    -- Outermost range
    exec_lua(function()
      vim.lsp.buf.selection_range(99)
    end)

    screen:expect([[
      {17:hello}                                             |*4
      {17:hell}^o                                             |
      {1:~                                                 }|*3
      {5:-- VISUAL --}                                      |
    ]])

    -- Back to innermost
    exec_lua(function()
      vim.lsp.buf.selection_range(-99)
    end)

    screen:expect([[
      hello                                             |*2
      {17:hell}^o                                             |
      hello                                             |*2
      {1:~                                                 }|*3
      {5:-- VISUAL --}                                      |
    ]])

    -- Middle range
    exec_lua(function()
      vim.lsp.buf.selection_range(1)
    end)

    screen:expect([[
      hello                                             |
      {17:hello}                                             |*2
      {17:hell}^o                                             |
      hello                                             |
      {1:~                                                 }|*3
      {5:-- VISUAL --}                                      |
    ]])
  end)
end)