From 0e7e25af20cf98a19f4f50ee80774fce07cef12e Mon Sep 17 00:00:00 2001 From: Jongwook Choi Date: Thu, 31 Aug 2023 04:14:20 -0400 Subject: refactor(lsp): add type annotation for lsp.Client.server_capabilities (#24925) The class `lsp.Client` has a public member `server_capabilities`, which is assumed to be non-nil once initialized, as documented in `:help vim.lsp.client`. Due to the possibility that it may be nil before initialization, `lsp.Client` was not having a proper lua type annotations on the field `server_capabilities`. Instead of having a nil `server_capabilities` until initialized in the RPC response callback, we can have an initial value of empty table. This CHANGES the behavior of the `server_capabilities` field in a way that it is no longer `nil` until initialization. Note that, as already documented, `server_capabilities` should never be nil when it is once initialized and thus ready to be used in user configs. --- runtime/lua/vim/lsp.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua index 1990c09561..b0caf5af35 100644 --- a/runtime/lua/vim/lsp.lua +++ b/runtime/lua/vim/lsp.lua @@ -397,8 +397,7 @@ do ---@return CTGroup local function get_group(client) local allow_inc_sync = if_nil(client.config.flags.allow_incremental_sync, true) - local change_capability = - vim.tbl_get(client.server_capabilities or {}, 'textDocumentSync', 'change') + local change_capability = vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'change') local sync_kind = change_capability or protocol.TextDocumentSyncKind.None if not allow_inc_sync and change_capability == protocol.TextDocumentSyncKind.Incremental then sync_kind = protocol.TextDocumentSyncKind.Full @@ -1312,6 +1311,9 @@ function lsp.start_client(config) --- - lsp.WorkDoneProgressEnd (extended with title from Begin) progress = vim.ringbuf(50), + --- @type lsp.ServerCapabilities + server_capabilities = {}, + ---@deprecated use client.progress instead messages = { name = name, messages = {}, progress = {}, status = {} }, dynamic_capabilities = require('vim.lsp._dynamic').new(client_id), @@ -1401,7 +1403,7 @@ function lsp.start_client(config) if not required_capability then return true end - if vim.tbl_get(client.server_capabilities or {}, unpack(required_capability)) then + if vim.tbl_get(client.server_capabilities, unpack(required_capability)) then return true else if client.dynamic_capabilities:supports_registration(method) then -- cgit