catppuccin / nvim

🍨 Soothing pastel theme for (Neo)vim
MIT License
5.49k stars 240 forks source link

Attempt to index global 'C' (a nil value) #667

Closed aretrosen closed 7 months ago

aretrosen commented 7 months ago

Description

For completions, I used to use the following code snippet, so that I have types on the left, with their foreground and background color swapped:

custom_highlights = function(C)
  local cmp = require("catppuccin.groups.integrations.cmp").get()
  for k, v in pairs(cmp) do
    if k:sub(8, 11) == "Kind" then
      cmp[k] = { bg = v.fg, fg = C.base }
    end
  end
  cmp["Folded"] = { bg = C.crust }
  return cmp
end

However, since I updated the plugin (admittedly after a long time), I got the following error:

2024-02-26T01:51:52 lazy.nvim  ERROR Failed to run `config` for catppuccin
...zy/catppuccin/lua/catppuccin/groups/integrations/cmp.lua:5: attempt to index global 'C' (a
 nil value)

# stacktrace:
  - /catppuccin/lua/catppuccin/groups/integrations/cmp.lua:5 _in_ **get**
  - colorscheme.lua:103 _in_ **v**
  - /catppuccin/lua/catppuccin/lib/hashing.lua:21 _in_ **hash**
  - /catppuccin/lua/catppuccin/lib/hashing.lua:17 _in_ **hash**
  - /catppuccin/lua/catppuccin/init.lua:169 _in_ **setup**
  - colorscheme.lua:8 _in_ **config**
  - ~/.config/nvim/lua/kyvernetes/init.lua:130
  - ~/.config/nvim/init.lua:54

I noticed that the catppuccin/groups/integrations/cmp.lua hasn't been updated for a while, so I have no idea where the module broke. Any help will be appreciated. After I remove the snippet, there is no such error.

Neovim version

NVIM v0.10.0-dev-2448+g0fcbda598
Build type: Release
LuaJIT 2.1.1707061634

Terminal and multiplexer

wezterm 20240201-171842-17dadbeb

Catppuccin version / branch / rev

catppuccin #c0de3b4

Steps to reproduce

Used this for the custom highlights:

custom_highlights = function(C)
  local cmp = require("catppuccin.groups.integrations.cmp").get()
  for k, v in pairs(cmp) do
    if k:sub(8, 11) == "Kind" then
      cmp[k] = { bg = v.fg, fg = C.base }
    end
  end
  cmp["Folded"] = { bg = C.crust }
  return cmp
end

Expected behavior

Don't error out, and show proper highlighting.

Actual behavior

Errors out, without proper highlighting.

Repro

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "catppuccin/nvim",
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("catppuccin")
-- add anything else here
mrtnvgr commented 7 months ago

It suppose that this commit removed global variables (C, O, U) from scope of integrations

nullchilly commented 7 months ago

Hi, you used our internal module which wasn't documented! Indeed, it was introduced in https://github.com/catppuccin/nvim/commit/836de8bc1898250b69332e66cbe993058870f849 as a way to fix our function cache.

Unfortunately, fixing this will introduce more complexity for me.

On the bright side, it's pretty easy to vim golf this with :'<,'>s/fg/fg = C.base, bg

image

The snippet below should gives the same behavior:

custom_highlights = function(C)
  return {
    Folded = { bg = C.crust },
    -- kind support
    CmpItemKindSnippet = { fg = C.base, bg = C.mauve },
    CmpItemKindKeyword = { fg = C.base, bg = C.red },
    CmpItemKindText = { fg = C.base, bg = C.teal },
    CmpItemKindMethod = { fg = C.base, bg = C.blue },
    CmpItemKindConstructor = { fg = C.base, bg = C.blue },
    CmpItemKindFunction = { fg = C.base, bg = C.blue },
    CmpItemKindFolder = { fg = C.base, bg = C.blue },
    CmpItemKindModule = { fg = C.base, bg = C.blue },
    CmpItemKindConstant = { fg = C.base, bg = C.peach },
    CmpItemKindField = { fg = C.base, bg = C.green },
    CmpItemKindProperty = { fg = C.base, bg = C.green },
    CmpItemKindEnum = { fg = C.base, bg = C.green },
    CmpItemKindUnit = { fg = C.base, bg = C.green },
    CmpItemKindClass = { fg = C.base, bg = C.yellow },
    CmpItemKindVariable = { fg = C.base, bg = C.flamingo },
    CmpItemKindFile = { fg = C.base, bg = C.blue },
    CmpItemKindInterface = { fg = C.base, bg = C.yellow },
    CmpItemKindColor = { fg = C.base, bg = C.red },
    CmpItemKindReference = { fg = C.base, bg = C.red },
    CmpItemKindEnumMember = { fg = C.base, bg = C.red },
    CmpItemKindStruct = { fg = C.base, bg = C.blue },
    CmpItemKindValue = { fg = C.base, bg = C.peach },
    CmpItemKindEvent = { fg = C.base, bg = C.blue },
    CmpItemKindOperator = { fg = C.base, bg = C.blue },
    CmpItemKindTypeParameter = { fg = C.base, bg = C.blue },
    CmpItemKindCopilot = { fg = C.base, bg = C.teal },
  }
end
aretrosen commented 7 months ago

Hey, so I actually stumbled upon a pretty hacky but effective solution to this issue (and TBH, I'm kinda proud of myself for figuring it out, lol). Check it out:

custom_highlights = function(C)
  local cmpfn = require("catppuccin.groups.integrations.cmp")
  setfenv(cmpfn.get, { C = C })
  local cmp = cmpfn.get()
  for k, v in pairs(cmp) do
    if k:sub(8, 11) == "Kind" then
      cmp[k] = { bg = v.fg, fg = C.base }
    end
  end
  cmp["Folded"] = { bg = C.crust }
  return cmp
end

Basically, nothing to do manually any more. Anyway, thanks for the prompt response earlier. Closing this issue now.