Skip to content
This repository has been archived by the owner on Aug 12, 2023. It is now read-only.

Commit

Permalink
feat: Initial implementation for completion support
Browse files Browse the repository at this point in the history
  • Loading branch information
Furkanzmc committed Nov 7, 2021
1 parent 64b269b commit be57387
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lua/null-ls/builtins/completion.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
local h = require("null-ls.helpers")
local methods = require("null-ls.methods")
local utils = require("null-ls.utils")

local COMPLETION = methods.internal.COMPLETION

local M = {}

M.tags = h.make_builtin({
method = COMPLETION,
filetypes = {},
name = "tags",
generator = {
fn = function(params, done)
local tags = vim.fn.taglist(params.word_to_complete)
local words = {}
local items = {}
for _, tag in ipairs(tags) do
table.insert(words, tag.name)
end
words = utils.tbl_uniq(words)
for _, word in ipairs(words) do
table.insert(items, {
label = word,
insertText = word,
})
end

done(items)
end,
async = true,
use_cache = true,
},
})

return M
1 change: 1 addition & 0 deletions lua/null-ls/builtins/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local paths = {
formatting = "null-ls.builtins.formatting",
code_actions = "null-ls.builtins.code-actions",
hover = "null-ls.builtins.hover",
completion = "null-ls.builtins.completion",
_test = "null-ls.builtins.test",
}

Expand Down
33 changes: 33 additions & 0 deletions lua/null-ls/completion.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
local u = require("null-ls.utils")
local methods = require("null-ls.methods")

local M = {}

M.handler = function(method, original_params, handler)
local params = u.make_params(original_params, methods.map[method])
if method == methods.lsp.COMPLETION then
require("null-ls.generators").run_registered({
filetype = params.ft,
method = methods.map[method],
params = params,
callback = function(results)
u.debug_log("received completion results from generators")
u.debug_log(results)
if #results == 0 then
handler({})
else
for index, item in ipairs(results) do
if type(item) == "string" then
results[index] = { label = item, insertText = item }
end
end

handler({ isIncomplete = false, items = results })
end
end,
})
original_params._null_ls_handled = true
end
end

return M
4 changes: 4 additions & 0 deletions lua/null-ls/methods.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local lsp_methods = {
DID_OPEN = "textDocument/didOpen",
DID_CLOSE = "textDocument/didClose",
HOVER = "textDocument/hover",
COMPLETION = "textDocument/completion",
}
vim.tbl_add_reverse_lookup(lsp_methods)

Expand All @@ -20,6 +21,7 @@ local internal_methods = {
FORMATTING = "NULL_LS_FORMATTING",
RANGE_FORMATTING = "NULL_LS_RANGE_FORMATTING",
HOVER = "NULL_LS_HOVER",
COMPLETION = "NULL_LS_COMPLETION",
}
vim.tbl_add_reverse_lookup(internal_methods)

Expand All @@ -30,6 +32,7 @@ local lsp_to_internal_map = {
[lsp_methods.DID_OPEN] = internal_methods.DIAGNOSTICS,
[lsp_methods.DID_CHANGE] = internal_methods.DIAGNOSTICS,
[lsp_methods.HOVER] = internal_methods.HOVER,
[lsp_methods.COMPLETION] = internal_methods.COMPLETION,
}

local readable_map = {
Expand All @@ -38,6 +41,7 @@ local readable_map = {
[internal_methods.FORMATTING] = "Formatting",
[internal_methods.RANGE_FORMATTING] = "Range formatting",
[internal_methods.HOVER] = "Hover",
[internal_methods.COMPLETION] = "Completion",
}

local M = {}
Expand Down
2 changes: 2 additions & 0 deletions lua/null-ls/rpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local capabilities = {
executeCommandProvider = true,
documentFormattingProvider = true,
documentRangeFormattingProvider = true,
completionProvider = true,
textDocumentSync = {
change = 1, -- prompt LSP client to send full document text on didOpen and didChange
openClose = true,
Expand Down Expand Up @@ -82,6 +83,7 @@ function M.start(dispatchers)
require("null-ls.code-actions").handler(method, params, send)
require("null-ls.formatting").handler(method, params, send)
require("null-ls.hover").handler(method, params, send)
require("null-ls.completion").handler(method, params, send)
if not params._null_ls_handled then
send()
end
Expand Down
20 changes: 20 additions & 0 deletions lua/null-ls/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ M.make_params = function(original_params, method)
params.range = M.range.from_lsp(original_params.range)
end

if params.lsp_method == methods.lsp.COMPLETION then
local pos = vim.api.nvim_win_get_cursor(0)
local line = vim.api.nvim_get_current_line()
local line_to_cursor = line:sub(1, pos[2])
params.word_to_complete = line:sub(vim.fn.match(line_to_cursor, "\\k*$") + 1, params.col)
end

return params
end

Expand Down Expand Up @@ -199,4 +206,17 @@ function M.throttle(ms, fn)
end
end

function M.tbl_uniq(t)
local new_table = {}
local hash = {}
for _, v in pairs(t) do
if not hash[v] then
table.insert(new_table, v)
hash[v] = true
end
end

return new_table
end

return M

0 comments on commit be57387

Please sign in to comment.