2019-11-24 03:01:18 -08:00
|
|
|
local vim = vim
|
2019-11-20 14:21:57 -08:00
|
|
|
local validate = vim.validate
|
2019-11-20 15:35:18 -08:00
|
|
|
local vfn = vim.fn
|
2019-11-20 14:21:57 -08:00
|
|
|
local util = require 'vim.lsp.util'
|
|
|
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
|
2021-08-22 14:55:28 -06:00
|
|
|
---@private
|
2020-08-19 18:17:08 +02:00
|
|
|
--- Returns nil if {status} is false or nil, otherwise returns the rest of the
|
|
|
|
|
--- arguments.
|
2019-11-20 14:21:57 -08:00
|
|
|
local function ok_or_nil(status, ...)
|
2019-11-20 16:16:36 -08:00
|
|
|
if not status then return end
|
|
|
|
|
return ...
|
2019-11-20 14:21:57 -08:00
|
|
|
end
|
2020-08-19 18:17:08 +02:00
|
|
|
|
2021-08-22 14:55:28 -06:00
|
|
|
---@private
|
2020-08-19 18:17:08 +02:00
|
|
|
--- Swallows errors.
|
|
|
|
|
---
|
2021-08-22 14:55:28 -06:00
|
|
|
---@param fn Function to run
|
|
|
|
|
---@param ... Function arguments
|
|
|
|
|
---@returns Result of `fn(...)` if there are no errors, otherwise nil.
|
2020-08-19 18:17:08 +02:00
|
|
|
--- Returns nil if errors occur during {fn}, otherwise returns
|
2019-11-20 14:21:57 -08:00
|
|
|
local function npcall(fn, ...)
|
2019-11-20 16:16:36 -08:00
|
|
|
return ok_or_nil(pcall(fn, ...))
|
2019-11-20 14:21:57 -08:00
|
|
|
end
|
|
|
|
|
|
2021-08-22 14:55:28 -06:00
|
|
|
---@private
|
2020-08-19 18:17:08 +02:00
|
|
|
--- Sends an async request to all active clients attached to the current
|
|
|
|
|
--- buffer.
|
|
|
|
|
---
|
2021-08-22 14:55:28 -06:00
|
|
|
---@param method (string) LSP method name
|
|
|
|
|
---@param params (optional, table) Parameters to send to the server
|
|
|
|
|
---@param handler (optional, functionnil) See |lsp-handler|. Follows |lsp-handler-resolution|
|
2020-08-19 18:17:08 +02:00
|
|
|
--
|
2021-08-22 14:55:28 -06:00
|
|
|
---@returns 2-tuple:
|
2020-08-19 18:17:08 +02:00
|
|
|
--- - Map of client-id:request-id pairs for all successful requests.
|
|
|
|
|
--- - Function which can be used to cancel all the requests. You could instead
|
|
|
|
|
--- iterate all clients and call their `cancel_request()` methods.
|
|
|
|
|
---
|
2021-08-22 14:55:28 -06:00
|
|
|
---@see |vim.lsp.buf_request()|
|
lsp: vim.lsp.diagnostic (#12655)
Breaking Changes:
- Deprecated all `vim.lsp.util.{*diagnostics*}()` functions.
- Instead, all functions must be found in vim.lsp.diagnostic
- For now, they issue a warning ONCE per neovim session. In a
"little while" we will remove them completely.
- `vim.lsp.callbacks` has moved to `vim.lsp.handlers`.
- For a "little while" we will just redirect `vim.lsp.callbacks` to
`vim.lsp.handlers`. However, we will remove this at some point, so
it is recommended that you change all of your references to
`callbacks` into `handlers`.
- This also means that for functions like |vim.lsp.start_client()|
and similar, keyword style arguments have moved from "callbacks"
to "handlers". Once again, these are currently being forward, but
will cease to be forwarded in a "little while".
- Changed the highlight groups for LspDiagnostic highlight as they were
inconsistently named.
- For more information, see |lsp-highlight-diagnostics|
- Changed the sign group names as well, to be consistent with
|lsp-highlight-diagnostics|
General Enhancements:
- Rewrote much of the getting started help document for lsp. It also
provides a much nicer configuration strategy, so as to not recommend
globally overwriting builtin neovim mappings.
LSP Enhancements:
- Introduced the concept of |lsp-handlers| which will allow much better
customization for users without having to copy & paste entire files /
functions / etc.
Diagnostic Enhancements:
- "goto next diagnostic" |vim.lsp.diagnostic.goto_next()|
- "goto prev diagnostic" |vim.lsp.diagnostic.goto_prev()|
- For each of the gotos, auto open diagnostics is available as a
configuration option
- Configurable diagnostic handling:
- See |vim.lsp.diagnostic.on_publish_diagnostics()|
- Delay display until after insert mode
- Configure signs
- Configure virtual text
- Configure underline
- Set the location list with the buffers diagnostics.
- See |vim.lsp.diagnostic.set_loclist()|
- Better performance for getting counts and line diagnostics
- They are now cached on save, to enhance lookups.
- Particularly useful for checking in statusline, etc.
- Actual testing :)
- See ./test/functional/plugin/lsp/diagnostic_spec.lua
- Added `guisp` for underline highlighting
NOTE: "a little while" means enough time to feel like most plugins and
plugin authors have had a chance to refactor their code to use the
updated calls. Then we will remove them completely. There is no need to
keep them, because we don't have any released version of neovim that
exposes these APIs. I'm trying to be nice to people following HEAD :)
Co-authored: [Twitch Chat 2020](https://twitch.tv/teej_dv)
2020-11-12 22:21:34 -05:00
|
|
|
local function request(method, params, handler)
|
2019-11-20 16:16:36 -08:00
|
|
|
validate {
|
|
|
|
|
method = {method, 's'};
|
lsp: vim.lsp.diagnostic (#12655)
Breaking Changes:
- Deprecated all `vim.lsp.util.{*diagnostics*}()` functions.
- Instead, all functions must be found in vim.lsp.diagnostic
- For now, they issue a warning ONCE per neovim session. In a
"little while" we will remove them completely.
- `vim.lsp.callbacks` has moved to `vim.lsp.handlers`.
- For a "little while" we will just redirect `vim.lsp.callbacks` to
`vim.lsp.handlers`. However, we will remove this at some point, so
it is recommended that you change all of your references to
`callbacks` into `handlers`.
- This also means that for functions like |vim.lsp.start_client()|
and similar, keyword style arguments have moved from "callbacks"
to "handlers". Once again, these are currently being forward, but
will cease to be forwarded in a "little while".
- Changed the highlight groups for LspDiagnostic highlight as they were
inconsistently named.
- For more information, see |lsp-highlight-diagnostics|
- Changed the sign group names as well, to be consistent with
|lsp-highlight-diagnostics|
General Enhancements:
- Rewrote much of the getting started help document for lsp. It also
provides a much nicer configuration strategy, so as to not recommend
globally overwriting builtin neovim mappings.
LSP Enhancements:
- Introduced the concept of |lsp-handlers| which will allow much better
customization for users without having to copy & paste entire files /
functions / etc.
Diagnostic Enhancements:
- "goto next diagnostic" |vim.lsp.diagnostic.goto_next()|
- "goto prev diagnostic" |vim.lsp.diagnostic.goto_prev()|
- For each of the gotos, auto open diagnostics is available as a
configuration option
- Configurable diagnostic handling:
- See |vim.lsp.diagnostic.on_publish_diagnostics()|
- Delay display until after insert mode
- Configure signs
- Configure virtual text
- Configure underline
- Set the location list with the buffers diagnostics.
- See |vim.lsp.diagnostic.set_loclist()|
- Better performance for getting counts and line diagnostics
- They are now cached on save, to enhance lookups.
- Particularly useful for checking in statusline, etc.
- Actual testing :)
- See ./test/functional/plugin/lsp/diagnostic_spec.lua
- Added `guisp` for underline highlighting
NOTE: "a little while" means enough time to feel like most plugins and
plugin authors have had a chance to refactor their code to use the
updated calls. Then we will remove them completely. There is no need to
keep them, because we don't have any released version of neovim that
exposes these APIs. I'm trying to be nice to people following HEAD :)
Co-authored: [Twitch Chat 2020](https://twitch.tv/teej_dv)
2020-11-12 22:21:34 -05:00
|
|
|
handler = {handler, 'f', true};
|
2019-11-20 16:16:36 -08:00
|
|
|
}
|
lsp: vim.lsp.diagnostic (#12655)
Breaking Changes:
- Deprecated all `vim.lsp.util.{*diagnostics*}()` functions.
- Instead, all functions must be found in vim.lsp.diagnostic
- For now, they issue a warning ONCE per neovim session. In a
"little while" we will remove them completely.
- `vim.lsp.callbacks` has moved to `vim.lsp.handlers`.
- For a "little while" we will just redirect `vim.lsp.callbacks` to
`vim.lsp.handlers`. However, we will remove this at some point, so
it is recommended that you change all of your references to
`callbacks` into `handlers`.
- This also means that for functions like |vim.lsp.start_client()|
and similar, keyword style arguments have moved from "callbacks"
to "handlers". Once again, these are currently being forward, but
will cease to be forwarded in a "little while".
- Changed the highlight groups for LspDiagnostic highlight as they were
inconsistently named.
- For more information, see |lsp-highlight-diagnostics|
- Changed the sign group names as well, to be consistent with
|lsp-highlight-diagnostics|
General Enhancements:
- Rewrote much of the getting started help document for lsp. It also
provides a much nicer configuration strategy, so as to not recommend
globally overwriting builtin neovim mappings.
LSP Enhancements:
- Introduced the concept of |lsp-handlers| which will allow much better
customization for users without having to copy & paste entire files /
functions / etc.
Diagnostic Enhancements:
- "goto next diagnostic" |vim.lsp.diagnostic.goto_next()|
- "goto prev diagnostic" |vim.lsp.diagnostic.goto_prev()|
- For each of the gotos, auto open diagnostics is available as a
configuration option
- Configurable diagnostic handling:
- See |vim.lsp.diagnostic.on_publish_diagnostics()|
- Delay display until after insert mode
- Configure signs
- Configure virtual text
- Configure underline
- Set the location list with the buffers diagnostics.
- See |vim.lsp.diagnostic.set_loclist()|
- Better performance for getting counts and line diagnostics
- They are now cached on save, to enhance lookups.
- Particularly useful for checking in statusline, etc.
- Actual testing :)
- See ./test/functional/plugin/lsp/diagnostic_spec.lua
- Added `guisp` for underline highlighting
NOTE: "a little while" means enough time to feel like most plugins and
plugin authors have had a chance to refactor their code to use the
updated calls. Then we will remove them completely. There is no need to
keep them, because we don't have any released version of neovim that
exposes these APIs. I'm trying to be nice to people following HEAD :)
Co-authored: [Twitch Chat 2020](https://twitch.tv/teej_dv)
2020-11-12 22:21:34 -05:00
|
|
|
return vim.lsp.buf_request(0, method, params, handler)
|
2019-11-20 15:35:18 -08:00
|
|
|
end
|
|
|
|
|
|
2020-08-19 18:17:08 +02:00
|
|
|
--- Checks whether the language servers attached to the current buffer are
|
|
|
|
|
--- ready.
|
|
|
|
|
---
|
2021-08-22 14:55:28 -06:00
|
|
|
---@returns `true` if server responds.
|
2020-02-26 20:22:14 +01:00
|
|
|
function M.server_ready()
|
|
|
|
|
return not not vim.lsp.buf_notify(0, "window/progress", {})
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-19 23:16:12 +02:00
|
|
|
--- Displays hover information about the symbol under the cursor in a floating
|
|
|
|
|
--- window. Calling the function twice will jump into the floating window.
|
2019-11-20 15:35:18 -08:00
|
|
|
function M.hover()
|
2019-11-21 15:41:32 -08:00
|
|
|
local params = util.make_position_params()
|
2019-11-26 05:59:40 -08:00
|
|
|
request('textDocument/hover', params)
|
2019-11-20 14:21:57 -08:00
|
|
|
end
|
|
|
|
|
|
2020-07-19 23:16:12 +02:00
|
|
|
--- Jumps to the declaration of the symbol under the cursor.
|
2021-08-22 14:55:28 -06:00
|
|
|
---@note Many servers do not implement this method. Generally, see |vim.lsp.buf.definition()| instead.
|
2020-07-19 23:16:12 +02:00
|
|
|
---
|
2019-11-20 14:21:57 -08:00
|
|
|
function M.declaration()
|
2019-11-21 15:41:32 -08:00
|
|
|
local params = util.make_position_params()
|
2019-11-26 05:59:40 -08:00
|
|
|
request('textDocument/declaration', params)
|
2019-11-20 15:35:18 -08:00
|
|
|
end
|
|
|
|
|
|
2020-07-19 23:16:12 +02:00
|
|
|
--- Jumps to the definition of the symbol under the cursor.
|
|
|
|
|
---
|
2019-11-20 15:35:18 -08:00
|
|
|
function M.definition()
|
2019-11-21 15:41:32 -08:00
|
|
|
local params = util.make_position_params()
|
2019-11-26 05:59:40 -08:00
|
|
|
request('textDocument/definition', params)
|
2019-11-20 14:21:57 -08:00
|
|
|
end
|
|
|
|
|
|
2020-07-19 23:16:12 +02:00
|
|
|
--- Jumps to the definition of the type of the symbol under the cursor.
|
|
|
|
|
---
|
2019-11-20 14:21:57 -08:00
|
|
|
function M.type_definition()
|
2019-11-21 15:41:32 -08:00
|
|
|
local params = util.make_position_params()
|
2019-11-26 05:59:40 -08:00
|
|
|
request('textDocument/typeDefinition', params)
|
2019-11-20 14:21:57 -08:00
|
|
|
end
|
|
|
|
|
|
2020-07-19 23:16:12 +02:00
|
|
|
--- Lists all the implementations for the symbol under the cursor in the
|
|
|
|
|
--- quickfix window.
|
2019-11-20 14:21:57 -08:00
|
|
|
function M.implementation()
|
2019-11-21 15:41:32 -08:00
|
|
|
local params = util.make_position_params()
|
2019-11-26 05:59:40 -08:00
|
|
|
request('textDocument/implementation', params)
|
2019-11-20 16:03:32 -08:00
|
|
|
end
|
|
|
|
|
|
2020-07-19 23:16:12 +02:00
|
|
|
--- Displays signature information about the symbol under the cursor in a
|
|
|
|
|
--- floating window.
|
2019-11-20 15:35:18 -08:00
|
|
|
function M.signature_help()
|
2019-11-21 15:41:32 -08:00
|
|
|
local params = util.make_position_params()
|
2019-11-26 05:59:40 -08:00
|
|
|
request('textDocument/signatureHelp', params)
|
2019-11-20 14:21:57 -08:00
|
|
|
end
|
|
|
|
|
|
2020-07-19 23:16:12 +02:00
|
|
|
--- Retrieves the completion items at the current cursor position. Can only be
|
|
|
|
|
--- called in Insert mode.
|
2020-08-19 18:17:08 +02:00
|
|
|
---
|
2021-08-22 14:55:28 -06:00
|
|
|
---@param context (context support not yet implemented) Additional information
|
2020-08-19 18:17:08 +02:00
|
|
|
--- about the context in which a completion was triggered (how it was triggered,
|
|
|
|
|
--- and by which trigger character, if applicable)
|
|
|
|
|
---
|
2021-08-22 14:55:28 -06:00
|
|
|
---@see |vim.lsp.protocol.constants.CompletionTriggerKind|
|
2019-11-20 15:35:18 -08:00
|
|
|
function M.completion(context)
|
2019-11-21 15:41:32 -08:00
|
|
|
local params = util.make_position_params()
|
2019-11-20 16:16:36 -08:00
|
|
|
params.context = context
|
2019-11-26 05:59:40 -08:00
|
|
|
return request('textDocument/completion', params)
|
2019-11-20 14:21:57 -08:00
|
|
|
end
|
|
|
|
|
|
2021-08-22 14:55:28 -06:00
|
|
|
---@private
|
2021-05-02 15:37:31 +02:00
|
|
|
--- If there is more than one client that supports the given method,
|
|
|
|
|
--- asks the user to select one.
|
2021-04-30 13:40:20 +02:00
|
|
|
--
|
2021-08-22 14:55:28 -06:00
|
|
|
---@returns The client that the user selected or nil
|
2021-05-02 15:37:31 +02:00
|
|
|
local function select_client(method)
|
2021-04-30 13:40:20 +02:00
|
|
|
local clients = vim.tbl_values(vim.lsp.buf_get_clients());
|
|
|
|
|
clients = vim.tbl_filter(function (client)
|
2021-05-02 15:37:31 +02:00
|
|
|
return client.supports_method(method)
|
2021-04-30 13:40:20 +02:00
|
|
|
end, clients)
|
|
|
|
|
-- better UX when choices are always in the same order (between restarts)
|
|
|
|
|
table.sort(clients, function (a, b) return a.name < b.name end)
|
|
|
|
|
|
|
|
|
|
if #clients > 1 then
|
|
|
|
|
local choices = {}
|
2021-07-11 11:34:26 -07:00
|
|
|
for k,v in pairs(clients) do
|
2021-04-30 13:40:20 +02:00
|
|
|
table.insert(choices, string.format("%d %s", k, v.name))
|
|
|
|
|
end
|
|
|
|
|
local user_choice = vim.fn.confirm(
|
2021-05-02 15:37:31 +02:00
|
|
|
"Select a language server:",
|
2021-04-30 13:40:20 +02:00
|
|
|
table.concat(choices, "\n"),
|
|
|
|
|
0,
|
|
|
|
|
"Question"
|
|
|
|
|
)
|
|
|
|
|
if user_choice == 0 then return nil end
|
|
|
|
|
return clients[user_choice]
|
|
|
|
|
elseif #clients < 1 then
|
|
|
|
|
return nil
|
|
|
|
|
else
|
|
|
|
|
return clients[1]
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-19 23:16:12 +02:00
|
|
|
--- Formats the current buffer.
|
|
|
|
|
---
|
2021-08-22 14:55:28 -06:00
|
|
|
---@param options (optional, table) Can be used to specify FormattingOptions.
|
2020-07-19 23:16:12 +02:00
|
|
|
--- Some unspecified options will be automatically derived from the current
|
|
|
|
|
--- Neovim options.
|
2020-08-19 18:17:08 +02:00
|
|
|
--
|
2021-08-22 14:55:28 -06:00
|
|
|
---@see https://microsoft.github.io/language-server-protocol/specification#textDocument_formatting
|
2019-11-20 20:51:44 -08:00
|
|
|
function M.formatting(options)
|
2021-05-02 15:37:31 +02:00
|
|
|
local client = select_client("textDocument/formatting")
|
2021-04-30 13:40:20 +02:00
|
|
|
if client == nil then return end
|
|
|
|
|
|
2020-05-05 17:23:45 +02:00
|
|
|
local params = util.make_formatting_params(options)
|
2021-06-16 15:53:43 +09:00
|
|
|
return client.request("textDocument/formatting", params, nil, vim.api.nvim_get_current_buf())
|
2019-11-20 20:51:44 -08:00
|
|
|
end
|
|
|
|
|
|
2020-08-19 18:17:08 +02:00
|
|
|
--- Performs |vim.lsp.buf.formatting()| synchronously.
|
2020-07-02 07:09:17 -04:00
|
|
|
---
|
|
|
|
|
--- Useful for running on save, to make sure buffer is formatted prior to being
|
2020-08-19 18:17:08 +02:00
|
|
|
--- saved. {timeout_ms} is passed on to |vim.lsp.buf_request_sync()|. Example:
|
|
|
|
|
---
|
|
|
|
|
--- <pre>
|
|
|
|
|
--- vim.api.nvim_command[[autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_sync()]]
|
|
|
|
|
--- </pre>
|
|
|
|
|
---
|
2021-08-22 14:55:28 -06:00
|
|
|
---@param options Table with valid `FormattingOptions` entries
|
|
|
|
|
---@param timeout_ms (number) Request timeout
|
|
|
|
|
---@see |vim.lsp.buf.formatting_seq_sync|
|
2020-05-05 17:23:45 +02:00
|
|
|
function M.formatting_sync(options, timeout_ms)
|
2021-05-02 15:37:31 +02:00
|
|
|
local client = select_client("textDocument/formatting")
|
2021-04-30 13:40:20 +02:00
|
|
|
if client == nil then return end
|
|
|
|
|
|
2020-05-05 17:23:45 +02:00
|
|
|
local params = util.make_formatting_params(options)
|
2021-06-16 15:53:43 +09:00
|
|
|
local result, err = client.request_sync("textDocument/formatting", params, timeout_ms, vim.api.nvim_get_current_buf())
|
2021-04-30 13:40:20 +02:00
|
|
|
if result and result.result then
|
|
|
|
|
util.apply_text_edits(result.result)
|
2021-05-02 16:16:49 +02:00
|
|
|
elseif err then
|
|
|
|
|
vim.notify("vim.lsp.buf.formatting_sync: " .. err, vim.log.levels.WARN)
|
2021-04-30 13:40:20 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Formats the current buffer by sequentially requesting formatting from attached clients.
|
|
|
|
|
---
|
|
|
|
|
--- Useful when multiple clients with formatting capability are attached.
|
|
|
|
|
---
|
|
|
|
|
--- Since it's synchronous, can be used for running on save, to make sure buffer is formatted
|
|
|
|
|
--- prior to being saved. {timeout_ms} is passed on to the |vim.lsp.client| `request_sync` method.
|
|
|
|
|
--- Example:
|
|
|
|
|
--- <pre>
|
|
|
|
|
--- vim.api.nvim_command[[autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_seq_sync()]]
|
|
|
|
|
--- </pre>
|
|
|
|
|
---
|
2021-08-22 14:55:28 -06:00
|
|
|
---@param options (optional, table) `FormattingOptions` entries
|
|
|
|
|
---@param timeout_ms (optional, number) Request timeout
|
|
|
|
|
---@param order (optional, table) List of client names. Formatting is requested from clients
|
2021-04-30 13:40:20 +02:00
|
|
|
---in the following order: first all clients that are not in the `order` list, then
|
|
|
|
|
---the remaining clients in the order as they occur in the `order` list.
|
|
|
|
|
function M.formatting_seq_sync(options, timeout_ms, order)
|
|
|
|
|
local clients = vim.tbl_values(vim.lsp.buf_get_clients());
|
|
|
|
|
|
|
|
|
|
-- sort the clients according to `order`
|
2021-07-11 11:34:26 -07:00
|
|
|
for _, client_name in pairs(order or {}) do
|
2021-04-30 13:40:20 +02:00
|
|
|
-- if the client exists, move to the end of the list
|
2021-07-11 11:34:26 -07:00
|
|
|
for i, client in pairs(clients) do
|
2021-04-30 13:40:20 +02:00
|
|
|
if client.name == client_name then
|
|
|
|
|
table.insert(clients, table.remove(clients, i))
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- loop through the clients and make synchronous formatting requests
|
2021-07-11 11:34:26 -07:00
|
|
|
for _, client in pairs(clients) do
|
2021-04-30 13:40:20 +02:00
|
|
|
if client.resolved_capabilities.document_formatting then
|
|
|
|
|
local params = util.make_formatting_params(options)
|
2021-06-16 15:53:43 +09:00
|
|
|
local result, err = client.request_sync("textDocument/formatting", params, timeout_ms, vim.api.nvim_get_current_buf())
|
2021-04-30 13:40:20 +02:00
|
|
|
if result and result.result then
|
|
|
|
|
util.apply_text_edits(result.result)
|
2021-05-02 16:16:49 +02:00
|
|
|
elseif err then
|
|
|
|
|
vim.notify(string.format("vim.lsp.buf.formatting_seq_sync: (%s) %s", client.name, err), vim.log.levels.WARN)
|
2021-04-30 13:40:20 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2020-05-05 17:23:45 +02:00
|
|
|
end
|
|
|
|
|
|
2020-08-19 18:17:08 +02:00
|
|
|
--- Formats a given range.
|
|
|
|
|
---
|
2021-08-22 14:55:28 -06:00
|
|
|
---@param options Table with valid `FormattingOptions` entries.
|
|
|
|
|
---@param start_pos ({number, number}, optional) mark-indexed position.
|
2020-08-19 18:17:08 +02:00
|
|
|
---Defaults to the start of the last visual selection.
|
2021-08-22 14:55:28 -06:00
|
|
|
---@param end_pos ({number, number}, optional) mark-indexed position.
|
2020-08-19 18:17:08 +02:00
|
|
|
---Defaults to the end of the last visual selection.
|
2019-11-20 20:51:44 -08:00
|
|
|
function M.range_formatting(options, start_pos, end_pos)
|
2021-05-02 15:37:31 +02:00
|
|
|
local client = select_client("textDocument/rangeFormatting")
|
|
|
|
|
if client == nil then return end
|
|
|
|
|
|
2020-09-25 04:53:08 +09:00
|
|
|
local params = util.make_given_range_params(start_pos, end_pos)
|
2021-05-02 15:37:31 +02:00
|
|
|
params.options = util.make_formatting_params(options).options
|
|
|
|
|
return client.request("textDocument/rangeFormatting", params)
|
2019-11-20 14:21:57 -08:00
|
|
|
end
|
|
|
|
|
|
2020-08-19 18:17:08 +02:00
|
|
|
--- Renames all references to the symbol under the cursor.
|
|
|
|
|
---
|
2021-08-22 14:55:28 -06:00
|
|
|
---@param new_name (string) If not provided, the user will be prompted for a new
|
2021-11-07 16:13:53 +01:00
|
|
|
---name using |vim.ui.input()|.
|
2019-11-20 16:03:32 -08:00
|
|
|
function M.rename(new_name)
|
2021-11-07 16:13:53 +01:00
|
|
|
local opts = {
|
|
|
|
|
prompt = "New Name: "
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
---@private
|
|
|
|
|
local function on_confirm(input)
|
|
|
|
|
if not (input and #input > 0) then return end
|
|
|
|
|
local params = util.make_position_params()
|
|
|
|
|
params.newName = input
|
|
|
|
|
request('textDocument/rename', params)
|
|
|
|
|
end
|
|
|
|
|
|
2021-09-08 23:00:15 +08:00
|
|
|
local function prepare_rename(err, result)
|
|
|
|
|
if err == nil and result == nil then
|
|
|
|
|
vim.notify('nothing to rename', vim.log.levels.INFO)
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
if result and result.placeholder then
|
2021-11-07 16:13:53 +01:00
|
|
|
opts.default = result.placeholder
|
|
|
|
|
if not new_name then npcall(vim.ui.input, opts, on_confirm) end
|
2021-09-08 23:00:15 +08:00
|
|
|
elseif result and result.start and result['end'] and
|
|
|
|
|
result.start.line == result['end'].line then
|
|
|
|
|
local line = vfn.getline(result.start.line+1)
|
|
|
|
|
local start_char = result.start.character+1
|
|
|
|
|
local end_char = result['end'].character
|
2021-11-07 16:13:53 +01:00
|
|
|
opts.default = string.sub(line, start_char, end_char)
|
|
|
|
|
if not new_name then npcall(vim.ui.input, opts, on_confirm) end
|
2021-09-08 23:00:15 +08:00
|
|
|
else
|
|
|
|
|
-- fallback to guessing symbol using <cword>
|
|
|
|
|
--
|
|
|
|
|
-- this can happen if the language server does not support prepareRename,
|
|
|
|
|
-- returns an unexpected response, or requests for "default behavior"
|
|
|
|
|
--
|
|
|
|
|
-- see https://microsoft.github.io/language-server-protocol/specification#textDocument_prepareRename
|
2021-11-07 16:13:53 +01:00
|
|
|
opts.default = vfn.expand('<cword>')
|
|
|
|
|
if not new_name then npcall(vim.ui.input, opts, on_confirm) end
|
2021-09-08 23:00:15 +08:00
|
|
|
end
|
2021-11-07 16:13:53 +01:00
|
|
|
if new_name then on_confirm(new_name) end
|
2021-09-08 23:00:15 +08:00
|
|
|
end
|
2021-11-07 16:13:53 +01:00
|
|
|
request('textDocument/prepareRename', util.make_position_params(), prepare_rename)
|
2019-11-20 16:03:32 -08:00
|
|
|
end
|
|
|
|
|
|
2020-07-19 23:16:12 +02:00
|
|
|
--- Lists all the references to the symbol under the cursor in the quickfix window.
|
|
|
|
|
---
|
2021-08-22 14:55:28 -06:00
|
|
|
---@param context (table) Context for the request
|
|
|
|
|
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references
|
2019-11-24 03:01:18 -08:00
|
|
|
function M.references(context)
|
|
|
|
|
validate { context = { context, 't', true } }
|
|
|
|
|
local params = util.make_position_params()
|
|
|
|
|
params.context = context or {
|
|
|
|
|
includeDeclaration = true;
|
|
|
|
|
}
|
2019-11-26 05:59:40 -08:00
|
|
|
request('textDocument/references', params)
|
2019-11-24 03:01:18 -08:00
|
|
|
end
|
|
|
|
|
|
2020-07-19 23:16:12 +02:00
|
|
|
--- Lists all symbols in the current buffer in the quickfix window.
|
|
|
|
|
---
|
2020-02-22 21:14:10 +09:00
|
|
|
function M.document_symbol()
|
|
|
|
|
local params = { textDocument = util.make_text_document_params() }
|
|
|
|
|
request('textDocument/documentSymbol', params)
|
|
|
|
|
end
|
|
|
|
|
|
2021-08-22 14:55:28 -06:00
|
|
|
---@private
|
2020-07-18 21:10:09 +02:00
|
|
|
local function pick_call_hierarchy_item(call_hierarchy_items)
|
|
|
|
|
if not call_hierarchy_items then return end
|
|
|
|
|
if #call_hierarchy_items == 1 then
|
|
|
|
|
return call_hierarchy_items[1]
|
|
|
|
|
end
|
|
|
|
|
local items = {}
|
2021-07-11 11:34:26 -07:00
|
|
|
for i, item in pairs(call_hierarchy_items) do
|
2020-07-18 21:10:09 +02:00
|
|
|
local entry = item.detail or item.name
|
|
|
|
|
table.insert(items, string.format("%d. %s", i, entry))
|
|
|
|
|
end
|
|
|
|
|
local choice = vim.fn.inputlist(items)
|
|
|
|
|
if choice < 1 or choice > #items then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
return choice
|
|
|
|
|
end
|
|
|
|
|
|
2021-08-22 14:55:28 -06:00
|
|
|
---@private
|
2021-05-18 17:32:40 -04:00
|
|
|
local function call_hierarchy(method)
|
|
|
|
|
local params = util.make_position_params()
|
2021-09-22 00:05:49 +02:00
|
|
|
request('textDocument/prepareCallHierarchy', params, function(err, result, ctx)
|
2021-05-18 17:32:40 -04:00
|
|
|
if err then
|
|
|
|
|
vim.notify(err.message, vim.log.levels.WARN)
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
local call_hierarchy_item = pick_call_hierarchy_item(result)
|
2021-09-22 00:05:49 +02:00
|
|
|
local client = vim.lsp.get_client_by_id(ctx.client_id)
|
|
|
|
|
if client then
|
|
|
|
|
client.request(method, { item = call_hierarchy_item }, nil, ctx.bufnr)
|
|
|
|
|
else
|
|
|
|
|
vim.notify(string.format(
|
|
|
|
|
'Client with id=%d disappeared during call hierarchy request', ctx.client_id),
|
|
|
|
|
vim.log.levels.WARN
|
|
|
|
|
)
|
|
|
|
|
end
|
2021-05-18 17:32:40 -04:00
|
|
|
end)
|
|
|
|
|
end
|
|
|
|
|
|
2020-08-19 18:17:08 +02:00
|
|
|
--- Lists all the call sites of the symbol under the cursor in the
|
|
|
|
|
--- |quickfix| window. If the symbol can resolve to multiple
|
|
|
|
|
--- items, the user can pick one in the |inputlist|.
|
2020-07-18 21:10:09 +02:00
|
|
|
function M.incoming_calls()
|
2021-05-18 17:32:40 -04:00
|
|
|
call_hierarchy('callHierarchy/incomingCalls')
|
2020-07-18 21:10:09 +02:00
|
|
|
end
|
|
|
|
|
|
2020-08-19 18:17:08 +02:00
|
|
|
--- Lists all the items that are called by the symbol under the
|
|
|
|
|
--- cursor in the |quickfix| window. If the symbol can resolve to
|
|
|
|
|
--- multiple items, the user can pick one in the |inputlist|.
|
2020-07-18 21:10:09 +02:00
|
|
|
function M.outgoing_calls()
|
2021-05-18 17:32:40 -04:00
|
|
|
call_hierarchy('callHierarchy/outgoingCalls')
|
2020-07-18 21:10:09 +02:00
|
|
|
end
|
2020-07-02 07:09:17 -04:00
|
|
|
|
2020-11-25 12:07:02 -08:00
|
|
|
--- List workspace folders.
|
2020-12-08 21:09:33 -05:00
|
|
|
---
|
2020-11-25 12:07:02 -08:00
|
|
|
function M.list_workspace_folders()
|
|
|
|
|
local workspace_folders = {}
|
2021-07-11 11:34:26 -07:00
|
|
|
for _, client in pairs(vim.lsp.buf_get_clients()) do
|
2021-11-22 09:52:24 -05:00
|
|
|
for _, folder in pairs(client.workspace_folders or {}) do
|
2020-11-25 12:07:02 -08:00
|
|
|
table.insert(workspace_folders, folder.name)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return workspace_folders
|
|
|
|
|
end
|
|
|
|
|
|
2020-12-08 21:09:33 -05:00
|
|
|
--- Add the folder at path to the workspace folders. If {path} is
|
|
|
|
|
--- not provided, the user will be prompted for a path using |input()|.
|
2020-11-25 12:07:02 -08:00
|
|
|
function M.add_workspace_folder(workspace_folder)
|
2021-06-25 14:23:53 +02:00
|
|
|
workspace_folder = workspace_folder or npcall(vfn.input, "Workspace Folder: ", vfn.expand('%:p:h'), 'dir')
|
2020-11-25 12:07:02 -08:00
|
|
|
vim.api.nvim_command("redraw")
|
|
|
|
|
if not (workspace_folder and #workspace_folder > 0) then return end
|
|
|
|
|
if vim.fn.isdirectory(workspace_folder) == 0 then
|
|
|
|
|
print(workspace_folder, " is not a valid directory")
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
local params = util.make_workspace_params({{uri = vim.uri_from_fname(workspace_folder); name = workspace_folder}}, {{}})
|
2021-07-11 11:34:26 -07:00
|
|
|
for _, client in pairs(vim.lsp.buf_get_clients()) do
|
2020-11-25 12:07:02 -08:00
|
|
|
local found = false
|
2021-11-22 09:52:24 -05:00
|
|
|
for _, folder in pairs(client.workspace_folders or {}) do
|
2020-11-25 12:07:02 -08:00
|
|
|
if folder.name == workspace_folder then
|
|
|
|
|
found = true
|
|
|
|
|
print(workspace_folder, "is already part of this workspace")
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if not found then
|
|
|
|
|
vim.lsp.buf_notify(0, 'workspace/didChangeWorkspaceFolders', params)
|
2021-11-22 09:52:24 -05:00
|
|
|
if not client.workspace_folders then
|
|
|
|
|
client.workspace_folders = {}
|
2021-11-11 01:15:59 -08:00
|
|
|
end
|
2021-11-22 09:52:24 -05:00
|
|
|
table.insert(client.workspace_folders, params.event.added[1])
|
2020-11-25 12:07:02 -08:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-12-08 21:09:33 -05:00
|
|
|
--- Remove the folder at path from the workspace folders. If
|
|
|
|
|
--- {path} is not provided, the user will be prompted for
|
|
|
|
|
--- a path using |input()|.
|
2020-11-25 12:07:02 -08:00
|
|
|
function M.remove_workspace_folder(workspace_folder)
|
|
|
|
|
workspace_folder = workspace_folder or npcall(vfn.input, "Workspace Folder: ", vfn.expand('%:p:h'))
|
|
|
|
|
vim.api.nvim_command("redraw")
|
|
|
|
|
if not (workspace_folder and #workspace_folder > 0) then return end
|
|
|
|
|
local params = util.make_workspace_params({{}}, {{uri = vim.uri_from_fname(workspace_folder); name = workspace_folder}})
|
2021-07-11 11:34:26 -07:00
|
|
|
for _, client in pairs(vim.lsp.buf_get_clients()) do
|
2021-11-22 09:52:24 -05:00
|
|
|
for idx, folder in pairs(client.workspace_folders) do
|
2020-11-25 12:07:02 -08:00
|
|
|
if folder.name == workspace_folder then
|
|
|
|
|
vim.lsp.buf_notify(0, 'workspace/didChangeWorkspaceFolders', params)
|
2021-11-22 09:52:24 -05:00
|
|
|
client.workspace_folders[idx] = nil
|
2020-11-25 12:07:02 -08:00
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
print(workspace_folder, "is not currently part of the workspace")
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-02 07:09:17 -04:00
|
|
|
--- Lists all symbols in the current workspace in the quickfix window.
|
|
|
|
|
---
|
2020-08-19 18:17:08 +02:00
|
|
|
--- The list is filtered against {query}; if the argument is omitted from the
|
|
|
|
|
--- call, the user is prompted to enter a string on the command line. An empty
|
|
|
|
|
--- string means no filtering is done.
|
|
|
|
|
---
|
2021-08-22 14:55:28 -06:00
|
|
|
---@param query (string, optional)
|
2020-05-02 17:56:05 +02:00
|
|
|
function M.workspace_symbol(query)
|
|
|
|
|
query = query or npcall(vfn.input, "Query: ")
|
|
|
|
|
local params = {query = query}
|
|
|
|
|
request('workspace/symbol', params)
|
|
|
|
|
end
|
|
|
|
|
|
2021-02-24 17:23:47 +01:00
|
|
|
--- Send request to the server to resolve document highlights for the current
|
|
|
|
|
--- text document position. This request can be triggered by a key mapping or
|
|
|
|
|
--- by events such as `CursorHold`, eg:
|
2020-02-26 20:10:16 +01:00
|
|
|
---
|
|
|
|
|
--- <pre>
|
|
|
|
|
--- vim.api.nvim_command [[autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()]]
|
|
|
|
|
--- vim.api.nvim_command [[autocmd CursorHoldI <buffer> lua vim.lsp.buf.document_highlight()]]
|
|
|
|
|
--- vim.api.nvim_command [[autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()]]
|
|
|
|
|
--- </pre>
|
2021-02-24 17:23:47 +01:00
|
|
|
---
|
|
|
|
|
--- Note: Usage of |vim.lsp.buf.document_highlight()| requires the following highlight groups
|
|
|
|
|
--- to be defined or you won't be able to see the actual highlights.
|
|
|
|
|
--- |LspReferenceText|
|
|
|
|
|
--- |LspReferenceRead|
|
|
|
|
|
--- |LspReferenceWrite|
|
2020-02-26 20:10:16 +01:00
|
|
|
function M.document_highlight()
|
|
|
|
|
local params = util.make_position_params()
|
|
|
|
|
request('textDocument/documentHighlight', params)
|
|
|
|
|
end
|
|
|
|
|
|
2020-08-19 18:17:08 +02:00
|
|
|
--- Removes document highlights from current buffer.
|
|
|
|
|
---
|
2020-02-26 20:10:16 +01:00
|
|
|
function M.clear_references()
|
|
|
|
|
util.buf_clear_references()
|
|
|
|
|
end
|
|
|
|
|
|
2021-09-28 23:04:01 +02:00
|
|
|
|
|
|
|
|
---@private
|
|
|
|
|
--
|
|
|
|
|
--- This is not public because the main extension point is
|
|
|
|
|
--- vim.ui.select which can be overridden independently.
|
|
|
|
|
---
|
|
|
|
|
--- Can't call/use vim.lsp.handlers['textDocument/codeAction'] because it expects
|
|
|
|
|
--- `(err, CodeAction[] | Command[], ctx)`, but we want to aggregate the results
|
|
|
|
|
--- from multiple clients to have 1 single UI prompt for the user, yet we still
|
|
|
|
|
--- need to be able to link a `CodeAction|Command` to the right client for
|
|
|
|
|
--- `codeAction/resolve`
|
|
|
|
|
local function on_code_action_results(results, ctx)
|
|
|
|
|
local action_tuples = {}
|
|
|
|
|
for client_id, result in pairs(results) do
|
|
|
|
|
for _, action in pairs(result.result or {}) do
|
|
|
|
|
table.insert(action_tuples, { client_id, action })
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if #action_tuples == 0 then
|
|
|
|
|
vim.notify('No code actions available', vim.log.levels.INFO)
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
---@private
|
|
|
|
|
local function apply_action(action, client)
|
|
|
|
|
if action.edit then
|
|
|
|
|
util.apply_workspace_edit(action.edit)
|
|
|
|
|
end
|
|
|
|
|
if action.command then
|
|
|
|
|
local command = type(action.command) == 'table' and action.command or action
|
2021-11-01 03:14:59 -07:00
|
|
|
local fn = client.commands[command.command] or vim.lsp.commands[command.command]
|
2021-09-28 23:04:01 +02:00
|
|
|
if fn then
|
|
|
|
|
local enriched_ctx = vim.deepcopy(ctx)
|
|
|
|
|
enriched_ctx.client_id = client.id
|
2021-11-01 20:29:50 +07:00
|
|
|
fn(command, enriched_ctx)
|
2021-09-28 23:04:01 +02:00
|
|
|
else
|
|
|
|
|
M.execute_command(command)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
---@private
|
|
|
|
|
local function on_user_choice(action_tuple)
|
|
|
|
|
if not action_tuple then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
-- textDocument/codeAction can return either Command[] or CodeAction[]
|
|
|
|
|
--
|
|
|
|
|
-- CodeAction
|
|
|
|
|
-- ...
|
|
|
|
|
-- edit?: WorkspaceEdit -- <- must be applied before command
|
|
|
|
|
-- command?: Command
|
|
|
|
|
--
|
|
|
|
|
-- Command:
|
|
|
|
|
-- title: string
|
|
|
|
|
-- command: string
|
|
|
|
|
-- arguments?: any[]
|
|
|
|
|
--
|
|
|
|
|
local client = vim.lsp.get_client_by_id(action_tuple[1])
|
|
|
|
|
local action = action_tuple[2]
|
|
|
|
|
if not action.edit
|
|
|
|
|
and client
|
|
|
|
|
and type(client.resolved_capabilities.code_action) == 'table'
|
|
|
|
|
and client.resolved_capabilities.code_action.resolveProvider then
|
|
|
|
|
|
|
|
|
|
client.request('codeAction/resolve', action, function(err, resolved_action)
|
|
|
|
|
if err then
|
|
|
|
|
vim.notify(err.code .. ': ' .. err.message, vim.log.levels.ERROR)
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
apply_action(resolved_action, client)
|
|
|
|
|
end)
|
|
|
|
|
else
|
|
|
|
|
apply_action(action, client)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
vim.ui.select(action_tuples, {
|
|
|
|
|
prompt = 'Code actions:',
|
2021-10-31 20:15:09 -04:00
|
|
|
kind = 'codeaction',
|
2021-09-28 23:04:01 +02:00
|
|
|
format_item = function(action_tuple)
|
|
|
|
|
local title = action_tuple[2].title:gsub('\r\n', '\\r\\n')
|
|
|
|
|
return title:gsub('\n', '\\n')
|
|
|
|
|
end,
|
|
|
|
|
}, on_user_choice)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
2021-07-18 10:58:35 +02:00
|
|
|
--- Requests code actions from all clients and calls the handler exactly once
|
|
|
|
|
--- with all aggregated results
|
2021-08-22 14:55:28 -06:00
|
|
|
---@private
|
2021-07-18 10:58:35 +02:00
|
|
|
local function code_action_request(params)
|
|
|
|
|
local bufnr = vim.api.nvim_get_current_buf()
|
|
|
|
|
local method = 'textDocument/codeAction'
|
|
|
|
|
vim.lsp.buf_request_all(bufnr, method, params, function(results)
|
2021-09-28 23:04:01 +02:00
|
|
|
on_code_action_results(results, { bufnr = bufnr, method = method, params = params })
|
2021-07-18 10:58:35 +02:00
|
|
|
end)
|
|
|
|
|
end
|
|
|
|
|
|
2021-09-26 23:40:28 +02:00
|
|
|
--- Selects a code action available at the current
|
2020-08-19 18:17:08 +02:00
|
|
|
--- cursor position.
|
2021-08-22 14:55:28 -06:00
|
|
|
---
|
2021-09-26 23:40:28 +02:00
|
|
|
---@param context table|nil `CodeActionContext` of the LSP specification:
|
|
|
|
|
--- - diagnostics: (table|nil)
|
|
|
|
|
--- LSP `Diagnostic[]`. Inferred from the current
|
|
|
|
|
--- position if not provided.
|
|
|
|
|
--- - only: (string|nil)
|
|
|
|
|
--- LSP `CodeActionKind` used to filter the code actions.
|
|
|
|
|
--- Most language servers support values like `refactor`
|
|
|
|
|
--- or `quickfix`.
|
2021-08-22 14:55:28 -06:00
|
|
|
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_codeAction
|
2020-05-16 01:18:59 +02:00
|
|
|
function M.code_action(context)
|
|
|
|
|
validate { context = { context, 't', true } }
|
2021-09-26 23:40:28 +02:00
|
|
|
context = context or {}
|
|
|
|
|
if not context.diagnostics then
|
|
|
|
|
context.diagnostics = vim.lsp.diagnostic.get_line_diagnostics()
|
|
|
|
|
end
|
2020-05-16 01:18:59 +02:00
|
|
|
local params = util.make_range_params()
|
|
|
|
|
params.context = context
|
2021-07-18 10:58:35 +02:00
|
|
|
code_action_request(params)
|
2020-05-16 01:18:59 +02:00
|
|
|
end
|
|
|
|
|
|
2020-09-25 04:53:08 +09:00
|
|
|
--- Performs |vim.lsp.buf.code_action()| for a given range.
|
|
|
|
|
---
|
2021-09-26 23:40:28 +02:00
|
|
|
---
|
|
|
|
|
---@param context table|nil `CodeActionContext` of the LSP specification:
|
|
|
|
|
--- - diagnostics: (table|nil)
|
|
|
|
|
--- LSP `Diagnostic[]`. Inferred from the current
|
|
|
|
|
--- position if not provided.
|
|
|
|
|
--- - only: (string|nil)
|
|
|
|
|
--- LSP `CodeActionKind` used to filter the code actions.
|
|
|
|
|
--- Most language servers support values like `refactor`
|
|
|
|
|
--- or `quickfix`.
|
2021-08-22 14:55:28 -06:00
|
|
|
---@param start_pos ({number, number}, optional) mark-indexed position.
|
2020-09-25 04:53:08 +09:00
|
|
|
---Defaults to the start of the last visual selection.
|
2021-08-22 14:55:28 -06:00
|
|
|
---@param end_pos ({number, number}, optional) mark-indexed position.
|
2020-09-25 04:53:08 +09:00
|
|
|
---Defaults to the end of the last visual selection.
|
|
|
|
|
function M.range_code_action(context, start_pos, end_pos)
|
|
|
|
|
validate { context = { context, 't', true } }
|
2021-09-26 23:40:28 +02:00
|
|
|
context = context or {}
|
|
|
|
|
if not context.diagnostics then
|
|
|
|
|
context.diagnostics = vim.lsp.diagnostic.get_line_diagnostics()
|
|
|
|
|
end
|
2020-09-25 04:53:08 +09:00
|
|
|
local params = util.make_given_range_params(start_pos, end_pos)
|
|
|
|
|
params.context = context
|
2021-07-18 10:58:35 +02:00
|
|
|
code_action_request(params)
|
2020-09-25 04:53:08 +09:00
|
|
|
end
|
|
|
|
|
|
2020-08-19 18:17:08 +02:00
|
|
|
--- Executes an LSP server command.
|
|
|
|
|
---
|
2021-08-22 14:55:28 -06:00
|
|
|
---@param command A valid `ExecuteCommandParams` object
|
|
|
|
|
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_executeCommand
|
2020-05-16 01:18:59 +02:00
|
|
|
function M.execute_command(command)
|
|
|
|
|
validate {
|
|
|
|
|
command = { command.command, 's' },
|
|
|
|
|
arguments = { command.arguments, 't', true }
|
|
|
|
|
}
|
|
|
|
|
request('workspace/executeCommand', command)
|
|
|
|
|
end
|
|
|
|
|
|
2019-11-20 14:21:57 -08:00
|
|
|
return M
|
2019-11-20 16:16:36 -08:00
|
|
|
-- vim:sw=2 ts=2 et
|