feat: improve default haxe_language_server init_options (#3284)

* feat: add '.git' as root pattern for haxe_language_server

* feat: use existing hxml for haxe ls configuration

Previously, it would detect the root dir by matching with "*.hxml", however, it
would use "build.hxml" as the default `displayArguments` even though it may not
exist. This could cause the error:

```
haxe_language_server: -32603: Error: Could not process argument build.hxml (file not found)
Invalid character:
```

Now it will use the first ".hxml" file that is found in the project. It will
only do this if no `displayArguments` value has been set in the `setup()` call,
so it will still respect user set values.

If no hxml file is found, then it uses empty `displayArguments`, which is still
better than a broken configuration.
This commit is contained in:
tobil4sk 2024-08-26 09:32:00 +01:00 committed by GitHub
parent 911167921d
commit acf17dc452
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,18 +1,33 @@
local util = require 'lspconfig.util'
local function find_hxml(path)
return vim.fs.find(function(name)
return name:match '.hxml$'
end, { path = path, type = 'file' })
end
return {
default_config = {
cmd = { 'haxe-language-server' },
filetypes = { 'haxe' },
root_dir = util.root_pattern '*.hxml',
root_dir = util.root_pattern('*.hxml', '.git'),
settings = {
haxe = {
executable = 'haxe',
},
},
init_options = {
displayArguments = { 'build.hxml' },
},
init_options = {},
on_new_config = function(new_config, new_root_dir)
if new_config.init_options.displayArguments then
return
end
local hxml = find_hxml(new_root_dir)[1]
if hxml then
vim.notify('Using HXML: ' .. hxml)
new_config.init_options.displayArguments = { hxml }
end
end,
},
docs = {
description = [[
@ -36,12 +51,24 @@ lspconfig.haxe_language_server.setup({
})
```
By default, an HXML compiler arguments file named `build.hxml` is expected in
your project's root directory. If your file is named something different,
specify it using the `init_options.displayArguments` setting.
By default, the language server is configured with the HXML compiler arguments
contained in the first `.hxml` file found in your project's root directory.
If you want to specify which one to use, set the `init_options.displayArguments`
setting:
```lua
lspconfig.haxe_language_server.setup({
-- ...
init_options = {
displayArguments = { "build.hxml" },
},
})
```
]],
default_config = {
root_dir = [[root_pattern("*.hxml")]],
root_dir = [[root_pattern("*.hxml", ".git")]],
init_options = 'default value is set by on_new_config',
},
},
}