amarakon / nvim-cmp-fonts

Neovim completion for fonts installed on the system
GNU Affero General Public License v3.0
24 stars 1 forks source link

Is there any way to enbale this plugin in a certain file instead of file type? #1

Closed tan-wei closed 1 year ago

tan-wei commented 1 year ago

For me (or any other people), I want to enable this plugin only for a certain file (e.g., options.lua). For now, I can only enable it with file type lua. But it is to wide I think.

Thanks!

tan-wei commented 1 year ago

OK. I think here can help. After I implement a function, I will upload code here to help others.

tan-wei commented 1 year ago

OK, it works:

local default_cmp_sources = cmp.config.sources {
  { name = "nvim_lsp" },
  { name = "nvim_lsp_signature_help" },
  { name = "luasnip" },
  { name = "buffer" },
  { name = "path" },
  { name = "nvim_lua" },
  { name = "emoji" },
  { name = "nerdfont" },
  { name = "rg", keyword_length = 3 },
}

-- Only enable `fonts` for `options.lua`

local buf_is_options_lua = function(bufnr)
  local bufname = vim.api.nvim_buf_get_name(bufnr)
  local enable_filename = "options.lua"
  return bufname:sub(-#enable_filename) == enable_filename
end

vim.api.nvim_create_autocmd("BufReadPre", {
  callback = function(t)
    local sources = default_cmp_sources
    if buf_is_options_lua(t.buf) then
      sources[#sources + 1] = { name = "fonts", option = { space_filter = "-" } }
    end

    cmp.setup.buffer {
      sources = sources,
    }
  end,
})