Lsp

Nvim 的 :help 頁面,使用 tree-sitter-vimdoc 解析器從 來源產生


LSP 客戶端/框架 LSP
Nvim 支援語言伺服器協議(Language Server Protocol,LSP),這表示它作為 LSP 伺服器的客戶端,並包含一個 Lua 框架 vim.lsp,用於建立增強的 LSP 工具。
LSP 利用語義全專案分析(與 ctags 不同),促進諸如跳至定義、尋找參考、懸停、補全、重新命名、格式化、重構等功能。

快速入門 lsp-quickstart

Nvim 提供了一個 LSP 客戶端,但伺服器是由協力廠商提供的。請按照以下步驟取得 LSP 功能
1. 使用您的套件管理器或按照上游安裝說明安裝語言伺服器。您可以在這裡找到語言伺服器:https://microsoft.github.io/language-server-protocol/implementors/servers/
2. 當檔案開啟時,使用 vim.lsp.start() 啟動 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,
})
3. 檢查緩衝區是否已附加到伺服器
:checkhealth lsp
4.(可選)設定快捷鍵和自動命令以使用 LSP 功能。lsp-config
lsp-defaults
當 Nvim LSP 客戶端啟動時,它會啟用診斷 vim.diagnostic (請參閱 vim.diagnostic.config() 以進行自訂)。如果(1)語言伺服器支援該功能,並且(2)選項為空或由內建的執行時間(ftplugin)檔案設定,它還會設定以下列出的各種預設選項。當 LSP 客戶端停止或分離時,這些選項不會還原。
'omnifunc' 設定為 vim.lsp.omnifunc(),使用 i_CTRL-X_CTRL-O 來觸發補全。
'tagfunc' 設定為 vim.lsp.tagfunc()。這啟用諸如跳至定義、:tjump 之類的功能,以及像 CTRL-]CTRL-W_]CTRL-W_} 這樣的快捷鍵來使用語言伺服器。
'formatexpr' 設定為 vim.lsp.formatexpr(),因此如果語言伺服器支援,您可以使用 gq 來格式化行。
若要退出此設定,請使用 gw 而不是 gq,或在 LspAttach 時清除 'formatexpr'
除非 'keywordprg' 已自訂或存在 K 的自訂快捷鍵,否則 K 會對應到 vim.lsp.buf.hover()
grr gra grn gri i_CTRL-S 當 Nvim 啟動時,會無條件建立一些快捷鍵
"grn" 在普通模式中對應到 vim.lsp.buf.rename()
"gra" 在普通和視覺模式中對應到 vim.lsp.buf.code_action()
"grr" 在普通模式中對應到 vim.lsp.buf.references()
"gri" 在普通模式中對應到 vim.lsp.buf.implementation()
"gO" 在普通模式中對應到 vim.lsp.buf.document_symbol()
CTRL-S 在插入模式中對應到 vim.lsp.buf.signature_help()
如果不需要這些快捷鍵,可以使用 vim.keymap.del():unmap 隨時移除它們(另請參閱 gr-default)。
lsp-defaults-disable
若要覆寫或刪除上述任何預設值,請在 LspAttach 設定或取消設定選項
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,
})
lsp-config
若要使用其他 LSP 功能,請在 LspAttach 設定快捷鍵和其他緩衝區選項。並非所有語言伺服器都提供相同的功能。使用功能檢查,以確保您只使用語言伺服器支援的功能。範例
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,
})
若要了解有哪些功能可用,您可以在已啟動 LSP 客戶端的緩衝區中執行以下命令
:lua =vim.lsp.get_clients()[1].server_capabilities
預設提供的完整功能清單可以在 lsp-buf 中找到。

常見問題解答 lsp-faq

問:如何強制重新載入 LSP?
答:停止所有客戶端,然後重新載入緩衝區。
:lua vim.lsp.stop_client(vim.lsp.get_clients())
:edit
問:為什麼補全無法運作?
答:在您要使用 LSP 的緩衝區中,檢查 'omnifunc' 是否設定為 "v:lua.vim.lsp.omnifunc"::verbose set omnifunc?
某些其他外掛程式可能會覆寫該選項。若要避免這種情況,您可以將該選項設定在 after-directory ftplugin 中,例如 "after/ftplugin/python.vim"。
問:如何同步執行請求(例如,在檔案儲存時進行格式化)?
答:檢查函數是否有 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 })
lsp-vs-treesitter
問:LSP 和 Treesitter 如何比較?
答:LSP 需要客戶端和語言伺服器。語言伺服器使用語義分析來了解專案層級的程式碼。這為語言伺服器提供了跨檔案重新命名、在外部程式庫中尋找定義等能力。
Treesitter 是一個語言解析程式庫,它提供了用於增量解析文字和處理錯誤的出色工具。這使其非常適合編輯器,以了解目前檔案的內容,用於諸如語法高亮、簡單的跳至定義、範圍分析等。
LSP 和 Treesitter 都是用於編輯和檢查程式碼的出色工具。

LSP API lsp-api

LSP 核心 API 在 lsp-core 中描述。這些是建立和管理客戶端的核心函數。
vim.lsp.buf_… 函數對附加到給定緩衝區的所有 LSP 客戶端執行操作。lsp-buf
LSP 請求/回應處理程式實作為 Lua 函數(請參閱 lsp-handler)。 lsp-method
LSP 規範定義的請求和通知稱為「LSP 方法」。Nvim LSP 客戶端在全球 vim.lsp.handlers 表格中提供預設處理程式,您可以使用此命令列出它們
: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'
lsp-handler
LSP 處理程式是處理 Nvim 向伺服器發出的請求的 lsp-response 的函數。(與請求相反,通知是一次性發送:沒有回應,因此無法處理。lsp-notification
每個回應處理程式都有這個簽名
function(err, result, ctx)
參數
{err} (table|nil) 錯誤資訊字典,如果請求完成,則為 nil
{result} (Result|Params|nil) lsp-responseresult 鍵,如果請求失敗,則為 nil
{ctx} (table) 與處理程式相關聯的呼叫狀態的表格,包含以下鍵
{method} (string) lsp-method 名稱。
{client_id} (number) vim.lsp.Client 識別碼。
{bufnr} (Buffer) 緩衝區控制代碼。
{params} (table|nil) 請求參數表格。
{version} (number) 請求時的文件版本。處理程式可以將其與目前的文件版本進行比較,以檢查回應是否「過時」。另請參閱 b:changedtick
傳回值
兩個值 result, err,其中 err 的形狀類似於 RPC 錯誤
{ code, message, data? }
您可以使用 vim.lsp.rpc.rpc_response_error() 來建立此物件。
lsp-handler-resolution
可以透過以下方式設定處理程式(優先順序遞增)
在 vim.lsp.handlers 中設定一個欄位。vim.lsp.handlers
vim.lsp.handlers 是一個全域表格,其中包含 lsp-method 名稱到 lsp-handlers 的預設對應。
範例
vim.lsp.handlers['textDocument/publishDiagnostics'] = my_custom_diagnostics_handler
注意:這僅適用於伺服器向客戶端發出的請求/通知。
vim.lsp.start(){handlers} 參數。這為特定伺服器設定預設的 lsp-handler
範例
vim.lsp.start {
  ..., -- Other configuration omitted.
  handlers = {
    ['textDocument/publishDiagnostics'] = my_custom_server_definition
  },
}
注意:這僅適用於伺服器向客戶端發出的請求/通知。
vim.lsp.buf_request_all(){handler} 參數。這僅為給定的請求設定 lsp-handler
範例
vim.lsp.buf_request_all(
  0,
  'textDocument/publishDiagnostics',
  my_request_params,
  my_handler
)
vim.lsp.log_levels
記錄層級在 vim.log.levels 中定義

VIM.LSP.PROTOCOL vim.lsp.protocol

模組 vim.lsp.protocol 定義了 LSP 規範所規定的常數,以及用於建立與協定相關的物件的輔助函數。 https://github.com/microsoft/language-server-protocol/raw/gh-pages/_specifications/specification-3-14.md
例如,vim.lsp.protocol.ErrorCodes 允許依編號或名稱反向查找
vim.lsp.protocol.TextDocumentSyncKind.Full == 1
vim.lsp.protocol.TextDocumentSyncKind[1] == "Full"

LSP 高亮 lsp-highlight

參考高亮
旨在由 vim.lsp.buf.document_highlight() 使用的高亮群組。
hl-LspReferenceText
LspReferenceText 用於高亮「文字」參考 hl-LspReferenceRead
LspReferenceRead 用於高亮「讀取」參考 hl-LspReferenceWrite
LspReferenceWrite 用於高亮「寫入」參考 hl-LspInlayHint
LspInlayHint 用於高亮內嵌提示
lsp-codelens 功能相關的高亮群組。
hl-LspCodeLens
LspCodeLens 用於著色程式碼鏡頭的虛擬文字。請參閱 nvim_buf_set_extmark()
LspCodeLensSeparator hl-LspCodeLensSeparator
用於著色兩個或多個程式碼鏡頭之間的分隔符號。
醒目提示與 vim.lsp.handlers.signature_help() 相關的群組。
hl-LspSignatureActiveParameter
LspSignatureActiveParameter 用於醒目提示簽名說明中的活動參數。請參閱 vim.lsp.handlers.signature_help()

LSP 語義醒目提示 lsp-semantic-highlight

在可用的情況下,LSP 客戶端會使用 lsp-semantic_tokens 來醒目提示程式碼,這是 LSP 伺服器提供原始碼資訊的另一種方式。請注意,這是樹狀結構語法醒目提示的補充;語義醒目提示不會取代語法醒目提示。
伺服器通常會為原始碼中的每個識別符號提供一個 token。token 將會有一個 type,例如「function」或「variable」,以及 0 個或多個 modifier,例如「readonly」或「deprecated」。標準的類型和修飾符在此處說明:https://microsoft.github.io/language-server-protocol/specification/#textDocument_semanticTokens。LSP 伺服器也可以使用非規範的類型和修飾符。
LSP 客戶端會為每個 token 新增一個或多個醒目提示。醒目提示群組衍生自 token 的類型和修飾符。
@lsp.type.<type>.<ft> 用於類型
@lsp.mod.<mod>.<ft> 用於每個修飾符
@lsp.typemod.<type>.<mod>.<ft> 用於每個修飾符。使用 :Inspect 來檢視特定 token 的醒目提示。使用 :hinvim_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
vim.hl.priorities.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
您可能希望將它們放在 ColorScheme 自動指令中。
使用 LspTokenUpdatevim.lsp.semantic_tokens.highlight_token() 來進行更複雜的醒目提示。
以下是 Nvim 查詢中使用的標準捕獲列表,根據目前的色彩配置醒目提示 (在其中一個上使用 :Inspect 來查看確切的定義)
@lsp.type.class 宣告或參考類別類型的識別符號 @lsp.type.comment 代表註解的 token @lsp.type.decorator 宣告或參考裝飾器和註解的識別符號 @lsp.type.enum 宣告或參考列舉類型的識別符號 @lsp.type.enumMember 宣告或參考列舉屬性、常數或成員的識別符號 @lsp.type.event 宣告事件屬性的識別符號 @lsp.type.function 宣告函式的識別符號 @lsp.type.interface 宣告或參考介面類型的識別符號 @lsp.type.keyword 代表語言關鍵字的 token @lsp.type.macro 宣告巨集的識別符號 @lsp.type.method 宣告成員函式或方法的識別符號 @lsp.type.modifier 代表修飾符的 token @lsp.type.namespace 宣告或參考命名空間、模組或套件的識別符號 @lsp.type.number 代表數字常值的 token @lsp.type.operator 代表運算子的 token @lsp.type.parameter 宣告或參考函式或方法參數的識別符號 @lsp.type.property 宣告或參考成員屬性、成員欄位或成員變數的識別符號 @lsp.type.regexp 代表正規表示式常值的 token @lsp.type.string 代表字串常值的 token @lsp.type.struct 宣告或參考結構類型的識別符號 @lsp.type.type 宣告或參考上述未涵蓋的類型的識別符號 @lsp.type.typeParameter 宣告或參考類型參數的識別符號 @lsp.type.variable 宣告或參考區域或全域變數的識別符號
@lsp.mod.abstract 抽象的類型和成員函式 @lsp.mod.async 標示為 async 的函式 @lsp.mod.declaration 符號的宣告 @lsp.mod.defaultLibrary 屬於標準程式庫一部分的符號 @lsp.mod.definition 符號的定義,例如在標頭檔中 @lsp.mod.deprecated 不應再使用的符號 @lsp.mod.documentation 文件中符號的出現 @lsp.mod.modification 變數被賦值的變數參考 @lsp.mod.readonly 唯讀變數和成員欄位 (常數) @lsp.mod.static 類別成員 (靜態成員)

事件 lsp-events

LspAttach LspAttach
在 LSP 客戶端附加到緩衝區之後。autocmd-pattern 是緩衝區的名稱。從 Lua 使用時,客戶端 ID 會以 "data" 表格傳遞給回呼。請參閱 lsp-config 以取得範例。
LspDetach LspDetach
在 LSP 客戶端從緩衝區分離之前。autocmd-pattern 是緩衝區的名稱。從 Lua 使用時,客戶端 ID 會以 "data" 表格傳遞給回呼。範例
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,
})
LspNotify LspNotify
此事件會在每次成功傳送通知到 LSP 伺服器後觸發。
從 Lua 使用時,客戶端 ID、LSP 方法和參數會以 "data" 表格傳送。範例
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,
})
LspProgress LspProgress
在收到伺服器的進度通知時。可以從 vim.lsp.Clientprogress 環形緩衝區輪詢通知,或使用 vim.lsp.status() 來取得彙總訊息。
如果伺服器傳送「工作完成進度」,則 pattern 會設定為 kindbeginreportend 其中之一)。
從 Lua 使用時,事件會包含一個 data 表格,其中包含 client_idparams 屬性。params 將包含伺服器傳送的要求參數(請參閱 lsp.ProgressParams)。
範例
autocmd LspProgress * redrawstatus
LspRequest LspRequest
對於傳送到 LSP 伺服器的每個要求,每次要求狀態變更時都會觸發此事件。狀態可以是 pendingcompletecancel,並作為傳遞給回呼函式的 "data" 表格上的 {type} 傳送。
當傳送初始要求時 ({type} == pending) 以及當 LSP 伺服器回應時 ({type} == complete) 會觸發此事件。如果使用 client.cancel_request(request_id) 要求取消,則此事件將會以 {type} == cancel 觸發。
從 Lua 使用時,客戶端 ID、要求 ID 和要求會以 "data" 表格傳送。請參閱 vim.lsp.Client 中的 {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,
})
LspTokenUpdate LspTokenUpdate
當 LSP 伺服器傳送或更新可見的語義 token 時,或當現有 token 首次變為可見時。autocmd-pattern 是緩衝區的名稱。從 Lua 使用時,token 和客戶端 ID 會以 "data" 表格傳遞給回呼。token 欄位記錄在 vim.lsp.semantic_tokens.get_at_pos() 中。範例
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,
})
注意:除了呼叫 vim.lsp.semantic_tokens.highlight_token() 之外的任何操作都被視為實驗性操作。

Lua 模組:vim.lsp lsp-core

buf_attach_client({bufnr}, {client_id}) vim.lsp.buf_attach_client()
實作追蹤任何語言伺服器的緩衝區所需的 textDocument/did… 通知。
如果未呼叫此項,伺服器將不會收到緩衝區變更的通知。
參數
{bufnr} (integer) 緩衝區控制代碼,或 0 表示目前
{client_id} (integer) 客戶端 ID
傳回
(boolean) 成功。如果客戶端成功附加則為 true;否則為 false
buf_detach_client({bufnr}, {client_id}) vim.lsp.buf_detach_client()
將客戶端從指定的緩衝區分離。注意:雖然伺服器收到文字文件 (緩衝區) 已關閉的通知,但如果它忽略此通知,仍然可以傳送通知。
參數
{bufnr} (integer) 緩衝區控制代碼,或 0 表示目前
{client_id} (integer) 客戶端 ID
buf_is_attached({bufnr}, {client_id}) vim.lsp.buf_is_attached()
檢查是否已為特定客戶端附加緩衝區。
參數
{bufnr} (integer) 緩衝區控制代碼,或 0 表示目前
{client_id} (integer) 客戶端 ID
buf_notify({bufnr}, {method}, {params}) vim.lsp.buf_notify()
傳送通知到伺服器
參數
{bufnr} (integer?) 緩衝區的數字
{method} (string) 要求方法名稱
{params} (any) 要傳送到伺服器的引數
傳回
(boolean) 成功。如果任何客戶端傳回 true 則為 true;否則為 false
vim.lsp.buf_request_all()
buf_request_all({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 取消所有要求的函式。
vim.lsp.buf_request_sync()
buf_request_sync({bufnr}, {method}, {params}, {timeout_ms}) 將要求傳送到所有伺服器,並等待所有伺服器的回應。
呼叫 vim.lsp.buf_request_all(),但在等待結果時會封鎖 Nvim。參數與 vim.lsp.buf_request_all() 相同,但結果不同。最多等待 {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_is_stopped({client_id}) vim.lsp.client_is_stopped()
檢查客戶端是否已停止。
參數
{client_id} (integer)
傳回
(boolean) 若客戶端已停止則為 true,否則為 false。
commands vim.lsp.commands
客戶端命令的註冊表。這是外掛程式處理非核心語言伺服器協定規格中自訂命令的擴充點。
此註冊表是一個表格,其中的鍵是唯一的命令名稱,而值是一個函式。如果任何 LSP 動作(程式碼動作、程式碼透鏡等)觸發了該命令,則會呼叫此函式。
如果 LSP 回應包含在此註冊表中沒有對應條目的命令,則將使用 workspace/executeCommand 通過 LSP 伺服器執行該命令。
函式的第一個參數會是 Command:命令標題:字串、命令:字串、參數?:任何型別的陣列。
第二個參數是 lsp-handlerctx
formatexpr({opts}) vim.lsp.formatexpr()
在內建客戶端和 formatexpr 函式之間提供一個介面。
目前僅支援單一客戶端。可以通過 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 毫秒) 格式化請求的逾時時間。
vim.lsp.get_buffers_by_client_id()
get_buffers_by_client_id({client_id}) 返回附加到 client_id 的緩衝區列表。
參數
{client_id} (integer) 客戶端 ID
傳回
(integer[]) 緩衝區 ID 的列表
get_client_by_id({client_id}) vim.lsp.get_client_by_id()
通過 ID 取得客戶端,如果 ID 無效則傳回 nil。傳回的客戶端可能尚未完全初始化。
參數
{client_id} (integer) 客戶端 ID
傳回
(vim.lsp.Client?) 客戶端 RPC 物件
get_clients({filter}) vim.lsp.get_clients()
取得作用中的客戶端。
參數
{filter} (table?) 用於篩選傳回的客戶端的鍵值對。
{id} (integer) 僅傳回具有指定 ID 的客戶端
{bufnr} (integer) 僅傳回附加到此緩衝區的客戶端
{name} (string) 僅傳回具有指定名稱的客戶端
{method} (string) 僅傳回支援指定方法的客戶端
傳回
(vim.lsp.Client[]) vim.lsp.Client 物件的列表
get_log_path() vim.lsp.get_log_path()
取得 LSP 客戶端使用的日誌檔路徑。
傳回
(string) 日誌檔的路徑
omnifunc({findstart}, {base}) vim.lsp.omnifunc()
實作與 'omnifunc' 相容的 LSP 完成功能。
參數
{findstart} (integer) 0 或 1,決定行為
{base} (integer) findstart=0,要比對的文字
傳回
(integer|table) 由 {findstart} 決定
findstart=0:完成開始的欄位,或 -2 或 -3
findstart=1:符合項目的列表(實際上只是呼叫 complete()
set_log_level({level}) vim.lsp.set_log_level()
設定 LSP 日誌記錄的全域日誌等級。
依名稱的等級:"TRACE"、"DEBUG"、"INFO"、"WARN"、"ERROR"、"OFF"
等級編號以 "TRACE" 從 0 開始
使用 lsp.log_levels 來反向查找。
參數
{level} (integer|string) 不區分大小寫的等級名稱或編號
另請參閱
start({config}, {opts}) vim.lsp.start()
建立一個新的 LSP 客戶端並啟動語言伺服器,或如果找到符合 nameroot_dir 的正在執行的客戶端,則重複使用它。將目前的緩衝區附加到客戶端。
範例
vim.lsp.start({
   name = 'my-server-name',
   cmd = {'name-of-language-server-executable'},
   root_dir = vim.fs.root(0, {'pyproject.toml', 'setup.py'}),
})
請參閱 vim.lsp.start_client() 以取得所有可用的選項。最重要的選項是
name LSP 客戶端的任意名稱。每個語言伺服器應該是唯一的。
cmd 命令字串陣列或函式,詳情請見 vim.lsp.start_client()
root_dir 專案根目錄的路徑。預設情況下,這用於決定是否應重複使用現有的客戶端。上面的範例使用 vim.fs.root() 通過從當前目錄向上遍歷檔案系統,直到找到 pyproject.tomlsetup.py 檔案來偵測根目錄。
workspace_folders { uri:string, name: string } 表格的列表,指定語言伺服器使用的專案根目錄資料夾。如果 nil,為了方便起見,該屬性從 root_dir 派生。
語言伺服器使用此資訊來探索中繼資料,例如專案的相依性,並且它們傾向於索引專案資料夾中的內容。
為了確保僅針對它可以處理的語言啟動語言伺服器,請務必在 FileType autocmd 內呼叫 vim.lsp.start()。可以使用 :aunvim_create_autocmd() 或將呼叫放在 ftplugin/<filetype_name>.lua 中(請參閱 ftplugin-name
參數
{config} (vim.lsp.ClientConfig) 伺服器的組態。請參閱 vim.lsp.ClientConfig
{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_id
start_client({config}) vim.lsp.start_client()
使用給定的組態啟動並初始化客戶端。
參數
{config} (vim.lsp.ClientConfig) 伺服器的組態。請參閱 vim.lsp.ClientConfig
傳回 (多個)
(integer?) client_id vim.lsp.get_client_by_id() 注意:客戶端可能尚未完全初始化。請使用 on_init 在客戶端初始化後執行任何動作。(string?)錯誤訊息(如果有的話)
status() vim.lsp.status()
使用來自所有客戶端的最新進度訊息,並將它們格式化為字串。如果沒有客戶端或沒有新訊息,則為空。
傳回
(string)
stop_client({client_id}, {force}) vim.lsp.stop_client()
停止客戶端。
您也可以在 vim.lsp.Client 物件上使用 stop() 函式。若要停止所有客戶端
vim.lsp.stop_client(vim.lsp.get_clients())
預設情況下,會要求伺服器關閉,除非已針對此客戶端要求停止,否則會嘗試強制關閉。
參數
{client_id} (integer|integer[]|vim.lsp.Client[]) ID、ID 列表或 vim.lsp.Client 物件列表
{force} (boolean?) 強制關閉
tagfunc({pattern}, {flags}) vim.lsp.tagfunc()
在內建客戶端和 'tagfunc' 之間提供介面。
當與正常模式命令(例如 CTRL-])一起使用時,這將呼叫 "textDocument/definition" LSP 方法以尋找游標下的標籤。否則,使用 "workspace/symbol"。如果任何 LSP 伺服器未傳回結果,則會回復為使用內建標籤。
參數
{pattern} (string) 用於尋找工作區符號的模式
{flags} (string) 請參閱 tag-function
傳回
(table[]) 標籤 符合標籤的列表

Lua 模組:vim.lsp.client lsp-client

欄位
{id} (integer) 分配給客戶端的 ID。
{name} (string) 如果在建立時指定了名稱,則會使用該名稱。否則,它只是客戶端 ID。這用於日誌和訊息。
{rpc} (vim.lsp.rpc.PublicClient) RPC 客戶端物件,用於與客戶端進行低階互動。請參閱 vim.lsp.rpc.start()
{offset_encoding} (string) 用於與伺服器通訊的編碼。您可以在將文字傳送到伺服器之前,在 configon_init 方法中修改此項。
{handlers} (table<string,lsp.Handler>) 客戶端使用的處理程序,如 lsp-handler 中所述。
{requests} (table<integer,{ type: string, bufnr: integer, method: string}>) 目前發送到伺服器的待處理請求。項目是鍵值對,其中鍵是請求 ID,而值是一個包含 typebufnrmethod 鍵值對的表格。type 對於作用中的請求為「pending」,對於取消請求則為「cancel」。當從伺服器收到回覆時,在執行 LspRequest autocmd 時,它會短暫地為「complete」。
{config} (vim.lsp.ClientConfig) 使用者傳遞給 vim.lsp.start_client() 的表格的副本。請參閱 vim.lsp.ClientConfig
{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 表格,並傳回一個包含成員函數 requestnotifyis_closingterminate 的表格。請參閱 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() 並修改其結果。
注意: 要傳送空的字典,請使用 vim.empty_dict(),否則它會被編碼為陣列。
{handlers} (table<string,function>) 從語言伺服器方法名稱到 lsp-handler 的映射。
{settings} (table) 包含語言伺服器特定設定的映射表。請參閱 vim.lsp.Client 中的 {settings}
{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)>) 在客戶端退出時呼叫的回調函數。
code:進程的退出代碼
signal:描述用於終止的訊號的數字(如果有)
client_id:客戶端句柄
{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 基於的目錄。
Client:exec_cmd({command}, {context}, {handler}) Client:exec_cmd()
執行 lsp 命令,可以透過客戶端命令函數(如果可用)或透過 workspace/executeCommand(如果伺服器支援)
參數
{command} (lsp.Command)
{context} ({bufnr?: integer}?)
{handler} (lsp.Handler?) 僅在伺服器命令時呼叫

Lua 模組:vim.lsp.buf lsp-buf

欄位
{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 })
如果您偏好 loclist 而不是 qflist
vim.lsp.buf.definition({ loclist = true })
vim.lsp.buf.references(nil, { loclist = true })
{loclist} (boolean)
欄位
{reuse_win} (boolean) 如果緩衝區已開啟,則跳到現有視窗。
欄位
{items} (table[]) 結構類似於 setqflist-what
{title} (string) 列表的標題。
{context} (table) 來自 lsp-handlerctx
欄位
{silent} (boolean)
欄位
{silent} (boolean)
vim.lsp.buf.add_workspace_folder()
add_workspace_folder({workspace_folder}) 將路徑上的資料夾新增到工作區資料夾。如果沒有提供 {path},則會提示使用者使用 input() 輸入路徑。
參數
{workspace_folder} (string?)
clear_references() vim.lsp.buf.clear_references()
從目前的緩衝區移除文件醒目提示。
code_action({opts}) vim.lsp.buf.code_action()
選取目前游標位置可用的程式碼動作。
參數
{opts} (table?) 包含以下欄位的表格
{context} (lsp.CodeActionContext) 對應於 LSP 規格的 CodeActionContext
{diagnostics} (table) LSP Diagnostic[]。如果未提供,則從目前位置推斷。
{only} (table) 用於篩選程式碼動作的 LSP CodeActionKind 清單。大多數語言伺服器都支援諸如 refactorquickfix 之類的值。
{triggerKind} (integer) 請求程式碼動作的原因。
{filter} (fun(x: lsp.CodeAction|lsp.Command):boolean) 接受 CodeAction 並傳回布林值的謂詞。
{apply} (boolean) 當設定為 true,且只有一個剩餘動作(在篩選之後)時,會在沒有使用者查詢的情況下套用動作。
{range} ({start: integer[], end: integer[]}) 應請求程式碼動作的範圍。如果在可視模式下,則預設為活動選取範圍。表格必須包含 startend 鍵,其中包含使用類似標記的索引的 {row,col} 元組。請參閱 api-indexing
declaration({opts}) vim.lsp.buf.declaration()
跳到游標下符號的宣告位置。
注意
許多伺服器並未實作此方法。一般來說,請改用 vim.lsp.buf.definition()
參數
{opts} (vim.lsp.LocationOpts?) 請參閱 vim.lsp.LocationOpts
definition({opts}) vim.lsp.buf.definition()
跳到游標下符號的定義位置。
參數
{opts} (vim.lsp.LocationOpts?) 請參閱 vim.lsp.LocationOpts
document_highlight() vim.lsp.buf.document_highlight()
傳送請求至伺服器,以解析目前文字文件位置的文件反白。此請求可由按鍵映射或 CursorHold 等事件觸發,例如:
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()
注意: 使用 vim.lsp.buf.document_highlight() 需要定義以下反白群組,否則您將無法看到實際的反白效果。hl-LspReferenceText hl-LspReferenceRead hl-LspReferenceWrite
document_symbol({opts}) vim.lsp.buf.document_symbol()
在 quickfix 視窗中列出目前緩衝區中的所有符號。
參數
{opts} (vim.lsp.ListOpts?) 請參閱 vim.lsp.ListOpts
format({opts}) vim.lsp.buf.format()
使用已附加 (且可選地經過篩選) 的語言伺服器用戶端格式化緩衝區。
參數
{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,格式化整個緩衝區) 要格式化的範圍。表格必須包含 startend 鍵,其中包含使用 (1,0) 索引的 {row,col} 元組。也可以是包含如上所述的 startend 鍵的表格清單,在這種情況下,需要 textDocument/rangesFormatting 支援。
hover({config}) vim.lsp.buf.hover()
在浮動視窗中顯示游標下符號的懸停資訊。視窗會在游標移動時關閉。呼叫此函式兩次將會跳到浮動視窗中 (因此預設情況下,「KK」會開啟懸停視窗並將焦點放在其上)。在浮動視窗中,所有指令和映射都與平常一樣可用,除了「q」會關閉視窗。您可以像捲動任何其他緩衝區一樣捲動內容。
參數
{config} (vim.lsp.buf.hover.Opts?) 請參閱 vim.lsp.buf.hover.Opts
implementation({opts}) vim.lsp.buf.implementation()
在 quickfix 視窗中列出游標下符號的所有實作。
參數
{opts} (vim.lsp.LocationOpts?) 請參閱 vim.lsp.LocationOpts
incoming_calls() vim.lsp.buf.incoming_calls()
quickfix 視窗中列出游標下符號的所有呼叫點。如果符號可以解析為多個項目,則使用者可以在 inputlist() 中選擇一個。
list_workspace_folders() vim.lsp.buf.list_workspace_folders()
列出工作區資料夾。
outgoing_calls() vim.lsp.buf.outgoing_calls()
quickfix 視窗中列出游標下符號呼叫的所有項目。如果符號可以解析為多個項目,則使用者可以在 inputlist() 中選擇一個。
references({context}, {opts}) vim.lsp.buf.references()
在 quickfix 視窗中列出游標下符號的所有參考。
參數
{context} (table?) 請求的內容
{opts} (vim.lsp.ListOpts?) 請參閱 vim.lsp.ListOpts
vim.lsp.buf.remove_workspace_folder()
remove_workspace_folder({workspace_folder}) 從工作區資料夾中移除路徑上的資料夾。如果未提供 {path},系統將使用 input() 提示使用者輸入路徑。
參數
{workspace_folder} (string?)
rename({new_name}, {opts}) vim.lsp.buf.rename()
重新命名游標下符號的所有參考。
參數
{new_name} (string?) 如果未提供,系統將使用 vim.ui.input() 提示使用者輸入新名稱。
{opts} (table?) 其他選項
{filter} (fun(client: vim.lsp.Client): boolean?) 用於篩選用戶端的謂詞。接收一個用戶端作為參數,且必須傳回布林值。符合謂詞的用戶端將會被包含。
{name} (string) 將用於重新命名的用戶端限制為 client.name 與此欄位符合的用戶端。
{bufnr} (integer) (預設值: 目前緩衝區)
signature_help({config}) vim.lsp.buf.signature_help()
在浮動視窗中顯示游標下符號的簽名資訊。
參數
{config} (vim.lsp.buf.signature_help.Opts?) 請參閱 vim.lsp.buf.signature_help.Opts
type_definition({opts}) vim.lsp.buf.type_definition()
跳到游標下符號的類型定義位置。
參數
{opts} (vim.lsp.LocationOpts?) 請參閱 vim.lsp.LocationOpts
typehierarchy({kind}) vim.lsp.buf.typehierarchy()
quickfix 視窗中列出游標下符號的所有子類型或超類型。如果符號可以解析為多個項目,則使用者可以使用 vim.ui.select() 選擇一個。
參數
{kind} ("subtypes"|"supertypes")
workspace_symbol({query}, {opts}) vim.lsp.buf.workspace_symbol()
在 quickfix 視窗中列出目前工作區中的所有符號。
清單會根據 {query} 進行篩選;如果從呼叫中省略參數,系統會提示使用者在命令列上輸入字串。空字串表示不進行篩選。
參數
{query} (string?) 可選
{opts} (vim.lsp.ListOpts?) 請參閱 vim.lsp.ListOpts

Lua 模組: vim.lsp.diagnostic lsp-diagnostic

from({diagnostics}) vim.lsp.diagnostic.from()
將輸入的 vim.Diagnostic 轉換為 LSP 診斷。
參數
{diagnostics} (vim.Diagnostic[])
傳回
(lsp.Diagnostic[])
vim.lsp.diagnostic.get_namespace()
get_namespace({client_id}, {is_pull}) 取得與 LSP 用戶端相關聯的診斷命名空間 vim.diagnostic,用於診斷
參數
{client_id} (integer) LSP 用戶端的 ID
{is_pull} (boolean?) 命名空間是否用於提取或推送用戶端。預設為推送
vim.lsp.diagnostic.on_diagnostic()
on_diagnostic({_}, {result}, {ctx}) 用於方法 "textDocument/diagnostic" 的 lsp-handler
請參閱 vim.diagnostic.config() 以取得組態選項。
參數
{result} (lsp.DocumentDiagnosticReport)
{ctx} (lsp.HandlerContext)
vim.lsp.diagnostic.on_publish_diagnostics()
on_publish_diagnostics({_}, {result}, {ctx}) 用於方法 "textDocument/publishDiagnostics" 的 lsp-handler
請參閱 vim.diagnostic.config() 以取得組態選項。
參數
{result} (lsp.PublishDiagnosticsParams)
{ctx} (lsp.HandlerContext)

Lua 模組: vim.lsp.codelens lsp-codelens

clear({client_id}, {bufnr}) vim.lsp.codelens.clear()
清除鏡頭。
參數
{client_id} (integer?) 按 client_id 篩選。如果為 nil,則為所有用戶端
{bufnr} (integer?) 按緩衝區篩選。如果為 nil,則為所有緩衝區;0 表示目前緩衝區
display({lenses}, {bufnr}, {client_id}) vim.lsp.codelens.display()
使用虛擬文字顯示鏡頭。
參數
{lenses} (lsp.CodeLens[]?) 要顯示的鏡頭
{bufnr} (integer)
{client_id} (integer)
get({bufnr}) vim.lsp.codelens.get()
傳回指定緩衝區的所有鏡頭。
參數
{bufnr} (integer) 緩衝區編號。0 可用於目前緩衝區。
傳回
(lsp.CodeLens[])
on_codelens({err}, {result}, {ctx}) vim.lsp.codelens.on_codelens()
用於方法 textDocument/codeLenslsp-handler
參數
{err} (lsp.ResponseError?)
{result} (lsp.CodeLens[])
{ctx} (lsp.HandlerContext)
refresh({opts}) vim.lsp.codelens.refresh()
重新整理鏡頭。
建議使用自動指令或透過按鍵映射觸發此操作。
範例
autocmd BufEnter,CursorHold,InsertLeave <buffer> lua vim.lsp.codelens.refresh({ bufnr = 0 })
參數
{opts} (table?) 可選欄位
{bufnr} (integer?) 按緩衝區篩選。如果為 nil,則為所有緩衝區;0 表示目前緩衝區
run() vim.lsp.codelens.run()
在目前行中執行程式碼鏡頭。
save({lenses}, {bufnr}, {client_id}) vim.lsp.codelens.save()
儲存特定緩衝區和用戶端的鏡頭。
參數
{lenses} (lsp.CodeLens[]?) 要儲存的鏡頭
{bufnr} (integer)
{client_id} (integer)

Lua 模組: vim.lsp.completion lsp-completion

欄位
{autotrigger} (boolean) 預設值: false。如果為 true,則會根據伺服器的 triggerCharacters 自動觸發完成。
{convert} (fun(item: lsp.CompletionItem): table) 將 LSP CompletionItem 轉換為 complete-items
vim.lsp.completion.enable()
enable({enable}, {client_id}, {bufnr}, {opts}) 在指定緩衝區中啟用或停用來自指定語言用戶端的完成功能。
參數
{enable} (boolean) True 為啟用,false 為停用
{client_id} (integer) 用戶端 ID
{bufnr} (integer) 緩衝區控制代碼,或 0 表示目前緩衝區
{opts} (vim.lsp.completion.BufferOpts?) 請參閱 vim.lsp.completion.BufferOpts
trigger() vim.lsp.completion.trigger()
在當前緩衝區中觸發 LSP 完成。

Lua 模組:vim.lsp.inlay_hint lsp-inlay_hint

enable({enable}, {filter}) vim.lsp.inlay_hint.enable()
為經過 {filter} 過濾的範圍啟用或停用內嵌提示。
若要「切換」,請傳遞 is_enabled() 的反向值。
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled())
屬性
自:0.10.0
參數
{enable} (boolean?) true/nil 表示啟用,false 表示停用
{filter} (table?) 可選的過濾器 kwargs,或 nil 表示全部。
{bufnr} (integer?) 緩衝區編號,或 0 表示目前緩衝區,或 nil 表示全部。
get({filter}) vim.lsp.inlay_hint.get()
取得內嵌提示的列表,(可選)受緩衝區或範圍限制。
範例用法
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,
})
屬性
自:0.10.0
參數
{filter} (table?) 可選的過濾器 kwargs
{bufnr} (integer?)
{range} (lsp.Range?)
傳回
(table[]) 具有以下欄位的物件列表
{bufnr} (integer)
{client_id} (integer)
{inlay_hint} (lsp.InlayHint)
is_enabled({filter}) vim.lsp.inlay_hint.is_enabled()
查詢是否在經過 {filter} 過濾的範圍中啟用內嵌提示
屬性
自:0.10.0
參數
{filter} (table?) 可選的過濾器 kwargs,或 nil 表示全部。
{bufnr} (integer?) 緩衝區編號,或 0 表示目前緩衝區,或 nil 表示全部。
傳回
(boolean)

Lua 模組:vim.lsp.semantic_tokens lsp-semantic_tokens

force_refresh({bufnr}) vim.lsp.semantic_tokens.force_refresh()
強制重新整理所有語意符號
僅在緩衝區目前為語意符號高亮顯示作用時才有效(已為其呼叫 vim.lsp.semantic_tokens.start())。
參數
{bufnr} (integer?) 按緩衝區過濾。如果為 nil,則為所有緩衝區;如果為 0,則為目前緩衝區
vim.lsp.semantic_tokens.get_at_pos()
get_at_pos({bufnr}, {row}, {col}) 傳回指定位置的語意符號。如果沒有引數呼叫,則傳回游標下的符號。
參數
{bufnr} (integer?) 緩衝區編號(0 表示目前緩衝區,預設值)
{row} (integer?) 位置列(預設為游標位置)
{col} (integer?) 位置欄(預設為游標位置)
傳回
(table?) 位置上的符號列表。每個符號都具有以下欄位
line (integer) 行號,從 0 開始
start_col (integer) 開始欄,從 0 開始
end_col (integer) 結束欄,從 0 開始
type (string) 符號類型,以字串表示,例如「variable」
modifiers (table) 符號修飾符,以集合表示。例如:{ static = true, readonly = true }
client_id (integer)
vim.lsp.semantic_tokens.highlight_token()
highlight_token({token}, {bufnr}, {client_id}, {hl_group}, {opts}) 高亮顯示語意符號。
為語意符號套用具有指定高亮顯示群組的 extmark。當適當時,語意符號引擎將會刪除該標記;例如,當 LSP 傳送更新的符號時。此函式旨在 LspTokenUpdate 回呼內部使用。
參數
{token} (table) 語意符號,在 LspTokenUpdate 中以 args.data.token 找到
{bufnr} (integer) 要高亮顯示的緩衝區,或 0 表示目前緩衝區
{client_id} (integer) vim.lsp.Client 的 ID
{hl_group} (string) 高亮顯示群組名稱
{opts} (table?) 可選參數
{priority} (integer, default: vim.hl.priorities.semantic_tokens + 3) 套用 extmark 的優先順序。
start({bufnr}, {client_id}, {opts}) vim.lsp.semantic_tokens.start()
使用指定的用戶端為指定的緩衝區啟動語意符號高亮顯示引擎。用戶端必須已附加到緩衝區。
注意:目前這是由 vim.lsp.buf_attach_client() 自動呼叫。若要選擇不使用支援語意高亮顯示的伺服器,您可以在您的 LspAttach 回呼或您設定的 on_attach 回呼中,從您用戶端的 {server_capabilities} 中刪除 semanticTokensProvider 表格
client.server_capabilities.semanticTokensProvider = nil
參數
{bufnr} (integer) 緩衝區編號,或 0 表示目前緩衝區
{client_id} (integer) vim.lsp.Client 的 ID
{opts} (table?) 可選的關鍵字參數
debounce (integer, default: 200): 將符號請求延遲到伺服器的毫秒數
stop({bufnr}, {client_id}) vim.lsp.semantic_tokens.stop()
停止使用指定用戶端為指定緩衝區啟動的語意符號高亮顯示引擎。
注意:這是由在 start() 中設定的 LspDetach 自動指令自動呼叫,因此您只需要使用此函式來手動取消語意符號引擎,而無需完全將 LSP 用戶端從緩衝區分離。
參數
{bufnr} (integer) 緩衝區編號,或 0 表示目前緩衝區
{client_id} (integer) vim.lsp.Client 的 ID

Lua 模組:vim.lsp.util lsp-util

欄位
{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」:根據游標哪一側具有較多行來放置視窗
「above」:將視窗放置在游標上方,除非沒有足夠的行來顯示完整視窗高度。
「below」:將視窗放置在游標下方,除非沒有足夠的行來顯示完整視窗高度。
vim.lsp.util.apply_text_document_edit()
apply_text_document_edit({text_document_edit}, {index}, {offset_encoding}) 套用 TextDocumentEdit,即單一文件變更的清單。
參數
{text_document_edit} (lsp.TextDocumentEdit)
{index} (integer?) 可選的編輯索引,如果是來自編輯清單(如果不是來自清單,則為 nil)
{offset_encoding} ('utf-8'|'utf-16'|'utf-32'?)
vim.lsp.util.apply_text_edits()
apply_text_edits({text_edits}, {bufnr}, {offset_encoding}) 將文字編輯清單套用到緩衝區。
參數
{text_edits} (lsp.TextEdit[])
{bufnr} (integer) 緩衝區 ID
{offset_encoding} ('utf-8'|'utf-16'|'utf-32')
vim.lsp.util.apply_workspace_edit()
apply_workspace_edit({workspace_edit}, {offset_encoding}) 套用 WorkspaceEdit
參數
{workspace_edit} (lsp.WorkspaceEdit)
{offset_encoding} ('utf-8'|'utf-16'|'utf-32') (必要)
buf_clear_references({bufnr}) vim.lsp.util.buf_clear_references()
從緩衝區移除文件高亮顯示。
參數
{bufnr} (integer?) 緩衝區 ID
vim.lsp.util.buf_highlight_references()
buf_highlight_references({bufnr}, {references}, {offset_encoding}) 顯示特定緩衝區的文件高亮顯示清單。
參數
{bufnr} (integer) 緩衝區 ID
{references} (lsp.DocumentHighlight[]) 要高亮顯示的物件
{offset_encoding} ('utf-8'|'utf-16'|'utf-32')
vim.lsp.util.character_offset()
character_offset({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 索引
vim.lsp.util.convert_input_to_markdown_lines()
convert_input_to_markdown_lines({input}, {contents}) 將任何 MarkedString | MarkedString[] | MarkupContent 轉換為包含有效 markdown 的行清單。可用於填入 textDocument/hover 的懸停視窗、剖析 textDocument/signatureHelp 的結果,以及其他潛在用途。
請注意,如果輸入類型為 MarkupContent 且其種類為 plaintext,則會傳回對應的值,而不會進行進一步修改。
參數
{input} (lsp.MarkedString|lsp.MarkedString[]|lsp.MarkupContent)
{contents} (string[]?) 要使用轉換後的行擴充的字串清單。預設值為 {}。
傳回
(string[]) 使用轉換後的 markdown 行擴充。
vim.lsp.util.convert_signature_help_to_markdown_lines()
convert_signature_help_to_markdown_lines({signature_help}, {ft}, {triggers}) 將 textDocument/signatureHelp 的回應轉換為 Markdown 行。
參數
{signature_help} (lsp.SignatureHelp) textDocument/SignatureHelp 的回應
{ft} (string?) 作為標籤 Markdown 程式碼區塊 lang 的檔案類型
{triggers} (string[]?) 來自 LSP 伺服器的觸發字元列表。用於更好地確定參數偏移量
傳回 (多個)
(string[]?) 已轉換的 Markdown 行。(Range4?) 活動參數的突出顯示範圍
get_effective_tabstop({bufnr}) vim.lsp.util.get_effective_tabstop()
傳回縮排大小。
參數
{bufnr} (integer?) 緩衝區控制代碼,預設為目前緩衝區
傳回
(integer) 縮排大小
另請參閱
vim.lsp.util.locations_to_items()
locations_to_items({locations}, {offset_encoding}) 傳回項目,這些項目具有正確計算的位元組位置並按排序順序排列,以便在快速修復和位置列表中顯示。
每個產生的項目的 user_data 欄位將包含計算它的原始 LocationLocationLink
結果可以傳遞到 setqflist()setloclist(){list} 引數。
參數
{locations} (lsp.Location[]|lsp.LocationLink[])
{offset_encoding} ('utf-8'|'utf-16'|'utf-32'?) 預設為緩衝區的第一個用戶端
傳回
(vim.quickfix.entry[]) 請參閱 setqflist() 以了解格式
vim.lsp.util.make_floating_popup_options()
make_floating_popup_options({width}, {height}, {opts}) 建立一個表格,其中包含浮動視窗的合理預設選項。該表格可以傳遞給 nvim_open_win()
參數
{width} (integer) 視窗寬度(以字元格為單位)
{height} (integer) 視窗高度(以字元格為單位)
{opts} (vim.lsp.util.open_floating_preview.Opts?) 請參閱 vim.lsp.util.open_floating_preview.Opts
傳回
(table) 選項
vim.lsp.util.make_formatting_params()
make_formatting_params({options}) 為目前的緩衝區和游標位置建立 DocumentFormattingParams 物件。
參數
{options} (lsp.FormattingOptions?) 具有有效的 FormattingOptions 條目
傳回
(lsp.DocumentFormattingParams) 物件
vim.lsp.util.make_given_range_params()
make_given_range_params({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 } }
vim.lsp.util.make_position_params()
make_position_params({window}, {offset_encoding}) 為目前的緩衝區和游標位置建立 TextDocumentPositionParams 物件。
參數
{window} (integer?) 視窗控制代碼或 0 表示目前,預設為目前
{offset_encoding} ('utf-8'|'utf-16'|'utf-32'?) 預設為 window 的緩衝區的第一個用戶端的 offset_encoding
傳回
(lsp.TextDocumentPositionParams)
vim.lsp.util.make_range_params()
make_range_params({window}, {offset_encoding}) 使用目前緩衝區中的目前位置,建立一個物件,該物件可以用作多個 LSP 請求(例如 textDocument/codeActiontextDocument/colorPresentationtextDocument/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 } }
vim.lsp.util.make_text_document_params()
make_text_document_params({bufnr}) 為目前的緩衝區建立 TextDocumentIdentifier 物件。
參數
{bufnr} (integer?) 緩衝區控制代碼,預設為目前緩衝區
傳回
(lsp.TextDocumentIdentifier)
vim.lsp.util.make_workspace_params()
make_workspace_params({added}, {removed}) 建立工作區參數
參數
{added} (lsp.WorkspaceFolder[])
{removed} (lsp.WorkspaceFolder[])
傳回
(lsp.WorkspaceFoldersChangeEvent)
vim.lsp.util.open_floating_preview()
open_floating_preview({contents}, {syntax}, {opts}) 在浮動視窗中顯示內容。
參數
{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
preview_location({location}, {opts}) vim.lsp.util.preview_location()
在浮動視窗中預覽位置
行為取決於位置的類型
對於 Location,顯示範圍(例如,函式定義)
對於 LocationLink,顯示 targetRange(例如,函式定義的主體)
參數
{location} (lsp.Location|lsp.LocationLink)
{opts} (vim.lsp.util.open_floating_preview.Opts?) 請參閱 vim.lsp.util.open_floating_preview.Opts
傳回 (多個)
(integer?) 浮動視窗的緩衝區 ID (integer?) 浮動視窗的視窗 ID
rename({old_fname}, {new_fname}, {opts}) vim.lsp.util.rename()
將 old_fname 重新命名為 new_fname
現有的緩衝區也會重新命名,同時保留其 bufnr。
僅在以下情況下,它會刪除與重新命名的檔案名稱衝突的現有緩衝區:
opts 要求覆寫;或
衝突的緩衝區未載入,因此刪除它們不會導致資料遺失。
參數
{old_fname} (string)
{new_fname} (string)
{opts} (table?) 選項
{overwrite} (boolean)
{ignoreIfExists} (boolean)
vim.lsp.util.show_document()
show_document({location}, {offset_encoding}, {opts}) 顯示文件並選擇性地跳到位置。
參數
{location} (lsp.Location|lsp.LocationLink)
{offset_encoding} ('utf-8'|'utf-16'|'utf-32'?)
{opts} (table?) 包含以下欄位的表格
{reuse_win} (boolean) 如果緩衝區已開啟,則跳到現有視窗。
{focus} (boolean) 是否在可能的情況下聚焦/跳到位置。(預設值:true)
傳回
(boolean) 如果成功,則為 true
vim.lsp.util.stylize_markdown()
stylize_markdown({bufnr}, {contents}, {opts}) 將 Markdown 轉換為語法突顯的區域,方法是移除程式碼區塊並將它們轉換為突顯的程式碼。依預設,這會在這些程式碼區塊區域後插入空白行分隔符號,以提高可讀性。
此方法會設定給定的緩衝區並傳回要設定的行。
如果您想要開啟具有精美 Markdown 的彈出視窗,請改用 open_floating_preview
參數
{bufnr} (integer)
{contents} (string[]) 要在視窗中顯示的行
{opts} (table?) 具有選用欄位
浮動視窗的高度
浮動視窗的寬度
用於計算高度的換行字元
浮動視窗的最大寬度
浮動視窗的最大高度
分隔符號在程式碼區塊後插入分隔符號
傳回
(table) 已移除的內容
symbols_to_items({symbols}, {bufnr}) vim.lsp.util.symbols_to_items()
將符號轉換為快速修復列表項目。
參數
{symbols} (lsp.DocumentSymbol[]|lsp.SymbolInformation[])
{bufnr} (integer?)
傳回
(vim.quickfix.entry[]) 請參閱 setqflist() 以了解格式

Lua 模組:vim.lsp.log lsp-log

get_filename() vim.lsp.log.get_filename()
傳回記錄檔名稱。
傳回
(string) 記錄檔名稱
get_level() vim.lsp.log.get_level()
取得目前的記錄層級。
傳回
(integer) 目前的記錄層級
set_format_func({handle}) vim.lsp.log.set_format_func()
設定用於格式化記錄的格式化函式
參數
{handle} (function) 要套用至記錄引數的函式,傳遞 vim.inspect 以進行多行格式化
set_level({level}) vim.lsp.log.set_level()
設定目前的記錄層級。
參數
{level} (string|integer) vim.lsp.log.levels 之一
should_log({level}) vim.lsp.log.should_log()
檢查該層級是否足以進行記錄。
參數
{level} (integer) 記錄層級
傳回
(bool) 如果會記錄則為 true,否則為 false

Lua 模組:vim.lsp.rpc lsp-rpc

欄位
{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()
{notify} (fun(method: string, params: any):boolean) 請參閱 vim.lsp.rpc.notify()
{is_closing} (fun(): boolean)
{terminate} (fun())
connect({host_or_path}, {port}) vim.lsp.rpc.connect()
建立一個 LSP RPC 用戶端工廠,該工廠會連線到:
具名管道 (windows)
網域通訊端 (unix)
透過 TCP 的主機和連接埠
傳回一個函式,該函式可以傳遞給 vim.lsp.start_client()vim.lsp.start()cmd 欄位。
參數
{host_or_path} (string) 要連線的主機或管道/網域通訊端的路徑
{port} (integer?) 要連線的 TCP 連接埠。如果不存在,則第一個引數必須是管道
傳回
(fun(dispatchers: vim.lsp.rpc.Dispatchers): vim.lsp.rpc.PublicClient)
format_rpc_error({err}) vim.lsp.rpc.format_rpc_error()
從 LSP 錯誤物件建構錯誤訊息。
參數
{err} (table) 錯誤物件
傳回
(string) error_message 格式化的錯誤訊息
notify({method}, {params}) vim.lsp.rpc.notify()
傳送通知至 LSP 伺服器。
參數
{method} (string) 呼叫的 LSP 方法
{params} (table?) 呼叫的 LSP 方法的參數
傳回
(boolean) 如果可以發送通知,則為 true,否則為 false
vim.lsp.rpc.request()
request({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
vim.lsp.rpc.rpc_response_error()
rpc_response_error({code}, {message}, {data}) 建立一個要發送到 LSP 回應的 RPC 回應表 error
參數
{code} (integer) 定義的 RPC 錯誤代碼,請參閱 vim.lsp.protocol.ErrorCodes
{message} (string?) 要發送到伺服器的任意訊息
{data} (any?) 要發送到伺服器的任意資料
傳回
(lsp.ResponseError)
另請參閱
lsp.ErrorCodes 請參閱 vim.lsp.protocol.ErrorCodes
start({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 伺服器程序
{env} (table<string,string>) LSP 伺服器程序的額外環境變數。請參閱 vim.system()
傳回
(vim.lsp.rpc.PublicClient) 客戶端 RPC 物件,具有以下方法
is_closing() 傳回一個布林值,指示 RPC 是否正在關閉。
terminate() 終止 RPC 客戶端。請參閱 vim.lsp.rpc.PublicClient

Lua 模組:vim.lsp.protocol lsp-protocol

vim.lsp.protocol.make_client_capabilities()
make_client_capabilities() 取得一個新的 ClientCapabilities 物件,描述 LSP 客戶端的功能。
傳回
(lsp.ClientCapabilities)
方法 vim.lsp.protocol.Methods
LSP 方法名稱。
vim.lsp.protocol.resolve_capabilities()
resolve_capabilities({server_capabilities}) 建立一個標準化的物件,描述 LSP 伺服器的功能。
參數
{server_capabilities} (table) 伺服器支援的功能表
傳回
(lsp.ServerCapabilities?) 標準化的功能表
主要
命令索引
快速參考