Skip to content

feat: use virtual title in notifications if title is not supported #2439

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

Merged
merged 7 commits into from
Oct 7, 2023

Conversation

Akmadan23
Copy link
Collaborator

Having [NvimTree] as a notification header while using a notification system that actually supports title is redundant, so with this simple change it will be added only if needed.

@Akmadan23
Copy link
Collaborator Author

And maybe now it's time to raise stylua's column width, splitting these boolean expressions would be really confusing.

@alex-courtis
Copy link
Member

alex-courtis commented Oct 1, 2023

It is hard to determine whether we have an alternate notify plugin installed.

Could we be clever and look for the __call metamethod on vim.notify to indicate that it is overridden?

Experimenting:

[2023-10-02 10:45:23] [dev] default vim.notify=<function 1>

[2023-10-02 10:49:32] [dev] "rcarriga/nvim-notify" vim.notify={
  _config = <function 1>,
  _print_history = <function 2>,
  async = <function 3>,
  dismiss = <function 4>,
  history = <function 5>,
  instance = <function 6>,
  notify = <function 7>,
  open = <function 8>,
  pending = <function 9>,
  setup = <function 10>,
  <metatable> = {
    __call = <function 11>
  }
}

[2023-10-02 10:55:12] [dev] "folke/noice.nvim" vim.notify=<function 1>

That's a no...

We couldn't detect whether vim.notify has changed over time either, due to plugin load/setup order.

Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will result in user happiness, thanks for taking it on.

We have options:

  1. Harden detection. It might be nicer to put it in notify.setup so it runs at a controlled time rather than on module load.
  2. Add notify.prefix boolean|string or similar, default "[NvimTree]\n"

I prefer 1 - we have too many options and users can add additional notify plugin detection if needed.

Edit: moving to setup is not possible.

@@ -1,5 +1,9 @@
local M = {}

M.supports_title = (pcall(require, "notify") and vim.notify == require "notify" or vim.notify == require("notify").notify)
or (pcall(require, "noice") and vim.notify == require("noice").notify or vim.notify == require("noice.source.notify").notify)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one's not happy when we don't have noice:

Error detected while processing /tmp/nvt-dev/nvt-dev.lua:
E5113: Error while calling lua chunk: ...v/site/pack/packer/start/master/lua/nvim-tree/notify.lua:4: module 'noice.source.notify' not found:
        no field package.preload['noice.source.notify']
        no file './noice/source/notify.lua'
        no file '/usr/share/luajit-2.1/noice/source/notify.lua'
        no file '/usr/local/share/lua/5.1/noice/source/notify.lua'
        no file '/usr/local/share/lua/5.1/noice/source/notify/init.lua'
        no file '/usr/share/lua/5.1/noice/source/notify.lua'
        no file '/usr/share/lua/5.1/noice/source/notify/init.lua'
        no file './noice/source/notify.so'
        no file '/usr/local/lib/lua/5.1/noice/source/notify.so'
        no file '/usr/lib/lua/5.1/noice/source/notify.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
        no file './noice.so'
        no file '/usr/local/lib/lua/5.1/noice.so'
        no file '/usr/lib/lua/5.1/noice.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
        [C]: in function 'require'
        ...v/site/pack/packer/start/master/lua/nvim-tree/notify.lua:4: in main chunk
        [C]: in function 'require'
        ...-dev/site/pack/packer/start/master/lua/nvim-tree/api.lua:1: in main chunk
        [C]: in function 'require'
        /tmp/nvt-dev/nvt-dev.lua:49: in main chunk

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I forgot to put parentheses on the second part, it did not show up to me because I have nvim-notify enabled.

@alex-courtis
Copy link
Member

alex-courtis commented Oct 2, 2023

And maybe now it's time to raise stylua's column width, splitting these boolean expressions would be really confusing.

Yeah, this is getting silly. The intent is to keep statements minimal however sometimes you just have to have long lines for readability.

      if
        (filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none")
        and vim.bo[data.buf].buftype == ""
      then

How about trying, say, 140 on a new branch and we see how it looks?

@alex-courtis
Copy link
Member

alex-courtis commented Oct 2, 2023

Would it be possible for us to move notify_once to notify.lua?

FYI: my nvim-tree availability is late Sat to early Mon AEST

@Akmadan23
Copy link
Collaborator Author

We have options:

  1. Harden detection. It might be nicer to put it in notify.setup so it runs at a controlled time rather than on module load.

  2. Add notify.prefix boolean|string or similar, default "[NvimTree]\n"

I prefer 1 - we have too many options and users can add additional notify plugin detection if needed.

Yeah, I also initially considered to add a user option to manage it, but this is such a slight and almost irrelevant improvement that probably no one would care about going out of their way to change it.

In order to harden detection, as it stands now the notification plugin must be loaded before nvim-tree, but that's something that the user should do manually, so it isn't ideal...

Otherwise we can put the boolean expression inside a function that gets evaluated each time. I'm trying to create an hybrid that evaluates the expression only once (the first time it gets called) and then reuse its result without other pointless evaluations.

@Akmadan23
Copy link
Collaborator Author

Akmadan23 commented Oct 2, 2023

Would it be possible for us to move notify_once to notify.lua?

That would make sense, but there are a couple of choices to be made to make it consistent with other members of the notify module. Maybe we can discuss it in a future PR (unless it can be useful to achieve our goal here).

Edit: now that I think about it... Can't we just use notify.warn? The "once" part seems unnecessary since in both cases the notification will be launched once.

@Akmadan23
Copy link
Collaborator Author

How about trying, say, 140 on a new branch and we see how it looks?

140 seems reasonable, I'm not sure what you mean by seeing how it looks on a new branch however. I mean, if we want to merge this when it will be ready I don't see any other decent alternative...

@Akmadan23
Copy link
Collaborator Author

This is the most efficient and effective solution I can find

@alex-courtis
Copy link
Member

Edit: now that I think about it... Can't we just use notify.warn? The "once" part seems unnecessary since in both cases the notification will be launched once.

Thank you. That simplifies greatly.

Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • nvim-notify
  • noice
  • notifier

@alex-courtis
Copy link
Member

140 seems reasonable, I'm not sure what you mean by seeing how it looks on a new branch however. I mean, if we want to merge this when it will be ready I don't see any other decent alternative...

Added a temporary suppression.

Changing format disrupts PRs in flight, so we will do that when the queue is empty.

@alex-courtis alex-courtis merged commit 85abe29 into master Oct 7, 2023
@alex-courtis alex-courtis deleted the virtual-title-notifications branch October 7, 2023 01:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants