@@ -45,12 +45,12 @@ local commands = {
45
45
--- GHC can leave behind corrupted files if it does not exit cleanly.
46
46
--- (https://gitlab.haskell.org/ghc/ghc/-/issues/14533)
47
47
--- To minimise the risk of this occurring, we attempt to shut down hls cleanly before exiting neovim.
48
- --- @param client number The LSP client
48
+ --- @param client lsp.Client The LSP client
49
49
--- @param bufnr number The buffer number
50
50
--- @return nil
51
51
local function ensure_clean_exit_on_quit (client , bufnr )
52
52
vim .api .nvim_create_autocmd (' VimLeavePre' , {
53
- group = vim .api .nvim_create_augroup (' haskell-tools-hls-clean-exit' , { clear = true }),
53
+ group = vim .api .nvim_create_augroup (' haskell-tools-hls-clean-exit- ' .. tostring ( client . id ) , { clear = true }),
54
54
callback = function ()
55
55
ht .log .debug (' Stopping LSP client...' )
56
56
vim .lsp .stop_client (client , false )
@@ -59,6 +59,22 @@ local function ensure_clean_exit_on_quit(client, bufnr)
59
59
})
60
60
end
61
61
62
+ --- A workaround for #48:
63
+ --- Some plugins that add LSP client capabilities which are not built-in to neovim
64
+ --- (like nvim-ufo and nvim-lsp-selection-range) cause error messages, because
65
+ --- haskell-language-server falsly advertises those server_capabilities for cabal files.
66
+ --- @param client lsp.Client
67
+ --- @return nil
68
+ local function fix_cabal_client (client )
69
+ if client .name == lsp_util .cabal_client_name and client .server_capabilities then
70
+ client .server_capabilities = vim .tbl_extend (' force' , client .server_capabilities , {
71
+ foldingRangeProvider = false ,
72
+ selectionRangeProvider = false ,
73
+ documentHighlightProvider = false ,
74
+ })
75
+ end
76
+ end
77
+
62
78
--- @class LoadHlsSettingsOpts
63
79
--- @field settings_file_pattern string | nil File name or pattern to search for. Defaults to ' hls.json'
64
80
@@ -144,10 +160,11 @@ function lsp.setup()
144
160
vim .notify (' haskell-tools: ' .. msg , vim .log .levels .ERROR )
145
161
return
146
162
end
163
+ local is_cabal = filetype == ' cabal' or filetype == ' cabalproject'
147
164
local project_root = ht .project .root_dir (file )
148
165
local hls_settings = type (hls_opts .settings ) == ' function' and hls_opts .settings (project_root ) or hls_opts .settings
149
166
local lsp_start_opts = {
150
- name = lsp_util .client_name ,
167
+ name = is_cabal and lsp_util .cabal_client_name or lsp_util . haskell_client_name ,
151
168
cmd = cmd ,
152
169
root_dir = project_root ,
153
170
capabilities = hls_opts .capabilities ,
@@ -176,12 +193,13 @@ function lsp.setup()
176
193
buf_refresh_codeLens ()
177
194
end
178
195
end ,
196
+ on_init = function (client , _ )
197
+ ensure_clean_exit_on_quit (client , bufnr )
198
+ fix_cabal_client (client )
199
+ end ,
179
200
}
180
201
ht .log .debug (' LSP start options: lsp_start_opts' )
181
202
local client_id = vim .lsp .start (lsp_start_opts )
182
- if client_id then
183
- ensure_clean_exit_on_quit (client_id , bufnr )
184
- end
185
203
return client_id
186
204
end
187
205
0 commit comments