Skip to content

Commit

Permalink
refactor: simplify handling of --justfile arg
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Nov 23, 2024
1 parent 8e65a3b commit f02c921
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 38 deletions.
32 changes: 6 additions & 26 deletions lua/justice/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ function M.runRecipe(rec)
local prev = vim.bo.makeprg

vim.bo.makeprg = "just"
local argsStr = rec.justfile and "--justfile=" .. rec.justfile .. " " or ""
argsStr = argsStr .. rec.name
local argsStr = table.concat({ rec.justfileArg or "", rec.name }, " ")
vim.cmd.make(argsStr)

pcall(vim.cmd.cfirst) -- if there is a quickfix item, move to the 1st one
Expand All @@ -23,17 +22,9 @@ function M.runRecipe(rec)
end

-- PRE-RUN NOTIFICATION
-- only snacks.nvim supports replacing notifications
-- only `snacks.nvim` supports replacing notifications
if package.loaded["snacks"] then notify("Running…", nil, { title = rec.name }) end

-- ARGS
local args = {
"just",
rec.justfile and "--justfile=" .. rec.justfile or nil,
rec.name,
}
args = vim.tbl_filter(function(a) return a end, args) -- remove nils

-- 2) STREAMING
if rec.type == "streaming" then
if not package.loaded["snacks"] then
Expand All @@ -48,7 +39,7 @@ function M.runRecipe(rec)
notify(data, severity, { title = rec.name })
end
vim.system(
args,
{ "just", rec.justfileArg, rec.name },
{ stdout = bufferedOut, stderr = bufferedOut },
vim.schedule_wrap(function() vim.cmd.checktime() end)
)
Expand All @@ -57,7 +48,7 @@ function M.runRecipe(rec)

-- 3) DEFAULT
vim.system(
args,
{ "just", rec.justfileArg, rec.name },
{},
vim.schedule_wrap(function(out)
vim.cmd.checktime()
Expand All @@ -71,13 +62,7 @@ end

---@param recipe Justice.Recipe
function M.showRecipe(recipe)
local args = {
"just",
recipe.justfile and "--justfile=" .. recipe.justfile or nil,
"--show",
recipe.name,
}

local args = { "just", recipe.justfileArg, "--show", recipe.name }
local stdout = vim.system(args):wait().stdout or "Error"
notify(stdout, "trace", {
title = recipe.name,
Expand All @@ -89,12 +74,7 @@ end

---@param recipe Justice.Recipe
function M.showVariables(recipe)
local args = {
"just",
recipe.justfile and "--justfile=" .. recipe.justfile or nil,
"--evaluate",
}

local args = { "just", recipe.justfileArg, "--evaluate" }
local stdout = vim.system(args):wait().stdout or "Error"
if vim.trim(stdout) == "" then
notify("No variables defined.", "warn", { title = "Variables" })
Expand Down
5 changes: 3 additions & 2 deletions lua/justice/get-recipes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ function M.get(opts)
if vim.bo.filetype == "just" then vim.cmd("silent! update") end

local config = require("justice.config").config
local justfileArg = opts.justfile and "--justfile=" .. opts.justfile or nil

local args = {
"just",
opts.justfile and "--justfile=" .. opts.justfile or nil,
justfileArg,
"--list",
"--unsorted",
"--list-heading=",
Expand Down Expand Up @@ -51,7 +52,7 @@ function M.get(opts)
comment = comment,
type = type,
displayText = displayText,
justfile = opts.justfile,
justfileArg = justfileArg,
}
end)
:totable()
Expand Down
8 changes: 1 addition & 7 deletions lua/justice/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,10 @@ local M = {}
M.setup = function(userConfig) require("justice.config").setup(userConfig) end

---@param opts? Justice.RunOptions
---@return Justice.RunOptions
local function prepareOpts(opts)
M.select = function(opts)
if not opts then opts = {} end
if opts.justfile then opts.justfile = vim.fs.normalize(opts.justfile) end
return opts
end

---@param opts? Justice.RunOptions
M.select = function(opts)
opts = prepareOpts(opts)
local allRecipes = require("justice.get-recipes").get(opts)
if not allRecipes then return end
require("justice.selection-window").select(allRecipes)
Expand Down
5 changes: 3 additions & 2 deletions lua/justice/selection-window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function M.select(allRecipes)
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, displayLines)
local footer = (" %dx %s "):format(ignoreCount, config.icons.ignore)
local showFooter = ignoreCount > 0 and config.icons.ignore ~= ""
local winnr = vim.api.nvim_open_win(bufnr, true, {
relative = "editor",
row = (vim.o.lines - winHeight) / 2,
Expand All @@ -45,8 +46,8 @@ function M.select(allRecipes)
style = "minimal",
title = title,
title_pos = "center",
footer = ignoreCount > 0 and { { footer, "Comment" } } or nil,
footer_pos = ignoreCount > 0 and "right" or nil,
footer = showFooter and { { footer, "Comment" } } or nil,
footer_pos = showFooter and "right" or nil,
})
vim.wo[winnr].sidescrolloff = 0
vim.wo[winnr].winfixbuf = true
Expand Down
2 changes: 1 addition & 1 deletion lua/justice/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
---@field comment string
---@field displayText string
---@field type? "streaming"|"quickfix"|"ignore"
---@field justfile? string
---@field justfileArg? string justfile prefixed with `--justfile=`

---@class Justice.RunOptions
---@field justfile? string

0 comments on commit f02c921

Please sign in to comment.