user12601679
user12601679

Reputation:

Auto format and indent code based on the file extension with different formaters in nvim

I have a plugin that autosaves the file. This plugin has hooks like .hook_before_saving etc. Here is an example:

local autosave = require("autosave")

autosave.hook_before_saving = function ()
    if <condition> then
        vim.g.auto_save_abort = true -- Save will be aborted
    end
end

I would like to use different formaters like prettier for javascript or cargo fmt for rust every time the file is autosaved.

Is it possible?

Upvotes: 1

Views: 9500

Answers (1)

lcheylus
lcheylus

Reputation: 2423

You could install Formatter.nvim or null-ls.nvim Neovim plugin to use different formatters according to filetype (Rust or JS in your case).

And call command to execute code formatting via plugin in your hook for autosave file.

Upvotes: 1

Related Questions