Skip to content

Commit 4728a26

Browse files
committed
Add new tree_dirs to path when switching config
1 parent d556c31 commit 4728a26

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

lua/forester.lua

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ end
2222

2323
local function setup()
2424
vim.filetype.add({ extension = { tree = "forester" } })
25+
local forester_group = vim.api.nvim_create_augroup("ForesterGroup", { clear = true })
2526

2627
local cfg = config.find_default_config()
2728
if cfg ~= "" then
@@ -45,22 +46,34 @@ local function setup()
4546
})
4647
end
4748

49+
-- Make links followable with `gf`
50+
local add_treedirs_to_path = function()
51+
pcall(function()
52+
local dirs = config.tree_dirs()
53+
for _, v in pairs(dirs) do
54+
vim.opt.path:append(v)
55+
end
56+
end)
57+
end
58+
59+
vim.api.nvim_create_autocmd("User", {
60+
group = forester_group,
61+
pattern = "SwitchedForesterConfig",
62+
callback = function()
63+
add_treedirs_to_path()
64+
end,
65+
})
66+
4867
local cmp = require("cmp")
4968

5069
cmp.register_source("forester", completionSource)
5170
cmp.setup.filetype("forester", { sources = { { name = "forester", dup = 0 } } })
5271

5372
add_treesitter_config()
5473

55-
-- Make links followable with `gf`
56-
57-
local _ = pcall(function()
58-
local dirs = config.tree_dirs()
59-
for _, v in pairs(dirs) do
60-
vim.opt.path:append(v)
61-
end
62-
end)
6374
vim.opt.suffixesadd:prepend(".tree")
75+
76+
add_treedirs_to_path()
6477
ui.setup()
6578
end
6679

lua/forester/commands.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ local util = require("forester.util")
22
local Forester = require("forester.bindings")
33
local Job = require("plenary.job")
44
local pickers = require("forester.pickers")
5-
local Config = require("forester.config")
5+
local config = require("forester.config")
66
local M = {}
77

88
M.commands = {
99
config = function()
10-
Config.switch_config()
10+
config.switch()
1111
end,
1212

1313
build = function()
@@ -31,7 +31,7 @@ M.commands = {
3131
end,
3232

3333
new_random = function()
34-
local prefixes = Config.all_prefixes()
34+
local prefixes = config.all_prefixes()
3535
vim.ui.select(prefixes, { -- TODO: Don't select when #all_prefixes == 1
3636
format_item = function(item)
3737
return item
@@ -43,7 +43,7 @@ M.commands = {
4343
end
4444
else
4545
do
46-
local path = Config.dir_of_latest_tree_of_prefix(choice)
46+
local path = config.dir_of_latest_tree_of_prefix(choice)
4747
local new_tree = Forester.new_random(choice, path, vim.g.forester_current_config)[1]
4848
vim.cmd("edit " .. new_tree)
4949
end
@@ -52,7 +52,7 @@ M.commands = {
5252
end,
5353

5454
new = function()
55-
local prefixes = Config.all_prefixes()
55+
local prefixes = config.all_prefixes()
5656
vim.ui.select(prefixes, { -- TODO: Don't select when #all_prefixes == 1
5757
format_item = function(item)
5858
return item
@@ -64,7 +64,7 @@ M.commands = {
6464
end
6565
else
6666
do
67-
local path = Config.dir_of_latest_tree_of_prefix(choice)
67+
local path = config.dir_of_latest_tree_of_prefix(choice)
6868
local new_tree = Forester.new(choice, path, vim.g.forester_current_config)[1]
6969
vim.cmd("edit " .. new_tree)
7070
end
@@ -73,7 +73,7 @@ M.commands = {
7373
end,
7474

7575
transclude_new = function()
76-
local prefixes = Config.all_prefixes()
76+
local prefixes = config.all_prefixes()
7777
vim.ui.select(prefixes, { -- TODO: Don't select when #all_prefixes == 1
7878
format_item = function(item)
7979
return item
@@ -85,7 +85,7 @@ M.commands = {
8585
end
8686
else
8787
do
88-
local path = Config.dir_of_latest_tree_of_prefix(choice)
88+
local path = config.dir_of_latest_tree_of_prefix(choice)
8989
local new_tree = Forester.new(choice, path, vim.g.forester_current_config)[1]
9090
local addr = util.filename(new_tree):match("(.+)%..+$")
9191
local content = { "\\transclude{" .. addr .. "}" }
@@ -96,7 +96,7 @@ M.commands = {
9696
end,
9797

9898
link_new = function()
99-
local prefixes = Config.all_prefixes()
99+
local prefixes = config.all_prefixes()
100100
vim.ui.select(prefixes, {
101101
format_item = function(item)
102102
return item
@@ -108,7 +108,7 @@ M.commands = {
108108
end
109109
else
110110
do
111-
local path = Config.dir_of_latest_tree_of_prefix(choice)
111+
local path = config.dir_of_latest_tree_of_prefix(choice)
112112
local new_tree = Forester.new(choice, path)[1]
113113
local addr = util.filename(new_tree):match("(.+)%..+$")
114114
local content = { "[](" .. addr .. ")" } -- NOTE: We should improve the workflow with snippets or something similar

lua/forester/config.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,14 @@ local function all_prefixes()
103103
return pfxs
104104
end
105105

106-
local function switch_config()
106+
local function switch()
107107
local configs = all_configs()
108108
pickers.pick_config(configs)
109+
vim.api.nvim_exec_autocmds("User", { pattern = "SwitchedForesterConfig" })
109110
end
110111

111112
M.all_prefixes = all_prefixes
112113
M.tree_dirs = tree_dirs
113-
M.switch_config = switch_config
114+
M.switch_config = switch
114115

115116
return M

0 commit comments

Comments
 (0)