Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1/2] feat: add formatter config option to change name of temporary file #332

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading