From b05962622d3eebeefe6b1a90deb9eb86947e0349 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli <506791+stevearc@users.noreply.github.com> Date: Fri, 15 Mar 2024 08:56:03 -0700 Subject: feat: add formatter config option to change name of temporary file (#332) --- README.md | 2 ++ doc/conform.txt | 2 ++ lua/conform/init.lua | 1 + lua/conform/runner.lua | 7 ++++++- scripts/options_doc.lua | 2 ++ 5 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4afc81a..9dd4b06 100644 --- a/README.md +++ b/README.md @@ -464,6 +464,8 @@ require("conform").setup({ 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 stdin=false, use this template to generate the temporary file that gets formatted + tmpfile_format = ".conform.$RANDOM.$FILENAME", -- When returns false, the formatter will not be used condition = function(ctx) return vim.fs.basename(ctx.filename) ~= "README.md" diff --git a/doc/conform.txt b/doc/conform.txt index b28ac3f..bcf6e31 100644 --- a/doc/conform.txt +++ b/doc/conform.txt @@ -72,6 +72,8 @@ OPTIONS *conform-option 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 stdin=false, use this template to generate the temporary file that gets formatted + tmpfile_format = ".conform.$RANDOM.$FILENAME", -- When returns false, the formatter will not be used condition = function(ctx) return vim.fs.basename(ctx.filename) ~= "README.md" diff --git a/lua/conform/init.lua b/lua/conform/init.lua index e1f3544..2412445 100644 --- a/lua/conform/init.lua +++ b/lua/conform/init.lua @@ -14,6 +14,7 @@ local M = {} ---@field cwd? fun(self: conform.JobFormatterConfig, ctx: conform.Context): nil|string ---@field require_cwd? boolean When cwd is not found, don't run the formatter (default false) ---@field stdin? boolean Send buffer contents to stdin (default true) +---@field tmpfile_format? string When stdin=false, use this format for temporary files (default ".conform.$RANDOM.$FILENAME") ---@field condition? fun(self: conform.JobFormatterConfig, ctx: conform.Context): boolean ---@field exit_codes? integer[] Exit codes that indicate success (default {0}) ---@field env? table|fun(self: conform.JobFormatterConfig, ctx: conform.Context): table diff --git a/lua/conform/runner.lua b/lua/conform/runner.lua index 2810948..ff58570 100644 --- a/lua/conform/runner.lua +++ b/lua/conform/runner.lua @@ -442,8 +442,13 @@ M.build_context = function(bufnr, config, range) end if not config.stdin then + local template = config.tmpfile_format + if not template then + template = ".conform.$RANDOM.$FILENAME" + end local basename = vim.fs.basename(filename) - local tmpname = string.format(".conform.%d.%s", math.random(1000000, 9999999), basename) + local tmpname = + template:gsub("$FILENAME", basename):gsub("$RANDOM", tostring(math.random(1000000, 9999999))) local parent = vim.fs.dirname(filename) filename = fs.join(parent, tmpname) end diff --git a/scripts/options_doc.lua b/scripts/options_doc.lua index 4b1687e..96c0f2b 100644 --- a/scripts/options_doc.lua +++ b/scripts/options_doc.lua @@ -59,6 +59,8 @@ require("conform").setup({ 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 stdin=false, use this template to generate the temporary file that gets formatted + tmpfile_format = ".conform.$RANDOM.$FILENAME", -- When returns false, the formatter will not be used condition = function(ctx) return vim.fs.basename(ctx.filename) ~= "README.md" -- cgit v1.2.3-70-g09d2