Skip to content

Commit

Permalink
Allow passing current file name/path as argument to formatter, fixes m…
Browse files Browse the repository at this point in the history
  • Loading branch information
insidewhy committed Mar 17, 2024
1 parent cb4778b commit 1594afe
Show file tree
Hide file tree
Showing 20 changed files with 37 additions and 29 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ return function(parser)
exe = "prettier",
args = {
"--stdin-filepath",
util.escape_path(util.get_current_buffer_file_path()),
"$FILE_PATH",
},
stdin = true,
try_node_modules = true,
Expand All @@ -35,7 +35,7 @@ return function(parser)
exe = "prettier",
args = {
"--stdin-filepath",
util.escape_path(util.get_current_buffer_file_path()),
"$FILE_PATH",
"--parser",
parser,
},
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ require("formatter").setup {
args = {
"--search-parent-directories",
"--stdin-filepath",
util.escape_path(util.get_current_buffer_file_path()),
"$FILE_PATH",
"--",
"-",
},
Expand Down
8 changes: 5 additions & 3 deletions doc/formatter.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ Setup:
-- You can also define your own configuration
function()
-- Supports conditional formatting
-- Note: util.get_current_buffer_file_name() will return the first file edited
-- so this will not work when using :edit etc. to open a new file
if util.get_current_buffer_file_name() == "special.lua" then
return nil
end
Expand All @@ -103,7 +105,7 @@ Setup:
args = {
"--search-parent-directories",
"--stdin-filepath",
util.escape_path(util.get_current_buffer_file_path()),
"$FILE_PATH",
"--",
"-",
},
Expand Down Expand Up @@ -276,7 +278,7 @@ default configuration function takes in a parser argument:
exe = "prettier",
args = {
"--stdin-filepath",
util.escape_path(util.get_current_buffer_file_path()),
"$FILE_PATH",
},
stdin = true,
try_node_modules = true,
Expand All @@ -287,7 +289,7 @@ default configuration function takes in a parser argument:
exe = "prettier",
args = {
"--stdin-filepath",
util.escape_path(util.get_current_buffer_file_path()),
"$FILE_PATH",
"--parser",
parser,
},
Expand Down
4 changes: 2 additions & 2 deletions lua/formatter/defaults/biome.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ return function()
args = {
"format",
"--stdin-file-path",
util.escape_path(util.get_current_buffer_file_path()),
"$FILE_PATH",
},
stdin = true,
}
end
end
2 changes: 1 addition & 1 deletion lua/formatter/defaults/clangformat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ return function()
exe = "clang-format",
args = {
"-assume-filename",
util.escape_path(util.get_current_buffer_file_name()),
"$FILE_PATH",
},
stdin = true,
try_node_modules = true,
Expand Down
2 changes: 1 addition & 1 deletion lua/formatter/defaults/eslint_d.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ return function()
args = {
"--stdin",
"--stdin-filename",
util.escape_path(util.get_current_buffer_file_path()),
"$FILE_PATH",
"--fix-to-stdout",
},
stdin = true,
Expand Down
2 changes: 1 addition & 1 deletion lua/formatter/defaults/ocamlformat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ return function()
exe = "ocamlformat",
args = {
"--enable-outside-detected-project",
util.escape_path(util.get_current_buffer_file_name()),
"$FILE_PATH",
},
stdin = true,
}
Expand Down
4 changes: 2 additions & 2 deletions lua/formatter/defaults/prettier.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ return function(parser)
exe = "prettier",
args = {
"--stdin-filepath",
util.escape_path(util.get_current_buffer_file_path()),
"$FILE_PATH",
},
stdin = true,
try_node_modules = true,
Expand All @@ -17,7 +17,7 @@ return function(parser)
exe = "prettier",
args = {
"--stdin-filepath",
util.escape_path(util.get_current_buffer_file_path()),
"$FILE_PATH",
"--parser",
parser,
},
Expand Down
2 changes: 1 addition & 1 deletion lua/formatter/defaults/prettierd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local util = require "formatter.util"
return function()
return {
exe = "prettierd",
args = { util.escape_path(util.get_current_buffer_file_path()) },
args = { "$FILE_PATH" },
stdin = true,
}
end
4 changes: 2 additions & 2 deletions lua/formatter/defaults/prettiereslint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ return function(parser)
args = {
"--stdin",
"--stdin-filepath",
util.escape_path(util.get_current_buffer_file_path()),
"$FILE_PATH",
},
stdin = true,
try_node_modules = true,
Expand All @@ -19,7 +19,7 @@ return function(parser)
args = {
"--stdin",
"--stdin-filepath",
util.escape_path(util.get_current_buffer_file_path()),
"$FILE_PATH",
"--parser",
parser,
},
Expand Down
2 changes: 1 addition & 1 deletion lua/formatter/filetypes/eruby.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local M = {}
function M.erbformatter()
return {
exe = "erb-formatter",
args = { util.escape_path(util.get_current_buffer_file_path()) },
args = { "$FILE_PATH" },
stdin = true,
}
end
Expand Down
2 changes: 1 addition & 1 deletion lua/formatter/filetypes/java.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function M.google_java_format()
exe = "google-java-format",
args = {
"--aosp",
util.escape_path(util.get_current_buffer_file_path()),
"$FILE_PATH",
"--replace"
},
stdin = true
Expand Down
2 changes: 1 addition & 1 deletion lua/formatter/filetypes/json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ end
function M.fixjson()
return {
exe = "fixjson",
args = { "--stdin-filename", util.get_current_buffer_file_name() },
args = { "--stdin-filename", "$FILE_NAME" },
stdin = true,
try_node_modules = true,
}
Expand Down
4 changes: 2 additions & 2 deletions lua/formatter/filetypes/lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ end
function M.luaformat()
return {
exe = "lua-format",
args = {util.escape_path(util.get_current_buffer_file_path())},
args = {"$FILE_PATH"},
stdin = true
}
end
Expand All @@ -30,7 +30,7 @@ function M.stylua()
args = {
"--search-parent-directories",
"--stdin-filepath",
util.escape_path(util.get_current_buffer_file_path()),
"$FILE_PATH",
"--",
"-",
},
Expand Down
4 changes: 2 additions & 2 deletions lua/formatter/filetypes/python.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function M.isort()
local util = require("formatter.util")
return {
exe = "isort",
args = { "-q", "--filename", util.escape_path(util.get_current_buffer_file_path()), "-" },
args = { "-q", "--filename", "$FILE_PATH", "-" },
stdin = true,
}
end
Expand All @@ -36,7 +36,7 @@ function M.black()
local util = require("formatter.util")
return {
exe = "black",
args = { "-q", "--stdin-filename", util.escape_path(util.get_current_buffer_file_name()), "-" },
args = { "-q", "--stdin-filename", "$FILE_NAME", "-" },
stdin = true,
}
end
Expand Down
4 changes: 2 additions & 2 deletions lua/formatter/filetypes/ruby.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function M.rubocop()
args = {
"--fix-layout",
"--stdin",
util.escape_path(util.get_current_buffer_file_name()),
"$FILE_PATH",
"--format",
"files",
"--stderr",
Expand All @@ -26,7 +26,7 @@ function M.standardrb()
"quiet",
"--stderr",
"--stdin",
util.escape_path(util.get_current_buffer_file_path()),
"$FILE_PATH",
},
stdin = true,
}
Expand Down
2 changes: 1 addition & 1 deletion lua/formatter/filetypes/toml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function M.taplo()
local util = require("formatter.util")
return {
exe = "taplo",
args = { "fmt", "--stdin-filepath", util.escape_path(util.get_current_buffer_file_path()),"-" },
args = { "fmt", "--stdin-filepath", "$FILE_PATH","-" },
stdin = true,
try_node_modules = true,
}
Expand Down
2 changes: 1 addition & 1 deletion lua/formatter/filetypes/xml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function M.xmllint()
exe = "xmllint",
args = {
"--format",
util.escape_path(util.get_current_buffer_file_path()),
"$FILE_PATH",
},
stdin = true,
}
Expand Down
2 changes: 1 addition & 1 deletion lua/formatter/filetypes/zsh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function M.beautysh()
args = {
"-i",
shiftwidth,
util.escape_path(util.get_current_buffer_file_path()),
"$FILE_PATH",
},
}
end
Expand Down
8 changes: 7 additions & 1 deletion lua/formatter/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,13 @@ function M.start_task(configs, start_line, end_line, opts)
local cmd = { current.config.exe }
if current.config.args ~= nil then
for _, arg in ipairs(current.config.args) do
table.insert(cmd, arg)
if arg == "$FILE_NAME" then
table.insert(cmd, util.escape_path(vim.fn.fnamemodify(bufname, ":t")))
elseif arg == "$FILE_PATH" then
table.insert(cmd, util.escape_path(bufname))
else
table.insert(cmd, arg)
end
end
end

Expand Down

0 comments on commit 1594afe

Please sign in to comment.