Skip to content

Commit

Permalink
extensive logging
Browse files Browse the repository at this point in the history
  • Loading branch information
fspv committed Jan 27, 2025
1 parent d7f9162 commit 343233b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 18 deletions.
59 changes: 49 additions & 10 deletions lua/tabby/client_methods.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- This file contains methods exposed by neovim instance to reply to the requests from tabby agent.
-- These methods are used by tabby agent to fetch missing information about files (such as semantic tokens, etc).

local tabby = require("tabby")

local default_error_code = -32603 -- suggested by chatgpt, have no idea what it means

Expand All @@ -21,7 +22,6 @@ local function _get_semantic_tokens(client, bufnr, range)
-- Request semantic tokens from the client
local tokens = client.request_sync('textDocument/semanticTokens/range', params, 1000, bufnr)
if tokens and tokens.err then
-- vim.notify('Error fetching semantic tokens from lsp client: ' .. vim.inspect(tokens.err), vim.log.levels.ERROR)
return nil
end
if tokens and tokens.result then
Expand Down Expand Up @@ -57,7 +57,6 @@ local function _get_language_lsp_client(uri, method)
)
) do
if client.name ~= "tabby" then
-- vim.notify("Got client " .. client.name .. " for buffer " .. bufnr)
return client
end
end
Expand All @@ -75,20 +74,34 @@ end
---@param params lsp.DefinitionParams
---@return lsp.Definition|WrappedErrorResponse|nil
local _handle_text_document_definition = function(client, bufnr, params)
-- vim.notify("Got text document declaration request: " .. vim.inspect(result))

tabby.log(
string.format("handling textDocument/definition for %s", vim.inspect(params)),
vim.log.levels.DEBUG
)
local response = client.request_sync("textDocument/definition", params, 1000, bufnr)
-- vim.notify(vim.inspect(response))

if response and response.result then
tabby.log(
string.format("textDocument/definition response: %s", vim.inspect(response.result)),
vim.log.levels.DEBUG
)
return response.result
end

if response and response.err then
tabby.log(
string.format("textDocument/definition error: %s", vim.inspect(response.err)),
vim.log.levels.ERROR
)
return response.err
end

tabby.log(
string.format("textDocument/definition response: %s", vim.inspect(response)),
vim.log.levels.ERROR
)

-- Often fails when editing files and syntax is temporarily incorrect
-- vim.notify("Tabby definition request failed: " .. vim.inspect(result), vim.log.levels.ERROR)
return {
error = {
code = default_error_code,
Expand All @@ -103,20 +116,35 @@ end
---@param params lsp.DeclarationParams
---@return lsp.Declaration|WrappedErrorResponse|nil
local _handle_text_document_declaration = function(client, bufnr, params)
-- vim.notify("Got text document declaration request: " .. vim.inspect(result))
tabby.log(
string.format("handling textDocument/declaration for %s", vim.inspect(params)),
vim.log.levels.DEBUG
)

local response = client.request_sync("textDocument/definition", params, 1000, bufnr)
-- vim.notify(vim.inspect(response))

if response and response.result then
tabby.log(
string.format("textDocument/declaration response: %s", vim.inspect(response.result)),
vim.log.levels.DEBUG
)
return response.result
end

if response and response.err then
tabby.log(
string.format("textDocument/declaration error: %s", vim.inspect(response.err)),
vim.log.levels.ERROR
)
return response.err
end

tabby.log(
string.format("textDocument/declaration response: %s", vim.inspect(response)),
vim.log.levels.ERROR
)

-- Often fails when editing files and syntax is temporarily incorrect
-- vim.notify("Tabby definition request failed: " .. vim.inspect(result), vim.log.levels.ERROR)
return {
error = {
code = default_error_code,
Expand Down Expand Up @@ -160,13 +188,19 @@ end
---@param params lsp.SemanticTokensRangeParams
---@return SemanticTokensRangeResponse|WrappedErrorResponse
local _handle_semantic_tokens_range = function(client, bufnr, params)
tabby.log(
string.format("handling semantic tokens request for %s", vim.inspect(params)),
vim.log.levels.DEBUG
)

local range = params.range

-- Get the semantic tokens and legend
local tokens = _get_semantic_tokens(client, bufnr, range)
local legend = _get_semantic_token_legend(client)

if not legend then
tabby.log("Failed to get legend for semantic tokens", vim.log.levels.ERROR)
return {
error = {
code = default_error_code,
Expand All @@ -176,6 +210,7 @@ local _handle_semantic_tokens_range = function(client, bufnr, params)
end

if not tokens then
tabby.log("Failed to get semantic tokens", vim.log.levels.ERROR)
return {
error = {
code = default_error_code,
Expand All @@ -185,10 +220,14 @@ local _handle_semantic_tokens_range = function(client, bufnr, params)
end

-- Return the result
return {
local result = {
legend = legend,
tokens = tokens
}

tabby.log(string.format("got semantic tokens: %s", vim.inspect(result)), vim.log.levels.DEBUG)

return result
end

-- Handler for the semantic tokens range request
Expand Down
41 changes: 35 additions & 6 deletions lua/tabby/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,28 @@ local M = {}
---@field trigger_or_dismiss string Keybinding to trigger or dismiss completion

-- Plugin configuration with defaults
---@type { inline_completion: InlineCompletionConfig }
---@type { inline_completion: InlineCompletionConfig, debug: boolean }
local plugin_config = {
inline_completion = {
trigger = "auto", -- or "manual"
keybindings = {
accept = "<Tab>",
trigger_or_dismiss = "<C-\\>"
}
}
},
debug = false
}

---@param msg string The message to log
---@param level? integer The log level (vim.log.levels)
---@return nil
function M.log(msg, level)
if plugin_config.debug or vim.env.PLENARY_TEST ~= nil then
msg = string.format("tabby: %s", msg)
vim.notify(msg, level, {})
end
end

---@class CompletionContext
---@field buf integer
---@field offset integer
Expand Down Expand Up @@ -53,6 +65,14 @@ end
---@param request_id string
---@param callback fun(completion_list: CompletionList)
function M.request_inline_completion(inline_completion_params, request_id, callback)
M.log(
string.format(
"inline_completion_params: %s, request_id: %s",
vim.inspect(inline_completion_params),
request_id
),
vim.log.levels.DEBUG
)
local lsp_client = _get_client()
if lsp_client == nil then
return
Expand All @@ -64,7 +84,7 @@ function M.request_inline_completion(inline_completion_params, request_id, callb
inline_completion_params,
function(err, result)
if err ~= nil then
-- vim.notify("Tabby inline completion request failed: " .. vim.inspect(err), vim.log.levels.ERROR)
M.log(string.format("Error: %s", vim.inspect(err)), vim.log.levels.ERROR)
return
end
wrapped_callback(result)
Expand Down Expand Up @@ -106,12 +126,14 @@ end
---@param params CompletionContext
---@param result CompletionList
local function _handle_completion_response(params, result)
M.log(string.format('params: %s, result: %s', vim.inspect(params), vim.inspect(result)), vim.log.levels.TRACE)
if not vim.deep_equal(completion_state.request_context, params) then
return
end

local ok, _ = schema.validate_completion_list(result)
local ok, err = schema.validate_completion_list(result)
if not ok then
M.log(string.format('Invalid completion list: %s', err), vim.log.levels.ERROR)
return
end

Expand All @@ -132,6 +154,7 @@ end

---@param is_manually boolean
function M.trigger(is_manually)
M.log(string.format("triggering completion: %s", is_manually and "manually" or "auto"), vim.log.levels.DEBUG)
if plugin_config.inline_completion.trigger ~= "auto" and not is_manually then
return
end
Expand Down Expand Up @@ -174,21 +197,24 @@ end

---@return nil
function M.accept()
M.log("calling accept()", vim.log.levels.DEBUG)
local function insert_tab()
vim.api.nvim_put({ "\t" }, "c", true, true)
end

-- If there is nothing to complete (i.e. regular typing), just use the accept
-- character as a regular character
local ok, _ = schema.validate_completion_list(completion_state.completion_list)
local ok, err = schema.validate_completion_list(completion_state.completion_list)
if not ok or #completion_state.completion_list.items == 0 then
M.log(string.format('Invalid completion list: %s', err), vim.log.levels.ERROR)
return insert_tab()
end

local item = completion_state.completion_list.items[1]

ok, _ = schema.validate_item(0, item)
ok, err = schema.validate_item(0, item)
if not ok then
M.log(string.format('Invalid item: %s', err), vim.log.levels.ERROR)
return insert_tab()
end

Expand All @@ -198,6 +224,7 @@ end

---@return nil
function M.clear()
M.log("calling clear()", vim.log.levels.DEBUG)
request_manager.cancel_all_requests()

completion_state.request_context = nil
Expand All @@ -210,6 +237,8 @@ end
-- Setup function to initialize the plugin
---@param opts { inline_completion: InlineCompletionConfig }
function M.setup(opts)
M.log(string.format("calling setup(%s)", vim.inspect(opts)), vim.log.levels.DEBUG)

-- Merge user config with defaults
plugin_config = vim.tbl_deep_extend("force", plugin_config, opts or {})

Expand Down
6 changes: 4 additions & 2 deletions lua/tabby/server_methods.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local tabby = require("tabby")

local M = {}

---@class Buffer
Expand All @@ -18,7 +20,6 @@ local function _collect_visible_buffers(except_active_buffer)

-- Only include buffers with valid file paths
if vim.fn.filereadable(fname) == 1 and fname:sub(1, 1) == "/" then
print("found " .. fname)
-- Get visible lines range for the window
local topline = vim.fn.line("w0")
local botline = vim.fn.line("w$")
Expand Down Expand Up @@ -78,13 +79,14 @@ function M.notify_buffer_change(bufnr, client)
visibleEditors = _collect_visible_buffers(true)
}

tabby.log(string.format("Sending %s to LSP client", vim.inspect(params)), vim.log.levels.DEBUG)
-- Send notification to all active LSP clients
local notify_success = client.notify(
"tabby/editors/didChangeActiveEditor",
params
)
if not notify_success then
vim.notify("Tabby LSP client is down")
tabby.log(string.format("Failed to send %s to LSP client", vim.inspect(params)), vim.log.levels.ERROR)
end
end
end
Expand Down

0 comments on commit 343233b

Please sign in to comment.