Skip to content
This repository has been archived by the owner on Aug 12, 2023. It is now read-only.

Can we add delay before formatting "on save" ? #1296

Answered by jose-elias-alvarez
ls-devs asked this question in Q&A
Discussion options

You must be logged in to vote

Not sure this is something we'd want to support, since to my knowledge this isn't supported by any actual LSP servers. If all you want is a delay, you should be able to achieve what you want using timers (untested, but it should get you on the right track):

local delay = 1000
local format_timer = vim.loop.new_timer()

-- use as vim.api.nvim_create_autocmd callback
local on_insert_leave = function()
    format_timer:start(delay, 0, function()
        vim.lsp.buf.format() -- add relevant options
    end)
end

-- you also probably want to stop the timer on insert re-entry to avoid issues
local on_insert_enter = function()
    format_timer:stop()
end

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@ls-devs
Comment options

Answer selected by ls-devs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants