vim-flog icon indicating copy to clipboard operation
vim-flog copied to clipboard

FR: fold the body of the commit under the cursor

Open gh-liu opened this issue 1 year ago • 6 comments

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.

gh-liu avatar Jan 20 '24 07:01 gh-liu

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?

rbong avatar Jan 20 '24 16:01 rbong

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

gh-liu avatar Jan 21 '24 00:01 gh-liu

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.

rbong avatar Jan 21 '24 01:01 rbong

This is part of why folding only works manually now.

Using foldexpr may work.

gh-liu avatar Jan 24 '24 09:01 gh-liu

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

rbong avatar Jan 24 '24 11:01 rbong

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

gh-liu avatar Jan 26 '24 05:01 gh-liu