@@ -116,31 +116,30 @@ end
116
116
--- asks the user to select one.
117
117
--
118
118
---@returns The client that the user selected or nil
119
-
local function select_client(method)
120
-
local clients = vim.tbl_values(vim.lsp.buf_get_clients());
121
-
clients = vim.tbl_filter(function (client)
119
+
local function select_client(method, on_choice)
120
+
validate {
121
+
on_choice = { on_choice, 'function', false },
122
+
}
123
+
local clients = vim.tbl_values(vim.lsp.buf_get_clients())
124
+
clients = vim.tbl_filter(function(client)
122
125
return client.supports_method(method)
123
126
end, clients)
124
127
-- better UX when choices are always in the same order (between restarts)
125
-
table.sort(clients, function (a, b) return a.name < b.name end)
128
+
table.sort(clients, function(a, b)
129
+
return a.name < b.name
130
+
end)
126
131
127
132
if #clients > 1 then
128
-
local choices = {}
129
-
for k,v in pairs(clients) do
130
-
table.insert(choices, string.format("%d %s", k, v.name))
131
-
end
132
-
local user_choice = vim.fn.confirm(
133
-
"Select a language server:",
134
-
table.concat(choices, "\n"),
135
-
0,
136
-
"Question"
137
-
)
138
-
if user_choice == 0 then return nil end
139
-
return clients[user_choice]
133
+
vim.ui.select(clients, {
134
+
prompt = 'Select a language server:',
135
+
format_item = function(client)
136
+
return client.name
137
+
end,
138
+
}, on_choice)
140
139
elseif #clients < 1 then
141
-
return nil
140
+
on_choice(nil)
142
141
else
143
-
return clients[1]
142
+
on_choice(clients[1])
144
143
end
145
144
end
146
145
@@ -152,11 +151,15 @@ end
152
151
--
153
152
---@see https://microsoft.github.io/language-server-protocol/specification#textDocument_formatting
154
153
function M.formatting(options)
155
-
local client = select_client("textDocument/formatting")
156
-
if client == nil then return end
157
-
158
154
local params = util.make_formatting_params(options)
159
-
return client.request("textDocument/formatting", params, nil, vim.api.nvim_get_current_buf())
155
+
local bufnr = vim.api.nvim_get_current_buf()
156
+
select_client('textDocument/formatting', function(client)
157
+
if client == nil then
158
+
return
159
+
end
160
+
161
+
return client.request('textDocument/formatting', params, nil, bufnr)
162
+
end)
160
163
end
161
164
162
165
--- Performs |vim.lsp.buf.formatting()| synchronously.
@@ -172,17 +175,20 @@ end
172
175
---@param timeout_ms (number) Request timeout
173
176
---@see |vim.lsp.buf.formatting_seq_sync|
174
177
function M.formatting_sync(options, timeout_ms)
175
-
local client = select_client("textDocument/formatting")
176
-
if client == nil then return end
177
-
178
178
local params = util.make_formatting_params(options)
179
179
local bufnr = vim.api.nvim_get_current_buf()
180
-
local result, err = client.request_sync("textDocument/formatting", params, timeout_ms, bufnr)
181
-
if result and result.result then
182
-
util.apply_text_edits(result.result, bufnr)
183
-
elseif err then
184
-
vim.notify("vim.lsp.buf.formatting_sync: " .. err, vim.log.levels.WARN)
185
-
end
180
+
select_client('textDocument/formatting', function(client)
181
+
if client == nil then
182
+
return
183
+
end
184
+
185
+
local result, err = client.request_sync('textDocument/formatting', params, timeout_ms, bufnr)
186
+
if result and result.result then
187
+
util.apply_text_edits(result.result, bufnr)
188
+
elseif err then
189
+
vim.notify('vim.lsp.buf.formatting_sync: ' .. err, vim.log.levels.WARN)
190
+
end
191
+
end)
186
192
end
187
193
188
194
--- Formats the current buffer by sequentially requesting formatting from attached clients.
@@ -238,12 +244,15 @@ end
238
244
---@param end_pos ({number, number}, optional) mark-indexed position.
239
245
---Defaults to the end of the last visual selection.
240
246
function M.range_formatting(options, start_pos, end_pos)
241
-
local client = select_client("textDocument/rangeFormatting")
242
-
if client == nil then return end
243
-
244
247
local params = util.make_given_range_params(start_pos, end_pos)
245
248
params.options = util.make_formatting_params(options).options
246
-
return client.request("textDocument/rangeFormatting", params)
249
+
select_client('textDocument/rangeFormatting', function(client)
250
+
if client == nil then
251
+
return
252
+
end
253
+
254
+
return client.request('textDocument/rangeFormatting', params)
255
+
end)
247
256
end
248
257
249
258
--- Renames all references to the symbol under the cursor.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4