Skip to content

Commit 14315aa

Browse files
committed
refactor: migrate vim.fn.system to vim.system and add statusline updates
- Replace `vim.fn.system` calls with `vim.system` for better error handling - Add statusline updates for terminal, plantuml, and GPT operations - Refactor statusline logic into a separate `stl.lua` module - Improve camel case conversion and visual selection handling - Add timer-based statusline updates for long-running operations - Clean up and organize code in `init.lua` and related modules
1 parent 47c650d commit 14315aa

File tree

12 files changed

+294
-181
lines changed

12 files changed

+294
-181
lines changed

init.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ require("options")
6969
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
7070
if not (vim.uv or vim.loop).fs_stat(lazypath) then
7171
local lazyrepo = "https://github.com/folke/lazy.nvim.git"
72-
local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
73-
if vim.v.shell_error ~= 0 then
72+
local out = vim.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath }):wait()
73+
if out.code ~= 0 then
7474
vim.api.nvim_echo({
7575
{ "Failed to clone lazy.nvim:\n", "ErrorMsg" },
76-
{ out, "WarningMsg" },
76+
{ out.stdout, "WarningMsg" },
7777
{ "\nPress any key to exit..." },
7878
}, true, {})
7979
vim.fn.getchar()

lua/kide/gpt/chat.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@ M.gpt_chat = function()
183183
---@diagnostic disable-next-line: param-type-mismatch
184184
local list = vim.api.nvim_buf_get_lines(state.chatbuf, 0, -1, false)
185185
local json = chat_request_json
186+
json.messages = {
187+
{
188+
content = "",
189+
role = "system",
190+
},
191+
}
192+
186193
json.messages[1].content = M.chat_config.system_prompt
187194
-- 1 user, 2 assistant
188195
local flag = 0

lua/kide/gpt/commit.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ function M.commit_message(diff, callback)
3737
end
3838

3939
M.commit_diff_msg = function()
40-
local diff = vim.fn.system({ "git", "diff", "--cached" })
41-
if vim.startswith(diff, "error:") then
40+
local diff = vim.system({ "git", "diff", "--cached" }):wait()
41+
if diff.code ~= 0 then
4242
return
4343
end
4444
local codebuf = vim.api.nvim_get_current_buf()
@@ -75,7 +75,7 @@ M.commit_diff_msg = function()
7575
vim.api.nvim_put(put_data, "c", true, true)
7676
end
7777
end
78-
M.commit_message(diff, callback)
78+
M.commit_message(diff.stdout, callback)
7979
end
8080

8181
M.setup = function()
@@ -88,7 +88,7 @@ M.setup = function()
8888
group = augroup("gpt_commit_msg"),
8989
callback = function(event)
9090
command(event.buf, "GptCommitMsg", function(_)
91-
require("kide.gpt.commit").commit_diff_msg()
91+
M.commit_diff_msg()
9292
end, {
9393
desc = "Gpt Commit Message",
9494
nargs = 0,

lua/kide/gpt/sse.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@ local function handle_sse_events(cmd, callback)
7474
end
7575
end,
7676
on_stderr = function(_, _, _) end,
77-
on_exit = function(_, _, _) end,
77+
on_exit = function(_, code, _)
78+
require("kide").clean_stl_status(code)
79+
end,
7880
})
7981
end
8082

8183
function M.request(json, callback)
84+
require("kide").timer_stl_status("")
8285
local body = vim.fn.json_encode(json)
8386
local cmd = {
8487
"curl",

lua/kide/init.lua

Lines changed: 26 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,32 @@
1-
local M = {}
2-
local uv = vim.uv or vim.loop
3-
local git_repo = true
4-
-- 参考 https://github.com/mfussenegger/dotfiles
5-
function M.statusline()
6-
local parts = {
7-
"%<",
8-
}
9-
local git = M.git_status()
10-
if git then
11-
table.insert(parts, " %#DiagnosticError# %#StatusLine#" .. git.head)
12-
if git.added and git.added > 0 then
13-
vim.list_extend(parts, { " %#Added# ", tostring(git.added) })
14-
end
15-
if git.removed and git.removed > 0 then
16-
vim.list_extend(parts, { " %#Removed#󰍵 ", tostring(git.removed) })
17-
end
18-
if git.changed and git.changed > 0 then
19-
vim.list_extend(parts, { " %#Changed# ", tostring(git.changed) })
20-
end
21-
end
22-
local fstatus = M.file_or_lsp_status()
23-
vim.list_extend(parts, fstatus)
24-
25-
local counts = vim.diagnostic.count(0, { severity = { min = vim.diagnostic.severity.WARN } })
26-
local num_errors = counts[vim.diagnostic.severity.ERROR] or 0
27-
local num_warnings = counts[vim.diagnostic.severity.WARN] or 0
28-
table.insert(parts, " %#DiagnosticWarn#%r%m")
29-
if num_errors > 0 then
30-
vim.list_extend(parts, { "%#DiagnosticError#", " 󰅙 ", tostring(num_errors), " " })
31-
elseif num_warnings > 0 then
32-
vim.list_extend(parts, { "%#DiagnosticWarn#", "", tostring(num_warnings), " " })
33-
end
34-
table.insert(parts, "%=")
35-
vim.list_extend(parts, { "%#StatusLine#", "%l:%c", " " })
36-
local ft = vim.bo.filetype
37-
if ft and ft ~= "" then
38-
local clients = vim.lsp.get_clients({ bufnr = 0 })
39-
if clients and #clients > 0 then
40-
vim.list_extend(parts, { "%#DiagnosticInfo#", "[ ", clients[1].name, "] " })
41-
end
42-
vim.list_extend(parts, { "%#StatusLine#", ft, " " })
43-
end
44-
vim.list_extend(parts, { "%#StatusLine#", "%{&ff}", " " })
45-
vim.list_extend(parts, { "%#StatusLine#", "%{&fenc}", " " })
46-
return table.concat(parts, "")
1+
local M = {
2+
stl_timer = vim.uv.new_timer(),
3+
stl_stop = false,
4+
}
5+
function M.clean_stl_status(code)
6+
if M.stl_timer then
7+
M.stl_stop = true
8+
M.stl_timer:stop()
9+
end
10+
require("kide.stl").exit_status(code)
4711
end
4812

49-
function M.git_status()
50-
return vim.b[0].gitsigns_status_dict
51-
end
52-
53-
function M.file_or_lsp_status()
54-
local mode = vim.api.nvim_get_mode().mode
55-
local lsp_status = vim.lsp.status()
56-
if mode ~= "n" or lsp_status == "" then
57-
local buf = vim.api.nvim_get_current_buf()
58-
local filename = vim.uri_from_bufnr(buf)
59-
local devicons = require("nvim-web-devicons")
60-
local icon, name = devicons.get_icon_by_filetype(vim.bo[buf].filetype, { default = true })
61-
if name then
62-
return { " ", "%#" .. name .. "#", icon, " %#StatusLine#", M.format_uri(filename) }
63-
else
64-
return { " ", icon, " ", M.format_uri(filename) }
65-
end
66-
end
67-
return { " ", "%#StatusLine#", lsp_status }
68-
end
69-
function M.format_uri(uri)
70-
if vim.startswith(uri, "jdt://") then
71-
local jar, pkg, class = uri:match("^jdt://contents/([^/]+)/([^/]+)/(.+)?")
72-
return string.format("%s::%s (%s)", pkg, class, jar)
73-
else
74-
local fname = vim.fn.fnamemodify(vim.uri_to_fname(uri), ":.")
75-
fname = fname:gsub("src/main/java/", "s/m/j/")
76-
fname = fname:gsub("src/test/java/", "s/t/j/")
77-
return fname
78-
end
79-
end
80-
function M.dap_status()
81-
local ok, dap = pcall(require, "dap")
82-
if not ok then
83-
return ""
84-
end
85-
local status = dap.status()
86-
if status ~= "" then
87-
return status .. " | "
13+
---@param title string
14+
function M.timer_stl_status(title)
15+
require("kide.stl").update_status(title)
16+
if M.stl_timer ~= nil then
17+
M.stl_stop = false
18+
M.stl_timer:stop()
19+
M.stl_timer:start(
20+
0,
21+
200,
22+
vim.schedule_wrap(function()
23+
if not M.stl_stop then
24+
require("kide.stl").update_status(title)
25+
vim.cmd.redrawstatus()
26+
end
27+
end)
28+
)
8829
end
89-
return ""
9030
end
9131

92-
function M.tabline()
93-
local parts = {}
94-
local devicons = require("nvim-web-devicons")
95-
for i = 1, vim.fn.tabpagenr("$") do
96-
local tabpage = vim.fn.gettabinfo(i)[1]
97-
local winnr = tabpage.windows[1]
98-
local bufnr = vim.fn.winbufnr(winnr)
99-
local bufname = vim.fn.bufname(bufnr)
100-
local filename = vim.fn.fnamemodify(bufname, ":t")
101-
102-
local icon, name = devicons.get_icon_by_filetype(vim.bo[bufnr].filetype, { default = true })
103-
table.insert(parts, " %#" .. name .. "#")
104-
table.insert(parts, icon)
105-
table.insert(parts, " ")
106-
if i == vim.fn.tabpagenr() then
107-
table.insert(parts, "%#TabLineSel#")
108-
else
109-
table.insert(parts, "%#TabLine#")
110-
end
111-
if not filename or filename == "" then
112-
filename = "[No Name]"
113-
end
114-
table.insert(parts, filename)
115-
end
116-
return table.concat(parts, "")
117-
end
11832
return M

lua/kide/lsp/jdtls.lua

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,19 @@ local function test_with_profile(test_fn)
410410
noDebug = true,
411411
},
412412
after_test = function()
413-
vim.fn.system(
414-
"java -jar "
415-
.. get_async_profiler_cov()
416-
.. " jfr2flame "
417-
.. utils.tmpdir_file("profile.jfr")
418-
.. " "
419-
.. utils.tmpdir_file("profile.html")
420-
)
421-
utils.open_fn(utils.tmpdir_file("profile.html"))
413+
local result = vim
414+
.system({
415+
"java",
416+
"-jar",
417+
get_async_profiler_cov(),
418+
"jfr2flame",
419+
utils.tmpdir_file("profile.jfr"),
420+
utils.tmpdir_file("profile.html"),
421+
})
422+
:wait()
423+
if result.code == 0 then
424+
utils.open_fn(utils.tmpdir_file("profile.html"))
425+
end
422426
end,
423427
})
424428
end)

0 commit comments

Comments
 (0)