Nvim 的 :help
頁面,使用 tree-sitter-vimdoc 解析器從 來源產生。
vim.lsp
,用於建立增強的 LSP 工具。-- Create an event handler for the FileType autocommand
vim.api.nvim_create_autocmd('FileType', {
-- This handler will fire when the buffer's 'filetype' is "python"
pattern = 'python',
callback = function(args)
vim.lsp.start({
name = 'my-server-name',
cmd = {'name-of-language-server-executable', '--option', 'arg1', 'arg2'},
-- Set the "root directory" to the parent directory of the file in the
-- current buffer (`args.buf`) that contains either a "setup.py" or a
-- "pyproject.toml" file. Files that share a root directory will reuse
-- the connection to the same LSP server.
root_dir = vim.fs.root(args.buf, {'setup.py', 'pyproject.toml'}),
})
end,
})
:checkhealth lsp
4.(可選)設定快捷鍵和自動命令以使用 LSP 功能。lsp-configCTRL-S
在插入模式中對應到 vim.lsp.buf.signature_help()vim.api.nvim_create_autocmd('LspAttach', {
callback = function(args)
-- Unset 'formatexpr'
vim.bo[args.buf].formatexpr = nil
-- Unset 'omnifunc'
vim.bo[args.buf].omnifunc = nil
-- Unmap K
vim.keymap.del('n', 'K', { buffer = args.buf })
end,
})
vim.api.nvim_create_autocmd('LspAttach', {
callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
if client.supports_method('textDocument/implementation') then
-- Create a keymap for vim.lsp.buf.implementation
end
if client.supports_method('textDocument/completion') then
-- Enable auto-completion
vim.lsp.completion.enable(true, client.id, args.buf, {autotrigger = true})
end
if client.supports_method('textDocument/formatting') then
-- Format the current buffer on save
vim.api.nvim_create_autocmd('BufWritePre', {
buffer = args.buf,
callback = function()
vim.lsp.buf.format({bufnr = args.buf, id = client.id})
end,
})
end
end,
})
:lua =vim.lsp.get_clients()[1].server_capabilities
預設提供的完整功能清單可以在 lsp-buf 中找到。:lua vim.lsp.stop_client(vim.lsp.get_clients())
:edit
:verbose set omnifunc?
async
參數,並將值設定為 false。例如,程式碼格式化" Auto-format *.rs (rust) files prior to saving them
" (async = false is the default for format)
autocmd BufWritePre *.rs lua vim.lsp.buf.format({ async = false })
vim.lsp.buf_…
函數對附加到給定緩衝區的所有 LSP 客戶端執行操作。lsp-buf:lua vim.print(vim.tbl_keys(vim.lsp.handlers))
'callHierarchy/incomingCalls'
'callHierarchy/outgoingCalls'
'textDocument/codeAction'
'textDocument/completion'
'textDocument/declaration'
'textDocument/definition'
'textDocument/diagnostic'
'textDocument/documentHighlight'
'textDocument/documentSymbol'
'textDocument/formatting'
'textDocument/hover'
'textDocument/implementation'
'textDocument/inlayHint'
'textDocument/prepareTypeHierarchy'
'textDocument/publishDiagnostics'
'textDocument/rangeFormatting'
'textDocument/rangesFormatting'
'textDocument/references'
'textDocument/rename'
'textDocument/semanticTokens/full'
'textDocument/semanticTokens/full/delta'
'textDocument/signatureHelp'
'textDocument/typeDefinition*'
'typeHierarchy/subtypes'
'typeHierarchy/supertypes'
'window/logMessage'
'window/showMessage'
'window/showDocument'
'window/showMessageRequest'
'workspace/applyEdit'
'workspace/configuration'
'workspace/executeCommand'
'workspace/inlayHint/refresh'
'workspace/symbol'
'workspace/workspaceFolders'
function(err, result, ctx)
{err}
(table|nil
) 錯誤資訊字典,如果請求完成,則為 nil
。{ctx}
(table
) 與處理程式相關聯的呼叫狀態的表格,包含以下鍵{bufnr}
(Buffer
) 緩衝區控制代碼。{params}
(table|nil
) 請求參數表格。result, err
,其中 err
的形狀類似於 RPC 錯誤{ code, message, data? }
vim.lsp.handlers
是一個全域表格,其中包含 lsp-method 名稱到 lsp-handlers 的預設對應。vim.lsp.handlers['textDocument/publishDiagnostics'] = my_custom_diagnostics_handler
vim.lsp.start {
..., -- Other configuration omitted.
handlers = {
['textDocument/publishDiagnostics'] = my_custom_server_definition
},
}
vim.lsp.buf_request_all(
0,
'textDocument/publishDiagnostics',
my_request_params,
my_handler
)
vim.lsp.protocol
定義了 LSP 規範所規定的常數,以及用於建立與協定相關的物件的輔助函數。 https://github.com/microsoft/language-server-protocol/raw/gh-pages/_specifications/specification-3-14.mdvim.lsp.protocol.ErrorCodes
允許依編號或名稱反向查找vim.lsp.protocol.TextDocumentSyncKind.Full == 1
vim.lsp.protocol.TextDocumentSyncKind[1] == "Full"
type
,例如「function」或「variable」,以及 0 個或多個 modifier
,例如「readonly」或「deprecated」。標準的類型和修飾符在此處說明:https://microsoft.github.io/language-server-protocol/specification/#textDocument_semanticTokens。LSP 伺服器也可以使用非規範的類型和修飾符。@lsp.type.<type>.<ft>
用於類型@lsp.mod.<mod>.<ft>
用於每個修飾符@lsp.typemod.<type>.<mod>.<ft>
用於每個修飾符。使用 :Inspect 來檢視特定 token 的醒目提示。使用 :hi 或 nvim_set_hl() 來變更語義醒目提示的外觀。hi @lsp.type.function guifg=Yellow " function names are yellow
hi @lsp.type.variable.lua guifg=Green " variables in lua are green
hi @lsp.mod.deprecated gui=strikethrough " deprecated is crossed out
hi @lsp.typemod.function.async guifg=Blue " async functions are blue
.semantic_tokens
是 @lsp.type.*
醒目提示的優先順序。@lsp.mod.*
和 @lsp.typemod.*
醒目提示的優先順序分別高一級和兩級。-- Hide semantic highlights for functions
vim.api.nvim_set_hl(0, '@lsp.type.function', {})
-- Hide all semantic highlights
for _, group in ipairs(vim.fn.getcompletion("@lsp", "highlight")) do
vim.api.nvim_set_hl(0, group, {})
end
vim.api.nvim_create_autocmd('LspDetach', {
callback = function(args)
-- Get the detaching client
local client = vim.lsp.get_client_by_id(args.data.client_id)
-- Remove the autocommand to format the buffer on save, if it exists
if client.supports_method('textDocument/formatting') then
vim.api.nvim_clear_autocmds({
event = 'BufWritePre',
buffer = args.buf,
})
end
end,
})
vim.api.nvim_create_autocmd('LspNotify', {
callback = function(args)
local bufnr = args.buf
local client_id = args.data.client_id
local method = args.data.method
local params = args.data.params
-- do something with the notification
if method == 'textDocument/...' then
update_buffer(bufnr)
end
end,
})
progress
環形緩衝區輪詢通知,或使用 vim.lsp.status() 來取得彙總訊息。pattern
會設定為 kind
(begin
、report
或 end
其中之一)。data
表格,其中包含 client_id
和 params
屬性。params
將包含伺服器傳送的要求參數(請參閱 lsp.ProgressParams
)。autocmd LspProgress * redrawstatus
pending
、complete
或 cancel
,並作為傳遞給回呼函式的 "data" 表格上的 {type}
傳送。{type}
== pending
) 以及當 LSP 伺服器回應時 ({type}
== complete
) 會觸發此事件。如果使用 client.cancel_request(request_id)
要求取消,則此事件將會以 {type}
== cancel
觸發。{requests}
,以取得 {request}
值的詳細資訊。如果要求類型為 complete
,則在呼叫事件的回呼後,要求會立即從客戶端的待處理要求表格中刪除。範例vim.api.nvim_create_autocmd('LspRequest', {
callback = function(args)
local bufnr = args.buf
local client_id = args.data.client_id
local request_id = args.data.request_id
local request = args.data.request
if request.type == 'pending' then
-- do something with pending requests
track_pending(client_id, bufnr, request_id, request)
elseif request.type == 'cancel' then
-- do something with pending cancel requests
track_canceling(client_id, bufnr, request_id, request)
elseif request.type == 'complete' then
-- do something with finished requests. this pending
-- request entry is about to be removed since it is complete
track_finish(client_id, bufnr, request_id, request)
end
end,
})
vim.api.nvim_create_autocmd('LspTokenUpdate', {
callback = function(args)
local token = args.data.token
if token.type == 'variable' and not token.modifiers.readonly then
vim.lsp.semantic_tokens.highlight_token(
token, args.buf, args.data.client_id, 'MyMutableVariableHighlight'
)
end
end,
})
{bufnr}
, {client_id}
) vim.lsp.buf_attach_client()textDocument/did…
通知。{bufnr}
(integer
) 緩衝區控制代碼,或 0 表示目前{client_id}
(integer
) 客戶端 IDboolean
) 成功。如果客戶端成功附加則為 true
;否則為 false
{bufnr}
, {client_id}
) vim.lsp.buf_detach_client(){bufnr}
(integer
) 緩衝區控制代碼,或 0 表示目前{client_id}
(integer
) 客戶端 ID{bufnr}
(integer
) 緩衝區控制代碼,或 0 表示目前{client_id}
(integer
) 客戶端 ID{bufnr}
(integer?
) 緩衝區的數字{method}
(string
) 要求方法名稱{params}
(any
) 要傳送到伺服器的引數boolean
) 成功。如果任何客戶端傳回 true 則為 true;否則為 false{bufnr}
, {method}
, {params}
, {handler}
) 為附加到緩衝區的所有活動客戶端傳送非同步要求,並使用組合的結果執行 handler
回呼。{bufnr}
(integer
) 緩衝區控制代碼,或 0 表示目前。{method}
(string
) LSP 方法名稱{params}
(table|(fun(client: vim.lsp.Client, bufnr: integer): table?)?
) 要傳送到伺服器的參數。也可以作為函式傳遞,該函式會傳回參數表格,以用於參數特定於客戶端的情況。{handler}
(function
) 在所有要求完成後呼叫的處理常式。伺服器結果會以 client_id:result
對應傳遞。function
) cancel 取消所有要求的函式。{bufnr}
, {method}
, {params}
, {timeout_ms}
) 將要求傳送到所有伺服器,並等待所有伺服器的回應。{timeout_ms}
。{bufnr}
(integer
) 緩衝區控制代碼,或 0 表示目前。{method}
(string
) LSP 方法名稱{params}
(table?
) 要傳送到伺服器的參數{timeout_ms}
(integer?
,預設值:1000
) 等待結果的最長時間 (以毫秒為單位)。table<integer, {error: lsp.ResponseError?, result: any}>?
) 客戶端 ID:請求結果的結果映射表。(string?
)若發生逾時、取消或錯誤,err
會是一個描述失敗原因的字串,而 result
則為 nil。{client_id}
(integer
)boolean
) 若客戶端已停止則為 true,否則為 false。workspace/executeCommand
通過 LSP 伺服器執行該命令。Command
:命令標題:字串、命令:字串、參數?:任何型別的陣列。ctx
。setlocal formatexpr=v:lua.vim.lsp.formatexpr()
設定,或(更常見的情況)在 on_attach
中通過 vim.bo[bufnr].formatexpr = 'v:lua.vim.lsp.formatexpr(#{timeout_ms:250})'
設定。{opts}
(table?
) 包含以下欄位的表格{timeout_ms}
(integer
,預設值:500 毫秒) 格式化請求的逾時時間。{client_id}
(integer
) 客戶端 IDinteger[]
) 緩衝區 ID 的列表{client_id}
) vim.lsp.get_client_by_id(){client_id}
(integer
) 客戶端 IDvim.lsp.Client?
) 客戶端 RPC 物件{filter}
(table?
) 用於篩選傳回的客戶端的鍵值對。{id}
(integer
) 僅傳回具有指定 ID 的客戶端{bufnr}
(integer
) 僅傳回附加到此緩衝區的客戶端{name}
(string
) 僅傳回具有指定名稱的客戶端{method}
(string
) 僅傳回支援指定方法的客戶端string
) 日誌檔的路徑{findstart}
(integer
) 0 或 1,決定行為{base}
(integer
) findstart=0,要比對的文字integer|table
) 由 {findstart}
決定lsp.log_levels
來反向查找。{level}
(integer|string
) 不區分大小寫的等級名稱或編號{config}
, {opts}
) vim.lsp.start()name
和 root_dir
的正在執行的客戶端,則重複使用它。將目前的緩衝區附加到客戶端。vim.lsp.start({
name = 'my-server-name',
cmd = {'name-of-language-server-executable'},
root_dir = vim.fs.root(0, {'pyproject.toml', 'setup.py'}),
})
name
LSP 客戶端的任意名稱。每個語言伺服器應該是唯一的。cmd
命令字串陣列或函式,詳情請見 vim.lsp.start_client()。root_dir
專案根目錄的路徑。預設情況下,這用於決定是否應重複使用現有的客戶端。上面的範例使用 vim.fs.root() 通過從當前目錄向上遍歷檔案系統,直到找到 pyproject.toml
或 setup.py
檔案來偵測根目錄。workspace_folders
{ uri:string, name: string }
表格的列表,指定語言伺服器使用的專案根目錄資料夾。如果 nil
,為了方便起見,該屬性從 root_dir
派生。ftplugin/<filetype_name>.lua
中(請參閱 ftplugin-name){opts}
(table?
) 可選的關鍵字參數{reuse_client}
(fun(client: vim.lsp.Client, config: vim.lsp.ClientConfig): boolean
) 用於決定是否應重複使用客戶端的述詞。在所有正在執行的客戶端上使用。預設的實作方式是在名稱和 root_dir 相符時重複使用客戶端。{bufnr}
(integer
) 若啟動或重複使用客戶端,要附加到的緩衝區控制代碼(目前為 0)。{silent}
(boolean
) 如果 LSP 伺服器無法啟動,則抑制錯誤報告(預設為 false)。integer?
) client_idinteger?
) client_id vim.lsp.get_client_by_id() 注意:客戶端可能尚未完全初始化。請使用 on_init
在客戶端初始化後執行任何動作。(string?
)錯誤訊息(如果有的話)string
){force}
(boolean?
) 強制關閉table[]
) 標籤 符合標籤的列表{id}
(integer
) 分配給客戶端的 ID。{name}
(string
) 如果在建立時指定了名稱,則會使用該名稱。否則,它只是客戶端 ID。這用於日誌和訊息。{offset_encoding}
(string
) 用於與伺服器通訊的編碼。您可以在將文字傳送到伺服器之前,在 config
的 on_init
方法中修改此項。{requests}
(table<integer,{ type: string, bufnr: integer, method: string}>
) 目前發送到伺服器的待處理請求。項目是鍵值對,其中鍵是請求 ID,而值是一個包含 type
、bufnr
和 method
鍵值對的表格。type
對於作用中的請求為「pending」,對於取消請求則為「cancel」。當從伺服器收到回覆時,在執行 LspRequest autocmd 時,它會短暫地為「complete」。{server_capabilities}
(lsp.ServerCapabilities?
) 從伺服器在 initialize
時傳送的回應,描述伺服器的功能。{progress}
(vim.lsp.Client.Progress
) 包含伺服器傳送的進度訊息的循環緩衝區 (vim.ringbuf())。請參閱 vim.lsp.Client.Progress。{initialized}
(true?
){workspace_folders}
(lsp.WorkspaceFolder[]?
) 伺服器啟動時在客戶端中設定的工作區資料夾。僅當客戶端支援工作區資料夾時,此屬性才可用。如果客戶端支援工作區資料夾,但未設定任何工作區資料夾,則可以為 null
。{root_dir}
(string?
){attached_buffers}
(table<integer,true>
){commands}
(table<string,fun(command: lsp.Command, ctx: table)>
) 從命令名稱到函式的表格。如果任何 LSP 動作(程式碼動作、程式碼透鏡等)觸發該命令,則會呼叫該函式。客戶端命令優先於全域命令註冊表。{settings}
(table
) 包含語言伺服器特定設定的映射表。如果透過 workspace/configuration
請求,這些設定會回傳給語言伺服器。鍵值區分大小寫。{flags}
(table
) 包含客戶端旗標的表格。目前(實驗性)的旗標有:{allow_incremental_sync}
(boolean
, 預設值:true
) 允許對緩衝區編輯使用增量同步。{debounce_text_changes}
(integer
, 預設值:150
) 以毫秒為單位,延遲 didChange
通知到伺服器的時間。如果為 nil
,則不執行延遲。{exit_timeout}
(integer|false
, 預設值:false
) 在發送 "shutdown" 請求後,等待伺服器乾淨關閉的毫秒數,之後發送 kill -15。如果設定為 false,nvim 會在向伺服器發送 "shutdown" 請求後立即退出。{get_language_id}
(fun(bufnr: integer, filetype: string): string
){capabilities}
(lsp.ClientCapabilities
) 客戶端(編輯器或工具)提供的功能。{dynamic_capabilities}
(lsp.DynamicCapabilities
){request}
(fun(method: string, params: table?, handler: lsp.Handler?, bufnr: integer?): boolean, integer?
) 向伺服器發送請求。這是對 {client.rpc.request}
的一個精簡封裝,帶有一些額外的檢查。如果未指定 {handler}
且沒有相應的全局處理程式,則會發生錯誤。傳回值:{status}
、{client_id}
。{status}
是一個布林值,指示通知是否成功。如果為 false
,則它始終為 false
(客戶端已關閉)。如果 {status}
為 true
,則該函數會將 {request_id}
作為第二個結果傳回。您可以使用它搭配 client.cancel_request(request_id)
來取消請求。{request_sync}
(fun(method: string, params: table?, timeout_ms: integer?, bufnr: integer): {err: lsp.ResponseError?, result:any}?, string?
) err # 一個字典{notify}
(fun(method: string, params: table?): boolean
) 向 LSP 伺服器發送通知。傳回值:一個布林值,指示通知是否成功。如果為 false,則它始終為 false(客戶端已關閉)。{cancel_request}
(fun(id: integer): boolean
) 取消具有指定請求 ID 的請求。傳回值:與 notify()
相同。{stop}
(fun(force?: boolean)
) 停止客戶端,可選擇強制停止。預設情況下,它只會要求伺服器關閉而不強制。如果您請求停止一個先前已請求關閉的客戶端,它會自動升級並強制關閉。{on_attach}
(fun(bufnr: integer)
) 如果定義了,則從客戶端的設定執行 on_attach 函數。對於緩衝區本機設定很有用。{supports_method}
(fun(method: string, opts?: {bufnr: integer?}): boolean
) 檢查客戶端是否支援給定的方法。對於未知的非規格方法,始終傳回 true。{opts}
是一個可選的 {bufnr?: integer}
表格。某些語言伺服器的功能可能特定於檔案。{is_stopped}
(fun(): boolean
) 檢查客戶端是否已停止。傳回值:如果客戶端完全停止,則為 true。{exec_cmd}
(fun(self: vim.lsp.Client, command: lsp.Command, context: {bufnr?: integer}?, handler: lsp.Handler?)
) 執行 lsp 命令,可以透過客戶端命令函數(如果可用)或透過 workspace/executeCommand(如果伺服器支援){pending}
(table<lsp.ProgressToken,lsp.LSPAny>
){cmd}
(string[]|fun(dispatchers: vim.lsp.rpc.Dispatchers): vim.lsp.rpc.PublicClient
) 啟動語言伺服器的命令字串 [](視為 jobstart() 中的命令,必須是絕對路徑或位於 $PATH
中,像 "~" 這樣的 shell 結構不會被展開),或建立 RPC 客戶端的函數。函數接收一個 dispatchers
表格,並傳回一個包含成員函數 request
、notify
、is_closing
和 terminate
的表格。請參閱 vim.lsp.rpc.request()、vim.lsp.rpc.notify()。對於 TCP,有一個內建的 RPC 客戶端工廠:vim.lsp.rpc.connect(){cmd_cwd}
(string
, 預設值:cwd) 啟動 cmd
進程的目錄。與 root_dir
無關。{cmd_env}
(table
) 在產生時傳遞給 LSP 的環境旗標。必須使用表格指定。非字串值會被強制轉換為字串。範例{ PORT = 8080; HOST = "0.0.0.0"; }
{detached}
(boolean
, 預設值:true) 將伺服器進程守護化,使其在與 Nvim 不同的進程群組中執行。Nvim 會在退出時關閉進程,但如果 Nvim 無法乾淨退出,可能會留下孤立的伺服器進程。{workspace_folders}
(lsp.WorkspaceFolder[]
) 傳遞給語言伺服器的工作區資料夾清單。為了向後兼容,rootUri 和 rootPath 將從此清單中的第一個工作區資料夾衍生。請參閱 LSP 規格中的 workspaceFolders
。{capabilities}
(lsp.ClientCapabilities
) 覆蓋 vim.lsp.protocol.make_client_capabilities() 所定義預設功能的映射,在初始化時傳遞給語言伺服器。提示:使用 make_client_capabilities() 並修改其結果。{commands}
(table<string,fun(command: lsp.Command, ctx: table)>
) 將客戶端命令字串映射到使用者定義函數的表格。傳遞給 start_client 的命令優先於全局命令註冊表。每個鍵必須是唯一的命令名稱,值是一個函數,如果任何 LSP 動作(程式碼動作、程式碼鏡頭...)觸發命令,則會呼叫該函數。{init_options}
(table
) 作為 initializationOptions
在初始化請求中傳遞的值。請參閱 LSP 規格中的 initialize
。{name}
(string
, 預設值:client-id) 在記錄訊息中的名稱。{get_language_id}
(fun(bufnr: integer, filetype: string): string
) 語言 ID 作為字串。預設為檔案類型。{offset_encoding}
('utf-8'|'utf-16'|'utf-32'
) LSP 伺服器期望的編碼。客戶端不驗證這是否正確。{on_error}
(fun(code: integer, err: string)
) 當客戶端操作拋出錯誤時呼叫的回調函數。code
是一個描述錯誤的數字。根據錯誤類型,可能會傳遞其他參數。請參閱 vim.lsp.rpc.client_errors
以取得可能的錯誤。使用 vim.lsp.rpc.client_errors[code]
來取得人類可讀的名稱。{before_init}
(fun(params: lsp.InitializeParams, config: vim.lsp.ClientConfig)
) 在 LSP "initialize" 階段之前呼叫的回調函數,其中 params
包含發送給伺服器的參數,而 config
是傳遞給 vim.lsp.start_client() 的設定。您可以使用此函數在傳送參數之前修改參數。{on_init}
(elem_or_list<fun(client: vim.lsp.Client, initialize_result: lsp.InitializeResult)>
) 在 LSP "initialize" 之後呼叫的回調函數,其中 result
是一個包含 capabilities
以及伺服器可能發送的任何其他內容的表格。例如,如果向 clangd 發送了 capabilities.offsetEncoding
,它會發送 initialize_result.offsetEncoding
。您只能在此處修改 client.offset_encoding
,才能發送任何通知。{on_exit}
(elem_or_list<fun(code: integer, signal: integer, client_id: integer)>
) 在客戶端退出時呼叫的回調函數。{on_attach}
(elem_or_list<fun(client: vim.lsp.Client, bufnr: integer)>
) 當客戶端附加到緩衝區時呼叫的回調函數。{trace}
('off'|'messages'|'verbose'
, 預設值:"off") 直接在初始化請求中傳遞給語言伺服器。無效/空值將會{flags}
(table
) 包含客戶端旗標的表格。目前(實驗性)的旗標有:{allow_incremental_sync}
(boolean
, 預設值:true
) 允許對緩衝區編輯使用增量同步。{debounce_text_changes}
(integer
, 預設值:150
) 以毫秒為單位,延遲 didChange
通知到伺服器的時間。如果為 nil
,則不執行延遲。{exit_timeout}
(integer|false
, 預設值:false
) 在發送 "shutdown" 請求後,等待伺服器乾淨關閉的毫秒數,之後發送 kill -15。如果設定為 false,nvim 會在向伺服器發送 "shutdown" 請求後立即退出。{root_dir}
(string
) LSP 伺服器在初始化時將其 workspaceFolders、rootUri 和 rootPath 基於的目錄。{command}
, {context}
, {handler}
) Client:exec_cmd(){command}
(lsp.Command
){context}
({bufnr?: integer}?
){handler}
(lsp.Handler?
) 僅在伺服器命令時呼叫{on_list}
(fun(t: vim.lsp.LocationOpts.OnList)
) 取代預設處理程式的列表處理程式。針對任何非空結果呼叫。此表格可以與 setqflist() 或 setloclist() 一起使用。例如local function on_list(options)
vim.fn.setqflist({}, ' ', options)
vim.cmd.cfirst()
end
vim.lsp.buf.definition({ on_list = on_list })
vim.lsp.buf.references(nil, { on_list = on_list })
vim.lsp.buf.definition({ loclist = true })
vim.lsp.buf.references(nil, { loclist = true })
{loclist}
(boolean
){reuse_win}
(boolean
) 如果緩衝區已開啟,則跳到現有視窗。{title}
(string
) 列表的標題。{silent}
(boolean
){silent}
(boolean
){workspace_folder}
) 將路徑上的資料夾新增到工作區資料夾。如果沒有提供 {path}
,則會提示使用者使用 input() 輸入路徑。{workspace_folder}
(string?
){opts}
(table?
) 包含以下欄位的表格{context}
(lsp.CodeActionContext
) 對應於 LSP 規格的 CodeActionContext
{diagnostics}
(table
) LSP Diagnostic[]
。如果未提供,則從目前位置推斷。{only}
(table
) 用於篩選程式碼動作的 LSP CodeActionKind
清單。大多數語言伺服器都支援諸如 refactor
或 quickfix
之類的值。{triggerKind}
(integer
) 請求程式碼動作的原因。{filter}
(fun(x: lsp.CodeAction|lsp.Command):boolean
) 接受 CodeAction
並傳回布林值的謂詞。{apply}
(boolean
) 當設定為 true
,且只有一個剩餘動作(在篩選之後)時,會在沒有使用者查詢的情況下套用動作。{range}
({start: integer[], end: integer[]}
) 應請求程式碼動作的範圍。如果在可視模式下,則預設為活動選取範圍。表格必須包含 start
和 end
鍵,其中包含使用類似標記的索引的 {row,col}
元組。請參閱 api-indexingCursorHold
等事件觸發,例如:autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
autocmd CursorHoldI <buffer> lua vim.lsp.buf.document_highlight()
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
{opts}
(table?
) 包含以下欄位的表格{formatting_options}
(table
) 可用於指定 FormattingOptions。某些未指定的選項將會自動從目前的 Nvim 選項衍生。請參閱 https://microsoft.github.io/language-server-protocol/specification/#formattingOptions{timeout_ms}
(integer
, 預設值: 1000
) 以毫秒為單位的時間,用於封鎖格式化請求。如果 async=true,則無效。{bufnr}
(integer
, 預設值: 目前緩衝區) 將格式化限制為附加到指定緩衝區的用戶端。{filter}
(fun(client: vim.lsp.Client): boolean?
) 用於篩選用戶端的謂詞。接收一個用戶端作為參數,且必須傳回布林值。符合謂詞的用戶端將會被包含。範例-- Never request typescript-language-server for formatting
vim.lsp.buf.format {
filter = function(client) return client.name ~= "tsserver" end
}
{async}
(boolean
, 預設值: false) 如果為 true,則此方法不會封鎖。在非同步格式化的同時編輯緩衝區可能會導致意外的變更。{id}
(integer
) 將格式化限制為 ID (client.id) 與此欄位符合的用戶端。{name}
(string
) 將格式化限制為名稱 (client.name) 與此欄位符合的用戶端。{range}
({start:[integer,integer],end:[integer, integer]}|{start:[integer,integer],end:[integer,integer]}[]
, 預設值: 視覺模式中的目前選取範圍,在其他模式中為 nil
,格式化整個緩衝區) 要格式化的範圍。表格必須包含 start
和 end
鍵,其中包含使用 (1,0) 索引的 {row,col}
元組。也可以是包含如上所述的 start
和 end
鍵的表格清單,在這種情況下,需要 textDocument/rangesFormatting
支援。{config}
) vim.lsp.buf.hover(){workspace_folder}
) 從工作區資料夾中移除路徑上的資料夾。如果未提供 {path}
,系統將使用 input() 提示使用者輸入路徑。{workspace_folder}
(string?
){opts}
(table?
) 其他選項{filter}
(fun(client: vim.lsp.Client): boolean?
) 用於篩選用戶端的謂詞。接收一個用戶端作為參數,且必須傳回布林值。符合謂詞的用戶端將會被包含。{name}
(string
) 將用於重新命名的用戶端限制為 client.name 與此欄位符合的用戶端。{bufnr}
(integer
) (預設值: 目前緩衝區){kind}
) vim.lsp.buf.typehierarchy(){kind}
("subtypes"|"supertypes"
){query}
進行篩選;如果從呼叫中省略參數,系統會提示使用者在命令列上輸入字串。空字串表示不進行篩選。{diagnostics}
(vim.Diagnostic[]
)lsp.Diagnostic[]
){client_id}
, {is_pull}
) 取得與 LSP 用戶端相關聯的診斷命名空間 vim.diagnostic,用於診斷{client_id}
(integer
) LSP 用戶端的 ID{is_pull}
(boolean?
) 命名空間是否用於提取或推送用戶端。預設為推送{_}
, {result}
, {ctx}
) 用於方法 "textDocument/diagnostic" 的 lsp-handler{result}
(lsp.DocumentDiagnosticReport
){ctx}
(lsp.HandlerContext
){_}
, {result}
, {ctx}
) 用於方法 "textDocument/publishDiagnostics" 的 lsp-handler{result}
(lsp.PublishDiagnosticsParams
){ctx}
(lsp.HandlerContext
){client_id}
(integer?
) 按 client_id 篩選。如果為 nil,則為所有用戶端{bufnr}
(integer?
) 按緩衝區篩選。如果為 nil,則為所有緩衝區;0 表示目前緩衝區{lenses}
(lsp.CodeLens[]?
) 要顯示的鏡頭{bufnr}
(integer
){client_id}
(integer
){bufnr}
(integer
) 緩衝區編號。0 可用於目前緩衝區。lsp.CodeLens[]
){err}
, {result}
, {ctx}
) vim.lsp.codelens.on_codelens()textDocument/codeLens
的 lsp-handler{err}
(lsp.ResponseError?
){result}
(lsp.CodeLens[]
){ctx}
(lsp.HandlerContext
)autocmd BufEnter,CursorHold,InsertLeave <buffer> lua vim.lsp.codelens.refresh({ bufnr = 0 })
{opts}
(table?
) 可選欄位{bufnr}
(integer?
) 按緩衝區篩選。如果為 nil,則為所有緩衝區;0 表示目前緩衝區{lenses}
(lsp.CodeLens[]?
) 要儲存的鏡頭{bufnr}
(integer
){client_id}
(integer
){autotrigger}
(boolean
) 預設值: false。如果為 true,則會根據伺服器的 triggerCharacters
自動觸發完成。{enable}
, {client_id}
, {bufnr}
, {opts}
) 在指定緩衝區中啟用或停用來自指定語言用戶端的完成功能。{enable}
(boolean
) True 為啟用,false 為停用{client_id}
(integer
) 用戶端 ID{bufnr}
(integer
) 緩衝區控制代碼,或 0 表示目前緩衝區is_enabled()
的反向值。vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled())
{enable}
(boolean?
) true/nil 表示啟用,false 表示停用{bufnr}
(integer?
) 緩衝區編號,或 0 表示目前緩衝區,或 nil 表示全部。local hint = vim.lsp.inlay_hint.get({ bufnr = 0 })[1] -- 0 for current buffer
local client = vim.lsp.get_client_by_id(hint.client_id)
local resp = client.request_sync('inlayHint/resolve', hint.inlay_hint, 100, 0)
local resolved_hint = assert(resp and resp.result, resp.err)
vim.lsp.util.apply_text_edits(resolved_hint.textEdits, 0, client.encoding)
location = resolved_hint.label[1].location
client.request('textDocument/hover', {
textDocument = { uri = location.uri },
position = location.range.start,
})
table[]
) 具有以下欄位的物件列表{bufnr}
(integer
){client_id}
(integer
){inlay_hint}
(lsp.InlayHint
)boolean
){bufnr}
(integer?
) 按緩衝區過濾。如果為 nil,則為所有緩衝區;如果為 0,則為目前緩衝區{bufnr}
, {row}
, {col}
) 傳回指定位置的語意符號。如果沒有引數呼叫,則傳回游標下的符號。{bufnr}
(integer?
) 緩衝區編號(0 表示目前緩衝區,預設值){row}
(integer?
) 位置列(預設為游標位置){col}
(integer?
) 位置欄(預設為游標位置)table?
) 位置上的符號列表。每個符號都具有以下欄位{token}
, {bufnr}
, {client_id}
, {hl_group}
, {opts}
) 高亮顯示語意符號。{bufnr}
(integer
) 要高亮顯示的緩衝區,或 0
表示目前緩衝區{hl_group}
(string
) 高亮顯示群組名稱{opts}
(table?
) 可選參數{priority}
(integer
, default: vim.hl.priorities.semantic_tokens + 3
) 套用 extmark 的優先順序。{bufnr}
, {client_id}
, {opts}
) vim.lsp.semantic_tokens.start()on_attach
回呼中,從您用戶端的 {server_capabilities}
中刪除 semanticTokensProvider 表格client.server_capabilities.semanticTokensProvider = nil
{bufnr}
(integer
) 緩衝區編號,或 0
表示目前緩衝區{opts}
(table?
) 可選的關鍵字參數start()
中設定的 LspDetach 自動指令自動呼叫,因此您只需要使用此函式來手動取消語意符號引擎,而無需完全將 LSP 用戶端從緩衝區分離。{height}
(integer
) 浮動視窗的高度{width}
(integer
) 浮動視窗的寬度{wrap}
(boolean
, default: true
) 自動換行長行{wrap_at}
(integer
) 在啟用自動換行時,用於計算高度的換行字元{max_width}
(integer
) 浮動視窗的最大寬度{max_height}
(integer
) 浮動視窗的最大高度{focus_id}
(string
) 如果開啟具有此 ID 的快顯視窗,則將焦點放在它上面{close_events}
(table
) 關閉浮動視窗的事件列表{focusable}
(boolean
, default: true
) 使浮動視窗可聚焦。{focus}
(boolean
, default: true
) 如果為 true
,且 {focusable}
也為 true
,則將焦點放在具有相同 {focus_id}
的現有浮動視窗上{offset_x}
(integer
) 要加到 col
的位移{offset_y}
(integer
) 要加到 row
的位移{border}
(string|(string|[string,string])[]
) 覆寫 border
{zindex}
(integer
) 覆寫 zindex
,預設為 50{title}
(string
){title_pos}
('left'|'center'|'right'
){relative}
('mouse'|'cursor'
) (default: 'cursor'
){anchor_bias}
('auto'|'above'|'below'
, default: 'auto'
) - 「auto」:根據游標哪一側具有較多行來放置視窗{text_document_edit}
, {index}
, {offset_encoding}
) 套用 TextDocumentEdit
,即單一文件變更的清單。{text_document_edit}
(lsp.TextDocumentEdit
){index}
(integer?
) 可選的編輯索引,如果是來自編輯清單(如果不是來自清單,則為 nil){offset_encoding}
('utf-8'|'utf-16'|'utf-32'?
){text_edits}
, {bufnr}
, {offset_encoding}
) 將文字編輯清單套用到緩衝區。{text_edits}
(lsp.TextEdit[]
){bufnr}
(integer
) 緩衝區 ID{offset_encoding}
('utf-8'|'utf-16'|'utf-32'
){workspace_edit}
, {offset_encoding}
) 套用 WorkspaceEdit
。{workspace_edit}
(lsp.WorkspaceEdit
){offset_encoding}
('utf-8'|'utf-16'|'utf-32'
) (必要){bufnr}
(integer?
) 緩衝區 ID{bufnr}
, {references}
, {offset_encoding}
) 顯示特定緩衝區的文件高亮顯示清單。{bufnr}
(integer
) 緩衝區 ID{references}
(lsp.DocumentHighlight[]
) 要高亮顯示的物件{offset_encoding}
('utf-8'|'utf-16'|'utf-32'
){buf}
, {row}
, {col}
, {offset_encoding}
) 傳回特定緩衝區中位置的 UTF-32 和 UTF-16 位移。{buf}
(integer
) 緩衝區編號(0 表示目前){row}
(integer
) 從 0 開始的行{col}
(integer
) 從 0 開始的行位元組位移{offset_encoding}
('utf-8'|'utf-16'|'utf-32'?
) 預設為 buf
的第一個用戶端的 offset_encoding
integer
) 緩衝區 {buf}
中行 {row}
欄 {col}
的字元 offset_encoding
索引{input}
, {contents}
) 將任何 MarkedString
| MarkedString[]
| MarkupContent
轉換為包含有效 markdown 的行清單。可用於填入 textDocument/hover
的懸停視窗、剖析 textDocument/signatureHelp
的結果,以及其他潛在用途。MarkupContent
且其種類為 plaintext
,則會傳回對應的值,而不會進行進一步修改。{input}
(lsp.MarkedString|lsp.MarkedString[]|lsp.MarkupContent
){contents}
(string[]?
) 要使用轉換後的行擴充的字串清單。預設值為 {}。string[]
) 使用轉換後的 markdown 行擴充。{signature_help}
, {ft}
, {triggers}
) 將 textDocument/signatureHelp
的回應轉換為 Markdown 行。{signature_help}
(lsp.SignatureHelp
) textDocument/SignatureHelp
的回應{ft}
(string?
) 作為標籤 Markdown 程式碼區塊 lang
的檔案類型{triggers}
(string[]?
) 來自 LSP 伺服器的觸發字元列表。用於更好地確定參數偏移量string[]?
) 已轉換的 Markdown 行。(Range4?
) 活動參數的突出顯示範圍{bufnr}
(integer?
) 緩衝區控制代碼,預設為目前緩衝區integer
) 縮排大小{locations}
, {offset_encoding}
) 傳回項目,這些項目具有正確計算的位元組位置並按排序順序排列,以便在快速修復和位置列表中顯示。user_data
欄位將包含計算它的原始 Location
或 LocationLink
。{locations}
(lsp.Location[]|lsp.LocationLink[]
){offset_encoding}
('utf-8'|'utf-16'|'utf-32'?
) 預設為緩衝區的第一個用戶端{width}
, {height}
, {opts}
) 建立一個表格,其中包含浮動視窗的合理預設選項。該表格可以傳遞給 nvim_open_win()。{width}
(integer
) 視窗寬度(以字元格為單位){height}
(integer
) 視窗高度(以字元格為單位)table
) 選項{options}
) 為目前的緩衝區和游標位置建立 DocumentFormattingParams
物件。{options}
(lsp.FormattingOptions?
) 具有有效的 FormattingOptions
條目lsp.DocumentFormattingParams
) 物件{start_pos}
, {end_pos}
, {bufnr}
, {offset_encoding}
) 使用目前緩衝區中給定的範圍,建立一個類似於 vim.lsp.util.make_range_params() 的物件。{start_pos}
([integer,integer]?
) {row,col}
標記索引位置。預設為最後一個可視選取範圍的開頭。{end_pos}
([integer,integer]?
) {row,col}
標記索引位置。預設為最後一個可視選取範圍的結尾。{bufnr}
(integer?
) 緩衝區控制代碼或 0 表示目前,預設為目前{offset_encoding}
('utf-8'|'utf-16'|'utf-32'?
) 預設為 bufnr
第一個用戶端的 offset_encoding
table
) { textDocument = { uri = current_file_uri
}, range = { start = start_position
, end = end_position
} }{window}
, {offset_encoding}
) 為目前的緩衝區和游標位置建立 TextDocumentPositionParams
物件。{window}
(integer?
) 視窗控制代碼或 0 表示目前,預設為目前{offset_encoding}
('utf-8'|'utf-16'|'utf-32'?
) 預設為 window
的緩衝區的第一個用戶端的 offset_encoding
lsp.TextDocumentPositionParams
){window}
, {offset_encoding}
) 使用目前緩衝區中的目前位置,建立一個物件,該物件可以用作多個 LSP 請求(例如 textDocument/codeAction
、textDocument/colorPresentation
、textDocument/rangeFormatting
)的建構區塊。{window}
(integer?
) 視窗控制代碼或 0 表示目前,預設為目前{offset_encoding}
("utf-8"|"utf-16"|"utf-32"?
) 預設為 window
的緩衝區的第一個用戶端的 offset_encoding
table
) { textDocument = { uri = current_file_uri
}, range = { start = current_position
, end = current_position
} }{bufnr}
) 為目前的緩衝區建立 TextDocumentIdentifier
物件。{bufnr}
(integer?
) 緩衝區控制代碼,預設為目前緩衝區lsp.TextDocumentIdentifier
){added}
(lsp.WorkspaceFolder[]
){removed}
(lsp.WorkspaceFolder[]
)lsp.WorkspaceFoldersChangeEvent
){contents}
(table
) 要在視窗中顯示的行{syntax}
(string
) 要為開啟的緩衝區設定的語法{opts}
(vim.lsp.util.open_floating_preview.Opts?
) 具有選用欄位(其他索引鍵在使用 vim.lsp.util.make_floating_popup_options() 篩選後會傳遞給 nvim_open_win())。請參閱 vim.lsp.util.open_floating_preview.Opts。integer
) 新建立的浮動視窗的 bufnr (integer
) 新建立的浮動視窗預覽視窗的 winid{location}
(lsp.Location|lsp.LocationLink
)integer?
) 浮動視窗的緩衝區 ID (integer?
) 浮動視窗的視窗 IDopts
要求覆寫;或{old_fname}
(string
){new_fname}
(string
){opts}
(table?
) 選項{overwrite}
(boolean
){ignoreIfExists}
(boolean
){location}
(lsp.Location|lsp.LocationLink
){offset_encoding}
('utf-8'|'utf-16'|'utf-32'?
){opts}
(table?
) 包含以下欄位的表格{reuse_win}
(boolean
) 如果緩衝區已開啟,則跳到現有視窗。{focus}
(boolean
) 是否在可能的情況下聚焦/跳到位置。(預設值:true)boolean
) 如果成功,則為 true
{bufnr}
, {contents}
, {opts}
) 將 Markdown 轉換為語法突顯的區域,方法是移除程式碼區塊並將它們轉換為突顯的程式碼。依預設,這會在這些程式碼區塊區域後插入空白行分隔符號,以提高可讀性。open_floating_preview
{bufnr}
(integer
){contents}
(string[]
) 要在視窗中顯示的行{opts}
(table?
) 具有選用欄位table
) 已移除的內容{symbols}
(lsp.DocumentSymbol[]|lsp.SymbolInformation[]
){bufnr}
(integer?
)string
) 記錄檔名稱integer
) 目前的記錄層級{handle}
(function
) 要套用至記錄引數的函式,傳遞 vim.inspect 以進行多行格式化{level}
(string|integer
) vim.lsp.log.levels
之一{level}
(integer
) 記錄層級bool
) 如果會記錄則為 true,否則為 false{request}
(fun(method: string, params: table?, callback: fun(err: lsp.ResponseError?, result: any), notify_reply_callback: fun(message_id: integer)?):boolean,integer?
) 請參閱 vim.lsp.rpc.request(){is_closing}
(fun(): boolean
){terminate}
(fun()
){host_or_path}
, {port}
) vim.lsp.rpc.connect(){host_or_path}
(string
) 要連線的主機或管道/網域通訊端的路徑{port}
(integer?
) 要連線的 TCP 連接埠。如果不存在,則第一個引數必須是管道fun(dispatchers: vim.lsp.rpc.Dispatchers): vim.lsp.rpc.PublicClient
){err}
(table
) 錯誤物件string
) error_message 格式化的錯誤訊息{method}
(string
) 呼叫的 LSP 方法{params}
(table?
) 呼叫的 LSP 方法的參數boolean
) 如果可以發送通知,則為 true
,否則為 false
{method}
, {params}
, {callback}
, {notify_reply_callback}
) 發送請求到 LSP 伺服器,並在收到回覆時執行 {callback}
。{method}
(string
) 呼叫的 LSP 方法{params}
(table?
) 呼叫的 LSP 方法的參數{callback}
(fun(err: lsp.ResponseError?, result: any)
) 要調用的回呼函式{notify_reply_callback}
(fun(message_id: integer)?
) 一旦請求不再處於待處理狀態時,要調用的回呼函式boolean
) 如果可以發送請求,則成功為 true
,否則為 false
(integer?
) 如果可以發送請求,則為 message_id,否則為 nil
{code}
, {message}
, {data}
) 建立一個要發送到 LSP 回應的 RPC 回應表 error
。{code}
(integer
) 定義的 RPC 錯誤代碼,請參閱 vim.lsp.protocol.ErrorCodes
{message}
(string?
) 要發送到伺服器的任意訊息{data}
(any?
) 要發送到伺服器的任意資料lsp.ResponseError
)vim.lsp.protocol.ErrorCodes
{cmd}
, {dispatchers}
, {extra_spawn_params}
) vim.lsp.rpc.start() 啟動 LSP 伺服器程序,並建立一個 LSP RPC 客戶端物件以與之互動。與衍生程序的通訊透過 stdio 進行。若要透過 TCP 通訊,請手動衍生程序並使用 vim.lsp.rpc.connect(){cmd}
(string[]
) 啟動 LSP 伺服器的命令。{dispatchers}
(table?
) LSP 訊息類型的分派器。{notification}
(fun(method: string, params: table)
){server_request}
(fun(method: string, params: table): any?, lsp.ResponseError?
){on_exit}
(fun(code: integer, signal: integer)
){on_error}
(fun(code: integer, err: any)
){extra_spawn_params}
(table?
) LSP 伺服器程序的額外上下文。{cwd}
(string
) LSP 伺服器程序的工作目錄{detached}
(boolean
) 從目前程序分離 LSP 伺服器程序vim.lsp.rpc.PublicClient
) 客戶端 RPC 物件,具有以下方法notify()
vim.lsp.rpc.notify()request()
vim.lsp.rpc.request()is_closing()
傳回一個布林值,指示 RPC 是否正在關閉。terminate()
終止 RPC 客戶端。請參閱 vim.lsp.rpc.PublicClient。lsp.ClientCapabilities
){server_capabilities}
) 建立一個標準化的物件,描述 LSP 伺服器的功能。{server_capabilities}
(table
) 伺服器支援的功能表lsp.ServerCapabilities?
) 標準化的功能表