mirror of
https://github.com/zoriya/jj.nvim.git
synced 2026-08-15 23:53:18 +00:00
Fixes default command parsing
This commit is contained in:
committed by
Nicolas GB
parent
3d78c28f7e
commit
d483544483
+6
-10
@@ -833,22 +833,18 @@ function M.j(args)
|
||||
local cmd = nil
|
||||
if #args == 0 then
|
||||
local default_cmd_str, success = runner.execute_command(
|
||||
"jj config get ui.default-command",
|
||||
"jj config list ui.default-command",
|
||||
"Error getting user's default command",
|
||||
nil,
|
||||
true
|
||||
)
|
||||
if not success then
|
||||
terminal.run("jj", M.terminal_keymaps())
|
||||
return
|
||||
if success then
|
||||
cmd = parser.parse_default_cmd(default_cmd_str or "")
|
||||
end
|
||||
|
||||
local default_cmd = parser.parse_default_cmd(default_cmd_str and default_cmd_str or "")
|
||||
if default_cmd == nil then
|
||||
terminal.run("jj", M.terminal_keymaps())
|
||||
return
|
||||
-- jj's built-in default command is "log"
|
||||
if cmd == nil then
|
||||
cmd = { "log" }
|
||||
end
|
||||
cmd = default_cmd
|
||||
end
|
||||
|
||||
if type(args) == "string" then
|
||||
|
||||
@@ -9,8 +9,9 @@ function M.parse_default_cmd(cmd_output)
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Remove whitespace and parse TOML output
|
||||
-- Remove whitespace and strip "key = " prefix from `jj config list` output
|
||||
local trimmed_cmd = vim.trim(cmd_output)
|
||||
trimmed_cmd = trimmed_cmd:gsub("^[%w._-]+ = ", "")
|
||||
|
||||
-- Try to parse as TOML array: ["item1", "item2", ...]
|
||||
-- Pattern "%[(.*)%]" captures everything between square brackets
|
||||
|
||||
@@ -27,6 +27,20 @@ local function assert_is_nil(value, msg)
|
||||
end
|
||||
end
|
||||
|
||||
local function assert_table_equals(expected, actual, msg)
|
||||
if type(expected) ~= "table" or type(actual) ~= "table" then
|
||||
error(string.format("%s\nExpected table, got: %s and %s", msg or "Assertion failed", type(expected), type(actual)))
|
||||
end
|
||||
if #expected ~= #actual then
|
||||
error(string.format("%s\nLength mismatch: expected %d, got %d", msg or "Assertion failed", #expected, #actual))
|
||||
end
|
||||
for i, v in ipairs(expected) do
|
||||
if v ~= actual[i] then
|
||||
error(string.format("%s\nAt index %d: expected %s, got %s", msg or "Assertion failed", i, tostring(v), tostring(actual[i])))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function run_test(name, test_fn)
|
||||
local status, err = pcall(test_fn)
|
||||
if status then
|
||||
@@ -344,6 +358,41 @@ run_test("parses divergent change at end of line", function()
|
||||
assert_equals("bcd890/1", parser.get_revset(line))
|
||||
end)
|
||||
|
||||
print("\n=== Running parse_default_cmd tests ===\n")
|
||||
|
||||
run_test("parse_default_cmd: parses config list array output", function()
|
||||
local output = 'ui.default-command = ["log", "--no-pager", "--limit", "18"]'
|
||||
assert_table_equals({ "log", "--no-pager", "--limit", "18" }, parser.parse_default_cmd(output))
|
||||
end)
|
||||
|
||||
run_test("parse_default_cmd: parses config list single string output", function()
|
||||
local output = 'ui.default-command = "log"'
|
||||
assert_table_equals({ "log" }, parser.parse_default_cmd(output))
|
||||
end)
|
||||
|
||||
run_test("parse_default_cmd: parses bare array (config get format)", function()
|
||||
local output = '["log", "--limit", "10"]'
|
||||
assert_table_equals({ "log", "--limit", "10" }, parser.parse_default_cmd(output))
|
||||
end)
|
||||
|
||||
run_test("parse_default_cmd: parses bare string (config get format)", function()
|
||||
local output = '"log"'
|
||||
assert_table_equals({ "log" }, parser.parse_default_cmd(output))
|
||||
end)
|
||||
|
||||
run_test("parse_default_cmd: parses unquoted string", function()
|
||||
local output = "log"
|
||||
assert_table_equals({ "log" }, parser.parse_default_cmd(output))
|
||||
end)
|
||||
|
||||
run_test("parse_default_cmd: returns nil for empty string", function()
|
||||
assert_is_nil(parser.parse_default_cmd(""))
|
||||
end)
|
||||
|
||||
run_test("parse_default_cmd: returns nil for nil", function()
|
||||
assert_is_nil(parser.parse_default_cmd(nil))
|
||||
end)
|
||||
|
||||
-- Print summary
|
||||
print(string.format("\n=== Test Summary ==="))
|
||||
print(string.format("Passed: %d", tests_passed))
|
||||
|
||||
Reference in New Issue
Block a user