Repository navigation
Expand file tree
/
Copy pathlsp.lua
More file actions
102 lines (91 loc) · 2.79 KB
/
Copy pathlsp.lua
File metadata and controls
102 lines (91 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
-- Code generated by litgo from litgo.lit.md; DO NOT EDIT.
local M = {}
--- The completion plugins that drive completion themselves.
local engines = { "blink.cmp", "cmp", "coq" }
local function engine_loaded()
for _, name in ipairs(engines) do
if package.loaded[name] then
return true
end
end
return false
end
--- Turn Neovim's own completion on for this buffer, unless something else
--- is doing the job. Deferred to the first insert, because that is when a
--- lazily loaded completion plugin arrives.
local function complete_with(id, buf)
vim.api.nvim_create_autocmd("InsertEnter", {
buffer = buf,
>
callback = function()
vim.schedule(function()
if not engine_loaded() and vim.api.nvim_buf_is_valid(buf) then
vim.lsp.completion.enable(true, id, buf, { autotrigger = true })
end
end)
end,
})
end
local function litgo()
return require("litgo")
end
--- What to tell the server about gopls: where it is, to do without ("off"),
--- or to look for itself ("").
local function gopls(want)
if want == false then
return "off"
elseif type(want) == "string" then
return vim.fn.expand(want)
end
local mason = vim.fn.stdpath("data") .. "/mason/bin/gopls"
if vim.fn.executable("gopls") == 0 and vim.fn.executable(mason) == 1 then
return mason
end
return ""
end
function M.start(buf)
local cfg = litgo().config.lsp
local bin = cfg.enabled and litgo().bin()
if not bin then
return
end
local name = vim.api.nvim_buf_get_name(buf)
vim.lsp.start({
name = "litgo",
cmd = { bin, "lsp", "--gopls", gopls(cfg.gopls) },
init_options = cfg.settings,
root_dir = vim.fs.root(name, { "go.mod", ".git" }) or vim.fs.dirname(name),
on_attach = function(client, bufnr)
vim.diagnostic.config({
virtual_lines = cfg.virtual_lines,
virtual_text = not cfg.virtual_lines,
severity_sort = true,
[require("litgo.proof").handler] = true,
}, vim.lsp.diagnostic.get_namespace(client.id))
if cfg.completion == "auto" then
complete_with(client.id, bufnr)
elseif cfg.completion then
vim.lsp.completion.enable(true, client.id, bufnr, { autotrigger = true })
end
end,
}, { bufnr = buf })
end
local function client(buf)
return vim.lsp.get_clients({ bufnr = buf, name = "litgo" })[1]
end
--- Is the server attached to this buffer?
function M.active(buf)
return client(buf) ~= nil
end
--- Is the server reporting errors in this buffer right now? If it is, a
--- run's compile errors would only repeat them.
function M.reporting(buf)
local c = client(buf)
return c ~= nil
and #vim.diagnostic.get(buf, {
namespace = vim.lsp.diagnostic.get_namespace(c.id),
severity = vim.diagnostic.severity.ERROR,
})
> 0
end
return M