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
27 changes: 21 additions & 6 deletions lua/forester/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local util = require("forester.util")
local Forester = require("forester.bindings")
local Job = require("plenary.job")
local config = require("forester.config")
local pickers = require("forester.pickers")
local M = {}

local select = function(items, callback)
Expand All @@ -26,14 +27,28 @@ local select = function(items, callback)
end
end

local select_prefix = function(callback)
if vim.g.forester_current_config.prefixes == nil then
do
vim.notify(
"Prefixes are not configured in "
.. vim.g.forester_current_config["path"]
.. '. Add them like this: \nprefixes = ["foo"]'
)
end
else
select(vim.g.forester_current_config.prefixes, callback)
end
end

M.commands = {
-- Select the forester configuration file to use
config = function()
local configs = config.all_configs()
if #configs == 0 then
vim.notify("No forester configs available in the current directory!", vim.log.levels.WARN)
else
-- pickers.pick_config(configs)
pickers.pick_config(configs)
vim.api.nvim_exec_autocmds("User", { pattern = "SwitchedForesterConfig" })
end
-- config.switch()
Expand All @@ -45,7 +60,7 @@ M.commands = {
end,

browse = function()
local trees = Forester.query_all(vim.g.forester_current_config)
local trees = Forester.query_all(vim.g.forester_current_config.path)
local t = {}
for k, v in pairs(trees) do
v.addr = k
Expand All @@ -56,19 +71,19 @@ M.commands = {
vim.print("No trees found!")
end
end
-- pickers.pick_by_title(t, {})
pickers.pick_by_title(t, {})
end,

new_random = function()
select(vim.g.forester_current_config.prefixes, function(choice)
select_prefix(function(choice)
local path = config.dir_of_latest_tree_of_prefix(choice)
local new_tree = Forester.new_random(choice, path, vim.g.forester_current_config)[1]
vim.cmd("edit " .. new_tree)
end)
end,

new = function()
select(vim.forester_current_config.prefixes, function(choice)
select_prefix(function(choice)
do
local path = config.dir_of_latest_tree_of_prefix(choice)
local new_tree = Forester.new(choice, path, vim.g.forester_current_config)[1]
Expand All @@ -78,7 +93,7 @@ M.commands = {
end,

transclude_new = function()
select(vim.forester_current_config.prefixes, function(choice)
select(vim.g.forester_current_config.prefixes, function(choice)
do
local path = config.dir_of_latest_tree_of_prefix(choice)
local new_tree = Forester.new(choice, path, vim.g.forester_current_config)[1]
Expand Down
96 changes: 52 additions & 44 deletions lua/forester/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local util = require("forester.util")
local FORESTER_BUILTINS = require("forester.consts").FORESTER_BUILTINS
local map = util.map
local source = {}
local cache
local cache = {}

---Return whether this source is available in the current context or not (optional).
---@return boolean
Expand Down Expand Up @@ -92,52 +92,60 @@ end
function source:complete(params, callback)
local input = string.sub(params.context.cursor_before_line, params.offset - 1)
local text_before_cursor = params.context.cursor_before_line
if vim.startswith(input, "\\") then
callback(default_items)
if vim.g.forester_current_config == nil then
return
else
local items = {}
local prefix_items = map(vim.g.forester_current_config.prefixes, function(pfx)
return {
label = pfx,
documentation = "create a new tree with prefix `" .. pfx .. "`",
data = { isPrefix = true },
}
end)

local prefix_random_items = map(vim.g.forester_current_config.prefixes, function(pfx)
return {
label = pfx,
filterText = pfx .. " " .. "random",
documentation = "create a new tree with prefix `" .. pfx .. "` (randomized id)",
labelDetails = { description = "random" },
data = { isPrefix = true, isRandom = true, closingDelim = source:closing_delim(text_before_cursor) },
}
end)
for _, v in pairs(prefix_items) do
table.insert(items, v)
end
for _, v in pairs(prefix_random_items) do
table.insert(items, v)
end
local function insert_text(addr)
return addr .. source:closing_delim(text_before_cursor)
end
for addr, data in pairs(cache) do
local title
if data.title == vim.NIL then
title = "<untitled>"
else
title = data.title
if vim.startswith(input, "\\") then
callback(default_items)
else
local items = {}
local prefix_items = {}
local prefix_random_items = {}
if vim.g.forester_current_config.prefixes ~= nil then
prefix_items = map(vim.g.forester_current_config.prefixes, function(pfx)
return {
label = pfx,
documentation = "create a new tree with prefix `" .. pfx .. "`",
data = { isPrefix = true },
}
end)

prefix_random_items = map(vim.g.forester_current_config.prefixes, function(pfx)
return {
label = pfx,
filterText = pfx .. " " .. "random",
documentation = "create a new tree with prefix `" .. pfx .. "` (randomized id)",
labelDetails = { description = "random" },
data = { isPrefix = true, isRandom = true, closingDelim = source:closing_delim(text_before_cursor) },
}
end)
end
for _, v in pairs(prefix_items) do
table.insert(items, v)
end
for _, v in pairs(prefix_random_items) do
table.insert(items, v)
end
local function insert_text(addr)
return addr .. source:closing_delim(text_before_cursor)
end
for addr, data in pairs(cache) do
local title
if data.title == vim.NIL then
title = "<untitled>"
else
title = data.title
end
table.insert(items, {
filterText = addr .. " " .. title,
label = addr,
insertText = insert_text(addr),
documentation = title,
data = { isPrefix = false },
})
end
table.insert(items, {
filterText = addr .. " " .. title,
label = addr,
insertText = insert_text(addr),
documentation = title,
data = { isPrefix = false },
})
callback({ items = items, isIncomplete = true })
end
callback({ items = items, isIncomplete = true })
end
end

Expand Down
31 changes: 0 additions & 31 deletions lua/forester/config.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local Scan = require("plenary.scandir")
local Path = require("plenary.path")
local util = require("forester.util")

local M = {}
Expand Down Expand Up @@ -113,36 +112,6 @@ local function set_default_config()
end
end

local tree_dirs = function()
local config = vim.g.forester_current_config
local text = get_file_contents(config)
local parser = vim.treesitter.get_string_parser(text, "toml")

local tree_dir_query = vim.treesitter.query.parse(
"toml",
[[

(document
(table
(pair
(bare_key) @key (#eq? @key "trees")
(array (string) @dir))))
]]
)

local dirs = {}
local root_dir = Path:new(config):parents()[1]
for id, node in tree_dir_query:iter_captures(parser:parse()[1]:root(), text) do
local name = tree_dir_query.captures[id]
if name == "dir" then
local dir = vim.treesitter.get_node_text(node, text)
local str = dir:gsub('^"(.*)"$', "%1")
table.insert(dirs, root_dir .. "/" .. str)
end
end
return dirs
end

-- NOTE: This function computes the `dest_dir` argument of `forester new`
-- Check each configured tree directory for trees matching the prefix,
-- get the highest id in each tree and return the directory containing the
Expand Down