Skip to content

Commit ccd7319

Browse files
chore(checkhealth): check for version mismatch
1 parent c39d562 commit ccd7319

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

lua/orgmode/health.lua

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,23 @@ function M.check()
1010
end
1111

1212
function M.check_has_treesitter()
13-
local ts = require('orgmode.utils.treesitter.install')
14-
if ts.not_installed() then
13+
local version_info = require('orgmode.utils.treesitter.install').get_version_info()
14+
if not version_info.installed then
1515
return h.error('Treesitter grammar is not installed. Run `:Org install_treesitter_grammar` to install it.')
1616
end
17-
if ts.outdated() then
17+
if version_info.outdated then
1818
return h.error('Treesitter grammar is out of date. Run `:Org install_treesitter_grammar` to update it.')
1919
end
20-
return h.ok('Treesitter grammar installed')
20+
21+
if version_info.version_mismatch then
22+
return h.warn(
23+
('Treesitter grammar version mismatch (installed %s, required %s). Run `:Org install_treesitter_grammar` to update it.'):format(
24+
version_info.installed_version,
25+
version_info.required_version
26+
)
27+
)
28+
end
29+
return h.ok(('Treesitter grammar installed (version %s)'):format(version_info.installed_version))
2130
end
2231

2332
function M.check_setup()

lua/orgmode/utils/init.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,6 @@ end
628628
function utils.notify(msg, opts)
629629
opts = vim.tbl_extend('force', {
630630
title = 'Orgmode',
631-
id = 'orgmode',
632631
level = 'info',
633632
}, opts or {})
634633

lua/orgmode/utils/treesitter/install.lua

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,48 @@ local M = {
88
local required_version = '2.0.0'
99

1010
function M.install()
11-
if M.not_installed() then
11+
local version_info = M.get_version_info()
12+
if not version_info.installed then
1213
M.run('install')
1314
return true
1415
end
1516

16-
if M.outdated() then
17+
if version_info.outdated then
1718
M.run('update')
1819
return true
1920
end
2021

22+
if version_info.version_mismatch then
23+
M.reinstall()
24+
return true
25+
end
26+
2127
return false
2228
end
2329

2430
function M.reinstall()
2531
return M.run('reinstall')
2632
end
2733

28-
function M.outdated()
34+
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+
}
44+
end
2945
local installed_version = M.get_installed_version()
30-
return vim.version.lt(installed_version, required_version)
46+
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,
52+
}
3153
end
3254

3355
function M.not_installed()
@@ -119,11 +141,11 @@ function M.get_path(url, type)
119141
local is_local_path = vim.fn.isdirectory(local_path) == 1
120142

121143
if is_local_path then
122-
utils.notify('Using local version of tree-sitter grammar...')
144+
utils.notify('Using local version of tree-sitter grammar...', { id = 'orgmode-treesitter-install' })
123145
return Promise.resolve(local_path)
124146
end
125147

126-
local path = ('%s/tree-sitter-org'):format(vim.fn.stdpath('cache'))
148+
local path = vim.fs.joinpath(vim.fn.stdpath('cache'), 'tree-sitter-org')
127149
vim.fn.delete(path, 'rf')
128150

129151
local msg = {
@@ -132,7 +154,7 @@ function M.get_path(url, type)
132154
reinstall = 'Reinstalling',
133155
}
134156

135-
utils.notify(('%s tree-sitter grammar...'):format(msg[type]))
157+
utils.notify(('%s tree-sitter grammar...'):format(msg[type]), { id = 'orgmode-treesitter-install' })
136158
return M.exe('git', {
137159
args = { 'clone', '--filter=blob:none', '--depth=1', '--branch=' .. required_version, url, path },
138160
}):next(function(code)
@@ -177,12 +199,16 @@ function M.run(type)
177199
error('[orgmode] Failed to move generated tree-sitter parser to runtime folder', 0)
178200
end
179201
utils.writefile(M.get_lock_file(), vim.json.encode({ version = required_version })):wait()
180-
local msg = { 'Done!' }
202+
local msg = {
203+
'Tree-sitter grammar installed!',
204+
('Version: %s'):format(required_version),
205+
}
181206
if type == 'update' then
182207
table.insert(msg, 'Please restart Neovim to apply the changes.')
183208
end
184-
utils.notify(msg)
185-
vim.treesitter.language.add('org')
209+
utils.notify(msg, {
210+
id = 'orgmode-treesitter-install',
211+
})
186212
return true
187213
end))
188214
:wait(60000)

0 commit comments

Comments
 (0)