Skip to content

Commit c9000bb

Browse files
committed
fix(mesonlsp): improved root detection
This commit enhances root directory detection of mesonlsp language server. Current behavior: root is set on first meson file user is opened New behavior: root is set to parent of meson.build with project function call
1 parent 8b0f47d commit c9000bb

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

lsp/mesonlsp.lua

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,34 @@
33
--- https://github.com/JCWasmx86/mesonlsp
44
---
55
--- An unofficial, unendorsed language server for meson written in C++
6+
7+
---Checks if a given path contains a valid Meson project root file
8+
local meson_matcher = function(_, path)
9+
local pattern = 'meson.build'
10+
local f = vim.fn.glob(table.concat({ path, pattern }, '/'))
11+
if f == '' then
12+
return false
13+
end
14+
for line in io.lines(f) do
15+
-- skip meson comments
16+
if not line:match '^%s*#.*' then
17+
local str = line:gsub('%s+', '')
18+
if str ~= '' then
19+
if str:match '^project%(' then
20+
return true
21+
else
22+
break
23+
end
24+
end
25+
end
26+
end
27+
return false
28+
end
29+
630
return {
731
cmd = { 'mesonlsp', '--lsp' },
832
filetypes = { 'meson' },
9-
root_markers = { 'meson.build', 'meson_options.txt', 'meson.options', '.git' },
33+
root_dir = function(bufnr, on_dir)
34+
on_dir(vim.fs.root(bufnr, meson_matcher) or vim.fs.root(bufnr, '.git'))
35+
end,
1036
}

0 commit comments

Comments
 (0)