Skip to content

Commit

Permalink
fix: wait to override cmp till it loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Dec 1, 2022
1 parent 5ca31af commit 712180f
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 36 deletions.
16 changes: 9 additions & 7 deletions lua/noice/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,15 @@ function M.check(opts)
},
}

local ok, mod = pcall(_G.require, "cmp.entry")
table.insert(checks, {
opt = 'lsp.override["cmp.entry.get_documentation"]',
enabled = Config.options.lsp.override["cmp.entry.get_documentation"],
handler = ok and mod.get_documentation,
handler_str = "cmp.entry.get_documentation",
})
if package.loaded["cmp.entry"] then
local mod = package.loaded["cmp.entry"]
table.insert(checks, {
opt = 'lsp.override["cmp.entry.get_documentation"]',
enabled = Config.options.lsp.override["cmp.entry.get_documentation"],
handler = mod.get_documentation,
handler_str = "cmp.entry.get_documentation",
})
end

for _, check in ipairs(checks) do
if check.handler then
Expand Down
5 changes: 3 additions & 2 deletions lua/noice/lsp/override.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ local Markdown = require("noice.text.markdown")
local Config = require("noice.config")
local Format = require("noice.lsp.format")
local Message = require("noice.message")
local Hacks = require("noice.util.hacks")

local M = {}

function M.setup()
if Config.options.lsp.override["cmp.entry.get_documentation"] then
pcall(function()
require("cmp.entry").get_documentation = function(self)
Hacks.on_module("cmp.entry", function(mod)
mod.get_documentation = function(self)
local item = self:get_completion_item()
if item.documentation then
return Format.format_markdown(item.documentation)
Expand Down
68 changes: 41 additions & 27 deletions lua/noice/util/hacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,39 +193,34 @@ end

-- Fixes cmp cmdline position
function M.fix_cmp()
if not Util.module_exists("cmp.utils.api") then
-- cmp not availablle
return
end

local api = require("cmp.utils.api")

local get_cursor = api.get_cursor
api.get_cursor = function()
if api.is_cmdline_mode() then
local pos = Api.get_cmdline_position()
if pos then
return { pos.bufpos.row, vim.fn.getcmdpos() - 1 }
M.on_module("cmp.utils.api", function(api)
local get_cursor = api.get_cursor
api.get_cursor = function()
if api.is_cmdline_mode() then
local pos = Api.get_cmdline_position()
if pos then
return { pos.bufpos.row, vim.fn.getcmdpos() - 1 }
end
end
return get_cursor()
end
return get_cursor()
end

local get_screen_cursor = api.get_screen_cursor
api.get_screen_cursor = function()
if api.is_cmdline_mode() then
local pos = Api.get_cmdline_position()
if pos then
local col = vim.fn.getcmdpos() - Cmdline.last().offset
return { pos.screenpos.row, pos.screenpos.col + col }
local get_screen_cursor = api.get_screen_cursor
api.get_screen_cursor = function()
if api.is_cmdline_mode() then
local pos = Api.get_cmdline_position()
if pos then
local col = vim.fn.getcmdpos() - Cmdline.last().offset
return { pos.screenpos.row, pos.screenpos.col + col }
end
end
return get_screen_cursor()
end
return get_screen_cursor()
end

table.insert(M._disable, function()
api.get_cursor = get_cursor
api.get_screen_cursor = get_screen_cursor
table.insert(M._disable, function()
api.get_cursor = get_cursor
api.get_screen_cursor = get_screen_cursor
end)
end)
end

Expand Down Expand Up @@ -274,4 +269,23 @@ function M.show_cursor()
end
end

---@param fn fun(mod)
function M.on_module(module, fn)
if package.loaded[module] then
return fn(package.loaded[module])
end
package.preload[module] = function()
for l, loader in pairs(package.loaders) do
if l > 1 then
local ret = loader(module)
if ret then
local mod = ret()
fn(mod)
return mod
end
end
end
end
end

return M
1 change: 1 addition & 0 deletions selene.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std="vim"
46 changes: 46 additions & 0 deletions vim.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[selene]
base = "lua51"
name = "vim"

[vim]
any = true

[[describe.args]]
type = "string"
[[describe.args]]
type = "function"

[[it.args]]
type = "string"
[[it.args]]
type = "function"

[[before_each.args]]
type = "function"
[[after_each.args]]
type = "function"

[assert.is_not]
any = true

[[assert.equals.args]]
type = "any"
[[assert.equals.args]]
type = "any"
[[assert.equals.args]]
type = "any"
required = false

[[assert.same.args]]
type = "any"
[[assert.same.args]]
type = "any"

[[assert.truthy.args]]
type = "any"

[[assert.spy.args]]
type = "any"

[[assert.stub.args]]
type = "any"

0 comments on commit 712180f

Please sign in to comment.