Skip to content

Commit

Permalink
feat: add support for environment variables (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Aug 29, 2023
1 parent bf68b4a commit 03a37f1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ require("conform").setup({
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()
Expand Down
4 changes: 4 additions & 0 deletions doc/conform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ OPTIONS *conform-option
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()
Expand Down
1 change: 1 addition & 0 deletions lua/conform/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ local M = {}
---@field stdin? boolean Send buffer contents to stdin (default true)
---@field condition? fun(ctx: conform.Context): boolean
---@field exit_codes? integer[] Exit codes that indicate success (default {0})
---@field env? table<string, any>|fun(ctx: conform.Context): table<string, any>

---@class (exact) conform.FileFormatterConfig : conform.FormatterConfig
---@field meta conform.FormatterMeta
Expand Down
8 changes: 8 additions & 0 deletions lua/conform/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ local function run_formatter(bufnr, formatter, config, ctx, input_lines, callbac
if config.cwd then
cwd = config.cwd(ctx)
end
local env = config.env
if type(env) == "function" then
env = env(ctx)
end
log.info("Run %s on %s", formatter.name, vim.api.nvim_buf_get_name(bufnr))
if not config.stdin then
log.debug("Creating temp file %s", ctx.filename)
Expand All @@ -121,11 +125,15 @@ local function run_formatter(bufnr, formatter, config, ctx, input_lines, callbac
if cwd then
log.debug("Run CWD: %s", cwd)
end
if env then
log.debug("Run ENV: %s", env)
end
local stdout
local stderr
local exit_codes = config.exit_codes or { 0 }
local jid = vim.fn.jobstart(cmd, {
cwd = cwd,
env = env,
stdout_buffered = true,
stderr_buffered = true,
stdin = config.stdin and "pipe" or "null",
Expand Down
4 changes: 4 additions & 0 deletions tests/options_doc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ require("conform").setup({
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()
Expand Down

0 comments on commit 03a37f1

Please sign in to comment.