rbong / vim-flog

A blazingly fast, stunningly beautiful, exceptionally powerful git branch viewer for Vim/Neovim.
750 stars 22 forks source link

FR: fold the body of the commit under the cursor #118

Open gh-liu opened 8 months ago

gh-liu commented 8 months ago

Provide the fold way to collapse the body of the commit under the cursor, like the git file does.

The doc says using < > = can collapse the body of the commit under the cursor, but fold may be another intuitional way.

rbong commented 8 months ago

Sorry, I'm not quite sure what you mean, can you provide a step-by-step description of how you would like this to work?

gh-liu commented 8 months ago

Sorry, I'm not quite sure what you mean, can you provide a step-by-step description of how you would like this to work?

  1. Select a line of file by V, then run the :'<,'>Flog command
  2. The gloggraph buffer will list commits and each commit fllowed by a diff
  3. Open the diff in a flod-side-window and run :set foldmethod=syntax command then the diff in a flod-side-window is foldable
  4. What I would like is the diff in the step2 could be folded like in the step3 Screenshot 2024-01-21 at 08 35 20
rbong commented 8 months ago

I see what you mean. Thanks for the suggestion!

Unfortunately this may be technically impossible in every case because there's no way syntactically to determine the end of a commit with -no-graph enabled. There may be no way to draw a distinction between the first first line of a new commit and a line in the diff (unless if all diffs never contains a newline - which I think they do). This is part of why folding only works manually now.

Even if it's implemented partially, the Flog syntax groups are quite complex, so it's not so easy to implement, and I'm guessing the Fugitive fold groups are complex as well. I'm not against the idea but I will probably leave this up to a PR.

gh-liu commented 8 months ago

This is part of why folding only works manually now.

Using foldexpr may work.

rbong commented 8 months ago

You're right, still I will probably leave this up to a PR.

gh-liu commented 8 months ago

I do not know if the code below is the best way, but there is a issue that the foldtext can not be highlighted. Do you have any thoughts?

" vim-flog/autoload/flog/floggraph/fold.vim
function! flog#floggraph#fold#foldexpr() abort
  let l:cur_line = v:lnum
  let l:commit = flog#floggraph#commit#GetAtLine(l:cur_line)
  if empty(l:commit)
    return 0
  endif
  if l:cur_line == l:commit.line
    return 0
  endif
  return 1
endfunction

function! flog#floggraph#fold#foldtext() abort
  let l:commit = flog#floggraph#commit#GetAtLine(v:foldstart)
  if empty(l:commit)
    return getline(v:foldstart)
  endif
  return l:commit.collapsed_body
endfunction
setlocal foldmethod=expr
setlocal foldexpr=flog#floggraph#fold#foldexpr()
setlocal foldtext=flog#floggraph#fold#foldtext()
Snipaste_2024-01-26_11-41-41 Snipaste_2024-01-26_13-18-33