norcalli / nvim-colorizer.lua

The fastest Neovim colorizer.
Other
2.25k stars 117 forks source link

Bug: lua require'colorizer'.setup() has no effect #29

Closed sophiabrandt closed 3 years ago

sophiabrandt commented 4 years ago

First, thanks for the plugin. I was using Hexokinase before, but didn't want to upgrade to the new version due to the required dependencies.

I have a question regarding setup.

Description I have installed the plugin with minpac. My init.vim sources a file where I load all plugins:

source ~/.config/nvim/plugins.vim

I load nvim-colorizer.lua in the plugins.vim file:

call minpac#init()
call minpac#add('norcalli/nvim-colorizer.lua')

lua require'colorizer'.setup()

When I open a css file, I see no color highlighting. If I call :ColorizerAttachToBuffer or :ColorizerToggle manually from within the buffer, I get syntax highlighting.

:messages doesn't print errors.

I'm unsure on how to debug this. Is there a command or a lua function I can call?

Expected behavior nvim-colorizer.lua should highlight as soon as NeoVim enters the buffer, without having to call a command.

Operating System: Linux 4.19.85-1-MANJARO x86_64 GNU/Linux

Neovim Version: NVIM v0.4.3 Build type: Release LuaJIT 2.0.5

Colorizer Version: cfe8c495452272ed88da29a6babd906c483accf4

norcalli commented 4 years ago

How are you opening the css file? Are you doing it as nvim *.css? If you do :e to reload the file (outside of any other commands), does it add the highlighting?

Basically it's set with FileType autocmds which may not trigger for the first file you open correctly. I have in the past considered just attaching to the first buffer available as a "fix".

sophiabrandt commented 4 years ago

Hm. I open nvim from terminal with the file directly. For example: nvim css/style.css. Filetype is set correctly:

set ft?
> filetype=css

Neither :redraw! nor re-opening the buffer with :e make any difference.

As you said, I can attach to the buffer with :ColorizerAttachToBuffer. I even get auto-complete suggestion in Ex mode. For example, as soon as I type :Colo, Nvim sugests the nvim-colorizer.lua commands.

If you don't have any other ideas, you can close the issue, if you like.

Thanks for your help.

DLWood1001 commented 4 years ago

I worked around this issue by using the VimEnter autocmd. From there I call a function, which runs the colorizer setup() function and then runs an initial filetype detect which retriggers the FileType autocmds.

sophiabrandt commented 3 years ago

A followup: What's working for me is loading the plugin with a key binding. (VimEnter command would also work.)

Something like this:

nnoremap <LocalLeader>c :<C-u>lua require 'nvim-colorizer'<CR>:<C-u>ColorizerAttachToBuffer<CR>

I've delegated the setup to a lua function which seems to avoid common pitalls.

I've made a separate folder called lua and added a file called nvim-colorizer.lua with the following content (adjust to your needs):

require 'colorizer'.setup ({
  'css';
  'html';
  'javascript';
  'javascriptreact';
  'typescript';
  'typescriptreact';
}, {
  mode = 'background';
  names = true;
  css = true;
  css_fn = true;
})