From 339129ebc9503883a3f060d3eff620d67a9eadaf Mon Sep 17 00:00:00 2001 From: Ilia Choly Date: Sun, 19 May 2024 13:03:06 -0400 Subject: [PATCH] refactor(lsp): use supports_method where applicable --- runtime/lua/vim/lsp.lua | 14 +++++++++----- runtime/lua/vim/lsp/client.lua | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua index 6f12bc457b..ad1794e98d 100644 --- a/runtime/lua/vim/lsp.lua +++ b/runtime/lua/vim/lsp.lua @@ -66,6 +66,10 @@ lsp._request_name_to_capability = { [ms.inlayHint_resolve] = { 'inlayHintProvider', 'resolveProvider' }, [ms.textDocument_documentLink] = { 'documentLinkProvider' }, [ms.documentLink_resolve] = { 'documentLinkProvider', 'resolveProvider' }, + [ms.textDocument_didClose] = { 'textDocumentSync', 'openClose' }, + [ms.textDocument_didOpen] = { 'textDocumentSync', 'openClose' }, + [ms.textDocument_willSave] = { 'textDocumentSync', 'willSave' }, + [ms.textDocument_willSaveWaitUntil] = { 'textDocumentSync', 'willSaveWaitUntil' }, } -- TODO improve handling of scratch buffers with LSP attached. @@ -522,10 +526,10 @@ local function buf_attach(bufnr) }, reason = protocol.TextDocumentSaveReason.Manual, ---@type integer } - if vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'willSave') then + if client.supports_method(ms.textDocument_willSave) then client.notify(ms.textDocument_willSave, params) end - if vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'willSaveWaitUntil') then + if client.supports_method(ms.textDocument_willSaveWaitUntil) then local result, err = client.request_sync(ms.textDocument_willSaveWaitUntil, params, 1000, ctx.buf) if result and result.result then @@ -560,7 +564,7 @@ local function buf_attach(bufnr) local params = { textDocument = { uri = uri } } for _, client in ipairs(clients) do changetracking.reset_buf(client, bufnr) - if vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'openClose') then + if client.supports_method(ms.textDocument_didClose) then client.notify(ms.textDocument_didClose, params) end end @@ -573,7 +577,7 @@ local function buf_attach(bufnr) local params = { textDocument = { uri = uri } } for _, client in ipairs(lsp.get_clients({ bufnr = bufnr })) do changetracking.reset_buf(client, bufnr) - if vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'openClose') then + if client.supports_method(ms.textDocument_didClose) then client.notify(ms.textDocument_didClose, params) end end @@ -665,7 +669,7 @@ function lsp.buf_detach_client(bufnr, client_id) changetracking.reset_buf(client, bufnr) - if vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'openClose') then + if client.supports_method(ms.textDocument_didClose) then local uri = vim.uri_from_bufnr(bufnr) local params = { textDocument = { uri = uri } } client.notify(ms.textDocument_didClose, params) diff --git a/runtime/lua/vim/lsp/client.lua b/runtime/lua/vim/lsp/client.lua index 448f986cd3..8fb5879e9b 100644 --- a/runtime/lua/vim/lsp/client.lua +++ b/runtime/lua/vim/lsp/client.lua @@ -905,7 +905,7 @@ end --- @param bufnr integer Number of the buffer, or 0 for current function Client:_text_document_did_open_handler(bufnr) changetracking.init(self, bufnr) - if not vim.tbl_get(self.server_capabilities, 'textDocumentSync', 'openClose') then + if not self.supports_method(ms.textDocument_didOpen) then return end if not api.nvim_buf_is_loaded(bufnr) then