Skip to content

Commit 48003b3

Browse files
fix(mesonlsp): improved root detection #3775
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 7a96d4b commit 48003b3

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

lsp/mesonlsp.lua

+28-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,35 @@
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+
---Looks for 'meson.build' file which contains 'project()' function
9+
local meson_matcher = function(_, path)
10+
local pattern = 'meson.build'
11+
local f = vim.fn.glob(table.concat({ path, pattern }, '/'))
12+
if f == '' then
13+
return false
14+
end
15+
for line in io.lines(f) do
16+
-- skip meson comments
17+
if not line:match '^%s*#.*' then
18+
local str = line:gsub('%s+', '')
19+
if str ~= '' then
20+
if str:match '^project%(' then
21+
return true
22+
else
23+
break
24+
end
25+
end
26+
end
27+
end
28+
return false
29+
end
30+
631
return {
732
cmd = { 'mesonlsp', '--lsp' },
833
filetypes = { 'meson' },
9-
root_markers = { 'meson.build', 'meson_options.txt', 'meson.options', '.git' },
34+
root_dir = function(bufnr, on_dir)
35+
on_dir(vim.fs.root(bufnr, meson_matcher) or vim.fs.root(bufnr, '.git'))
36+
end,
1037
}

0 commit comments

Comments
 (0)