preservim / vim-pencil

Rethinking Vim as a tool for writing
Other
1.57k stars 38 forks source link

gitcommit autoformat #63

Open mattmahn opened 7 years ago

mattmahn commented 7 years ago

The issue

I use the following git commit template:

# If applied, this commit will...

# Why is this change being made?

# Provide links to any relevant tickets, URLs or other resources

So I press o to start writing my commit message (e.g., "Fix all the bugs") on the next line, but the text is auto-formatted up to the comment line (cursor starts at line 1 column 1):

# If applied, this commit will... Fix all the bugs

# Why is this change being made?

# Provide links to any relevant tickets, URLs or other resources

Attempt to fix

I used this function to find out the highlighting group that applies to the first/comment lines. It is ['gitcommitComment'].

I added this to my .vimrc:

let g:pencil#autoformat_config = {
      \ 'gitcommit': {
      \   'black': [
      \     'gitcommitComment'
      \   ],
      \   'enforce-previous-line': 1
      \ }
      \ }
augroup pencil
  autocmd!
  autocmd FileType git,*commit* call pencil#init({
        \ 'wrap': 'hard'
        \ })
augroup end

Lines that are not adjacent to comments are auto-formatted properly, but lines that are adjacent get auto-formatted with the comment line. (I get the same results when I remove the 'enforce-previous-line': 1 line, too.)

Is there some setting that I'm missing?

reedes commented 7 years ago

I'll try to reproduce this in the next week or so.

casr commented 4 years ago

I've come across this today too. Here's my example, if it helps...

I haven't changed the default message so I receive this as a starting point:


# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch example
# Your branch is ahead of 'origin/master' by 5 commits.
#   (use "git push" to publish your local commits)
#
# Changes not staged for commit:
#   modified:   example/tests/widget.py
#
# Untracked files:
#   example/tests/foo.py
#

Normally at this point I hit i and start typing the summary and then Enter twice to start the description. However, after pressing i and the first character, the autoformat kicks in and I get:

T # Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit. # # On
branch example # Your branch is ahead of 'origin/master' by 5 commits.
#   (use "git push" to publish your local commits) # # Changes not staged
for commit: #   modified:   example/tests/widget.py # # Untracked files:
#   example/tests/foo.py #

You can avoid this by hitting O instead which gives the gap required for autoformat to ignore it but that means I'll have to retrain my muscle memory :)

My current configuration is:


" reedes/vim-pencil {{{
let g:pencil#autoformat_config = {
    \   'gitcommit': {
    \     'black': [
    \       'gitcommit(Blank|Comment|FirstLine|Summary)',
    \     ],
    \   },
    \ }
" }}}

" Prose {{{
augroup prose
    autocmd!
    autocmd FileType gitcommit,gitsendmail
        \   call pencil#init({'wrap': 'hard'})
        \ | setlocal spell spelllang=en_gb
augroup END
" }}}
alerque commented 4 years ago

In my git there seem to be two layers of defense against this:

  1. the commit comments start out on line 3, and there are two blank lines when I first open the commit, meaning that even after i my cursor is not in a paragraph attached to the comments.

  2. Pencil is ignoring comment lines entirely even if I do simulate your cursor position / actions.

Those are with an out of the box configuration without even the extra configuration you've used. I wonder what is actually different...

casr commented 4 years ago

Strange that it works for you. Did you initialise it like I have (call pencil#init on the filetype) or in some other way?

Looking at the default configuration it appears that nothing special happens for gitcommit and I had tried it without setting g:pencil#autoformat_config before seeing if it need additional configuration.

alerque commented 4 years ago

Yes, I tried with pencil#init being called from an auto-command on the file type (not by default setup, but I tried it).

I wonder if having tpope's vim-fugitive enabled is overriding some vim-pencil things and setting things right for me.

casr commented 4 years ago

I tried with and without vim-fugitive but it didn't seem to make a difference in my setup.

Here's a minimal .vimrc to help us narrow down the problem.

runtime defaults.vim

augroup prose
    autocmd!
    autocmd FileType gitcommit call pencil#init({'wrap': 'hard'})
augroup END

if exists('*minpac#init')
    call minpac#init()
    call minpac#add('k-takata/minpac', {'type': 'opt'})

    call minpac#add('reedes/vim-pencil')
"   call minpac#add('tpope/vim-fugitive')
endif

command! PackUpdate packadd minpac | source $MYVIMRC |
    \ call minpac#update('', {'do': 'call minpac#status()'})
command! PackClean packadd minpac | source $MYVIMRC | call minpac#clean()
command! PackStatus packadd minpac | source $MYVIMRC | call minpac#status()

To setup:

mkdir -p /tmp/test-vim
cd /tmp/test-vim

# vim package manager
git clone --depth 1 git://github.com/k-takata/minpac.git pack/minpac/opt/minpac

vim vimrc
# copy the above in

# backup current vim setup and move over to test vim setup
mv ~/.vim ~/backup-vim
ln -s /tmp/test-vim ~/.vim

vim
# Run :PackUpdate then :q

git init
EDITOR=vim git commit --allow-empty