Skip to content

fix(mesonlsp): improved root detection #3775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion lsp/mesonlsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,35 @@
--- https://github.com/JCWasmx86/mesonlsp
---
--- An unofficial, unendorsed language server for meson written in C++

---Checks if a given path contains a valid Meson project root file
---Looks for 'meson.build' file which contains 'project()' function
local meson_matcher = function(_, path)
local pattern = 'meson.build'
local f = vim.fn.glob(table.concat({ path, pattern }, '/'))
if f == '' then
return false
end
for line in io.lines(f) do
-- skip meson comments
if not line:match '^%s*#.*' then
local str = line:gsub('%s+', '')
if str ~= '' then
if str:match '^project%(' then
return true
else
break
Comment on lines +15 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vim.filetype.match() exists for this purpose. However,

vim.filetype.match({ filename = f, contents = 'project' })

seems to return "meson" even if the file doesn't contain "project", which isn't what you want here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For mesonlsp root_dir is required to be at folder with 'project' file. If root will be in different place all file will be underlined with red. I just take code from vala_ls which finds project root perfectly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just take code from vala_ls which finds project root perfectly.

Yeah, I'm trying to find more concise patterns so that we don't have to maintain 10x as much code in all of these configs.

end
end
end
end
return false
end

return {
cmd = { 'mesonlsp', '--lsp' },
filetypes = { 'meson' },
root_markers = { 'meson.build', 'meson_options.txt', 'meson.options', '.git' },
root_dir = function(bufnr, on_dir)
on_dir(vim.fs.root(bufnr, meson_matcher) or vim.fs.root(bufnr, '.git'))
end,
}
Loading