From 2242961925be4549e81ecdca5505f9845158d116 Mon Sep 17 00:00:00 2001 From: jdhao Date: Sun, 16 Feb 2025 19:13:24 +0100 Subject: [PATCH] Remove obsolete func (#377) - remove function: SynGroup, HasColorscheme and GetGitBranch - rewrite Inside_git_repo in lua instead --- autoload/utils.vim | 38 -------------------------------------- lua/custom-autocmd.lua | 6 ++++-- lua/mappings.lua | 3 --- lua/utils.lua | 14 ++++++++++++++ 4 files changed, 18 insertions(+), 43 deletions(-) diff --git a/autoload/utils.vim b/autoload/utils.vim index a004bd5..14fb651 100644 --- a/autoload/utils.vim +++ b/autoload/utils.vim @@ -10,23 +10,6 @@ function! s:Single_quote(str) abort return "'" . substitute(copy(a:str), "'", "''", 'g') . "'" endfunction -" Check the syntax group in the current cursor position, see -" https://stackoverflow.com/q/9464844/6064933 and -" https://jordanelver.co.uk/blog/2015/05/27/working-with-vim-colorschemes/ -function! utils#SynGroup() abort - if !exists('*synstack') - return - endif - echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")') -endfunction - -" Check if a colorscheme exists in runtimepath. -" The following two functions are inspired by https://stackoverflow.com/a/5703164/6064933. -function! utils#HasColorscheme(name) abort - let l:pat = printf('colors/%s.vim', a:name) - return !empty(globpath(&runtimepath, l:pat)) -endfunction - " Custom fold expr, adapted from https://vi.stackexchange.com/a/9094/15292 function! utils#VimFolds(lnum) abort " get content of current line and the line below @@ -146,27 +129,6 @@ function! utils#iso_time(timestamp) abort endfunction -" Check if we are inside a Git repo. -function! utils#Inside_git_repo() abort - let res = system('git rev-parse --is-inside-work-tree') - if match(res, 'true') == -1 - return v:false - else - " Manually trigger a special user autocmd InGitRepo (used lazyloading. - doautocmd User InGitRepo - return v:true - endif -endfunction - -function! utils#GetGitBranch() - let l:res = systemlist('git rev-parse --abbrev-ref HEAD')[0] - if match(l:res, 'fatal') != -1 - return '' - else - return l:res - endif -endfunction - " Redirect command output to a register for later processing. " Ref: https://stackoverflow.com/q/2573021/6064933 and https://unix.stackexchange.com/q/8101/221410 . function! utils#CaptureCommandOutput(command) abort diff --git a/lua/custom-autocmd.lua b/lua/custom-autocmd.lua index 0d6b1b0..e03a022 100644 --- a/lua/custom-autocmd.lua +++ b/lua/custom-autocmd.lua @@ -206,14 +206,16 @@ api.nvim_create_autocmd({ "VimEnter", "DirChanged" }, { group = api.nvim_create_augroup("git_repo_check", { clear = true }), pattern = "*", desc = "check if we are inside Git repo", - command = "call utils#Inside_git_repo()", + callback = function() + utils.inside_git_repo() + end, }) -- ref: https://vi.stackexchange.com/a/169/15292 api.nvim_create_autocmd("BufReadPre", { group = api.nvim_create_augroup("large_file", { clear = true }), pattern = "*", - desc = "check if we are inside Git repo", + desc = "optimize for large file", callback = function(ev) local file_size_limit = 524288 -- 0.5MB local f = ev.file diff --git a/lua/mappings.lua b/lua/mappings.lua index 7b4980e..368035b 100644 --- a/lua/mappings.lua +++ b/lua/mappings.lua @@ -143,9 +143,6 @@ keymap.set("x", "c", '"_c') -- Remove trailing whitespace characters keymap.set("n", "", "StripTrailingWhitespace", { desc = "remove trailing space" }) --- check the syntax group of current cursor position -keymap.set("n", "st", "call utils#SynGroup()", { desc = "check syntax group" }) - -- Copy entire buffer. keymap.set("n", "y", "%yank", { desc = "yank entire buffer" }) diff --git a/lua/utils.lua b/lua/utils.lua index 86a97d0..a1a3777 100644 --- a/lua/utils.lua +++ b/lua/utils.lua @@ -79,4 +79,18 @@ function M.is_compatible_version(expected_version) return true end +--- check if we are inside a git repo +--- @return boolean +function M.inside_git_repo() + local result = vim.system({ "git", "rev-parse", "--is-inside-work-tree" }, { text = true }):wait() + if result.code ~= 0 then + return false + end + + -- Manually trigger a special user autocmd InGitRepo (used lazyloading. + vim.cmd([[doautocmd User InGitRepo]]) + + return true +end + return M