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

fix(apply_icons): filename may be truncated #243

Merged
merged 2 commits into from
Jan 2, 2024

Conversation

fwy
Copy link
Contributor

@fwy fwy commented Jan 2, 2024

Version: Neovim v0.9.4 and Vim 9.0 running on Linux and Windows

I am a long-time dirvish user, but only recently started using the dirvish#add_icon_fn() to add devicons to the dirvish display. However, I found that occasionally the filenames displayed by dirvish are truncated from the left when the icons are being applied.

To see the issue, create files 'abc-file.txt', 'defg-file.txt', and 'file.txt' in a directory and display them in a dirvish buffer. All filenames display as 'file.txt' as shown below.

image

This Neovim screenshot was taken on a Windows workstation, but the same behavior can be seen in Neovim and Vim on both Linux and Windows machines.

The problem seems to occur when the base name of one of the files in the directory matches the right-hand portion of one or more other filenames in the same directory. However, experimentation shows that it does not always happen in this case, so there must be more specific conditions contributing to it.

After some debugging, I found that the cause is in the syntax matching performed by the s:apply_icons() function in autoload/dirvish.vim. I am new to vim syntax matching, but believe that the problem is caused by the iteration of the match operation across all the files in the directory. When the common match pattern encounters multiple files whose names end in the same characters, it 'swallows up' the leading characters of the names that do not match, leaving identical filename suffixes shown in the dirvish display.

To fix it, I inserted a line counter into the match pattern inside the loop that iterates through the file list, forcing a unique pattern to be matched against each file. This fix has worked fine on my machines for several weeks now.

@@ -433,7 +435,7 @@ func! s:apply_icons() abort
let isdir = (f[-1:] == s:sep)
let f = substitute(s:f(f), escape(s:sep,'\').'$', '', 'g') " Full path, trim slash.
let tail_esc = escape(fnamemodify(f,':t').(isdir?(s:sep):''), '[,*.^$~\')
exe 'syntax match DirvishColumnHead =^.\{-}\ze'.tail_esc.'$= conceal cchar='.icon
exe 'syntax match DirvishColumnHead =\%'.i.'l^.\{-}\ze'.tail_esc.'$= conceal cchar='.icon
Copy link
Owner

@justinmk justinmk Jan 2, 2024

Choose a reason for hiding this comment

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

Since the match is per-line, I wonder if tail_esc is needed at all. We could possibly simplify it by matching up to a hardcoded %c (column). That could possibly simplify the code a bit, or improve the regex performance..

@justinmk justinmk changed the title Fix apply_icons syntax matching to prevent filename truncation. fix(apply_icons): filename may be truncated Jan 2, 2024
@justinmk justinmk merged commit aed4e49 into justinmk:master Jan 2, 2024
@justinmk
Copy link
Owner

justinmk commented Jan 2, 2024

Thank you!

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