lightline-gitdiff


Ⅰ. 插件描述

Show added, deleted and modified lines (`git diff`) in your statusline or lightline

Ⅱ. 基本信息

创建日期:  2019-03-30
使用用户:  6
Github星:  14
插件作者:  Niklaas Baudet von Gersdorff

Ⅲ. 安装方法

使用Vundle管理器安装

在你的.vimrc下添加:
Plugin 'niklaas/lightline-gitdiff-are-made-of'
… 然后在Vim中运行以下命令:
:source %
:PluginInstall

对于Vundle版本 < 0.10.2,请用上面的Bundle替换Plugin。

使用NeoBundle管理器安装

在你的.vimrc下添加:
NeoBundle 'niklaas/lightline-gitdiff-are-made-of'
… 然后在Vim中运行以下命令:
:source %
:NeoBundleInstall

使用VimPlug管理器安装

在你的.vimrc下添加:
Plug 'niklaas/lightline-gitdiff-are-made-of'
… 然后在Vim中运行以下命令:
:source %
:PlugInstall

使用Pathogen管理器安装

在终端中运行以下命令:
cd ~/.vim/bundle
git clone https://github.com/niklaas/lightline-gitdiff

Ⅳ. 文档说明

# lightline-gitdiff

I had been using airblade/vim-gitgutter for a while, however, I
felt distracted by the indicators shown in the sign column in the end. That
said, I wanted some lightweight signal indicating whether the current file
contains uncommitted changes to the repository or not.

So, this little plugin was born. I myself use
itchyny/lightline.vim to configure the statusline of vim easily,
so this is where the name of the plugin comes from. In addition, I embrace
lightlines's philosophy to provide a lightweight and stable, yet configurable
plugin that "just works". However, you can also integrate the plugin with vim's
vanilla statusline.

By default the plugin shows indicators such as the following:

A: 4 D: 6 M: 2

This says that, in comparison to the git index, the current buffer contains 12
uncommitted changes: four lines were deleted, six lines were added and two
lines only modified. If there are no uncommitted changes, nothing is shown to
reduce distraction.

You can see the plugin in action in my statusline/lightline:

screenshot

Installation

Use your favorite plugin manager to install the plugin. I personally prefer
vim-plug but feel free to choose another one:

Plug 'niklaas/lightline-gitdiff'

Configuration

Using vim's vanilla statusline

set statusline=%!lightline#gitdiff#get()

which let's your statusline consist of gitdiff's indicators only. (Probably
not what you want but you can consult :h statusline for further information
on how to include additional elements.)

Using lightline

let g:lightline = {
      \ 'active': {
      \   'left': [ [ 'mode', 'paste' ],
      \             [ 'gitbranch', 'filename', 'readonly', 'modified' ],
      \             [ 'gitdiff' ] ],
      \   'right': [ [ 'lineinfo' ],
      \              [ 'percent' ] ]
      \ },
      \ 'inactive': {
      \   'left': [ [ 'filename', 'gitversion' ] ],
      \ },
      \ 'component_function': {
      \   'gitbranch': 'fugitive#head',
      \ },
      \ 'component_expand': {
      \   'gitdiff': 'lightline#gitdiff#get',
      \ },
      \ 'component_type': {
      \   'gitdiff': 'middle',
      \ },
      \ }

which should give you pretty much the same result as shown in the screenshot.

Configuration

You can configure the appearance of the indicators and the separator between

  1. The following are the defaults:
let g:lightline#gitdiff#indicator_added = 'A: '
let g:lightline#gitdiff#indicator_deleted = 'D: '
let g:lightline#gitdiff#separator = ' '

A callback function is called every time the diff is updated and written to
the cache. By default this is lightline#update() to update lightline with
the newly calculated diff. However, you can also provide you own callback
function in the following way:

let g:lightline#gitdiff#update_callback = { -> MyCustomCallback() }

If the callback function is not defined, the error is caught. This allows to
use the plugin with any type of statusline plugin.

You can even change the algorithm that is used to calculate the diff. The
plugin comes bundled with two algorithms: numstat and word_diff_porcelain.
By default, the latter one is used because it allows to display modified lines.
numstat is much simpler but only supports showing added and deleted lines.
This resembles the default:

let g:lightline#gitdiff#algorithm =
      \ { buffer -> lightline#gitdiff#algorithms#word_diff_porcelain#calculate(buffer) }

Substitute word_diff_porcelain with numstat if you want to switch -- or
provide your own. Take a look at the source of both functions for inspiration
or consult me if you need help. I am happy to bundle additional faster and more
feature-rich algorithms in the package.

How it works / performance

In the background, lightline#gitdiff#get() calls git --numstat or `git
--word-diff=porcelain` (depending on the algorithm you choose, the latter being
the default) for the current buffer and caches the result.

If possible e.g., when an already open buffer is entered, the cache is used and
no call to git is made. git is only executed when reading or writing to a

  1. See the augroup in plugin/lightline/gitdiff.vim.

If you have any suggestions to improve the performance, please let me know. I
am happy to implement your suggestions on my own -- or you can create a pull
request.

Bugs etc.

Probably this code has some sharp edges. Feel free to report bugs, suggestions
and pull requests. I'll try to fix them as soon as possible.

添加新评论