Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Delta as a Vim/NeoVim library #338

Open
srcrip opened this issue Sep 26, 2020 · 13 comments
Open

Using Delta as a Vim/NeoVim library #338

srcrip opened this issue Sep 26, 2020 · 13 comments

Comments

@srcrip
Copy link

srcrip commented Sep 26, 2020

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
Copy link
Owner

dandavison commented Sep 26, 2020

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
Copy link
Owner

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
Copy link
Contributor

Kr1ss-XD commented Sep 26, 2020

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 �[0m�[1;34mbranches�[0m
-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 �[1;34mhooks�[0m
-rw-rw-r-- 1 kriss build 289  9. Sep 19:51 index
drwxrwsr-x 1 kriss build  14  9. Sep 19:51 �[1;34minfo�[0m
drwxrwsr-x 1 kriss build  16  9. Sep 19:51 �[1;34mlogs�[0m
drwxrwsr-x 1 kriss build  52  9. Sep 19:51 �[1;34mobjects�[0m
-rw-rw-r-- 1 kriss build 114  9. Sep 19:51 packed-refs
drwxrwsr-x 1 kriss build  32  9. Sep 19:51 �[1;34mrefs�[0m

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
Copy link
Author

srcrip commented Sep 28, 2020

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
Copy link

kevinhwang91 commented Dec 1, 2020

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
Copy link

ghost commented Dec 9, 2020

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
Copy link

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
Copy link

ghost commented Dec 9, 2020

@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
Copy link

kevinhwang91 commented Dec 9, 2020

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
Copy link

ghost commented Dec 9, 2020

@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
Copy link

@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
Copy link

ghost commented Dec 12, 2020

Update:

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

junegunn/fzf.vim#1202

cc: @dandavison , @Sevensidedmarble

@pbnj-dragon
Copy link

A similar question was asked on tpope/vim-fugitive here: tpope/vim-fugitive#2249 (comment)

TL;DR

:Git -c pager.diff=delta diff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants