From 0c52ee248245f40610a4957b6bc9515ce1fd9ab6 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Fri, 29 Sep 2023 12:04:30 -0700 Subject: fix: format_after_save blocks on exit for lsp formatting --- lua/conform/lsp_format.lua | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'lua/conform/lsp_format.lua') diff --git a/lua/conform/lsp_format.lua b/lua/conform/lsp_format.lua index 0315314..17d835f 100644 --- a/lua/conform/lsp_format.lua +++ b/lua/conform/lsp_format.lua @@ -1,4 +1,5 @@ ---This module replaces the default vim.lsp.buf.format() so that we can inject our own logic +local log = require("conform.log") local util = require("vim.lsp.util") local M = {} @@ -75,12 +76,22 @@ function M.format(options, callback) return callback() end local params = set_range(client, util.make_formatting_params(options.formatting_options)) + local auto_id = vim.api.nvim_create_autocmd("LspDetach", { + buffer = bufnr, + callback = function(args) + if args.data.client_id == client.id then + log.warn("LSP %s detached during format request", client.name) + callback("LSP detached") + end + end, + }) client.request(method, params, function(err, result, ctx, _) + vim.api.nvim_del_autocmd(auto_id) if not result then return callback(err or "No result returned from LSP formatter") elseif not vim.api.nvim_buf_is_valid(bufnr) then return callback("buffer was deleted") - elseif vim.b[bufnr].changedtick ~= changedtick then + elseif changedtick ~= util.buf_get_changedtick(bufnr) then return callback( string.format( -- cgit v1.2.3-70-g09d2