From 25599c77b21a3572184c74a3c43ec574e849d7f2 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 1 Sep 2024 14:31:09 +0200 Subject: [PATCH] nvim/lua/user/extras.lua: create Format command. --- nvim/lua/user/extras.lua | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/nvim/lua/user/extras.lua b/nvim/lua/user/extras.lua index 9821892..8a55452 100644 --- a/nvim/lua/user/extras.lua +++ b/nvim/lua/user/extras.lua @@ -3,3 +3,20 @@ if user.highlight_yank.enable or false then require('user.utils.misc').highlight_yank(user.highlight_yank) end + +-- Default formatter. will select the whole file and use formatexpr to format the file. +-- this is slow and ugly, but works. +if user.formatter == nil then + user.formatter = [[mfgggqG`f=zz]] +end + +vim.api.nvim_create_user_command('Format', function() + if type(user.formatter) == "string" then + local cmd = vim.api.nvim_replace_termcodes(user.formatter, true, false, true) + vim.api.nvim_feedkeys(cmd, 'n', false) + elseif type(user.formatter) == "function" then + user.formatter() + else + vim.api.nvim_err_writeln("No formatter found") + end +end, {})