Skip to content

Commit

Permalink
feat: add formatter config option to change name of temporary file (#332
Browse files Browse the repository at this point in the history
)
  • Loading branch information
stevearc committed Mar 15, 2024
1 parent a605ce4 commit b059626
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions doc/conform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions lua/conform/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
7 changes: 6 additions & 1 deletion lua/conform/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions scripts/options_doc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit b059626

Please sign in to comment.