obsidian.nvim
obsidian.nvim copied to clipboard
cmp: Allow disabling of specific lsp (problem with marksman)
🚀 The feature, motivation and pitch
Hi,
I have
- marksman lsp and
nvim_lspenabled for cmp. That causes the obsidian cmp source not working for some not understood reason, the refs are just not shown on[[<name>.
Here is how to work around that and disable nvim_lsp (buffer local) for obsidian docs:
... -- all other lazy setup
completion = {
nvim_cmp = false, -- preventing the internal logic
},
opts = options, -- defined module wide, needed in init below as well
init = function()
local lazy_setup = function()
local cmp = require("cmp")
local sources = {
{ name = "obsidian", option = options },
{ name = "obsidian_new", option = options },
}
for _, source in pairs(cmp.get_config().sources) do
if source.name ~= "obsidian" and source.name ~= "obsidian_new" and source.name ~= "nvim_lsp" then
table.insert(sources, source)
end
end
cmp.setup.buffer({ sources = sources })
end
local group = vim.api.nvim_create_augroup("obsidian_setup2", { clear = true })
vim.api.nvim_create_autocmd({ "BufEnter" }, {
group = group,
pattern = "<YOUR VAULT DIRECTORY>/**.md",
callback = lazy_setup,
})
end,
It is basically a copy of your internal way, but with and added exclusion of nvim_lsp in the for loop.
The feature request would be, to support sth like this:
-- Optional, completion.
completion = {
nvim_cmp = true,
skip_sources = {"nvim_lsp"}, -- <-- THIS
},
and respecting skip_sources within your internal obsidian/init.lua.
PS: thanks for documenting the lazy setup, very helpful
Alternatives
No response
Additional context
No response
Hey @AXGKl I'm trying to reproduce this so I can understand what the underlying issue is, but so far no luck... it seems to work okay for me with the nvim_lsp cmp source + marksman. Can you post your nvim-cmp and marksman lsp config?
Hey.
So, tried to create a minimal setup, dropping all other plugins - aaaaand: "It works" :man_facepalming:
Culprint was: Was playing with cmp and added a custom cmp source, based on this one - but with hardcoded users in the module, so I took it as a blueprint to be able to reference persons.
Problem: My copy source had it NOT in nvim/lua/wincent/cmp but directly in nvim/lua/cmp/github_handles.lua - and I reproduced w/o thinking (and most cmp stuff worked, so I forgot).
So my cmp config contained:
cmp.register_source("github_handles", require("cmp.github_handles").new())
And that working require('cmp') - pointing to a different dir killed yours - guess the cmp = pcall(require, "cmp") messed it up in funny ways...
=> Totally sorry to have wasted your time with that.
I close and let you decide if you can think of a use case for disabling cmp sources specifically in obsidian markdown, while avail in all other markdowns. I find none ;-)
Cheers, ty again, Gunther
No worries! Glad you got it figured out.
Crazy. Bugging you again with it. It only looked solved - seems a funny timing problem with when lsp_attach happens - or maybe it is related to how long find takes, I don't know.
Watch this: https://asciinema.org/a/etS9Htr4Pw101bwmD7WEwTl6i
Sorry, a bit hard to see in asciinema. Behaviour really:
After open of the doc, no references, 3 times. Then LspStop => reliable working, shwoing the ref on same input ([[asdf).
Then LspStart again - and all works as well.
Again: After :LspStop it works - and then even after :LspStart
I have created a somewhat minimal test setup which was the config for the screencast.
https://github.com/AXGKl/test_obsidian_cmp/tree/master/lua/plugins
You can simply install it:
(Maybe stash all your nvim setup away before)
Clone:
cd $HOME/.config
[email protected]:AXGKl/test_obsidian_cmp nvim
Then start nvim...
@AXGKl when you reproduce the error can you run this lua script:
local cmp = require("cmp")
for _, source in pairs(cmp.get_config().sources) do
print(source.name)
end
You could put this in a file called tmp.lua in your vault, then from nvim call :luafile tmp.lua. This will tell us if something else is overriding cmp's config after Obsidian.

More infos:
I tried to find differences between marksman and other LSPs. So I match on *. for your plugin, install pyright Lsp and open a python file.
Finding: After opening, I have reliable working of yours (plus python completion).

But when I LspStop / LspStart, I get sometimes failures:

(i.e. somewhat opposite behaviourr to the combi of yours and marksman, where after Stop/Start all seems to work)
Further analysing it a bit. Added a notify for the matches (and a handle:close(), that should not hurt or does lua auto close the handle after nil (?))

Returns the matching file fine - but it is not offered in cmp.
Tried to check if it's timing and statically returned the filename as a string, w/o running the find. Same result, not offered cmp.
But I see you actually read the file in from_file - and with my restricted lua "skills", I was not able to mock this, for further analysis.
Note: All done using the nvim test setup from [email protected]:AXGKl/test_obsidian_cmp
Maybe @hrsh7th has an idea?
No stress, I have the workaround to disable nvim_lsp cmp source via that lazy init hack of my initial post, then yours and all other cmp sources work perfectly.
I think that the behavior has to with the cmp.setup.buffer({ sources = sources }) which you do at BufEnter - vs. what's happening here https://github.com/hrsh7th/cmp-nvim-lsp/blob/main/lua/cmp_nvim_lsp/init.lua - after LspAttach and at InsertEnter.