folke / trouble.nvim

🚦 A pretty diagnostics, references, telescope results, quickfix and location list to help you solve all the trouble your code is causing.
Apache License 2.0
5.11k stars 173 forks source link

bug: compatibility with nvim-notify #495

Closed kiyoon closed 3 weeks ago

kiyoon commented 3 weeks ago

Did you check docs and existing issues?

Neovim version (nvim -v)

NVIM v0.10.0

Operating system/version

MacOS 14.4.1

Describe the bug

In https://github.com/rcarriga/nvim-notify, it demonstrates that we can override vim.notify to theirs.

vim.notify = require("notify")

However, if you set it like this, the trouble notifications result in an error. It's due to a slightly different interface they have. I know it's not ideal to remap such a command but it is a widely-used plugin so I thought others would be wondering about it, thus reporting it :)

To address this, maybe trouble.nvim can check require("notify") and send the notification via the plugin, otherwise use vim.notify

Steps To Reproduce

  1. Install nvim-notify
  2. call deprecated function like require("trouble.providers.telescope").open_with_trouble()

Expected Behavior

No error should be thrown.

Repro

No response

folke commented 3 weeks ago

If you do it like that then trouble can't intercept notifications, so yes, that's an error

kiyoon commented 3 weeks ago

Here's my proposal. Don't worry if it's not what you want.

M = {}

local status2, vscode = pcall(require, "vscode")
if status2 then
  ---@param message string|string[]
  M.notify = function(message, level, opts)
    if type(message) == "table" then
      message = table.concat(message, "\n")
    end
    vscode.notify(message, level)
  end
else
  local status, nvim_notify = pcall(require, "notify")
  if status then
    ---@param message string|string[]
    M.notify = function(message, level, opts)
      if type(message) == "string" then
        message = vim.split(message, "\n")
      end
      nvim_notify(message, level, opts)
    end
  else
    M.notify = vim.notify
  end
end

return M
require("trouble.notify").notify("Now you can\ndo\nmultiline", "info")

I think vscode.notify can't handle tables and nvim-notify can't handle multiline strings.