Saghen / blink.compat

Compatibility layer for using nvim-cmp sources on blink.cmp
MIT License
48 stars 2 forks source link

Compatibility with `crates.nvim` #12

Open mainrs opened 4 days ago

mainrs commented 4 days ago

Feature Description

I was trying to make blink work with https://github.com/saecki/crates.nvim using the compat layer. But I couldn't get it to work correctly. I don't get completions for the version inside of Cargo.toml files (as showcased in the video inside the readme file).

My configuration looks like this:

-- plugins/blink.lua
return {
  -- Required for crates.nvim.
  { 'saghen/blink.compat' },

  'saghen/blink.cmp',
  lazy = false, -- built-in support
  version = 'v0.*',
  dependencies = { 'saecki/crates.nvim' },
  sources = {
    completion = {
      enabled_providers = { 'crates' },
    },

    providers = {
      crates = {
        name = 'crates',
        module = 'blink.compat.source',
        opts = {},
      }
    },
  },
  ---@module 'blink.cmp'
  ---@type blink.cmp.Config
  opts = {
    keymap = {
      preset = 'super-tab',
      ['<C-k>'] = { 'select_prev', 'fallback' },
      ['<C-j>'] = { 'select_next', 'fallback' },
    },
    nerd_font_variant = 'mono',
  },
}

-- plugins/crates.lua
return {
  {
    'saecki/crates.nvim',
    event = { "BufRead Cargo.toml" },
    config = function()
      local crates = require('crates')

      crates.setup({
        completion = {
          cmp = { enabled = true },
          crates = {
            enabled = true,
            min_chars = 3,
            max_results = 8
          }
        },
        popup = {
          autofocus = true
        }
      })

      local opts = { silent = true }

      vim.keymap.set("n", "<leader>ct", crates.toggle, opts)
      vim.keymap.set("n", "<leader>cr", crates.reload, opts)

      vim.keymap.set("n", "<leader>cv", crates.show_versions_popup, opts)
      vim.keymap.set("n", "<leader>cf", crates.show_features_popup, opts)
      vim.keymap.set("n", "<leader>cd", crates.show_dependencies_popup, opts)

      vim.keymap.set("n", "<leader>cu", crates.update_crate, opts)
      vim.keymap.set("v", "<leader>cu", crates.update_crates, opts)
      vim.keymap.set("n", "<leader>ca", crates.update_all_crates, opts)
      vim.keymap.set("n", "<leader>cU", crates.upgrade_crate, opts)
      vim.keymap.set("v", "<leader>cU", crates.upgrade_crates, opts)
      vim.keymap.set("n", "<leader>cA", crates.upgrade_all_crates, opts)

      vim.keymap.set("n", "<leader>cx", crates.expand_plain_crate_to_inline_table, opts)
      vim.keymap.set("n", "<leader>cX", crates.extract_crate_into_table, opts)

      vim.keymap.set("n", "<leader>cH", crates.open_homepage, opts)
      vim.keymap.set("n", "<leader>cR", crates.open_repository, opts)
      vim.keymap.set("n", "<leader>cD", crates.open_documentation, opts)
      vim.keymap.set("n", "<leader>cC", crates.open_crates_io, opts)
      vim.keymap.set("n", "<leader>cL", crates.open_lib_rs, opts)
    end,
  }
}

I was also wondering if it is possible to rely on the lazy loading event that I configured for crates.nvim when using blink.compat, since I would have to add it inside the dependencies field.

stefanboca commented 3 days ago

I also use crates.nvim, and when I was writing blink.compat I also had trouble getting it to work. I believe it was due the the difference in how blink triggers sources and how nvim-cmp handled the same, but I don't recall the exact problem. However, I switched to using crates.nvim's in-process LSP server, rather than directly using it as a source. Here's my config for that:

{
  "Saecki/crates.nvim",
  event = { "BufRead Cargo.toml" },
  opts = {
    completion = {
      crates = {
        enabled = true,
      },
    },
    lsp = {
      enabled = true,
      actions = true,
      completion = true,
      hover = true,
    },
  },
},

As far as I know, this does the same as using just the nvim-cmp source, as well as more (LSP hover, etc). Is there a specific use case for using crates directly as a source that isn't covered by using its LSP server? If so, I'll look into fixing it.