From 2ffafc7aa91fb1d9a71fff12051e40961a7b7f69 Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Mon, 9 May 2022 12:00:27 -0600 Subject: feat(lsp): add LspAttach and LspDetach autocommands The current approach of using `on_attach` callbacks for configuring buffers for LSP is suboptimal: 1. It does not use the standard Nvim interface for driving and hooking into events (i.e. autocommands) 2. There is no way for "third parties" (e.g. plugins) to hook into the event. This means that *all* buffer configuration must go into the user-supplied on_attach callback. This also makes it impossible for these configurations to be modular, since it all must happen in the same place. 3. There is currently no way to do something when a client detaches from a buffer (there is no `on_detach` callback). The solution is to use the traditional method of event handling in Nvim: autocommands. When a LSP client is attached to a buffer, fire a `LspAttach`. Likewise, when a client detaches from a buffer fire a `LspDetach` event. This enables plugins to easily add LSP-specific configuration to buffers as well as enabling users to make their own configurations more modular (e.g. by creating multiple LspAttach autocommands that each do something unique). --- src/nvim/auevents.lua | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src') diff --git a/src/nvim/auevents.lua b/src/nvim/auevents.lua index 9e3eb06752..93a870fe04 100644 --- a/src/nvim/auevents.lua +++ b/src/nvim/auevents.lua @@ -70,6 +70,8 @@ return { 'InsertEnter', -- when entering Insert mode 'InsertLeave', -- just after leaving Insert mode 'InsertLeavePre', -- just before leaving Insert mode + 'LspAttach', -- after an LSP client attaches to a buffer + 'LspDetach', -- after an LSP client detaches from a buffer 'MenuPopup', -- just before popup menu is displayed 'ModeChanged', -- after changing the mode 'OptionSet', -- after setting any option @@ -133,6 +135,8 @@ return { nvim_specific = { BufModifiedSet=true, DiagnosticChanged=true, + LspAttach=true, + LspDetach=true, RecordingEnter=true, RecordingLeave=true, Signal=true, -- cgit