From 55b1bbac5a8704852e9d4558726c94b9d1092f61 Mon Sep 17 00:00:00 2001 From: Kento Okura Date: Mon, 21 Oct 2024 16:17:08 +0200 Subject: [PATCH 1/8] I don't know how this change made it here. --- lua/forester/commands.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/forester/commands.lua b/lua/forester/commands.lua index f6140a9..7556a71 100644 --- a/lua/forester/commands.lua +++ b/lua/forester/commands.lua @@ -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) @@ -33,7 +34,7 @@ M.commands = { 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() From e06fbeabb6699602c514311aa25f985768bc79cc Mon Sep 17 00:00:00 2001 From: Kento Okura Date: Mon, 21 Oct 2024 16:20:47 +0200 Subject: [PATCH 2/8] undo bad changes introduced by 13227ca --- lua/forester/commands.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/forester/commands.lua b/lua/forester/commands.lua index 7556a71..e24d5f8 100644 --- a/lua/forester/commands.lua +++ b/lua/forester/commands.lua @@ -57,7 +57,7 @@ M.commands = { vim.print("No trees found!") end end - -- pickers.pick_by_title(t, {}) + pickers.pick_by_title(t, {}) end, new_random = function() From 7e919f515847b882488678183ec3f983e87c99dd Mon Sep 17 00:00:00 2001 From: Kento Okura Date: Sun, 20 Oct 2024 13:55:35 +0200 Subject: [PATCH 3/8] don't attempt to complete if there is no current config --- lua/forester/completion.lua | 90 +++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 43 deletions(-) diff --git a/lua/forester/completion.lua b/lua/forester/completion.lua index 33cc035..af6f1f4 100644 --- a/lua/forester/completion.lua +++ b/lua/forester/completion.lua @@ -92,52 +92,56 @@ 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 = "" - else - title = data.title + if vim.startswith(input, "\\") then + callback(default_items) + 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 = "" + 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 From 350fc976ccb07ba9938eb23c233b53ed90644c28 Mon Sep 17 00:00:00 2001 From: Kento Okura Date: Sun, 20 Oct 2024 17:17:24 +0200 Subject: [PATCH 4/8] Only create prefix completion items if field is present --- lua/forester/completion.lua | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lua/forester/completion.lua b/lua/forester/completion.lua index af6f1f4..b88f1e9 100644 --- a/lua/forester/completion.lua +++ b/lua/forester/completion.lua @@ -99,13 +99,16 @@ function source:complete(params, callback) callback(default_items) 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_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) + end local prefix_random_items = map(vim.g.forester_current_config.prefixes, function(pfx) return { From 7c8f63392644fd6dd297850b5fc95031024ea3cd Mon Sep 17 00:00:00 2001 From: Kento Okura Date: Mon, 21 Oct 2024 13:12:22 +0200 Subject: [PATCH 5/8] check for nil --- lua/forester/completion.lua | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lua/forester/completion.lua b/lua/forester/completion.lua index b88f1e9..44a974d 100644 --- a/lua/forester/completion.lua +++ b/lua/forester/completion.lua @@ -100,6 +100,7 @@ function source:complete(params, callback) 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 { @@ -108,17 +109,17 @@ function source:complete(params, callback) data = { isPrefix = true }, } end) - 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) + 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 From e5dafd86e316f8c4900d397a2523d0acacc620e4 Mon Sep 17 00:00:00 2001 From: Kento Okura Date: Fri, 25 Oct 2024 13:52:50 +0200 Subject: [PATCH 6/8] fix mistaken global variable access --- lua/forester/commands.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/forester/commands.lua b/lua/forester/commands.lua index e24d5f8..b3be0f6 100644 --- a/lua/forester/commands.lua +++ b/lua/forester/commands.lua @@ -69,7 +69,7 @@ M.commands = { end, 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] @@ -79,7 +79,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] From 7db95d332b661909f0d5f037c3947453a6f75402 Mon Sep 17 00:00:00 2001 From: Kento Okura Date: Fri, 25 Oct 2024 16:47:53 +0200 Subject: [PATCH 7/8] remove unused function, warn when there are no prefixes configured --- lua/forester/commands.lua | 18 ++++++++++++++++-- lua/forester/completion.lua | 2 +- lua/forester/config.lua | 31 ------------------------------- 3 files changed, 17 insertions(+), 34 deletions(-) diff --git a/lua/forester/commands.lua b/lua/forester/commands.lua index b3be0f6..b6a2a75 100644 --- a/lua/forester/commands.lua +++ b/lua/forester/commands.lua @@ -27,6 +27,20 @@ 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() @@ -61,7 +75,7 @@ M.commands = { 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) @@ -69,7 +83,7 @@ M.commands = { end, new = function() - select(vim.g.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] diff --git a/lua/forester/completion.lua b/lua/forester/completion.lua index 44a974d..2c1826e 100644 --- a/lua/forester/completion.lua +++ b/lua/forester/completion.lua @@ -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 diff --git a/lua/forester/config.lua b/lua/forester/config.lua index d790178..17e20eb 100644 --- a/lua/forester/config.lua +++ b/lua/forester/config.lua @@ -1,5 +1,4 @@ local Scan = require("plenary.scandir") -local Path = require("plenary.path") local util = require("forester.util") local M = {} @@ -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 From e81d180af6198290ea2f6076b0b537ecfe7a8e7d Mon Sep 17 00:00:00 2001 From: Kento Okura Date: Fri, 25 Oct 2024 16:49:33 +0200 Subject: [PATCH 8/8] pass path to query_all --- lua/forester/commands.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/forester/commands.lua b/lua/forester/commands.lua index b6a2a75..8fab379 100644 --- a/lua/forester/commands.lua +++ b/lua/forester/commands.lua @@ -60,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