Skip to content

Commit

Permalink
add completion
Browse files Browse the repository at this point in the history
  • Loading branch information
kentookura committed Jan 17, 2024
1 parent cb49af2 commit 57a4ebb
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lua/forester.lua
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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")
Expand Down
70 changes: 70 additions & 0 deletions lua/forester/completion.lua
Original file line number Diff line number Diff line change
@@ -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
Empty file added lua/forester/lsp.lua
Empty file.
4 changes: 3 additions & 1 deletion lua/forester/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 57a4ebb

Please sign in to comment.