Skip to content

Commit c48043b

Browse files
committed
fix(treesitter): update parser setup logic to account for external
installs, i.e. Nix
1 parent f1a55eb commit c48043b

File tree

1 file changed

+78
-54
lines changed

1 file changed

+78
-54
lines changed

lua/kulala/config/parser.lua

Lines changed: 78 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,45 @@ local Logger = require("kulala.logger")
33

44
local M = {}
55

6-
local function get_parser_ver(parser_path)
6+
local parser_name = "kulala_http"
7+
local filetypes = { "http", "rest" }
8+
local parser_exts = { "so", "dylib", "dll" }
9+
local install_error =
10+
"Failed to install kulala_http parser. Please check your nvim-treesitter setup or install by other means."
11+
12+
local parser_path = Fs.get_plugin_path { "..", "tree-sitter" }
13+
14+
local function get_parser_ver()
715
local ts = Fs.read_json(parser_path .. "/tree-sitter.json") or {}
816
return ts.metadata and ts.metadata.version
917
end
1018

11-
local function get_parser_path()
12-
return Fs.get_plugin_path { "..", "tree-sitter" }
19+
local function is_parser_ver_current()
20+
return require("kulala.db").settings.parser_ver == get_parser_ver()
21+
end
22+
23+
local function register_parser()
24+
vim.treesitter.language.register(parser_name, filetypes)
25+
end
26+
27+
local function append_parser_path_to_rtp()
28+
vim.opt.rtp:append(parser_path)
29+
end
30+
31+
local function save_parser_ver()
32+
require("kulala.db").settings:write { parser_ver = get_parser_ver() }
1333
end
1434

15-
local function setup_treesitter_main()
16-
local Db = require("kulala.db")
35+
local function handle_install_result(is_installed)
36+
if is_installed then
37+
save_parser_ver()
38+
else
39+
Logger.error(install_error)
40+
end
41+
end
1742

43+
local function setup_nvim_treesitter_main()
1844
local ts_config = require("nvim-treesitter.config")
19-
local parser_path = get_parser_path()
2045

2146
local install_dir = vim.fs.joinpath(vim.fn.stdpath("data"), "site")
2247
vim.opt.rtp:prepend(install_dir)
@@ -38,34 +63,15 @@ local function setup_treesitter_main()
3863
})
3964

4065
register_parser_config()
41-
vim.opt.rtp:append(parser_path) -- make kulala_http queries available
42-
43-
if
44-
vim.tbl_contains(ts_config.get_installed("parsers"), "kulala_http")
45-
and Db.settings.parser_ver == get_parser_ver(parser_path)
46-
then
47-
return vim.treesitter.language.register("kulala_http", { "http", "rest" })
48-
end
4966

50-
require("nvim-treesitter").install({ "kulala_http" }):wait(10000)
51-
52-
if vim.tbl_contains(ts_config.get_installed("parsers"), "kulala_http") then
53-
Db.settings:write { parser_ver = get_parser_ver(parser_path) }
54-
vim.treesitter.language.register("kulala_http", { "http", "rest" })
55-
else
56-
Logger.error("Failed to install kulala_http parser. Please check your nvim-treesitter setup.")
57-
end
67+
require("nvim-treesitter").install({ parser_name }):wait(10000)
68+
handle_install_result(vim.tbl_contains(ts_config.get_installed("parsers"), parser_name))
5869
end
5970

60-
local function setup_treesitter_master()
61-
local Db = require("kulala.db")
62-
71+
local function setup_nvim_treesitter_master()
6372
local parsers = require("nvim-treesitter.parsers")
64-
local parser_config = parsers.get_parser_configs()
65-
local parser_path = get_parser_path()
66-
67-
vim.opt.rtp:append(parser_path) -- make kulala_http queries available
6873

74+
local parser_config = parsers.get_parser_configs()
6975
parser_config.kulala_http = {
7076
install_info = {
7177
url = parser_path,
@@ -76,21 +82,11 @@ local function setup_treesitter_master()
7682
filetype = "http",
7783
}
7884

79-
if parsers.has_parser("kulala_http") and Db.settings.parser_ver == get_parser_ver(parser_path) then
80-
return vim.treesitter.language.register("kulala_http", { "http", "rest" })
81-
end
82-
83-
require("nvim-treesitter.install").commands.TSInstallSync["run!"]("kulala_http")
84-
85-
if parsers.has_parser("kulala_http") then
86-
Db.settings:write { parser_ver = get_parser_ver(parser_path) }
87-
vim.treesitter.language.register("kulala_http", { "http", "rest" })
88-
else
89-
Logger.error("Failed to install kulala_http parser. Please check your nvim-treesitter setup.")
90-
end
85+
require("nvim-treesitter.install").commands.TSInstallSync["run!"](parser_name)
86+
handle_install_result(parsers.has_parser(parser_name))
9187
end
9288

93-
local function setup_nvim_treesitter()
89+
local function setup_with_nvim_treesitter()
9490
local parsers = vim.F.npcall(require, "nvim-treesitter.parsers")
9591

9692
if not parsers then
@@ -100,35 +96,63 @@ local function setup_nvim_treesitter()
10096
end
10197

10298
if parsers.get_parser_configs then
103-
setup_treesitter_master()
99+
setup_nvim_treesitter_master()
104100
else
105-
setup_treesitter_main()
101+
setup_nvim_treesitter_main()
106102
end
107103
end
108104

105+
local function get_nvim_treesitter_install_dirs()
106+
local dirs = {}
107+
108+
-- main branch install location
109+
table.insert(dirs, vim.fs.joinpath(vim.fn.stdpath("data"), "site", "parser"))
110+
111+
-- master branch install location (uses nvim-treesitter config or package path)
112+
local ts_configs = vim.F.npcall(require, "nvim-treesitter.configs")
113+
if ts_configs and ts_configs.get_parser_install_dir then
114+
local ts_dir = ts_configs.get_parser_install_dir()
115+
if ts_dir then table.insert(dirs, ts_dir) end
116+
end
117+
118+
return dirs
119+
end
120+
121+
local function is_installed_by_nvim_treesitter()
122+
for _, dir in ipairs(get_nvim_treesitter_install_dirs()) do
123+
for _, ext in ipairs(parser_exts) do
124+
local parser_file = vim.fs.joinpath(dir, parser_name .. "." .. ext)
125+
if vim.uv.fs_stat(parser_file) then return true end
126+
end
127+
end
128+
return false
129+
end
130+
109131
local function has_kulala_parser()
110132
local ok, inspected = pcall(function()
111-
return vim.treesitter and vim.treesitter.language and vim.treesitter.language.inspect("kulala_http")
133+
return vim.treesitter and vim.treesitter.language and vim.treesitter.language.inspect(parser_name)
112134
end)
113135

114136
if ok and type(inspected) == "table" and next(inspected) ~= nil then return true end
115137

116-
local exts = { "so", "dylib", "dll" }
117-
for _, ext in ipairs(exts) do
118-
if #vim.api.nvim_get_runtime_file("parser/kulala_http." .. ext, true) > 0 then return true end
138+
for _, ext in ipairs(parser_exts) do
139+
if #vim.api.nvim_get_runtime_file("parser/" .. parser_name .. "." .. ext, true) > 0 then return true end
119140
end
120141

121142
return false
122143
end
123144

124145
M.set_kulala_parser = function()
125-
if has_kulala_parser() then
126-
local parser_path = get_parser_path()
127-
vim.opt.rtp:append(parser_path)
128-
vim.treesitter.language.register("kulala_http", { "http", "rest" })
129-
else
130-
setup_nvim_treesitter()
146+
append_parser_path_to_rtp()
147+
148+
if not has_kulala_parser() then return setup_with_nvim_treesitter() end
149+
150+
if not is_parser_ver_current() then
151+
if is_installed_by_nvim_treesitter() then return setup_with_nvim_treesitter() end
152+
save_parser_ver()
131153
end
154+
155+
register_parser()
132156
end
133157

134158
return M

0 commit comments

Comments
 (0)