robin/laufey

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

Marcus Caiseyfeat(lsp): fallback to textDocument/formatting from vim.lsp.formatexpr #40079d9b4fb1273

master
7.4 KiB275 linesraw
1local n = require('test.functional.testnvim')()
2
3local clear = n.clear
4local exec_lua = n.exec_lua
5local run = n.run
6local stop = n.stop
7local api = n.api
8local NIL = vim.NIL
9
10local M = {}
11
12function M.clear_notrace()
13  -- problem: here be dragons
14  -- solution: don't look too closely for dragons
15  clear {
16    env = {
17      NVIM_LUA_NOTRACK = '1',
18      NVIM_APPNAME = 'nvim_lsp_test',
19      VIMRUNTIME = os.getenv 'VIMRUNTIME',
20    },
21  }
22end
23
24M.create_tcp_echo_server = function()
25  --- Create a TCP server that echos the first message it receives.
26  --- @param host string
27  --- @return integer
28  function _G._create_tcp_server(host)
29    local uv = vim.uv
30    local server = assert(uv.new_tcp())
31    local on_read = require('vim.lsp.rpc').create_read_loop(
32      function(body)
33        vim.rpcnotify(1, 'body', body)
34      end,
35      nil,
36      function(err, code)
37        vim.rpcnotify(1, 'error', err, code)
38      end
39    )
40    server:bind(host, 0)
41    server:listen(127, function(e)
42      assert(not e, e)
43      local socket = assert(uv.new_tcp())
44      server:accept(socket)
45      socket:read_start(function(err, chunk)
46        on_read(err, chunk)
47        socket:shutdown()
48        socket:close()
49        server:shutdown()
50        server:close()
51      end)
52    end)
53    return server:getsockname().port
54  end
55  function _G._send_msg_to_server(msg)
56    local port = _G._create_tcp_server('127.0.0.1')
57    local client = assert(vim.uv.new_tcp())
58    client:connect('127.0.0.1', port, function()
59      client:write(msg, function()
60        client:shutdown()
61        client:close()
62      end)
63    end)
64  end
65end
66
67M.create_server_definition = function()
68  ---@class test.functional.plugin.lsp.testutil._create_server.Opts
69  ---@field capabilities lsp.ServerCapabilities?
70  ---@field handlers table<vim.lsp.protocol.Method.ClientToServer.Request, fun(method: vim.lsp.protocol.Method.ClientToServer.Request, params: table?, callback: fun(err?: lsp.ResponseError, result: any))>?
71
72  ---@class test.functional.plugin.lsp.testutil._create_server.Message
73  ---@field method vim.lsp.protocol.Method.ClientToServer
74  ---@field params table?
75
76  ---@class test.functional.plugin.lsp.testutil._create_server.Server
77  ---@field messages test.functional.plugin.lsp.testutil._create_server.Message[]
78  ---@field cmd fun(dispatchers: vim.lsp.rpc.Dispatchers, config: vim.lsp.ClientConfig): vim.lsp.rpc.Client
79
80  ---@param opts test.functional.plugin.lsp.testutil._create_server.Opts?
81  ---@return test.functional.plugin.lsp.testutil._create_server.Server
82  function _G._create_server(opts)
83    opts = opts or {}
84    local server = {}
85    server.messages = {}
86
87    function server.cmd(dispatchers, _config)
88      local closing = false
89      local handlers = opts.handlers or {}
90      local srv = {}
91
92      function srv.request(method, params, callback)
93        table.insert(server.messages, {
94          method = method,
95          params = params,
96        })
97        local handler = handlers[method]
98        if handler then
99          handler(method, params, callback)
100        elseif method == 'initialize' then
101          callback(nil, {
102            capabilities = opts.capabilities or {},
103          })
104        elseif method == 'shutdown' then
105          callback(nil, nil)
106        end
107        local request_id = #server.messages
108        return true, request_id
109      end
110
111      function srv.notify(method, params)
112        table.insert(server.messages, {
113          method = method,
114          params = params,
115        })
116        if method == 'exit' then
117          dispatchers.on_exit(0, 15)
118        end
119        return true
120      end
121
122      function srv.is_closing()
123        return closing
124      end
125
126      function srv.terminate()
127        closing = true
128        dispatchers.on_exit(0, 15)
129      end
130
131      return srv
132    end
133
134    return server
135  end
136end
137
138-- Fake LSP server.
139M.fake_lsp_code = 'test/functional/fixtures/fake-lsp-server.lua'
140M.fake_lsp_logfile = 'Xtest-fake-lsp.log'
141
142local function fake_lsp_server_setup(test_name, timeout_ms, options, settings)
143  exec_lua(function(fake_lsp_code, fake_lsp_logfile, timeout)
144    options = options or {}
145    settings = settings or {}
146    _G.lsp = require('vim.lsp')
147    _G.TEST_RPC_CLIENT_ID = _G.lsp.start_client {
148      cmd_env = {
149        NVIM_LOG_FILE = fake_lsp_logfile,
150        NVIM_LUA_NOTRACK = '1',
151        NVIM_APPNAME = 'nvim_lsp_test',
152      },
153      cmd = {
154        vim.v.progpath,
155        '-l',
156        fake_lsp_code,
157        test_name,
158        tostring(timeout),
159      },
160      handlers = setmetatable({}, {
161        __index = function(_t, _method)
162          return function(...)
163            return vim.rpcrequest(1, 'handler', ...)
164          end
165        end,
166      }),
167      workspace_folders = {
168        {
169          uri = 'file://' .. vim.uv.cwd(),
170          name = 'test_folder',
171        },
172      },
173      before_init = function(_params, _config)
174        vim.schedule(function()
175          vim.rpcrequest(1, 'setup')
176        end)
177      end,
178      on_init = function(client, result)
179        _G.TEST_RPC_CLIENT = client
180        vim.rpcrequest(1, 'init', result)
181      end,
182      flags = {
183        allow_incremental_sync = options.allow_incremental_sync or false,
184        debounce_text_changes = options.debounce_text_changes or 0,
185      },
186      settings = settings,
187      on_exit = function(...)
188        vim.rpcnotify(1, 'exit', ...)
189      end,
190    }
191  end, M.fake_lsp_code, M.fake_lsp_logfile, timeout_ms or 1e3)
192end
193
194--- @class test.lsp.Config
195--- @field test_name string
196--- @field timeout_ms? integer
197--- @field options? table
198--- @field settings? table
199---
200--- @field on_setup? fun()
201--- @field on_init? fun(client: vim.lsp.Client, ...)
202--- @field on_handler? fun(...)
203--- @field on_exit? fun(code: integer, signal: integer)
204
205--- @param config test.lsp.Config
206function M.test_rpc_server(config)
207  if config.test_name then
208    M.clear_notrace()
209    fake_lsp_server_setup(
210      config.test_name,
211      config.timeout_ms or 1e3,
212      config.options,
213      config.settings
214    )
215  end
216  local client = setmetatable({}, {
217    __index = function(t, name)
218      -- Workaround for not being able to yield() inside __index for Lua 5.1 :(
219      -- Otherwise I would just return the value here.
220      return function(arg1, ...)
221        local ismethod = arg1 == t
222        return exec_lua(function(...)
223          local client = _G.TEST_RPC_CLIENT
224          if type(client[name]) == 'function' then
225            return client[name](ismethod and client or arg1, ...)
226          end
227          return client[name]
228        end, ...)
229      end
230    end,
231  })
232  --- @type integer, integer
233  local code, signal
234  local busy = 0
235  local exited = false
236  local function on_request(method, args)
237    busy = busy + 1
238    if method == 'setup' and config.on_setup then
239      config.on_setup()
240    end
241    if method == 'init' and config.on_init then
242      config.on_init(client, unpack(args))
243    end
244    if method == 'handler' and config.on_handler then
245      config.on_handler(unpack(args))
246    end
247    busy = busy - 1
248    if busy == 0 and exited then
249      stop()
250    end
251    return NIL
252  end
253  local function on_notify(method, args)
254    if method == 'exit' then
255      code, signal = unpack(args)
256      exited = true
257      if busy == 0 then
258        stop()
259      end
260    end
261    return NIL
262  end
263  --  TODO specify timeout?
264  --  run(on_request, on_notify, nil, 1000)
265  run(on_request, on_notify, nil)
266  if config.on_exit then
267    config.on_exit(code, signal)
268  end
269  stop()
270  if config.test_name then
271    api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
272  end
273end
274
275return M