From 2d7ab84b3aba135c6d4b8855a9ebfbf9478d5b1d Mon Sep 17 00:00:00 2001 From: Ade Attwood Date: Sun, 15 Oct 2023 15:40:41 +0100 Subject: [PATCH] feat(vim): add prr plugin for a better code review experience Sets code folding of diffs and hunks so we can review hunks on there own. This is good for larger pull requests, you can focus on the hunk and not get overwhelmed by the rest of the code. Adds a go to definition mapping for prr lines. This will allow you to go straight to the line of code when reviewing a pull request. Seeing the code in full context is really helpful when reviewing. You are then there in your editor with all the linters and the LSP. --- site-modules/core/files/vim/ftplugin/prr.vim | 31 ++++++++++++++++++++ site-modules/core/files/vim/plugin/prr.lua | 26 ++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 site-modules/core/files/vim/ftplugin/prr.vim create mode 100644 site-modules/core/files/vim/plugin/prr.lua diff --git a/site-modules/core/files/vim/ftplugin/prr.vim b/site-modules/core/files/vim/ftplugin/prr.vim new file mode 100644 index 0000000..c70a486 --- /dev/null +++ b/site-modules/core/files/vim/ftplugin/prr.vim @@ -0,0 +1,31 @@ +" +" function for the `foldexpr` to fold git diffs and hunks +" +" Inspired from https://github.com/sgeb/vim-diff-fold +" +function! s:fold_diff_hunk() + let l:line=getline(v:lnum) + + if l:line =~# '^> \(diff\|Index\)' + return '>1' + elseif l:line =~# '^> \(@@\|\d\)' + return '>2' + elseif l:line =~# '^> \*\*\* \d\+,\d\+ \*\*\*\*$' + return '>2' + elseif l:line =~# '^> --- \d\+,\d\+ ----$' + return '>2' + else + return '=' + endif +endfunction + +setlocal foldenable +setlocal nomodeline formatoptions-=croq formatoptions+=tl +setlocal foldmethod=expr +setlocal foldexpr=s:fold_diff_hunk() +setlocal foldlevel=99 + +" Add "go to definition" mapping for prr lines. This will allow you to go to +" the line of code when reviewing a pull request. Seeing the code in full +" context is really helpful when reviewing. +noremap gd :PrrGoToLine diff --git a/site-modules/core/files/vim/plugin/prr.lua b/site-modules/core/files/vim/plugin/prr.lua new file mode 100644 index 0000000..7e2ada9 --- /dev/null +++ b/site-modules/core/files/vim/plugin/prr.lua @@ -0,0 +1,26 @@ +vim.api.nvim_create_user_command("PrrGoToLine", function() + local content = vim.api.nvim_buf_get_lines(0, 0, -1, false) + local line, column = unpack(vim.api.nvim_win_get_cursor(0)) + + local target_file = "" + local target_line = 0 + + for i = 1, line do + local line_content = content[i] + local file_match = line_content:match "> diff .* b/(.*)" + if file_match ~= nil then + target_file = file_match + end + + local line_match = line_content:match "+(%d+)" + if line_match ~= nil then + target_line = line_match + elseif line_content:match "> (-).*" == nil then + target_line = target_line + 1 + end + end + + vim.cmd("e " .. target_file) + vim.fn.cursor(target_line - 1, column) +end, { bang = true, desc = "Open the current file at the poin in a prr buffer" }) +