Skip to content
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

feat: handle visual highlight using ModeChanged event #38

Merged
merged 5 commits into from
Oct 10, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 36 additions & 36 deletions lua/modes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -195,49 +195,49 @@ M.setup = function(opts)
local ok, current_mode = pcall(vim.fn.mode)
if not ok then
M.reset()
else
if current_mode == 'i' then
if key == utils.replace_termcodes('<esc>') then
M.reset()
return
end
return
end

if current_mode == 'i' then
if key == utils.replace_termcodes('<esc>') then
M.reset()
return
end
end

if current_mode == 'n' then
-- reset if coming back from operator pending mode
if operator_started then
M.reset()
return
end

if current_mode == 'n' then
-- reset if coming back from operator pending mode
if operator_started then
M.reset()
return
end

if key == 'y' then
M.highlight('copy')
operator_started = true
return
end

if key == 'd' then
M.highlight('delete')
operator_started = true
return
end
if key == 'y' then
M.highlight('copy')
operator_started = true
return
end

if key == 'd' then
M.highlight('delete')
operator_started = true
return
end

if
current_mode:lower() == 'v'
or current_mode == utils.replace_termcodes('<c-v>')
key:lower() == 'v'
or key == utils.replace_termcodes('<c-v>')
then
if
key == utils.replace_termcodes('<esc>')
or key == current_mode
then
M.reset()
else
M.highlight('visual')
operator_started = true
end
M.highlight('visual')
operator_started = true
return
end
end

if key == utils.replace_termcodes('<esc>') or key == current_mode then
Copy link
Contributor

@TheBlob42 TheBlob42 Sep 8, 2022

Choose a reason for hiding this comment

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

I guess you introduced key == current_mode here in case the user presses v in visual mode to exit? But unfortunately this breaks other modes and their highlighting:
For example in insert mode if I press the character i or if I press the character r in replace mode (if using R, see :h Replace) the highlighting is reset, but I'm still in the respective mode.

Copy link
Contributor

Choose a reason for hiding this comment

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

My proposal would be to limit this special exit condition (key == current_mode) to visual mode only as I don't know any other mode that can be exited by pressing the same "key" again (if I'm forgetting one please correct me). The general check for <esc> however makes perfect sense to me 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For example in insert mode if I press the character i or if I press the character r in replace mode (if using R, see :h Replace) the highlighting is reset, but I'm still in the respective mode.

You are right, I never realized it since I disabled the cursor line in insert mode

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, I think I got a more robust solution

M.reset()
return
end
end)

---Set highlights when colorscheme changes
Expand All @@ -256,7 +256,7 @@ M.setup = function(opts)

---Reset highlights
vim.api.nvim_create_autocmd(
{ 'CmdlineLeave', 'InsertLeave', 'TextYankPost', 'WinLeave' },
{ 'CmdlineLeave', 'InsertLeave', 'TextYankPost', 'WinNew', 'WinLeave' },
{
pattern = '*',
callback = M.reset,
Expand Down