dandavison / delta

A syntax-highlighting pager for git, diff, grep, and blame output
https://dandavison.github.io/delta/
MIT License
22.92k stars 381 forks source link

Using Delta as a Vim/NeoVim library #338

Open srcrip opened 4 years ago

srcrip commented 4 years ago

I've recently become very interested in the fact that the library used by vim and nvim for diffing, called xdiff, doesn't support in line diffs (at the word boundary) like your tool does. Obviously your tool isn't written to be a library, but I think if it were possible to create a vim or nvim plugin to use this tool to generate the two-way diffs used in the editor, it would be some massive exposure.

I don't think it even needs an API, cause vim/nvim can call an external program for diffing as far as I'm aware.

Does anyone know how doable this would be?

dandavison commented 4 years ago

Hi @Sevensidedmarble, thanks for opening this. Have you seen my project doing this for Emacs (magit)? https://github.com/dandavison/magit-delta It might contain some parallels to what would be needed for vim/nvim.

It required very little code. What it relies on is an existing emacs-lisp library that can take raw text with ANSI color escape sequences as input, parse it, excise the escape sequences and annotate the strings in the Emacs buffer with the data structures that Emacs uses to add colors to text (i.e. "text properties" or "overlays").

In fact, I use delta in magit a lot and find it beneficial, so I do believe that it would be beneficial to make the same thing possible in vim/nvim. I'm happy to help where I can, but I don't know anything about vim/nvim. From a very quick google search what we want to do is similar to what is discussed in

https://unix.stackexchange.com/questions/7695/how-to-make-vim-display-colors-as-indicated-by-color-codes https://vi.stackexchange.com/questions/485/can-vim-interpret-terminal-color-escape-codes https://stackoverflow.com/questions/10592715/ansi-color-codes-in-vim/38357506

dandavison commented 4 years ago

Does vim/nvim have the ability to run a shell and display colored output from the shell faithfully within the vim/nvim application? If there's something like that that works well, then it suggests that the required technology exists already.

Kr1ss-XD commented 4 years ago

As far as I'm aware, there's no such feature in (vanilla) Vim. There's a way to run a shell command and have its output inserted into the edited text, but the color escape codes won't be processed. For example, if I do :r!ls -Al --color .git/, Vim inserts the following verbatim into the text (:r!<command> is the command to open a subshell and execute the command) :

insgesamt 20
drwxrwsr-x 1 kriss build   0  9. Sep 19:51 branches
-rw-rw-r-- 1 kriss build 262  9. Sep 19:51 config
-rw-rw-r-- 1 kriss build  73  9. Sep 19:51 description
-rw-rw-r-- 1 kriss build  23  9. Sep 19:51 HEAD
drwxrwsr-x 1 kriss build 460  9. Sep 19:51 hooks
-rw-rw-r-- 1 kriss build 289  9. Sep 19:51 index
drwxrwsr-x 1 kriss build  14  9. Sep 19:51 info
drwxrwsr-x 1 kriss build  16  9. Sep 19:51 logs
drwxrwsr-x 1 kriss build  52  9. Sep 19:51 objects
-rw-rw-r-- 1 kriss build 114  9. Sep 19:51 packed-refs
drwxrwsr-x 1 kriss build  32  9. Sep 19:51 refs

Note this is plain Vim. Maybe there's a plugin which can interpret the escape codes. Also, I don't know what this looks like with Neovim either.

srcrip commented 4 years ago

They both have the option of using terminal buffers, which obviously parses all codes correctly. My gut tells me this would probably be the way to do it, I'm just not great with vimscript, or know enough about the nvim lua api, to say exactly how.

kevinhwang91 commented 3 years ago

Is anyone going to do this plugin(or have done)? I wanna try it out, otherwise, I will complete it in neovim after the documents of two personal plugins are done.(Not a promise)

ghost commented 3 years ago

I find this issue now and wonder what @Sevensidedmarble and @kevinhwang91 wants is like this?

ss

This could be done by fugitive.vim.

If you would like to just print, not opening on editor, just :bo term git diff (in the case you set core.pager = delta)

ss 1
kevinhwang91 commented 3 years ago

I find this issue now and wonder what @Sevensidedmarble and @kevinhwang91 wants is like this?

ss

This could be done by fugitive.vim.

If you would like to just print, not opening on editor, just :bo term git diff (in the case you set core.pager = delta)

ss 1

Thanks, I knew this information before commented. There're two reasons I wanna build a plugin library based on fugitive.vim which is easier to implement:

  1. I prefer a floating window for preview rather than a tab or a splited window while navigating commit, more like modern T/GUI git tools, such as lazygit, tig, gitui and so on.
  2. I also use vim-flog, extra library is better than a simple script based on fugitive.vim.

neovim is migrating from vimscript to lua, I'm following their steps and learning lua to refactor my private config and plugin. Not enough time to make this plugin recently.

ghost commented 3 years ago

@kevinhwang91

I prefer a floating window

This could be done by fzf-preview.vim. It's already integrated with fugitive and can custom preview.

kevinhwang91 commented 3 years ago

git diff is not only to a commit, but also to a file. Run :Git with fugitive in vim, you will know my idea if you have an experience for using IDE for code review before committing.

I only use fzf preview git commit in the current buffer.

ghost commented 3 years ago

@kevinhwang91 You can do git diff to file as well. Use CocCommand fzf-preview.GitStatus with config below as I said before.

let g:fzf_preview_git_status_preview_command =  "[[ $(git diff --cached -- {-1}) != \"\" ]] && git diff --cached --color=always -- {-1} | delta || " .
\ "[[ $(git diff -- {-1}) != \"\" ]] && git diff --color=always -- {-1} | delta || " .
\ g:fzf_preview_command
ss
kevinhwang91 commented 3 years ago

@kevinhwang91 You can do git diff to file as well. Use CocCommand fzf-preview.GitStatus with config below as I said before.

let g:fzf_preview_git_status_preview_command =  "[[ $(git diff --cached -- {-1}) != \"\" ]] && git diff --cached --color=always -- {-1} | delta || " .
\ "[[ $(git diff -- {-1}) != \"\" ]] && git diff --color=always -- {-1} | delta || " .
\ g:fzf_preview_command
ss

Pretty cool, I think I have to try for a while, but I really hate the redraw in FZF during the interaction.

ghost commented 3 years ago

Update:

Someone who saw my example on reddit sent a request to fzf.vim, and now fzf also implements delta.

https://github.com/junegunn/fzf.vim/issues/1202

cc: @dandavison , @Sevensidedmarble

pbnj-dragon commented 7 months ago

A similar question was asked on tpope/vim-fugitive here: https://github.com/tpope/vim-fugitive/issues/2249#issuecomment-1874566335

TL;DR

:Git -c pager.diff=delta diff