sindrets / diffview.nvim

Single tabpage interface for easily cycling through diffs for all modified files for any git rev.
Other
3.86k stars 105 forks source link

Question: Different highlight in both panels #241

Closed jgarciaokode closed 1 year ago

jgarciaokode commented 1 year ago

Hi, first of all, thanks for this awesome plugin

I am wondering how do you manage to get this highlight?

image

I mean i just manage to make diffchange with a background and difftext with another background but both left and right side panels have the same background from diffchange. In your image you have left side red right side green and in both panels it seems like you highlight difftext differently

image
sindrets commented 1 year ago

@jgarciaokode I have a rather involved setup in my own config where I dynamically generate diff highlights. Then I use some undocumented parts of the internal plugin API to adjust 'winhighlight' for the two diff windows.

This is not builtin functionality in the plugin because the highlight generation makes some assumptions about how diff colors should be presented that would be very difficult to generalize such that it would produce good results for any arbitrary color scheme. There's simply too much variation in how color scheme creators choose to approach diff highlights.

jgarciaokode commented 1 year ago

@sindrets Many thanks for the detailed explanation, i will have a look at your config and give it a try.

jgarciaokode commented 1 year ago

In case someone is interested, i managed to get it working pretty easily with this modifications.

Step 1: Set this highlights groups

vim.cmd([[highlight DiffAdd gui=none guifg=none guibg=#103235]])
vim.cmd([[highlight DiffChange gui=none guifg=none guibg=#272D43]])
vim.cmd([[highlight DiffText gui=none guifg=none guibg=#394b70]])
vim.cmd([[highlight DiffDelete gui=none guifg=none guibg=#3F2D3D]])
vim.cmd([[highlight DiffviewDiffAddAsDelete guibg=#3f2d3d gui=none guifg=none]])
vim.cmd([[highlight DiffviewDiffDelete gui=none guifg=#3B4252 guibg=none]])

-- Left panel
-- "DiffChange:DiffAddAsDelete",
-- "DiffText:DiffDeleteText",
vim.cmd([[highlight DiffAddAsDelete gui=none guifg=none guibg=#3F2D3D]])
vim.cmd([[highlight DiffDeleteText gui=none guifg=none guibg=#4B1818]])

-- Right panel
-- "DiffChange:DiffAdd",
-- "DiffText:DiffAddText",
vim.cmd([[highlight DiffAddText gui=none guifg=none guibg=#1C5458]])

Step 2: Set a hook in diffview plugin

hooks = {
    ---@param view StandardView
    view_opened = function(view)
      local utils = require("jg.core.utils");
      -- Highlight 'DiffChange' as 'DiffDelete' on the left, and 'DiffAdd' on
      -- the right.
      local function post_layout()
        utils.tbl_ensure(view, "winopts.diff2.a")
        utils.tbl_ensure(view, "winopts.diff2.b")
        -- left
        view.winopts.diff2.a = utils.tbl_union_extend(view.winopts.diff2.a, {
          winhl = {
            "DiffChange:DiffAddAsDelete",
            "DiffText:DiffDeleteText",
          },
        })
        -- right
        view.winopts.diff2.b = utils.tbl_union_extend(view.winopts.diff2.b, {
          winhl = {
            "DiffChange:DiffAdd",
            "DiffText:DiffAddText",
          },
        })
      end

      view.emitter:on("post_layout", post_layout)
      post_layout()
    end,
  },         -- See ':h diffview-config-hooks'

Btw, you will also need some util functions that you can pick from utils.lua file in sindrets config files. https://github.com/sindrets/dotfiles/blob/cdaae8429e1a041073e9d3e7ea318bfb8a072ce7/.config/nvim/lua/user/common/utils.lua

image