Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user defined is_available #632

Open
lukas-reineke opened this issue Dec 10, 2021 · 5 comments
Open

user defined is_available #632

lukas-reineke opened this issue Dec 10, 2021 · 5 comments

Comments

@lukas-reineke
Copy link
Contributor

lukas-reineke commented Dec 10, 2021

sources can define the is_available function, but there is no way for users to do this.

I think it would be nice to expose this to users as well. So sources don't have to build custom solutions for this.

require("cmp").setup({
	sources = {
		{
			name = "foo",
			is_available = function()
				-- user logic
				return false
			end,
		},
	},
})

I think it should not overwrite the source defined is_available. cmp should check both. If either one returns false, the source is not available.

I can make a PR if you want this

@hrsh7th
Copy link
Owner

hrsh7th commented Dec 13, 2021

Currently, I think I have to clean up the source configuration API. (But it's difficult.)
I would appreciate any ideas.

@dmitmel
Copy link
Collaborator

dmitmel commented Dec 13, 2021

I think it should not overwrite the source defined is_available. cmp should check both. If either one returns false, the source is not available.

What about passing plugin's original is_available function to user's is_available, like this:

cmp.setup({
  sources = {
    {
      name = 'rg',
      is_available = function(original_is_available_fn, ...)
        local is_in_git_tree = vim.trim(vim.fn.system('git rev-parse --is-inside-work-tree')) == 'true'
        return is_in_git_tree and original_is_available_fn(...)
      end,
    }
  }
})

? This way the user gets to decide whether the source-defined is_available rule should be used or not.

@hrsh7th
Copy link
Owner

hrsh7th commented Dec 13, 2021

sources = {
  {
    name = 'buffer',
    override = {
      get_keyword_pattern = function(source, ...)
        return source:get_keyword_pattern(...)
      end,
      get_trigger_characters = function(source, ...)
        return source:get_trigger_characters(...)
      end,
      complete = function(source, params, callback)
        source:complete(params, function(response)
          -- modify responses...
          callback(response)
        end)
      end
    }
}

@hrsh7th
Copy link
Owner

hrsh7th commented Dec 13, 2021

I think if we introduce this, we should deprecate the keyword_pattern option etc.

@lukas-reineke
Copy link
Contributor Author

lukas-reineke commented Dec 13, 2021

overwrite system makes sense to me 👍

It might be easier to maintain if it just works as a sort of constructor function? Instead of override being an object, it is a function that gets source as the argument.
And it returns a new source. (then the new source is set as __index)

This way, users can do whatever they want to the source object. And you never have to touch this part again, even if you add more things to sources.

sources = {
    {
        name = "buffer",
        init = function(source)
            -- I can do more stuff here.
            return {
                get_keyword_pattern = function(...)
                    return source:get_keyword_pattern(...)
                end,
                get_trigger_characters = function(...)
                    return source:get_trigger_characters(...)
                end,
                complete = function(params, callback)
                    source:complete(params, function(response)
                        -- modify responses...
                        callback(response)
                    end)
                end,
            }
        end,
    },
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants