neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

codelens_spec.lua (11454B)


      1 local t = require('test.testutil')
      2 local n = require('test.functional.testnvim')()
      3 local t_lsp = require('test.functional.plugin.lsp.testutil')
      4 local Screen = require('test.functional.ui.screen')
      5 
      6 local dedent = t.dedent
      7 local eq = t.eq
      8 
      9 local api = n.api
     10 local exec_lua = n.exec_lua
     11 local insert = n.insert
     12 local feed = n.feed
     13 
     14 local clear_notrace = t_lsp.clear_notrace
     15 local create_server_definition = t_lsp.create_server_definition
     16 
     17 describe('vim.lsp.codelens', function()
     18  local text = dedent([[
     19    https://github.com/neovim/neovim/issues/16166
     20    struct S {
     21        a: i32,
     22        b: String,
     23    }
     24 
     25    impl S {
     26        fn new(a: i32, b: String) -> Self {
     27            S { a, b }
     28        }
     29    }
     30 
     31    fn main() {
     32        let s = S::new(42, String::from("Hello, world!"));
     33        println!("S.a: {}, S.b: {}", s.a, s.b);
     34    }
     35  ]])
     36 
     37  local grid_with_lenses = dedent([[
     38    ^https://github.com/neovim/neovim/issues/16166        |
     39    {1:1 implementation}                                     |
     40    struct S {                                           |
     41        a: i32,                                          |
     42        b: String,                                       |
     43    }                                                    |
     44                                                         |
     45    impl S {                                             |
     46        fn new(a: i32, b: String) -> Self {              |
     47            S { a, b }                                   |
     48        }                                                |
     49    }                                                    |
     50                                                         |
     51    {1:▶︎ Run }                                               |
     52    fn main() {                                          |
     53        let s = S::new(42, String::from("Hello, world!"))|
     54    ;                                                    |
     55        println!("S.a: {}, S.b: {}", s.a, s.b);          |
     56    }                                                    |
     57                                                         |*2
     58  ]])
     59 
     60  local grid_without_lenses = dedent([[
     61    ^https://github.com/neovim/neovim/issues/16166        |
     62    struct S {                                           |
     63        a: i32,                                          |
     64        b: String,                                       |
     65    }                                                    |
     66                                                         |
     67    impl S {                                             |
     68        fn new(a: i32, b: String) -> Self {              |
     69            S { a, b }                                   |
     70        }                                                |
     71    }                                                    |
     72                                                         |
     73    fn main() {                                          |
     74        let s = S::new(42, String::from("Hello, world!"))|
     75    ;                                                    |
     76        println!("S.a: {}, S.b: {}", s.a, s.b);          |
     77    }                                                    |
     78                                                         |
     79    {1:~                                                    }|*2
     80                                                         |
     81  ]])
     82 
     83  --- @type test.functional.ui.screen
     84  local screen
     85 
     86  --- @type integer
     87  local client_id
     88 
     89  before_each(function()
     90    clear_notrace()
     91    exec_lua(create_server_definition)
     92 
     93    screen = Screen.new(nil, 21)
     94 
     95    client_id = exec_lua(function()
     96      _G.server = _G._create_server({
     97        capabilities = {
     98          codeLensProvider = {
     99            resolveProvider = true,
    100          },
    101        },
    102        handlers = {
    103          ['textDocument/codeLens'] = function(_, _, callback)
    104            callback(nil, {
    105              {
    106                data = {
    107                  kind = {
    108                    impls = {
    109                      position = {
    110                        character = 7,
    111                        line = 1,
    112                      },
    113                    },
    114                  },
    115                  version = 0,
    116                },
    117                range = {
    118                  ['end'] = {
    119                    character = 8,
    120                    line = 1,
    121                  },
    122                  start = {
    123                    character = 7,
    124                    line = 1,
    125                  },
    126                },
    127              },
    128              {
    129                command = {
    130                  arguments = {},
    131                  command = 'rust-analyzer.runSingle',
    132                  title = '▶︎ Run ',
    133                },
    134                range = {
    135                  ['end'] = {
    136                    character = 7,
    137                    line = 12,
    138                  },
    139                  start = {
    140                    character = 3,
    141                    line = 12,
    142                  },
    143                },
    144              },
    145            })
    146          end,
    147          ['codeLens/resolve'] = function(_, _, callback)
    148            vim.schedule(function()
    149              callback(nil, {
    150                command = {
    151                  arguments = {},
    152                  command = 'rust-analyzer.showReferences',
    153                  title = '1 implementation',
    154                },
    155                range = {
    156                  ['end'] = {
    157                    character = 8,
    158                    line = 1,
    159                  },
    160                  start = {
    161                    character = 7,
    162                    line = 1,
    163                  },
    164                },
    165              })
    166            end)
    167          end,
    168        },
    169      })
    170 
    171      return vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
    172    end)
    173 
    174    insert(text)
    175 
    176    exec_lua(function()
    177      vim.lsp.codelens.enable()
    178    end)
    179 
    180    feed('gg')
    181    screen:expect({ grid = grid_with_lenses })
    182  end)
    183 
    184  it('clears code lenses when disabled', function()
    185    exec_lua(function()
    186      vim.lsp.codelens.enable(false)
    187    end)
    188 
    189    screen:expect({ grid = grid_without_lenses })
    190  end)
    191 
    192  it('clears code lenses when sole client detaches', function()
    193    exec_lua(function()
    194      vim.lsp.get_client_by_id(client_id):stop()
    195    end)
    196 
    197    screen:expect({ grid = grid_without_lenses })
    198  end)
    199 
    200  it('get code lenses in the current buffer', function()
    201    local result = exec_lua(function()
    202      vim.api.nvim_win_set_cursor(0, { 12, 3 })
    203      return vim.lsp.codelens.get()
    204    end)
    205 
    206    eq({
    207      {
    208        client_id = 1,
    209        lens = {
    210          command = {
    211            arguments = {},
    212            command = 'rust-analyzer.showReferences',
    213            title = '1 implementation',
    214          },
    215          range = {
    216            ['end'] = {
    217              character = 8,
    218              line = 1,
    219            },
    220            start = {
    221              character = 7,
    222              line = 1,
    223            },
    224          },
    225        },
    226      },
    227      {
    228        client_id = 1,
    229        lens = {
    230          command = {
    231            arguments = {},
    232            command = 'rust-analyzer.runSingle',
    233            title = '▶︎ Run ',
    234          },
    235          range = {
    236            ['end'] = {
    237              character = 7,
    238              line = 12,
    239            },
    240            start = {
    241              character = 3,
    242              line = 12,
    243            },
    244          },
    245        },
    246      },
    247    }, result)
    248  end)
    249 
    250  it('refreshes code lenses on request', function()
    251    feed('2Gdd')
    252 
    253    screen:expect([[
    254      https://github.com/neovim/neovim/issues/16166        |
    255      {1:1 implementation}                                     |
    256          ^a: i32,                                          |
    257          b: String,                                       |
    258      }                                                    |
    259                                                           |
    260      impl S {                                             |
    261          fn new(a: i32, b: String) -> Self {              |
    262              S { a, b }                                   |
    263          }                                                |
    264      }                                                    |
    265                                                           |
    266      {1:▶︎ Run }                                               |
    267      fn main() {                                          |
    268          let s = S::new(42, String::from("Hello, world!"))|
    269      ;                                                    |
    270          println!("S.a: {}, S.b: {}", s.a, s.b);          |
    271      }                                                    |
    272                                                           |
    273      {1:~                                                    }|*1
    274                                                           |
    275    ]])
    276    exec_lua(function()
    277      vim.lsp.codelens.on_refresh(
    278        nil,
    279        nil,
    280        { method = 'workspace/codeLens/refresh', client_id = client_id }
    281      )
    282    end)
    283    screen:expect([[
    284      https://github.com/neovim/neovim/issues/16166        |
    285      {1:    1 implementation}                                 |
    286          ^a: i32,                                          |
    287          b: String,                                       |
    288      }                                                    |
    289                                                           |
    290      impl S {                                             |
    291          fn new(a: i32, b: String) -> Self {              |
    292              S { a, b }                                   |
    293          }                                                |
    294      }                                                    |
    295                                                           |
    296      fn main() {                                          |
    297      {1:    ▶︎ Run }                                           |
    298          let s = S::new(42, String::from("Hello, world!"))|
    299      ;                                                    |
    300          println!("S.a: {}, S.b: {}", s.a, s.b);          |
    301      }                                                    |
    302                                                           |
    303      {1:~                                                    }|*1
    304                                                           |
    305    ]])
    306  end)
    307 
    308  it('clears extmarks beyond the bottom of the buffer', function()
    309    feed('13G4dd')
    310    screen:expect([[
    311      https://github.com/neovim/neovim/issues/16166        |
    312      {1:1 implementation}                                     |
    313      struct S {                                           |
    314          a: i32,                                          |
    315          b: String,                                       |
    316      }                                                    |
    317                                                           |
    318      impl S {                                             |
    319          fn new(a: i32, b: String) -> Self {              |
    320              S { a, b }                                   |
    321          }                                                |
    322      }                                                    |
    323                                                           |
    324      ^                                                     |
    325      {1:~                                                    }|*6
    326      4 fewer lines                                        |
    327    ]])
    328  end)
    329 
    330  after_each(function()
    331    api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
    332  end)
    333 end)