Skip to content

Commit 9395c20

Browse files
committed
WIP.
1 parent b5a366a commit 9395c20

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

lua/minuet/config.lua

+8
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ local M = {
133133
blink = {
134134
enable_auto_complete = true,
135135
},
136+
lsp = {
137+
-- Whether start the in-process lsp to provide completion.
138+
-- When `enabled = true`, manual completion will always be available.
139+
enabled = false,
140+
-- Enable or disable auto-completion. Note that this option is only
141+
-- effective when `enabled = true`.
142+
enable_auto_complete = false,
143+
},
136144
virtualtext = {
137145
-- Specify the filetypes to enable automatic virtual text completion,
138146
-- e.g., { 'python', 'lua' }. Note that you can still invoke manual

lua/minuet/lsp.lua

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
local M = {}
2+
3+
M.augroup = vim.api.nvim_create_augroup('MinuetLSP', { clear = true })
4+
5+
function M.get_trigger_characters()
6+
return { '@', '.', '(', '[', ':', ' ' }
7+
end
8+
9+
function M.get_capabilities()
10+
return {
11+
completionProvider = {
12+
triggerCharacters = M.get_trigger_characters(),
13+
},
14+
}
15+
end
16+
17+
function M.generate_request_id()
18+
return os.time()
19+
end
20+
21+
M.request_handler = {}
22+
23+
M.request_handler.initialize = function(_, _, callback, _)
24+
local id = M.generate_request_id()
25+
vim.schedule(function()
26+
callback(nil, { capabilities = M.get_capabilities() })
27+
end)
28+
return true, id
29+
end
30+
31+
M.request_handler['textDocument/completion'] = function(_, params, callback, notify_callback)
32+
local id = M.generate_request_id()
33+
vim.schedule(function()
34+
callback(nil, { isIncomplete = false, items = { { label = 'hello', kind = 20 } } })
35+
-- We want to trigger the notification callback to explicitly mark the
36+
-- operation as complete. This ensures the "complete" event is
37+
-- dispatched and associated completion requests are no longer
38+
-- considered pending.
39+
if notify_callback then
40+
notify_callback(id)
41+
end
42+
end)
43+
return true, id
44+
end
45+
46+
M.request_handler.shutdown = function(_, _, callback, _)
47+
local id = M.generate_request_id()
48+
vim.schedule(function()
49+
callback(nil, nil)
50+
end)
51+
return true, id
52+
end
53+
54+
---@param dispatchers vim.lsp.rpc.Dispatchers
55+
---@return vim.lsp.rpc.PublicClient
56+
function M.server(dispatchers)
57+
local closing = false
58+
return {
59+
request = function(method, params, callback, notify_callback)
60+
if M.request_handler[method] then
61+
local ok, id = M.request_handler[method](method, params, callback, notify_callback)
62+
return ok, id
63+
else
64+
return false, nil
65+
end
66+
end,
67+
notify = function(method, _)
68+
if method == 'exit' then
69+
-- code 0 (success), signal 15 (SIGTERM)
70+
dispatchers.on_exit(0, 15)
71+
end
72+
return true
73+
end,
74+
is_closing = function()
75+
return closing
76+
end,
77+
terminate = function()
78+
closing = true
79+
end,
80+
}
81+
end
82+
83+
function M.start_server(args)
84+
---@type vim.lsp.ClientConfig
85+
local config = {
86+
name = 'minuet',
87+
cmd = M.server,
88+
}
89+
---@type vim.lsp.start.Opts
90+
local opts = {
91+
bufnr = args.buf,
92+
reuse_client = function(lsp_client, lsp_config)
93+
return lsp_client.name == lsp_config.name
94+
end,
95+
}
96+
vim.lsp.start(config, opts)
97+
end
98+
99+
function M.setup()
100+
vim.api.nvim_create_autocmd('FileType', {
101+
pattern = '*',
102+
callback = M.start_server,
103+
desc = 'Starts the minuet LSP server',
104+
group = M.augroup,
105+
})
106+
end
107+
108+
return M

lua/minuet/utils.lua

+21
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,27 @@ function M.make_cmp_context(blink_context)
177177
return self
178178
end
179179

180+
---@class LSPPositionParams
181+
---@field context {triggerKind: number}
182+
---@field position {character: number, line: number}
183+
---@field textDocument {uri: string}
184+
185+
---@param params LSPPositionParams
186+
function M.make_cmp_context_from_lsp_params(params)
187+
local bufnr
188+
local self = {}
189+
if params.textDocument.uri == 'file://' then
190+
bufnr = 0
191+
else
192+
bufnr = params.vim.uri_to_bufnr(params.textDocument.uri)
193+
end
194+
self.cursor = {
195+
row = params.position.line,
196+
col = params.position.character + 1,
197+
}
198+
return self
199+
end
200+
180201
--- Get the context around the cursor position for code completion
181202
---@param cmp_context table The completion context object containing cursor position and line info
182203
---@return table Context information with the following fields:

0 commit comments

Comments
 (0)