diff options
author | Steven Arcangeli <506791+stevearc@users.noreply.github.com> | 2023-08-31 08:54:11 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-31 08:54:11 -0700 |
commit | 3f34f2de48e393b2ee289f2c8fa613c7eabae6d8 (patch) | |
tree | e4f6e725d345cdbd07cd3f39f69ee7243592378e /scripts | |
parent | 860bd36663b9b02b42a80c6b7f19642add0551ab (diff) |
feat: format() takes an optional callback (#21)
* refactor: replicate lsp.buf.format call
* feat: format() takes an optional callback
* fix: improper logging
* fix: callback returns error if buffer is no longer valid
* fix: provide more detailed error message to callback
* fix: properly detect task interruption
* cleanup: remove unnecessary error code translation
* fix: lsp formatting for Neovim 0.9
* doc: add example of async formatting on save
* fix: async LSP formatter discards changes if buffer was modified
* fix: error code comparison
* fix: use the same LSP client filtering logic everywhere
* fix: add buffer validity guard checks
* fix: add buffer validity guard to LSP formatter
* refactor: change the default log level to WARN
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/autoformat_doc.lua | 35 | ||||
-rwxr-xr-x | scripts/generate.py | 4 | ||||
-rw-r--r-- | scripts/options_doc.lua | 71 |
3 files changed, 108 insertions, 2 deletions
diff --git a/scripts/autoformat_doc.lua b/scripts/autoformat_doc.lua new file mode 100644 index 0000000..1ea8ac4 --- /dev/null +++ b/scripts/autoformat_doc.lua @@ -0,0 +1,35 @@ +-- Format synchronously on save +vim.api.nvim_create_autocmd("BufWritePre", { + pattern = "*", + callback = function(args) + -- Disable autoformat on certain filetypes + local ignore_filetypes = { "sql", "java" } + if vim.tbl_contains(ignore_filetypes, vim.bo[args.buf].filetype) then + return + end + -- Disable with a global or buffer-local variable + if vim.g.disable_autoformat or vim.b[args.buf].disable_autoformat then + return + end + -- Disable autoformat for files in a certain path + local bufname = vim.api.nvim_buf_get_name(args.buf) + if bufname:match("/node_modules/") then + return + end + require("conform").format({ timeout_ms = 500, lsp_fallback = true, buf = args.buf }) + end, +}) + +-- Format asynchronously on save +vim.api.nvim_create_autocmd("BufWritePost", { + pattern = "*", + callback = function(args) + require("conform").format({ async = true, lsp_fallback = true, buf = args.buf }, function(err) + if not err then + vim.api.nvim_buf_call(args.buf, function() + vim.cmd.update() + end) + end + end) + end, +}) diff --git a/scripts/generate.py b/scripts/generate.py index e8b0dad..06fbdb0 100755 --- a/scripts/generate.py +++ b/scripts/generate.py @@ -23,8 +23,8 @@ ROOT = os.path.abspath(os.path.join(HERE, os.path.pardir)) README = os.path.join(ROOT, "README.md") DOC = os.path.join(ROOT, "doc") VIMDOC = os.path.join(DOC, "conform.txt") -OPTIONS = os.path.join(ROOT, "tests", "options_doc.lua") -AUTOFORMAT = os.path.join(ROOT, "tests", "autoformat_doc.lua") +OPTIONS = os.path.join(ROOT, "scripts", "options_doc.lua") +AUTOFORMAT = os.path.join(ROOT, "scripts", "autoformat_doc.lua") @dataclass diff --git a/scripts/options_doc.lua b/scripts/options_doc.lua new file mode 100644 index 0000000..82447fb --- /dev/null +++ b/scripts/options_doc.lua @@ -0,0 +1,71 @@ +require("conform").setup({ + -- Map of filetype to formatters + formatters_by_ft = { + lua = { "stylua" }, + -- Conform will use the first available formatter in the list + javascript = { "prettier_d", "prettier" }, + -- Formatters can also be specified with additional options + python = { + formatters = { "isort", "black" }, + -- Run formatters one after another instead of stopping at the first success + run_all_formatters = true, + -- Don't run these formatters as part of the format_on_save autocmd (see below) + format_on_save = false, + }, + }, + -- If this is set, Conform will run the formatter on save. + -- It will pass the table to conform.format(). + format_on_save = { + -- I recommend these options. See :help conform.format for details. + lsp_fallback = true, + timeout_ms = 500, + }, + -- Set the log level. Use `:ConformInfo` to see the location of the log file. + log_level = vim.log.levels.ERROR, + -- Conform will notify you when a formatter errors + notify_on_error = true, + -- Define custom formatters here + formatters = { + my_formatter = { + -- This can be a string or a function that returns a string + command = "my_cmd", + -- OPTIONAL - all fields below this are optional + -- A list of strings, or a function that returns a list of strings + args = { "--stdin-from-filename", "$FILENAME" }, + -- If the formatter supports range formatting, create the range arguments here + range_args = function(ctx) + return { "--line-start", ctx.range.start[1], "--line-end", ctx.range["end"][1] } + end, + -- Send file contents to stdin, read new contents from stdout (default true) + -- When false, will create a temp file (will appear in "$FILENAME" args). The temp + -- file is assumed to be modified in-place by the format command. + stdin = true, + -- A function the calculates the directory to run the command in + cwd = require("conform.util").root_file({ ".editorconfig", "package.json" }), + -- When cwd is not found, don't run the formatter (default false) + require_cwd = true, + -- When returns false, the formatter will not be used + condition = function(ctx) + return vim.fs.basename(ctx.filename) ~= "README.md" + end, + -- Exit codes that indicate success (default {0}) + exit_codes = { 0, 1 }, + -- Environment variables. This can also be a function that returns a table. + env = { + VAR = "value", + }, + }, + -- These can also be a function that returns the formatter + other_formatter = function() + return { + command = "my_cmd", + } + end, + }, +}) + +-- You can set formatters_by_ft and formatters directly +require("conform").formatters_by_ft.lua = { "stylua" } +require("conform").formatters.my_formatter = { + command = "my_cmd", +} |