diff --git a/lua/forester.lua b/lua/forester.lua index 63c3aff..b45310f 100644 --- a/lua/forester.lua +++ b/lua/forester.lua @@ -1,4 +1,5 @@ local api = vim.api +local CompletionSource = require("forester.completion") local Commands = require("forester.commands") local Forester = require("forester.bindings") local util = require("forester.util") @@ -32,6 +33,10 @@ local function setup(config) if not config then config = { opts = { tree_dirs = "trees" } } end + + ---Register your source to nvim-cmp. + require("cmp").register_source("month", CompletionSource) + local opts = config.opts ensure_treesitter() vim.opt.path:append("trees") diff --git a/lua/forester/completion.lua b/lua/forester/completion.lua new file mode 100644 index 0000000..fb86f10 --- /dev/null +++ b/lua/forester/completion.lua @@ -0,0 +1,70 @@ +local source = {} + +---Return whether this source is available in the current context or not (optional). +---@return boolean +function source:is_available() + return true +end + +---Return the debug name of this source (optional). +---@return string +function source:get_debug_name() + return "forester cmp" +end + +---Return LSP's PositionEncodingKind. +---@NOTE: If this method is omitted, the default value will be `utf-16`. +---@return lsp.PositionEncodingKind +function source:get_position_encoding_kind() + return "utf-16" +end + +---Return the keyword pattern for triggering completion (optional). +---If this is omitted, nvim-cmp will use a default keyword pattern. See |cmp-config.completion.keyword_pattern|. +---@return string +function source:get_keyword_pattern() + return [[\k\+]] +end + +---Return trigger characters for triggering completion (optional). +function source:get_trigger_characters() + return { "\\" } +end + +---Invoke completion (required). +---@param params cmp.SourceCompletionApiParams +---@param callback fun(response: lsp.CompletionResponse|nil) +function source:complete(params, callback) + callback({ + { label = "title" }, + { label = "author" }, + { label = "date" }, + { label = "taxon" }, + { label = "def" }, + { label = "import" }, + { label = "export" }, + { label = "p" }, + { label = "strong" }, + { label = "transclude" }, + { label = "let" }, + { label = "code" }, + { label = "tex" }, + }) +end + +---Resolve completion item (optional). This is called right before the completion is about to be displayed. +---Useful for setting the text shown in the documentation window (`completion_item.documentation`). +---@param completion_item lsp.CompletionItem +---@param callback fun(completion_item: lsp.CompletionItem|nil) +function source:resolve(completion_item, callback) + callback(completion_item) +end + +---Executed after the item was selected. +---@param completion_item lsp.CompletionItem +---@param callback fun(completion_item: lsp.CompletionItem|nil) +function source:execute(completion_item, callback) + callback(completion_item) +end + +return source diff --git a/lua/forester/lsp.lua b/lua/forester/lsp.lua new file mode 100644 index 0000000..e69de29 diff --git a/lua/forester/ui.lua b/lua/forester/ui.lua index 129bfaf..583e6e2 100644 --- a/lua/forester/ui.lua +++ b/lua/forester/ui.lua @@ -5,7 +5,7 @@ local M = {} local draw_title_inlay = function(bufnr, pos, title) local r = pos[1] local c = pos[2] - --api.nvim_buf_set_extmark(bufnr, forester_ns, r, c, { virt_text = { { title, "@comment" } }, virt_text_pos = "eol" }) + api.nvim_buf_set_extmark(bufnr, forester_ns, r, c, { virt_text = { { title, "@comment" } }, virt_text_pos = "eol" }) end local draw_inline_hints = function(bufnr) @@ -40,6 +40,8 @@ local draw_inline_hints = function(bufnr) end end +draw_inline_hints(0) + M.draw_title_inlay = draw_title_inlay M.draw_inline_hints = draw_inline_hints