Skip to content

Commit

Permalink
feat: add "TranscludeNew"
Browse files Browse the repository at this point in the history
  • Loading branch information
kentookura committed Jan 3, 2024
1 parent d456d34 commit e2bb7b9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
49 changes: 42 additions & 7 deletions lua/forester.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ local function new_tree()
end

local function select(prefixes)
vim.ui.select(prefixes:result(), {}, function(prefix)
vim.ui.select(prefixes, {}, function(prefix)
--vim.ui.select(prefixes:result(), {}, function(prefix)
forester.new(prefix, tree_dir, edit_callback)
end)
end
Expand Down Expand Up @@ -82,12 +83,12 @@ local function open_tree()
forester.complete(tree_dir, select)
end

local function transclude_selection()
local function split_path(path)
-- Returns the Path, Filename, and Extension as 3 values
return string.match(path, "^(.-)([^\\/]-)(%.[^\\/%.]-)%.?$")
end
local function split_path(path)
-- Returns the Path, Filename, and Extension as 3 values
return string.match(path, "^(.-)([^\\/]-)(%.[^\\/%.]-)%.?$")
end

local function transclude_selection()
local function callback(data)
local path = data:result()[1]
local _, addr, _ = split_path(path)
Expand Down Expand Up @@ -115,10 +116,43 @@ local function transclude_selection()
forester.query("prefix", tree_dir, select_prefix())
end

local function transclude_new()
local function put(content)
local pos = vim.api.nvim_win_get_cursor(0)[2]
local line = vim.api.nvim_get_current_line()
local nline = line:sub(0, pos) .. content .. line:sub(pos + 1)
vim.api.nvim_set_current_line(nline)
end

local function callback(data)
local path = data:result()[1]
local _, addr, _ = split_path(path)
local content = "\\transclude{" .. addr .. "}"

put(content)
vim.cmd("edit " .. path)
--vim.api.nvim_feedkeys("Go", "n", false)
--vim.api.nvim_feedkeys(content, "i", false)
end
local function select_prefix()
return function(prefixes)
vim.ui.select(
prefixes,
{ prompt = "select a prefix" },
vim.schedule_wrap(function(prefix)
forester.new(prefix, tree_dir, callback)
end)
)
end
end
forester.query("prefix", tree_dir, select_prefix())
end

vim.api.nvim_create_user_command("ForestNew", new_tree, {})
vim.api.nvim_create_user_command("ForestTemplate", new_from_template, {})
vim.api.nvim_create_user_command("ForestOpen", open_tree, {})
vim.api.nvim_create_user_command("ForestTransclude", transclude_selection, {})
vim.api.nvim_create_user_command("ForestTranscludeNew", transclude_new, {})
vim.api.nvim_create_user_command("ForestTranscludeSelection", transclude_selection, {})

function M.setup(config)
vim.filetype.add({ extension = { tree = "tree" } })
Expand All @@ -133,6 +167,7 @@ function M.setup(config)
keymap("n", "<leader>nn", new_tree, default_opts)
keymap("n", "<leader>nt", new_from_template, default_opts)
keymap("n", "<leader>n.", open_tree, default_opts)
keymap("n", "<leader>nh", transclude_new, default_opts)
keymap("v", "<leader>t", transclude_selection, { noremap = true, silent = true })
end

Expand Down
1 change: 0 additions & 1 deletion plugin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ configs.forester = {
root_dir = lspconfig.util.root_pattern(".git"),
},
}

require("forester").setup()

0 comments on commit e2bb7b9

Please sign in to comment.