[nvim] Add gitlens, scratchfile lua plugins
Added gitlens to show git blame for cursorline after short time. Added makescratch plugin to create a new scratch window via simple command.
This commit is contained in:
parent
e6e384270a
commit
00b15048df
4 changed files with 81 additions and 0 deletions
42
nvim/.config/nvim/lua/gitlens.lua
Normal file
42
nvim/.config/nvim/lua/gitlens.lua
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
local M = {}
|
||||||
|
local api = vim.api
|
||||||
|
|
||||||
|
function M.blameVirtText()
|
||||||
|
|
||||||
|
-- get the current file extension
|
||||||
|
local ft = vim.fn.expand('%:h:t')
|
||||||
|
|
||||||
|
if ft == '' or ft == 'bin' then -- if we are in a scratch buffer, unknown filetype, or nvim's terminal window
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
M.clearBlameVirtText()
|
||||||
|
|
||||||
|
local currFile = vim.fn.expand('%')
|
||||||
|
local line = api.nvim_win_get_cursor(0)
|
||||||
|
local blame = vim.fn.system(string.format('git blame -c -L %d,%d %s', line[1], line[1], currFile))
|
||||||
|
local hash = vim.split(blame, '%s')[1]
|
||||||
|
local cmd = string.format("git show %s ", hash).."--format='%an | %ar | %s'"
|
||||||
|
|
||||||
|
if hash == '00000000' then
|
||||||
|
text = 'Not Committed Yet'
|
||||||
|
else
|
||||||
|
text = vim.fn.system(cmd)
|
||||||
|
text = vim.split(text, '\n')[1]
|
||||||
|
if text:find("fatal") then -- if the call to git show fails
|
||||||
|
text = 'Not Committed Yet'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- set virtual text for namespace 2 with the content from git and assign it to the higlight group 'GitLens'
|
||||||
|
api.nvim_buf_set_virtual_text(0, 2, line[1] - 1, {{ text,'GitLens' }}, {})
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
-- important for clearing out the text when our cursor moves
|
||||||
|
function M.clearBlameVirtText()
|
||||||
|
-- clear out virtual text from namespace 2 (the namespace we will set later)
|
||||||
|
api.nvim_buf_clear_namespace(0, 2, 0, -1)
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
11
nvim/.config/nvim/lua/scratchpad.lua
Normal file
11
nvim/.config/nvim/lua/scratchpad.lua
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
local api = vim.api
|
||||||
|
local M= {}
|
||||||
|
|
||||||
|
function M.makeScratch()
|
||||||
|
api.nvim_command('enew')
|
||||||
|
-- vim.bo[0].bufhidden=hide
|
||||||
|
vim.bo[0].buftype=nofile
|
||||||
|
vim.bo[0].swapfile=false
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
23
nvim/.config/nvim/plugin/personal/gitlens.vim
Normal file
23
nvim/.config/nvim/plugin/personal/gitlens.vim
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
function s:setupGitLens()
|
||||||
|
" -- disable gitlens unconditionally, and remove virtual text
|
||||||
|
lua require('gitlens').clearBlameVirtText()
|
||||||
|
augroup GitLens
|
||||||
|
autocmd!
|
||||||
|
augroup END
|
||||||
|
|
||||||
|
" -- if it is wanted enable gitlens on holding the cursor
|
||||||
|
if g:gitlens_enable
|
||||||
|
augroup GitLens
|
||||||
|
autocmd! CursorHold * lua require('gitlens').blameVirtText()
|
||||||
|
autocmd! CursorMoved * lua require('gitlens').clearBlameVirtText()
|
||||||
|
autocmd! CursorMovedI * lua require('gitlens').clearBlameVirtText()
|
||||||
|
augroup END
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function s:toggleGitLens()
|
||||||
|
let g:gitlens_enable = !get(g:, 'gitlens_enable', 1)
|
||||||
|
call s:setupGitLens()
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
command! GitLensToggle call s:toggleGitLens()
|
5
nvim/.config/nvim/plugin/personal/makescratch.vim
Normal file
5
nvim/.config/nvim/plugin/personal/makescratch.vim
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
function s:makeScratch()
|
||||||
|
lua require('scratchpad').makeScratch()
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
command! ScratchPad call s:makeScratch()
|
Loading…
Reference in a new issue