Skip to content
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
3 changes: 3 additions & 0 deletions modules/home/neovim/config/lua/plugins/lualine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ local all_splits_are_terminals = function()
:all(function(win)
local buf = vim.api.nvim_win_get_buf(win)
return vim.bo[buf].buftype == "terminal"
-- Make the jj log count as a terminal so that opening it below a
-- terminal doesn't bring up the statusline.
or vim.bo[buf].filetype == "jjlog"
end)
end

Expand Down
204 changes: 204 additions & 0 deletions modules/home/neovim/config/lua/plugins/nvim-jj.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
local jj = require("nvim_jj")

jj.setup()

-- Cut and copied changes look like the deleted and added lines of hunks.
local link_held = function()
vim.api.nvim_set_hl(0, "JjCut", { link = "HunksDelete" })
vim.api.nvim_set_hl(0, "JjCopy", { link = "HunksAdd" })
end
link_held()
vim.api.nvim_create_autocmd("ColorScheme", { callback = link_held })

--- Sets the options of a window showing a jj log. The backticks of code
--- stay hidden on the cursor's line in normal and insert mode.
---@param win integer
local set_window_options = function(win)
local wo = vim.wo[win][0]
wo.number = false
wo.relativenumber = false
wo.wrap = false
wo.conceallevel = 2
wo.concealcursor = "ni"
vim.api.nvim_win_call(
win,
function() vim.opt_local.listchars:remove("eol") end
)
end

vim.keymap.set("n", "jj", function()
local win = jj.toggle()
if not win then return end
set_window_options(win)
-- The window opened without autocommands, so the statusline hasn't drawn
-- the separator above it yet.
vim.api.nvim_exec_autocmds("WinNew", {
group = "noib3/lualine-statusline",
modeline = false,
})
end, { desc = "Toggle the jj log of the current workspace" })

--- Returns the lines of the visual selection, or the cursor's line outside
--- of visual mode, and leaves visual mode.
---@return integer first
---@return integer last
local selected_lines = function()
local cursor = vim.fn.line(".")
if not vim.fn.mode():match("^[vV\22]") then return cursor, cursor end
local start = vim.fn.line("v")
vim.api.nvim_feedkeys(vim.keycode("<Esc>"), "nx", false)
return start, cursor
end

local group = vim.api.nvim_create_augroup("noib3/nvim-jj", { clear = true })

vim.api.nvim_create_autocmd("BufWinEnter", {
group = group,
pattern = "jj://*",
desc = "Sets the options of the jj log's window",
callback = function() set_window_options(vim.api.nvim_get_current_win()) end,
})

vim.api.nvim_create_autocmd("FileType", {
group = group,
pattern = "jjlog",
desc = "Sets the keymaps of the jj log",
callback = function(ev)
local bufnr = ev.buf
vim.bo[bufnr].textwidth = 0
---@param modes string|string[]
---@param lhs string
---@param rhs fun()
---@param desc string
local map = function(modes, lhs, rhs, desc)
vim.keymap.set(modes, lhs, rhs, { buffer = bufnr, desc = desc })
end

-- Moving down doesn't wait for the `jj` that toggles the log, which would
-- close it while scrolling, so `q` closes it instead.
vim.keymap.set("n", "j", "j", { buffer = bufnr, nowait = true })
map("n", "q", function() jj.toggle() end, "Close the jj log")

-- Children are drawn above their parents, so a change opened above the
-- line is a child, and one opened below it is a parent. With `g`, it
-- takes no children.
for keys, position in pairs({ O = "above", o = "below" }) do
for _, branch in ipairs({ false, true }) do
map(
{ "n", "x" },
(branch and "g" or "") .. keys,
function()
local first, last = selected_lines()
jj.insert_change(bufnr, position, branch, first, last)
end,
("Insert an empty change %s the selected ones%s"):format(
position,
branch and ", on a new branch" or ""
)
)
end
end

-- Whole lines cut or copy their changes, while the operators with other
-- motions edit the subjects.
for operator, hold in pairs({ d = jj.cut, y = jj.copy }) do
local what = operator == "d" and "Cut" or "Copy"
map("n", operator .. operator, function()
local line = vim.fn.line(".")
hold(bufnr, line, line + vim.v.count1 - 1)
end, ("%s the change"):format(what))
map("x", operator, function()
if vim.fn.mode() == "V" then
hold(bufnr, selected_lines())
else
vim.api.nvim_feedkeys(operator, "n", false)
end
end, ("%s the selected changes"):format(what))
end

-- `ad` is a change with its descendants, and `od` only its descendants.
for keys, with_change in pairs({ ad = true, od = false }) do
vim.keymap.set("o", keys, function()
local hold = ({ d = jj.cut_descendants, y = jj.copy_descendants })[vim.v.operator]
local line = vim.fn.line(".")
if hold then
vim.schedule(function() hold(bufnr, line, with_change) end)
end
return "<Esc>"
end, {
buffer = bufnr,
expr = true,
desc = with_change and "The change and its descendants"
or "The change's descendants",
})
end

-- Without cut or copied changes, `p` and `P` paste text into the
-- subjects.
for keys, position in pairs({ p = "below", P = "above" }) do
map("n", keys, function()
if jj.has_held(bufnr) then
jj.paste(bufnr, vim.fn.line("."), position, false)
else
local count = vim.v.count > 0 and tostring(vim.v.count) or ""
local register = '"' .. vim.v.register
vim.api.nvim_feedkeys(register .. count .. keys, "n", false)
end
end, ("Paste the cut or copied changes %s the change"):format(
position
))
map(
"n",
"g" .. keys,
function() jj.paste(bufnr, vim.fn.line("."), position, true) end,
("Paste the cut or copied changes %s the change, on a new branch"):format(
position
)
)
end

map(
"n",
"u",
function() jj.undo(bufnr) end,
"Undo the last graph operation"
)
map(
"n",
"<C-r>",
function() jj.redo(bufnr) end,
"Redo the last undone graph operation"
)
map(
"n",
"<CR>",
function() jj.check_out(bufnr, vim.fn.line(".")) end,
"Check out the change"
)
map(
"n",
"F",
function() jj.fetch(bufnr) end,
"Fetch the remote's bookmarks"
)
map(
"n",
"B",
function() jj.edit_bookmark(bufnr, vim.fn.line(".")) end,
"Create, rename or delete the change's bookmark"
)
for _, branch in ipairs({ false, true }) do
map(
{ "n", "x" },
(branch and "g" or "") .. "U",
function()
local first, last = selected_lines()
jj.push(bufnr, first, last, branch)
end,
("Push the selected changes%s"):format(
branch and " on a new branch" or ""
)
)
end
end,
})
8 changes: 6 additions & 2 deletions modules/home/neovim/config/lua/plugins/trouble.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ end
---@return trouble.Action
local move_to = function(into)
return {
action = function(_, ctx) require("trouble.sources.hunks").move(ctx, into) end,
desc = ("Move the hunk or file to `%s`"):format(into),
action = function(view)
require("trouble.sources.hunks").move(view:selection(), into)
end,
desc = ("Move the selected hunks or files to `%s`"):format(into),
-- Trouble hands the modes to `vim.keymap.set`, which takes a list.
mode = { "n", "x" } --[[@as string]],
}
end

Expand Down
63 changes: 38 additions & 25 deletions modules/home/neovim/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -240,33 +240,32 @@
nvim = self.packages.${pkgs.stdenv.system}.default;
emmylua_check = pkgs.lib.getExe' pkgs.emmylua-check "emmylua_check";

# The configuration's settings, with the `lua` directories of the
# plugins in this repo as the workspace roots, and only the other
# plugins as libraries. Otherwise the plugins in this repo would be
# both, and EmmyLua would see each of their classes twice.
# `plugins/.emmyrc.json`, with the third-party plugins' Lua as the
# library instead of `$NVIM_PLUGINS_LUA`, which also has copies of
# this repo's plugins.
pluginsConfig =
let
config = builtins.fromJSON (builtins.readFile ./config/.emmyrc.json);
config = builtins.fromJSON (builtins.readFile ./plugins/.emmyrc.json);
library = map (
entry:
if entry.path or null == "$NVIM_PLUGINS_LUA" then
entry // { path = "${nvim.thirdPartyLua}"; }
else
entry
) config.workspace.library;
plugins = builtins.attrNames (
pkgs.lib.filterAttrs (_: type: type == "directory") (builtins.readDir ./plugins)
);
missing = builtins.filter (
name: !builtins.elem "./${name}/lua" config.workspace.workspaceRoots
) plugins;
in
assert pkgs.lib.assertMsg (
missing == [ ]
) "plugins/.emmyrc.json's workspaceRoots are missing ${toString missing}";
pkgs.writeText "emmyrc.json" (
builtins.toJSON (
pkgs.lib.recursiveUpdate config {
# The tests' runner defines `test()`.
diagnostics.globals = config.diagnostics.globals ++ [ "test" ];
workspace.library = library;
workspace.workspaceRoots = pkgs.lib.concatMap (
name:
[ "./${name}/lua" ]
++ pkgs.lib.optional (builtins.pathExists ./plugins/${name}/tests/lib) "./${name}/tests/lib"
) (builtins.attrNames (builtins.readDir ./plugins));
}
pkgs.lib.recursiveUpdate config { workspace.library = library; }
)
);
in
Expand Down Expand Up @@ -306,11 +305,14 @@
in
{
# Runs the `tests/*.lua` files of the plugins in this repo, each
# in its own headless Neovim, and exits with an error if any of them
# failed.
# in its own headless Neovim and all at once, then prints their
# results in order and exits with an error if any of them failed.
plugins = pkgs.writeShellApplication {
name = "test-neovim-plugins";
runtimeInputs = [ pkgs.jujutsu ];
runtimeInputs = [
pkgs.git
pkgs.jujutsu
];
text = ''
# The tests' repos and jj's config are kept out of the user's.
tmp=$(mktemp -d)
Expand All @@ -319,14 +321,25 @@
export JJ_USER=test JJ_EMAIL=test@example.com
export LIBVSCODE_DIFF=${nvim.env.LIBVSCODE_DIFF}

failed=0
for test in ${./plugins}/*/tests/*.lua; do
echo "--- ''${test#${./plugins}/}"
if ! TEST_FILE="$test" ${nvim.neovim}/bin/nvim --headless --clean -n -i NONE \
tests=(${./plugins}/*/tests/*.lua)
pids=()
for i in "''${!tests[@]}"; do
# Each file gets its own temporary directory.
mkdir "$tmp/$i"
TEST_FILE="''${tests[$i]}" HOME="$tmp/$i" TMPDIR="$tmp/$i" \
${nvim.neovim}/bin/nvim --headless --clean -n -i NONE \
--cmd "set rtp^=${rtp}" \
-c "luafile ${./tests/run.lua}"; then
failed=1
fi
-c "luafile ${./tests/run.lua}" >"$tmp/$i.out" 2>&1 &
pids+=("$!")
done

failed=0
for i in "''${!tests[@]}"; do
status=0
wait "''${pids[$i]}" || status=$?
echo "--- ''${tests[$i]#${./plugins}/}"
cat "$tmp/$i.out"
if [ "$status" -ne 0 ]; then failed=1; fi
done
exit "$failed"
'';
Expand Down
49 changes: 49 additions & 0 deletions modules/home/neovim/plugins/.emmyrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"$schema": "https://raw.githubusercontent.com/EmmyLuaLs/emmylua-analyzer-rust/0.25.1/crates/emmylua_code_analysis/resources/schema.json",
"diagnostics": {
"disable": [
"preferred-local-alias",
"redefined-local"
],
"globals": [
"test",
"vim"
]
},
"runtime": {
"version": "LuaJIT"
},
"workspace": {
"ignoreDir": [
".git",
".jj"
],
"library": [
"$VIMRUNTIME/lua",
{
"path": "$NVIM_PLUGINS_LUA",
"ignoreGlobs": [
"**/plenary/busted.lua",
"**/editable_term.lua",
"**/nvim_direnv.lua",
"**/nvim_flatten.lua",
"**/nvim_flatten/**",
"**/nvim_hunks.lua",
"**/nvim_hunks/**",
"**/trouble/sources/hunks.lua",
"**/nvim_jj.lua",
"**/nvim_jj/**"
]
}
],
"workspaceRoots": [
"./editable-term.lua/lua",
"./nvim-direnv/lua",
"./nvim-flatten/lua",
"./nvim-hunks/lua",
"./nvim-hunks/tests/lib",
"./nvim-jj/lua",
"./nvim-jj/tests/lib"
]
}
}
Loading
Loading