sainnhe / sonokai

High Contrast & Vivid Color Scheme based on Monokai Pro
MIT License
1.65k stars 119 forks source link

Nvim-navic has wrong background color in statusline from lualine #83

Closed ArtAndreev closed 1 year ago

ArtAndreev commented 1 year ago

I have done the following steps before reporting this issue:

Operating system/version

macOS Ventura 13.2.1

Terminal emulator/version

Alacritty 0.12.0-rc2

$TERM environment variable

tmux-256color

Tmux version

tmux 3.3a

Feature matrix


gitsigns: require("gitsigns.health").check()
========================================================================
  - OK: git version 2.39.2

man: require("man.health").check()
========================================================================
  - OK: plugin/man.vim not in $VIMRUNTIME
  - OK: autoload/man.vim not in $VIMRUNTIME

nvim: health#nvim#check
========================================================================
## Configuration
  - OK: no issues found

## Performance
  - OK: Build type: Release

## Remote Plugins
  - OK: Up to date

## terminal
  - INFO: key_backspace (kbs) terminfo entry: key_backspace=\177
  - INFO: key_dc (kdch1) terminfo entry: key_dc=\E[3~
  - INFO: $TERM_PROGRAM='tmux'
  - INFO: $COLORTERM='truecolor'

## tmux
  - OK: escape-time: 0
  - INFO: Checking stuff
  - OK: focus-events: on
  - INFO: $TERM: tmux-256color
  - WARNING: Neither Tc nor RGB capability set. True colors are disabled. |'termguicolors'| won't work properly.
    - ADVICE:
      - Put this in your ~/.tmux.conf and replace XXX by your $TERM outside of tmux:
          set-option -sa terminal-overrides ',XXX:RGB'
      - For older tmux versions use this instead:
          set-option -ga terminal-overrides ',XXX:Tc'

nvim-treesitter: require("nvim-treesitter.health").check()
========================================================================
## Installation
  - OK: `tree-sitter` found 0.20.7 (parser generator, only needed for :TSInstallFromGrammar)
  - OK: `node` found v19.7.0 (only needed for :TSInstallFromGrammar)
  - OK: `git` executable found.
  - OK: `cc` executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl", "zig" }
    Version: Apple clang version 14.0.0 (clang-1400.0.29.202)
  - OK: Neovim was compiled with tree-sitter runtime ABI version 14 (required >=13). Parsers must be compatible with runtime ABI.

## OS Info:
{
  machine = "arm64",
  release = "22.3.0",
  sysname = "Darwin",
  version = "Darwin Kernel Version 22.3.0: Mon Jan 30 20:38:43 PST 2023; root:xnu-8792.81.3~2/RELEASE_ARM64_T8112"
}

## Parser/Features         H L F I J
  - bash                ✓ ✓ ✓ . ✓
  - c                   ✓ ✓ ✓ ✓ ✓
  - dockerfile          ✓ . . . ✓
  - fish                ✓ ✓ ✓ ✓ ✓
  - go                  ✓ ✓ ✓ ✓ ✓
  - gomod               ✓ . . . ✓
  - gosum               ✓ . . . .
  - gowork              ✓ . . . ✓
  - help                ✓ . . . ✓
  - json                ✓ ✓ ✓ ✓ .
  - jsonc               ✓ ✓ ✓ ✓ ✓
  - lua                 ✓ ✓ ✓ ✓ ✓
  - make                ✓ . ✓ . ✓
  - proto               ✓ . ✓ . .
  - query               ✓ ✓ ✓ ✓ ✓
  - rust                ✓ ✓ ✓ ✓ ✓
  - sql                 ✓ . . . ✓
  - toml                ✓ ✓ ✓ ✓ ✓
  - vim                 ✓ ✓ ✓ . ✓
  - yaml                ✓ ✓ ✓ ✓ ✓

  Legend: H[ighlight], L[ocals], F[olds], I[ndents], In[j]ections
         +) multiple parsers found, only one will be used
         x) errors found in the query, try to run :TSUpdate {lang}

provider: health#provider#check
========================================================================
## Clipboard (optional)
  - OK: Clipboard tool found: pbcopy

## Python 3 provider (optional)
  - WARNING: No Python executable found that can `import neovim`. Using the first available executable for diagnostics.
  - WARNING: Could not load Python 3:
    /opt/homebrew/bin/python3 does not have the "neovim" module.
    /opt/homebrew/bin/python3.10 does not have the "neovim" module.
    python3.9 not found in search path or not executable.
    python3.8 not found in search path or not executable.
    python3.7 not found in search path or not executable.
    python not found in search path or not executable.
    - ADVICE:
      - See :help |provider-python| for more information.
      - You may disable this provider (and warning) by adding `let g:loaded_python3_provider = 0` to your init.vim
  - INFO: Executable: Not found

## Python virtualenv
  - OK: no $VIRTUAL_ENV

## Ruby provider (optional)
  - INFO: Ruby: ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.arm64e-darwin22]
  - WARNING: `neovim-ruby-host` not found.
    - ADVICE:
      - Run `gem install neovim` to ensure the neovim RubyGem is installed.
      - Run `gem environment` to ensure the gem bin directory is in $PATH.
      - If you are using rvm/rbenv/chruby, try "rehashing".
      - See :help |g:ruby_host_prog| for non-standard gem installations.
      - You may disable this provider (and warning) by adding `let g:loaded_ruby_provider = 0` to your init.vim

## Node.js provider (optional)
  - INFO: Node.js: v19.7.0
  - WARNING: Missing "neovim" npm (or yarn, pnpm) package.
    - ADVICE:
      - Run in shell: npm install -g neovim
      - Run in shell (if you use yarn): yarn global add neovim
      - Run in shell (if you use pnpm): pnpm install -g neovim
      - You may disable this provider (and warning) by adding `let g:loaded_node_provider = 0` to your init.vim

## Perl provider (optional)
  - WARNING: "Neovim::Ext" cpan module is not installed
    - ADVICE:
      - See :help |provider-perl| for more information.
      - You may disable this provider (and warning) by adding `let g:loaded_perl_provider = 0` to your init.vim

vim.lsp: require("vim.lsp.health").check()
========================================================================
  - INFO: LSP log level : WARN
  - INFO: Log path: /Users/artyom/.local/state/nvim/lsp.log
  - INFO: Log size: 43 KB

vim.treesitter: require("vim.treesitter.health").check()
========================================================================
  - INFO: Runtime ABI version : 14
  - OK: Loaded parser for bash: ABI version 14
  - OK: Loaded parser for c: ABI version 13
  - OK: Loaded parser for dockerfile: ABI version 14
  - OK: Loaded parser for fish: ABI version 14
  - OK: Loaded parser for go: ABI version 14
  - OK: Loaded parser for gomod: ABI version 13
  - OK: Loaded parser for gosum: ABI version 14
  - OK: Loaded parser for gowork: ABI version 13
  - OK: Loaded parser for help: ABI version 14
  - OK: Loaded parser for json: ABI version 13
  - OK: Loaded parser for jsonc: ABI version 13
  - OK: Loaded parser for lua: ABI version 13
  - OK: Loaded parser for make: ABI version 13
  - OK: Loaded parser for proto: ABI version 13
  - OK: Loaded parser for query: ABI version 14
  - OK: Loaded parser for rust: ABI version 14
  - OK: Loaded parser for sql: ABI version 14
  - OK: Loaded parser for toml: ABI version 13
  - OK: Loaded parser for vim: ABI version 14
  - OK: Loaded parser for yaml: ABI version 13
  - OK: Loaded parser for c: ABI version 13
  - OK: Loaded parser for help: ABI version 14
  - OK: Loaded parser for lua: ABI version 13
  - OK: Loaded parser for vim: ABI version 14

Minimal vimrc that can reproduce this bug.

vim.opt.termguicolors = true
vim.cmd.colorscheme('sonokai')

require('packer').startup(function(use)
  use 'wbthomason/packer.nvim'

  use {
    'neovim/nvim-lspconfig',
    requires = {
      'SmiteshP/nvim-navic',
    },
    config = function()
      local lsp = require('lspconfig')

      local on_attach = function(client, bufnr)
        if client.server_capabilities.documentSymbolProvider then
          navic.attach(client, bufnr)
        end
      end

      if vim.fn.executable('gopls') == 1 then
        lsp.gopls.setup({
          on_attach = on_attach,
        })
      end
    end,
  }
  use {
    'SmiteshP/nvim-navic',
    requires = 'neovim/nvim-lspconfig',
    config = function() require('nvim-navic').setup({highlight = true}) end,
  }

  use {
    'nvim-lualine/lualine.nvim',
    requires = {
      'SmiteshP/nvim-navic',
    },
    config = function()
      local navic = require('nvim-navic')

      require('lualine').setup({
        sections = {
          lualine_x = {
            { navic.get_location, cond = navic.is_available },
            'encoding',
            'fileformat',
            'filetype',
          },
        },
      })
    end,
  }
end)

Steps to reproduce this bug using minimal vimrc

  1. You need a project in language with LSP capability for nvim-navic.
  2. Take this code:

    package main
    
    type I interface {
    cc()
    }
  3. Launch neovim using nvim -u init.lua main.go.
  4. Hold your cursor on line 4, column 5.
  5. Take a look at the statusline.

Expected behavior

The background of component matches the background of statusline.

If I disable highlight in nvim-navic config, I get this:

image

Actual behavior

The background of component doesn't match the background of statusline.

image
antoineco commented 1 year ago

Could you please share the output of :set statusline? while on that symbol?

ArtAndreev commented 1 year ago
  statusline=%#lualine_a_command# COMMAND %<%#lualine_c_command# main.go [+] %#lualine_c_command#   %#lualine_c_command#%=%#NavicIconsInterface# 練%*%#NavicText#I%*%#NavicSeparator# 
 %*%#NavicIconsMethod# %*%#NavicText#cc%* %#lualine_c_command#%#lualine_c_command# utf-8 %#lualine_c_command#  %#lualine_x_filetype_DevIconGo_command# %#lualine_c_command# go %#lua
line_b_command# 80%% %#lualine_a_command#   4:5

Edit:

image
antoineco commented 1 year ago

Very sorry for the delayed answer. I finally found the time to look into this.

I was able to reproduce:

Screenshot 2023-04-05 121352 Screenshot 2023-04-05 121417

Quick analysis:

" highlight = false
statusline=%#lualine_a_2_command# 󰀘 %#lualine_b_3_command#  main %<%#lualine_c_diff_added_command#   11 %#lualine_c_diff_modified_command# 13 %#lualine_c_diff_removed_command# 1 %#lualine_c_command#%=%#lualine_c_command#  lvim.colorscheme %#lualine_b_command#  13:26 %#lualine_z_progress_command# %P/%L
" highlight = true
statusline=%#lualine_a_2_command# 󰀘 %#lualine_b_3_command#  main %<%#lualine_c_diff_added_command#   11 %#lualine_c_diff_modified_command# 13 %#lualine_c_diff_removed_command# 1 %#lualine_c_command#%=%#NavicIconsString#  %*%#NavicText#lvim.colorscheme%* %#lualine_b_command#  13:26 %#lualine_z_progress_command# %P/%L

I think the issue is that the NavicIcons* highlight groups indirectly reset the background to NONE, whereas they should inherit the background value of the lualine section they are located in.

Screenshot 2023-04-05 122208 Screenshot 2023-04-05 122301 Screenshot 2023-04-05 122818 --> No background

I don't think there is a way to achieve this in the colorscheme. I have a feeling that this requires some integration between lualine and navic to dynamically adjust the background values of all NavicIcons* highlight groups based on the lualine section.

One example is lualine's LSP diagnostic component, which creates highlight groups on the fly during initialization. The Git diff component—visible in the lualine section C in my screenshots—is initialized in the same manner (lualine_c_diff_added_*, lualine_c_diff_modified_*, etc.).

A few additional interesting code locations, for future reference: 1, 2, 3.


For the time being, the simplest workaround is to override NavicIcons* highlight groups using an autocmd as shown below. Simply add to your Neovim config:

-- Apply custom highlights on colorscheme change.
-- Must be declared before executing ':colorscheme'.
grpid = vim.api.nvim_create_augroup('custom_highlights_sonokai', {})
vim.api.nvim_create_autocmd('ColorScheme', {
  group = grpid,
  pattern = 'sonokai',
  callback = function()
    -- navic colors for lualine
    local config = vim.fn['sonokai#get_configuration']()
    local palette = vim.fn['sonokai#get_palette'](config.style, config.colors_override)
    local navic_bg = palette.bg1[1]

    local function set_hl(name, fg)
      vim.api.nvim_set_hl(0, name, { foreground = fg, background = navic_bg })
    end

    local function get_hl(name)
      return vim.api.nvim_get_hl_by_name(name, true).foreground
    end

    set_hl('NavicText', get_hl('Fg'))
    set_hl('NavicSeparator', get_hl('Grey'))
    for _, c in ipairs(vim.g.sonokai_lsp_kind_color) do
      set_hl('NavicIcons' .. c[1], get_hl(c[2]))
    end
  end
})

Result:

Screenshot 2023-04-05 143912

ArtAndreev commented 1 year ago

Thank you for your detailed answer and help!

antoineco commented 1 year ago

If turns out that navic had options for color correction, specially designed for it to be embedded into a statusline: https://github.com/SmiteshP/nvim-navic/blob/83dc174da915f9dbc9b51169e9b62a2e0d42b527/README.md?plain=1#L275-L281