Skip to content

Commit b471fe4

Browse files
committed
打磨
1 parent 793454c commit b471fe4

File tree

5 files changed

+125
-42
lines changed

5 files changed

+125
-42
lines changed

colors/gruvboxl.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@ local faded_purple = "#8f3f71"
5454
local faded_aqua = "#427b58"
5555
local faded_orange = "#af3a03"
5656

57+
-- term
58+
59+
vim.g.terminal_color_0 = dark0 -- 黑色
60+
vim.g.terminal_color_1 = neutral_red -- 红色
61+
vim.g.terminal_color_2 = neutral_green -- 绿色
62+
vim.g.terminal_color_3 = neutral_yellow -- 黄色
63+
vim.g.terminal_color_4 = neutral_blue -- 蓝色
64+
vim.g.terminal_color_5 = neutral_purple -- 洋红色
65+
vim.g.terminal_color_6 = neutral_aqua -- 青色
66+
vim.g.terminal_color_7 = light4 -- 白色
67+
vim.g.terminal_color_8 = gray_245 -- 亮黑色
68+
vim.g.terminal_color_9 = bright_red -- 亮红色
69+
vim.g.terminal_color_10 = bright_green -- 亮绿色
70+
vim.g.terminal_color_11 = bright_yellow -- 亮黄色
71+
vim.g.terminal_color_12 = bright_blue -- 亮蓝色
72+
vim.g.terminal_color_13 = bright_purple -- 亮洋红色
73+
vim.g.terminal_color_14 = bright_aqua -- 亮青色
74+
vim.g.terminal_color_15 = light1 -- 亮白色
75+
5776
-- 设置高亮
5877
local function hl(theme)
5978
for k, v in pairs(theme) do

ftplugin/java.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ vim.bo.tabstop = 4
33
vim.bo.softtabstop = 4
44

55
if vim.env["JDTLS_NVIM_ENABLE"] == "Y" then
6+
local spring_boot = vim.env["SPRING_BOOT_NVIM_ENABLE"] == "Y"
67
local jc = require("kide.lsp.jdtls")
78
local config
89
-- 防止 start_or_attach 重复修复 config
@@ -13,7 +14,14 @@ if vim.env["JDTLS_NVIM_ENABLE"] == "Y" then
1314
else
1415
config = jc.config
1516
jc.init = true
17+
18+
if spring_boot then
19+
vim.list_extend(config["init_options"].bundles, require("spring_boot").java_extensions())
20+
end
1621
end
1722
require("jdtls").start_or_attach(config, { dap = { config_overrides = {}, hotcodereplace = "auto" } })
18-
require("spring_boot.launch").start(require("kide.lsp.spring-boot").config)
23+
24+
if spring_boot then
25+
require("spring_boot.launch").start(require("kide.lsp.spring-boot").config)
26+
end
1927
end

lua/kide/lsp/jdtls.lua

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,10 +440,6 @@ end
440440
local me = require("kide.melspconfig")
441441
M.config.capabilities = me.capabilities()
442442
M.config.on_init = me.on_init
443-
local spring_boot = vim.env["SPRING_BOOT_NVIM_ENABLE"] == "Y"
444-
if spring_boot then
445-
vim.list_extend(bundles, require("spring_boot").java_extensions())
446-
end
447443

448444
M.config.on_attach = function(client, buffer)
449445
local function desc_opts(desc)

lua/kide/tools/ai.lua

Lines changed: 87 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ end
5757
---@param callback fun(opt)
5858
local function handle_sse_events(cmd, callback)
5959
local job
60+
local tmp = ""
6061
job = vim.fn.jobstart(cmd, {
6162
on_stdout = function(_, data, _)
6263
for _, value in ipairs(data) do
63-
if vim.startswith(value, "data:") then
64-
local _, _, text = string.find(value, "data: (.+)")
64+
if vim.startswith(value, "data: ") then
65+
local text = string.sub(value, 7, -1)
6566
if text == "[DONE]" then
67+
tmp = ""
6668
callback({
6769
data = text,
6870
done = true,
@@ -71,6 +73,7 @@ local function handle_sse_events(cmd, callback)
7173
else
7274
local ok, resp_json = pcall(vim.fn.json_decode, text)
7375
if ok then
76+
tmp = ""
7477
if resp_json.usage ~= nil and M.config.show_usage then
7578
callback({
7679
data = "\n",
@@ -94,9 +97,16 @@ local function handle_sse_events(cmd, callback)
9497
})
9598
end
9699
else
100+
tmp = text
101+
end
102+
end
103+
else
104+
if tmp ~= "" then
105+
tmp = tmp .. value
106+
local ok, resp_json = pcall(vim.fn.json_decode, tmp)
107+
if ok then
97108
callback({
98-
err = 1,
99-
data = text,
109+
data = resp_json.choices[1].delta.content,
100110
job = job,
101111
})
102112
end
@@ -222,11 +232,13 @@ M.translate_float = function(request)
222232
end
223233

224234
------------------ chat ------------------
225-
local charwin = nil
226-
local charbuf = nil
235+
local chatwin = nil
236+
local chatbuf = nil
227237
local codebuf = nil
228238
local chatclosed = false
239+
local cursormoved = false
229240
local chatruning = false
241+
local winleave = false
230242
local chat_request_json = {
231243
messages = {
232244
{
@@ -267,10 +279,10 @@ M.chat_config = {
267279
}
268280
local close_gpt_win = function()
269281
chatruning = false
270-
if charwin then
271-
pcall(vim.api.nvim_win_close, charwin, true)
272-
charwin = nil
273-
charbuf = nil
282+
if chatwin then
283+
pcall(vim.api.nvim_win_close, chatwin, true)
284+
chatwin = nil
285+
chatbuf = nil
274286
codebuf = nil
275287
chat_request_json.messages = {
276288
{
@@ -284,58 +296,82 @@ end
284296
local function create_gpt_win()
285297
codebuf = vim.api.nvim_get_current_buf()
286298
vim.cmd("belowright new")
287-
charwin = vim.api.nvim_get_current_win()
288-
charbuf = vim.api.nvim_get_current_buf()
289-
vim.bo[charbuf].buftype = "nofile"
290-
vim.bo[charbuf].bufhidden = "wipe"
291-
vim.bo[charbuf].buflisted = false
292-
vim.bo[charbuf].swapfile = false
293-
vim.bo[charbuf].filetype = "markdown"
299+
chatwin = vim.api.nvim_get_current_win()
300+
chatbuf = vim.api.nvim_get_current_buf()
301+
vim.bo[chatbuf].buftype = "nofile"
302+
vim.bo[chatbuf].bufhidden = "wipe"
303+
vim.bo[chatbuf].buflisted = false
304+
vim.bo[chatbuf].swapfile = false
305+
vim.bo[chatbuf].filetype = "markdown"
294306
vim.api.nvim_put({ M.chat_config.user_title, "" }, "c", true, true)
295307
chatclosed = false
296308

297309
vim.keymap.set("n", "q", function()
298310
chatclosed = true
299311
close_gpt_win()
300-
end, { noremap = true, silent = true, buffer = charbuf })
312+
end, { noremap = true, silent = true, buffer = chatbuf })
301313
vim.keymap.set("n", "<A-k>", function()
302314
M.gpt_chat()
303-
end, { noremap = true, silent = true, buffer = charbuf })
315+
end, { noremap = true, silent = true, buffer = chatbuf })
304316
vim.keymap.set("i", "<A-k>", function()
305317
vim.cmd("stopinsert")
306318
M.gpt_chat()
307-
end, { noremap = true, silent = true, buffer = charbuf })
319+
end, { noremap = true, silent = true, buffer = chatbuf })
308320

309-
vim.api.nvim_buf_create_user_command(charbuf, "GptSend", function()
321+
vim.api.nvim_buf_create_user_command(chatbuf, "GptSend", function()
310322
M.gpt_chat()
311323
end, { desc = "Gpt Send" })
312324

325+
vim.api.nvim_create_autocmd("BufWipeout", {
326+
buffer = chatbuf,
327+
callback = close_gpt_win,
328+
})
329+
313330
vim.api.nvim_create_autocmd("WinClosed", {
314-
buffer = charbuf,
331+
buffer = chatbuf,
315332
callback = close_gpt_win,
316333
})
334+
vim.api.nvim_create_autocmd("WinLeave", {
335+
buffer = chatbuf,
336+
callback = function()
337+
winleave = true
338+
end,
339+
})
340+
341+
vim.api.nvim_create_autocmd("CursorMoved", {
342+
buffer = chatbuf,
343+
callback = function()
344+
cursormoved = true
345+
end,
346+
})
317347
end
318348

319349
local function code_question(selection)
320350
if not selection then
321351
return
322352
end
323-
324-
local filetype = vim.bo[codebuf].filetype or "txt"
325-
local filename = vim.fn.fnamemodify(vim.fn.bufname(codebuf), ":.")
326-
local qs = {
327-
"请解释`" .. filename .. "`文件中的这段代码",
328-
"```" .. filetype,
329-
}
353+
local qs
354+
---@diagnostic disable-next-line: param-type-mismatch
355+
if vim.api.nvim_buf_is_valid(codebuf) then
356+
local filetype = vim.bo[codebuf].filetype or "text"
357+
local filename = vim.fn.fnamemodify(vim.fn.bufname(codebuf), ":.")
358+
qs = {
359+
"请解释`" .. filename .. "`文件中的这段代码",
360+
"```" .. filetype,
361+
}
362+
else
363+
qs = {
364+
"请解释这段代码",
365+
"```",
366+
}
367+
end
330368
vim.list_extend(qs, selection)
331369
table.insert(qs, "```")
332-
table.insert(qs, "")
333-
vim.cmd("normal! G")
334370
vim.api.nvim_put(qs, "c", true, true)
335371
end
336372

337373
M.toggle_gpt = function(selection)
338-
if charwin then
374+
if chatwin then
339375
close_gpt_win()
340376
else
341377
create_gpt_win()
@@ -344,7 +380,7 @@ M.toggle_gpt = function(selection)
344380
end
345381

346382
M.gpt_chat = function()
347-
if charwin == nil then
383+
if chatwin == nil then
348384
create_gpt_win()
349385
end
350386
if chatruning then
@@ -353,7 +389,8 @@ M.gpt_chat = function()
353389
return
354390
end
355391
chatruning = true
356-
local list = vim.api.nvim_buf_get_lines(charbuf, 0, -1, false)
392+
---@diagnostic disable-next-line: param-type-mismatch
393+
local list = vim.api.nvim_buf_get_lines(chatbuf, 0, -1, false)
357394
local json = chat_request_json
358395
json.messages[1].content = M.chat_config.system_prompt
359396
-- 1 user, 2 assistant
@@ -378,7 +415,7 @@ M.gpt_chat = function()
378415
end
379416
end
380417
-- 跳转到最后一行
381-
vim.cmd("normal! G")
418+
vim.cmd("normal! G$")
382419
vim.api.nvim_put({ "", M.chat_config.system_title, "" }, "l", true, true)
383420

384421
local callback = function(opt)
@@ -388,12 +425,26 @@ M.gpt_chat = function()
388425
vim.fn.jobstop(opt.job)
389426
return
390427
end
428+
if opt.err == 1 then
429+
vim.notify("AI respond Error: " .. opt.data, vim.log.levels.WARN)
430+
return
431+
end
432+
if winleave then
433+
-- 防止回答问题时光标已经移动走了
434+
vim.api.nvim_set_current_win(chatwin)
435+
winleave = false
436+
end
437+
if cursormoved then
438+
-- 防止光标移动打乱回答顺序, 总是移动到最后一行
439+
vim.cmd("normal! G$")
440+
cursormoved = false
441+
end
391442
if done then
392443
vim.api.nvim_put({ "", "", M.chat_config.user_title, "" }, "c", true, true)
393444
chatruning = false
394445
return
395446
end
396-
if charbuf and vim.api.nvim_buf_is_valid(charbuf) then
447+
if chatbuf and vim.api.nvim_buf_is_valid(chatbuf) then
397448
if data:match("\n") then
398449
local ln = vim.split(data, "\n")
399450
vim.api.nvim_put(ln, "c", true, true)

lua/mappings.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,9 @@ if vim.fn.executable("fzy") == 1 then
299299
return tools.format_uri(filename)
300300
end
301301
end, function(b, _)
302-
vim.cmd("b " .. b)
302+
if b then
303+
vim.cmd("b " .. b)
304+
end
303305
end)
304306
end, { desc = "Find buffer" })
305307
map("n", "<leader>ff", function()
@@ -406,3 +408,10 @@ end, {
406408
nargs = 0,
407409
range = true,
408410
})
411+
map("n", "<A-k>", require("kide.tools.ai").toggle_gpt, { desc = "Gpt" })
412+
map("i", "<A-k>", require("kide.tools.ai").toggle_gpt, { desc = "Gpt" })
413+
map("v", "<A-k>", function()
414+
vim.api.nvim_feedkeys("\027", "xt", false)
415+
local text = require("kide.tools").get_visual_selection()
416+
require("kide.tools.ai").toggle_gpt(text)
417+
end, { desc = "Gpt" })

0 commit comments

Comments
 (0)