-
Notifications
You must be signed in to change notification settings - Fork 282
Fix trailspace issues #1
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
Conversation
…' with 'pcall' to avoid errors
|
Thank you for the very first contribution to this project! 🎉🎉🎉 Yeah, |
|
|
||
| -- Create highlighting | ||
| vim.api.nvim_exec([[hi link MiniTrailspace Error]], false) | ||
| vim.api.nvim_exec([[hi default link MiniTrailspace Error]], false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like a valid change. Currently I am having trouble constructing an example where it really matters (and reading help didn't help). I can override MiniTrailspace and it works. Could you help me understand (ideally with some reproducible steps) when this can matter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I set MiniTrailspace before the plugin is loaded (lazy loading, for instance), your declaration here will overwrite whatever I set up. With this in place, it will only be defined if it is not already defined.
The key part from the :h hi-default is this line:
If highlighting has already been specified for the group the command will be ignored
which means users can define this before mini.trailspace is loaded, and the command used here won't overwrite it if they've done so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that sounds reasonable. I am still having trouble with example. I used this 'init.lua' with only these two plugins installed and it works as expected (trailing whitespace is green):
vim.cmd([[hi MiniTrailspace guibg=#00ff00]])
require('packer').startup(function()
use 'wbthomason/packer.nvim'
use "echasnovski/mini.nvim"
end)
vim.cmd([[set termguicolors]])
require('mini.trailspace').setup()There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try linking to another highlight group rather than defining it outright, so:
vim.cmd([[hi link MiniTrailspace WarningMsg]])
...Either way, I cannot imagine that it (this portion of the PR) will hurt to have.
| local win_match = H.window_matches[win_id] | ||
| if win_match ~= nil then | ||
| vim.fn.matchdelete(win_match) | ||
| pcall(vim.fn.matchdelete, win_match) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also feels like a valid change. Although if there is a use case when error is given, would you mind sharing it? I would really like to see it. Maybe it is a sign of a bigger problem in design.
I use 'teleslcope.nvim' myself and don't have any problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, it seems like it might be something a bit more "systemic", however I've never had any leftover matches after adding this (used my own fork for ~1 day now).
The error isn't super useful either, it just notes that "no match with ID ... found", seemingly as it doesn't apply to the current bufnr. This breaks anything really, but most notably Telescope (probably due to the FileType autocmd that is defined?) where an error is caught while it is forming the prompt buffer, so the entire thing ceases to work.
Not 100% sure that answers your question, but I can see if I can find a way to reproduce it with a "minimal" configuration. Mine is has a shit-load of plugins, personal libraries, and its all lazy-loaded with Packer so sometimes the issues I see don't tend to show up with "simpler" configurations. Let me know what you'd like to do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I've had that error when writing module and it really breaks everything. I don't have it anymore, even though I am using Telescope.
I'll wait for a day or two, maybe we'll figure something out. If not, I'll use this pcall.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the culprit on this one; it is a plugin that I have that is just calling clearmatches() (which clears all matches), when I remove that it doesn't happen anymore.
Either way at this point though, it doesn't seem like the end of the world to add a pcall wrapper to avoid breaking things (except perhaps in a scenario where we are missing an underlying bug, however I don't imagine that it'd be a feature-breaking bug to be trying to remove matches that don't exist). It's basically saying "try to delete this, and if it doesn't exist then I have nothing to delete in the first place", etc.
I wonder if we could use |
Nope :( Having What I believe might be happening is that |
Gotcha. So it seems then that one other solution would be to save off the vim.api.nvim_exec(
[[augroup MiniTrailspace
au!
au WinEnter,BufWinEnter,InsertLeave * lua MiniTrailspace.defer_highlight()
au WinLeave,BufWinLeave,InsertEnter * lua MiniTrailspace.unhighlight()
au FileType TelescopePrompt let b:minitrailspace_disable=v:true
augroup END]],
false
)
-- Create highlighting
vim.api.nvim_exec([[hi link MiniTrailspace Error]], false)
end
-- Module config
MiniTrailspace.config = {}
function MiniTrailspace.defer_highlight()
local win_id = vim.api.nvim_get_current_win()
vim.schedule(function()
MiniTrailspace.highlight(nil, { win_id = win_id })
end)
end
-- Functions to perform actions
--- Highlight trailing whitespace
---
---@param check_modifiable boolean: Whether to check |modifiable| (if it is off, don't highlight). Default: `true`.
---@param opts table: Additional options/state for highlighting, currently:
--- - win_id: The window when the autocommand was invoked
function MiniTrailspace.highlight(check_modifiable, opts)
check_modifiable = check_modifiable or true
-- Highlight only in normal mode
if H.is_disabled() or vim.fn.mode() ~= 'n' then
MiniTrailspace.unhighlight()
return
end
if check_modifiable and not vim.bo.modifiable then
return
end
local win_id = opts.win_id or vim.api.nvim_get_current_win()
if not vim.api.nvim_win_is_valid(win_id) then
return
end
...would work then? Basically just call a function that saves off the "proper" window ID during the scope of the By the way, do you have the "test" that you are using to find out whether or not these things do/don't work? When trying with things like |
|
Thank you very much for your patience. The reason I was so meticulous about both issues (highlighting and About Yeah, lack of tests is one very painful downside of this project. I hope to get myself together and write them, but not now, unfortunately. Thank you again! |
|
Absolutely, I appreciate the prodding from your end to find out why the problem was happening rather than just using Also note the problem with calling
however there is nothing for you to fix in this regard, it is purely within my own config. Thanks for the review, and the wonderful set of modules. |
|
Just let you know that I removed |
Fixes two issues:
defaultwhen callinghito handle these casesvim.fn.matchdeletewith apcallto avoid breaking things like Telescope if the match doesn't existAlso changes some calls that use
vim.fnto usevim.apiand check valid state, etc.Curious, is there a reason to
defer_fnon thehighlight:h autocmdcall? If we do that, the call to get the "current window" might not be accurate. Wanted to check before trying to change it here.