aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteven Arcangeli <506791+stevearc@users.noreply.github.com>2024-03-15 08:56:03 -0700
committerGitHub <noreply@github.com>2024-03-15 08:56:03 -0700
commitb05962622d3eebeefe6b1a90deb9eb86947e0349 (patch)
tree1da4f07fcc096c0071e08f3e5dd90e7b2c6394b4
parenta605ce4b2db397c84ae6fa8bcfc85f00b985bc73 (diff)
feat: add formatter config option to change name of temporary file (#332)
-rw-r--r--README.md2
-rw-r--r--doc/conform.txt2
-rw-r--r--lua/conform/init.lua1
-rw-r--r--lua/conform/runner.lua7
-rw-r--r--scripts/options_doc.lua2
5 files changed, 13 insertions, 1 deletions
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<string, any>|fun(self: conform.JobFormatterConfig, ctx: conform.Context): table<string, any>
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"