nvim-cmp icon indicating copy to clipboard operation
nvim-cmp copied to clipboard

Wrong colors when setting Pmenu highlight in Lua

Open phha opened this issue 3 years ago • 12 comments

FAQ

  • [X] I have checked the FAQ and it didn't resolve my problem.

Issues

  • [X] I have checked existing issues and there are no open or closed issues with the same problem.

Neovim Version

NVIM v0.7.0-dev+1429-g8bdcd832a

Minimal reproducible full config

if has('vim_starting')
  set encoding=utf-8
endif
scriptencoding utf-8
if &compatible
  set nocompatible
endif


let s:plug_dir = expand('/tmp/plugged/vim-plug')
if !filereadable(s:plug_dir .. '/plug.vim')
  execute printf('!curl -fLo %s/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim', s:plug_dir)
end

execute 'set runtimepath+=' . s:plug_dir
call plug#begin(s:plug_dir)
Plug 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/vim-vsnip'
Plug 'neovim/nvim-lspconfig'
call plug#end()
PlugInstall | quit


" Setup global configuration. More on configuration below.
lua << EOF
local cmp = require "cmp"
cmp.setup {
  snippet = {
    expand = function(args)
      vim.fn["vsnip#anonymous"](args.body)
    end,
  },

  mapping = {
    ['<CR>'] = cmp.mapping.confirm({ select = true })
  },

  sources = {
    { name = "nvim_lsp" },
    { name = "buffer" },
  },
}
EOF

lua << EOF
local capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())

require'lspconfig'.cssls.setup {
  capabilities = capabilities,
}
EOF

set termguicolors
lua << EOF
local set_hl = vim.api.nvim_set_hl
local set_hl_ns = vim.api.nvim__set_hl_ns or vim.api.nvim_set_hl_ns
local ns = vim.api.nvim_create_namespace("cmp-test")
set_hl(ns, "Pmenu", { fg="#FF0000", bg="#0000FF" })
set_hl(ns, "Normal", { fg="#FFFFFF", bg="#000000" })
set_hl_ns(ns)
EOF

Description

When setting highlights in Lua, the completion popup is rendered in Normal colors instead of Pmenu colors.

Steps to reproduce

Source the example config and open the completion popup.

Expected behavior

Popup should be rendered in red on blue.

Actual behavior

Popup is rendered in white on black

Additional context

Removing set_hl(ns, "Normal", { fg="#FFFFFF", bg="#000000" }) will cause the popup to be rendered in the correct colors, so it seems like Pmenu gets overwritten by the Normal setting somehow. Order doesn't matter.

Problem seems to only exist when using vim.api commands, the equivalent viml commands (hi Pmenu ...) yield the correct colors.

<C-p> in insert mode shows the correct colors, so it seems to be an issue of nvim-cmp specifically.

phha avatar Apr 12 '22 20:04 phha

This is what nvim-cmp looks like: Screenshot 2022-04-12 at 22 06 50

This is what <C-p> looks like: Screenshot 2022-04-12 at 22 09 58

phha avatar Apr 12 '22 20:04 phha

I think this is not nvim-cmp side problem if the problem occur only when to use vim.api.*.

hrsh7th avatar Apr 13 '22 16:04 hrsh7th

It seems to be related to namespaces. When I set ns = 0 it works correctly.

phha avatar Apr 13 '22 18:04 phha

It seems the nvim's api does not support highlight-link along with different namespaces...

Do you think this issue should be closed now?

hrsh7th avatar Apr 13 '22 18:04 hrsh7th

If it's indeed an upstream issue, I don't think it makes sense to leave it open. Closing it.

What's your Opinion about defining a dedicated hl group for the popup window and have it default to Pmenu? It would also give more flexibility for styling.

phha avatar Apr 13 '22 18:04 phha

I think typical colorschemes are using highlight command. So I don't think we should use namespace based API for this.

hrsh7th avatar Apr 13 '22 18:04 hrsh7th

I mean we already have highlight groups for CmpItemAbbr etc. Basically for everything except the background of the popup window. Why not have one for that as well. Currently it's not possible to set a color different from Pmenu, whether with :highlight or with vim.api.

I think we will be seeing more and more colorschemes using the lua api now that the required functions are available in neovim and with the general push to favor Lua over vimL, but that's beside the point. It will have to be fixed in neovim itself, it seems.

phha avatar Apr 13 '22 18:04 phha

@phha Could you please provide a pointer to the upstream issue? I think we may leave this open until the upstream merges a fix.

wookayin avatar Apr 13 '22 19:04 wookayin

@wookayin I don't think there is an upstream ticket yet. I also don't quite understand how nvim-cmp is applying the colors to file a useful issue.

I also noticed which-key.nvim had the same issue with Pmenu not getting applied.

phha avatar Apr 13 '22 20:04 phha

It could be related to the use of winhighlight in nvim-cmp.

As mentioned in this comment in neovim/neovim#16419 winhighlight is set to be deprecated in favor of namespaces, so it stands to reason that those two don't mix well.

I will reopen this, because it's probably something that needs to be fixed eventually when winhighlight is deprecated. Feel free to close it if you disagree.

phha avatar Apr 13 '22 20:04 phha

I think we should consider this problem but I don't know what we need to do about this at the moment. If anyone has a correct solution, feel free to comment or PR.

hrsh7th avatar May 01 '22 14:05 hrsh7th

It could be an upstream issue. My workaround right now is to apply highlights only in the global (0) namespace and that seems to work fine. This issue is also visible in other plugins that are using winhighlight. Apparently nvim does not honor the active namespace for winhighlight.

phha avatar May 02 '22 07:05 phha

I'll check again with latest nvim.

hrsh7th avatar Oct 13 '22 03:10 hrsh7th

How are things going ? I am a theme maintainer and this still seems to be an issue, even in the global namespace nothing changes.

A-Lamia avatar Jul 08 '23 16:07 A-Lamia