From fd9b0ecef4142a62b45404700ba1cff488f84a73 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Thu, 23 May 2024 13:31:11 +0200 Subject: refactor(modules/home): Setup as "normal" NixOS module --- .../home/conf/nvim/plgs/flatten-nvim/default.nix | 20 ++++ .../nvim/plgs/flatten-nvim/lua/flatten-nvim.lua | 106 +++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 modules/home/conf/nvim/plgs/flatten-nvim/default.nix create mode 100644 modules/home/conf/nvim/plgs/flatten-nvim/lua/flatten-nvim.lua (limited to 'modules/home/conf/nvim/plgs/flatten-nvim') diff --git a/modules/home/conf/nvim/plgs/flatten-nvim/default.nix b/modules/home/conf/nvim/plgs/flatten-nvim/default.nix new file mode 100644 index 00000000..cd6f8bcc --- /dev/null +++ b/modules/home/conf/nvim/plgs/flatten-nvim/default.nix @@ -0,0 +1,20 @@ +{ + pkgs, + lib, + ... +}: { + programs.nixvim = { + # TODO: package flatten-nvim though a module + + extraConfigLuaPre = '' + ${lib.strings.fileContents ./lua/flatten-nvim.lua} + if os.getenv("NVIM") ~= nil then + -- Avoid loading plugins because the host will take control of the instance anyways + return + end + ''; + extraPlugins = [ + pkgs.vimPlugins.flatten-nvim + ]; + }; +} diff --git a/modules/home/conf/nvim/plgs/flatten-nvim/lua/flatten-nvim.lua b/modules/home/conf/nvim/plgs/flatten-nvim/lua/flatten-nvim.lua new file mode 100644 index 00000000..42ea1eb6 --- /dev/null +++ b/modules/home/conf/nvim/plgs/flatten-nvim/lua/flatten-nvim.lua @@ -0,0 +1,106 @@ +---Types: +-- +-- Passed to callbacks that handle opening files +---@alias BufInfo { fname: string, bufnr: buffer } +-- +-- Needed aliases +---@alias buffer integer: Buffer id +---@alias window integer: Window id +-- +-- The first argument is a list of BufInfo tables representing the newly opened files. +-- The third argument is a single BufInfo table, only provided when a buffer is created from stdin. +-- +-- IMPORTANT: For `block_for` to work, you need to return a buffer number OR a buffer number and a window number. +-- The `winnr` return value is not required, `vim.fn.bufwinid(bufnr)` is used if it is not provided. +-- The `filetype` of this buffer will determine whether block should happen or not. +-- +---@alias OpenHandler fun(files: BufInfo[], argv: string[], stdin_buf: BufInfo, guest_cwd: string):window, buffer +-- +require("flatten").setup({ + callbacks = { + ---Called to determine if a nested session should wait for the host to close the file. + ---param argv: a list of all the arguments in the nested session + ---@type fun(argv: table): boolean + should_block = require("flatten").default_should_block, + + ---If this returns true, the nested session will be opened. + ---If false, default behavior is used, and + ---config.nest_if_no_args is respected. + ---@type fun(host: channel):boolean + should_nest = require("flatten").default_should_nest, + + ---Called before a nested session is opened. + pre_open = function() end, + + ---Called after a nested session is opened. + ---@param bufnr buffer + ---@param winnr window + ---@param filetype string + ---@param is_blocking boolean + ---@param is_diff boolean + post_open = function(bufnr, winnr, filetype, is_blocking, is_diff) + -- If the file is a git commit, create one-shot autocmd to delete its buffer on write + if filetype == "gitcommit" or filetype == "gitrebase" then + vim.api.nvim_create_autocmd("BufWritePost", { + buffer = bufnr, + once = true, + callback = vim.schedule_wrap(function() + vim.api.nvim_buf_delete(bufnr, {}) + end), + }) + end + end, + + ---Called when a nested session is done waiting for the host. + ---@param filetype string + block_end = function(filetype) end, + }, + -- dictionary of filetypes that should be blocking + block_for = { + gitcommit = true, + }, + -- Command passthrough + allow_cmd_passthrough = true, + -- Allow a nested session to open if Neovim is opened without arguments + nest_if_no_args = false, + -- Window options + window = { + -- Options: + -- current -> open in current window (default) + -- alternate -> open in alternate window (recommended) + -- tab -> open in new tab + -- split -> open in split + -- vsplit -> open in vsplit + -- smart -> smart open (avoids special buffers) + -- OpenHandler -> allows you to handle file opening yourself (see Types) + -- + -- TODO: Open gitcommit filetypes in the current buffer, everything else in a new tab <2023-08-29> + open = "split", + + -- Options: + -- vsplit -> opens files in diff vsplits + -- split -> opens files in diff splits + -- tab_vsplit -> creates a new tabpage, and opens diff vsplits + -- tab_split -> creates a new tabpage, and opens diff splits + -- OpenHandler -> allows you to handle file opening yourself (see Types) + diff = "tab_vsplit", + + -- Affects which file gets focused when opening multiple at once + -- Options: + -- "first" -> open first file of new files (default) + -- "last" -> open last file of new files + focus = "first", + }, + -- Override this function to use a different socket to connect to the host + -- On the host side this can return nil or the socket address. + -- On the guest side this should return the socket address + -- or a non-zero channel id from `sockconnect` + -- flatten.nvim will detect if the address refers to this instance of nvim, to determine if this is a host or a guest + pipe_path = require("flatten").default_pipe_path, + -- The `default_pipe_path` will treat the first nvim instance within a single kitty/wezterm session as the host + -- You can configure this behaviour using the following: + one_per = { + kitty = true, -- Flatten all instance in the current Kitty session + wezterm = true, -- Flatten all instance in the current Wezterm session + }, +}) -- cgit 1.4.1