Skip to content

Commit dca37ff

Browse files
feat(ts): install parser if not installed in correct location
It the parser is found in incorrect location (like nvim-treesitter), make sure to install the correct version in orgmode folder, and report to user to delete the conflicting one.
1 parent 88c71f3 commit dca37ff

File tree

2 files changed

+94
-23
lines changed

2 files changed

+94
-23
lines changed

lua/orgmode/health.lua

+21-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ function M.check_has_treesitter()
1515
if not version_info.installed then
1616
return h.error('Treesitter grammar is not installed. Run `:Org install_treesitter_grammar` to install it.')
1717
end
18+
19+
if not version_info.install_location then
20+
local list = vim.tbl_map(function(parser)
21+
return ('- `%s`'):format(parser)
22+
end, version_info.conflicting_parsers)
23+
return h.error(
24+
('Installed org parser found in incorrect location. Run `:Org install_treesitter_grammar` to install it in correct location and remove the conflicting parsers from these locations:\n%s'):format(
25+
table.concat(list, '\n')
26+
)
27+
)
28+
end
29+
1830
if version_info.outdated then
1931
return h.error('Treesitter grammar is out of date. Run `:Org install_treesitter_grammar` to update it.')
2032
end
@@ -27,18 +39,18 @@ function M.check_has_treesitter()
2739
)
2840
)
2941
end
30-
local installed_org_parsers = vim.api.nvim_get_runtime_file('parser/org.so', true)
31-
local parser_path = ts.get_parser_path()
32-
local extra_unexpected_parsers = vim.tbl_filter(function(parser)
33-
return parser ~= parser_path
34-
end, installed_org_parsers)
35-
if #extra_unexpected_parsers > 0 then
36-
return h.error(
37-
('There are conflicting org parser(s) installed in these locations: \n%s\nRemove them to avoid conflicts.'):format(
38-
table.concat(extra_unexpected_parsers, '\n')
42+
43+
if #version_info.conflicting_parsers > 0 then
44+
local list = vim.tbl_map(function(parser)
45+
return ('- `%s`'):format(parser)
46+
end, version_info.conflicting_parsers)
47+
return h.warn(
48+
('Conflicting org parser(s) found in these locations:\n%s\nRemove them to avoid conflicts.'):format(
49+
table.concat(list, '\n')
3950
)
4051
)
4152
end
53+
4254
return h.ok(('Treesitter grammar installed (version %s)'):format(version_info.installed_version))
4355
end
4456

lua/orgmode/utils/treesitter/install.lua

+73-14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ function M.install()
1414
return true
1515
end
1616

17+
-- Parser found but in invalid location
18+
if not version_info.install_location then
19+
M.run('install')
20+
M.notify_conflicting_parsers(version_info.conflicting_parsers)
21+
return true
22+
end
23+
1724
if version_info.outdated then
1825
M.run('update')
1926
return true
@@ -24,31 +31,83 @@ function M.install()
2431
return true
2532
end
2633

34+
M.notify_conflicting_parsers(version_info.conflicting_parsers)
35+
2736
return false
2837
end
2938

39+
function M.notify_conflicting_parsers(conflicting_parsers)
40+
if #conflicting_parsers > 0 then
41+
local list = vim.tbl_map(function(parser)
42+
return ('- `%s`'):format(parser)
43+
end, conflicting_parsers)
44+
utils.notify(
45+
('Conflicting org parser(s) found in these locations:\n%s\nRemove them to avoid conflicts.'):format(
46+
table.concat(list, '\n')
47+
),
48+
{
49+
level = 'warn',
50+
timeout = 5000,
51+
}
52+
)
53+
end
54+
end
55+
3056
function M.reinstall()
3157
return M.run('reinstall')
3258
end
3359

3460
function M.get_version_info()
35-
local not_installed = M.not_installed()
36-
if not_installed then
37-
return {
38-
installed = false,
39-
installed_version = nil,
40-
outdated = false,
41-
required_version = required_version,
42-
version_mismatch = false,
43-
}
61+
local result = {
62+
installed = false,
63+
correct_location = false,
64+
install_location = nil,
65+
installed_version = nil,
66+
outdated = false,
67+
required_version = required_version,
68+
version_mismatch = false,
69+
conflicting_parsers = {},
70+
}
71+
72+
if M.not_installed() then
73+
return result
4474
end
75+
76+
result.installed = true
77+
78+
local parser_locations = M.get_parser_locations()
79+
result.conflicting_parsers = parser_locations.conflicting_parsers
80+
81+
if not parser_locations.install_location then
82+
return result
83+
end
84+
85+
result.install_location = parser_locations.install_location
86+
4587
local installed_version = M.get_installed_version()
88+
result.installed_version = installed_version
89+
result.outdated = vim.version.lt(installed_version, required_version)
90+
result.version_mismatch = installed_version ~= required_version
91+
92+
return result
93+
end
94+
95+
function M.get_parser_locations()
96+
local installed_org_parsers = vim.api.nvim_get_runtime_file('parser/org.so', true)
97+
local parser_path = M.get_parser_path()
98+
local install_location = nil
99+
local conflicting_parsers = {}
100+
for _, parser in ipairs(installed_org_parsers) do
101+
if parser == parser_path then
102+
install_location = parser
103+
else
104+
table.insert(conflicting_parsers, parser)
105+
end
106+
end
107+
46108
return {
47-
installed = true,
48-
installed_version = installed_version,
49-
outdated = vim.version.lt(installed_version, required_version),
50-
required_version = required_version,
51-
version_mismatch = installed_version ~= required_version,
109+
install_location = install_location,
110+
conflicting_parsers = conflicting_parsers,
52111
}
53112
end
54113

0 commit comments

Comments
 (0)