1
0
mirror of https://github.com/jdhao/nvim-config.git synced 2025-06-08 14:14:33 +02:00

Compare commits

..

5 Commits

Author SHA1 Message Date
Li Peng
5cedd32ba3
Merge 074a53a66451d3445031633599c9aaa7a5c80f95 into ac421715d364949e72aa030afd83bd77fa49161a 2025-02-16 19:23:03 +08:00
jdhao
ac421715d3
Lualine async git info (#376)
make everything async and non-blocking and also remove the branch name fetching. It is not needed if we use the @{upstream} notation.
2025-02-16 01:15:07 +01:00
jdhao
28bda349e8
Update lualine config (#375)
- components are re-ordered
- add new component to show git ahead/behind info
- remove some unused component
2025-02-15 18:58:15 +01:00
jdhao
ee4bce4671 enable mouse in normal mode 2025-02-15 14:36:24 +01:00
jdhao
7f53743255 remove automcd to go to last place
This conflicts with the fuzzy finder, if you grep for a word using fuzzy
finder and then press enter to go to the place, it does not work
anymore, it will go the line where you are last time.
2025-02-14 22:12:33 +01:00
3 changed files with 99 additions and 70 deletions

View File

@ -1,5 +1,71 @@
local fn = vim.fn
local git_status_cache = {}
local on_exit_fetch = function(result)
if result.code == 0 then
git_status_cache.fetch_success = true
end
end
local function handle_numeric_result(cache_key)
return function(result)
if result.code == 0 then
git_status_cache[cache_key] = tonumber(result.stdout:match("(%d+)")) or 0
end
end
end
local async_cmd = function(cmd_str, on_exit)
local cmd = vim.tbl_filter(function(element)
return element ~= ""
end, vim.split(cmd_str, " "))
vim.system(cmd, { text = true }, on_exit)
end
local async_git_status_update = function()
-- Fetch the latest changes from the remote repository (replace 'origin' if needed)
async_cmd("git fetch origin", on_exit_fetch)
if not git_status_cache.fetch_success then
return
end
-- Get the number of commits behind
-- the @{upstream} notation is inspired by post: https://www.reddit.com/r/neovim/comments/t48x5i/git_branch_aheadbehind_info_status_line_component/
-- note that here we should use double dots instead of triple dots
local behind_cmd_str = "git rev-list --count HEAD..@{upstream}"
async_cmd(behind_cmd_str, handle_numeric_result("behind_count"))
-- Get the number of commits ahead
local ahead_cmd_str = "git rev-list --count @{upstream}..HEAD"
async_cmd(ahead_cmd_str, handle_numeric_result("ahead_count"))
end
local function get_ahead_behind_info()
local status = git_status_cache
if not status then
return ""
end
local msg = ""
if type(status.ahead_count) == "number" and status.ahead_count > 0 then
local ahead_str = string.format("↑[%d] ", status.ahead_count)
msg = msg .. ahead_str
end
if type(status.behind_count) == "number" and status.behind_count > 0 then
local behind_str = string.format("↓[%d] ", status.behind_count)
msg = msg .. behind_str
end
return msg
end
local timer = vim.loop.new_timer()
timer:start(0, 1000, async_git_status_update)
local function spell()
if vim.o.spell then
return string.format("[SPELL]")
@ -119,7 +185,7 @@ local virtual_env = function()
end
local get_active_lsp = function()
local msg = "No Active Lsp"
local msg = "🚫"
local buf_ft = vim.api.nvim_get_option_value("filetype", {})
local clients = vim.lsp.get_clients { bufnr = 0 }
if next(clients) == nil then
@ -147,7 +213,12 @@ require("lualine").setup {
},
sections = {
lualine_a = {
"mode",
{
"filename",
symbols = {
readonly = "[🔒]",
},
},
},
lualine_b = {
{
@ -159,21 +230,19 @@ require("lualine").setup {
color = { gui = "italic,bold" },
},
{
virtual_env,
color = { fg = "black", bg = "#F1CA81" },
},
},
lualine_c = {
{
"filename",
symbols = {
readonly = "[🔒]",
},
get_ahead_behind_info,
color = { fg = "#E0C479" },
},
{
"diff",
source = diff,
},
{
virtual_env,
color = { fg = "black", bg = "#F1CA81" },
},
},
lualine_c = {
{
"%S",
color = { gui = "bold", fg = "cyan" },
@ -184,22 +253,29 @@ require("lualine").setup {
},
},
lualine_x = {
{
ime_state,
color = { fg = "black", bg = "#f46868" },
},
{
get_active_lsp,
icon = " LSP:",
icon = "📡",
},
{
"diagnostics",
sources = { "nvim_diagnostic" },
symbols = { error = "🆇 ", warn = "⚠️ ", info = " ", hint = "" },
},
{
trailing_space,
color = "WarningMsg",
},
{
mixed_indent,
color = "WarningMsg",
},
},
lualine_y = {
{ "encoding", fmt = string.upper },
{
"encoding",
fmt = string.upper,
},
{
"fileformat",
symbols = {
@ -209,16 +285,12 @@ require("lualine").setup {
},
},
"filetype",
{
ime_state,
color = { fg = "black", bg = "#f46868" },
},
},
lualine_z = {
{
trailing_space,
color = "WarningMsg",
},
{
mixed_indent,
color = "WarningMsg",
},
"location",
"progress",
},

View File

@ -142,49 +142,6 @@ api.nvim_create_autocmd("TermOpen", {
end,
})
-- Return to last cursor position when opening a file, note that here we cannot use BufReadPost
-- as event. It seems that when BufReadPost is triggered, FileType event is still not run.
-- So the filetype for this buffer is empty string.
api.nvim_create_autocmd("FileType", {
group = api.nvim_create_augroup("resume_cursor_position", { clear = true }),
pattern = "*",
callback = function(ev)
local mark_pos = api.nvim_buf_get_mark(ev.buf, '"')
local last_cursor_line = mark_pos[1]
local max_line = vim.fn.line("$")
local buf_filetype = api.nvim_get_option_value("filetype", { buf = ev.buf })
local buftype = api.nvim_get_option_value("buftype", { buf = ev.buf })
-- only handle normal files
if buf_filetype == "" or buftype ~= "" then
return
end
-- Only resume last cursor position when there is no go-to-line command (something like '+23').
if vim.fn.match(vim.v.argv, [[\v^\+(\d){1,}$]]) ~= -1 then
return
end
if last_cursor_line > 1 and last_cursor_line <= max_line then
-- vim.print(string.format("mark_pos: %s", vim.inspect(mark_pos)))
-- it seems that without vim.schedule, the cursor position can not be set correctly
vim.schedule(function()
local status, result = pcall(api.nvim_win_set_cursor, 0, mark_pos)
if not status then
api.nvim_err_writeln(
string.format("Failed to resume cursor position. Context %s, error: %s", vim.inspect(ev), result)
)
end
end)
-- the following two ways also seem to work,
-- ref: https://www.reddit.com/r/neovim/comments/104lc26/how_can_i_press_escape_key_using_lua/
-- vim.api.nvim_feedkeys("g`\"", "n", true)
-- vim.fn.execute("normal! g`\"")
end
end,
})
local number_toggle_group = api.nvim_create_augroup("numbertoggle", { clear = true })
api.nvim_create_autocmd({ "BufEnter", "FocusGained", "InsertLeave", "WinEnter" }, {
pattern = "*",

View File

@ -72,7 +72,7 @@ set wildmode=list:longest
set scrolloff=3
" Use mouse to select and resize windows, etc.
set mouse=
set mouse=n
set mousemodel=popup " Set the behaviour of mouse
set mousescroll=ver:1,hor:0