Fix buffer sorting and display issues#119
Merged
jlanzarotta merged 17 commits intojlanzarotta:masterfrom Feb 18, 2025
Merged
Conversation
This allows quick lookup of buffer details given the buffer number.
The `term://` prefix on the buffer name is Neovim-specific, but the check of `&buftype` works for both Vim and Neovim.
- Use `simplify()` to normalize paths like `dir/../file`. - Remove trailing path separator for directories. - Eliminate the shortname `<DIRECTORY>` for directories; instead, use the directory's basename as is done for files. Handle the special case of root directory paths (where the trailing path separator cannot be removed); use a shortname of `.` for this case. - Add `homepath` to `s.types`; this is `fullpath` shortened for paths in `$HOME` and without the trailing path separator. Other displayable paths (`path`, `relativepath`, `relativename`) are similarly shortened for paths in `$HOME`.
- Undesirable substitution could occur anywhere in the path (not just at the start). Shortening for paths in `$HOME` is now done via `homename` and associated variables from `s:types`.
This eliminates anomalies caused by sorting based on the displayed text.
- The sort order no longer changes when toggling `Absolute`/`Relative`
and `Split`/`Full` display options.
- For `Relative Split path` view, files below the current working
directory now remain grouped together. For example, launching Vim via
this invocation:
vim README.md LICENSE doc/bufexplorer.txt plugin/bufexplorer.vim \
~/.vimrc /etc/passwd
Previously would lead to this order when sorting by `fullpath`, where
the files of `bufexplorer/` are not kept together:
2 h LICENSE . line 1
1 %a README.md . line 1
6 #h= passwd /etc line 1
3 h bufexplorer.txt doc line 1
4 h bufexplorer.vim plugin line 1
5 h .vimrc ~ line 1
They are now sorted by the overall `fullpath`:
6 passwd /etc line 0
5 .vimrc ~ line 0
3 bufexplorer.txt doc line 0
2 LICENSE . line 0
4 bufexplorer.vim plugin line 0
1 %a README.md . line 16
This allows a directory icon to be chosen.
Users who don't want to see unlisted buffers shouldn't have to pay for the expensive work of calculating buffer details for unlisted buffers, only to have that information ignored. Calculate an unlisted buffer's details only after we know the user wants to view them.
Contributor
Author
|
This pull request also includes a change to the method of detecting a terminal window. Instead of checking for the prefix |
Owner
|
Very nice changes along with fantastic explanation. Let me pull the code and give it a try. |
inko94
approved these changes
Mar 2, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This pull request addresses a few issues surrounding buffer sort order and buffer name displaying. The initial motivation for these changes was to ensure that files and directories below a common directory would sort near each other when using the
fullpathsort order. Withfullpath, the ordering should depend only on each buffer's full path name; historically, however, sorting has been a best-effort function of the displayed strings in the BufExplorer window, leading to some anomalies.Issues addressed by these changes
Sorting by
fullnamenow depends only on the buffer's absolute path using a new sorting mechanism. Other sort modes have been converted to use this mechanism as well.Display of directory buffers is now suppressed when
g:bufExplorerShowDirectories == 0as described in the BufExplorer documentation.Path calculations for the elements in
s:typeshave been normalized.fullpathnow hassimplify()applied to normalize paths likedir/../file. Trailing path separators for directories have been removed. The shortname<DIRECTORY>for directories has been eliminated; instead, the directory's basename is used as is done for files (except for root directories, where the trailing path separator cannot be removed; a shortname of.is used for this case). A new element,homepath, has been added tos.types; this isfullpathshortened for paths in$HOMEand without the trailing path separator. Other displayable paths (path,relativepath,relativename) are similarly shortened for paths in$HOME.Textual substitution of
$HOME->~in paths has been eliminated. This substitution could occur anywhere in the path, not just at the start, causing undesirable anomalies. Shortening for paths in$HOMEis now done viahomenameand associated variables froms:types.If the devicons plugin is installed, a buffer's
buf.isdirstatus has been added as a parameter passed to that plugin, allowing the plugin to supply a directory icon for buffer directories.Avoid calculating buffer details until they are needed for display. Users who don't want to see unlisted buffers shouldn't have to pay for the expensive work of calculating buffer details for unlisted buffers, only to have that information ignored. This resolves bufexplorer is slow to open with many unlisted buffers #20 ("bufexplorer is slow to open with many unlisted buffers").
Below are some examples demonstrating the changes
Where "Before" and "After" examples are shown below, "Before" applies to BufExplorer 7.5.0 and "After" applies to this pull request.
fullpathsortingStart from the
bufexplorer/source directory and invoke Vim as:Then launch BufExplorer via
\be.Absolute Full pathview (useshomename):Before:
After:
Relative Full pathview (usesrelativename):Before:
After:
Absolute Split pathview (usesshortname+path):Before:
After:
Relative Split pathview (usesshortname+relativepath):Before:
After:
Substitution of
~with$HOMETo demonstrate the problem of substituting
~with$HOME, create a test directory and launch Vim as follows:Then invoke BufExplorer via
\be. Notice that the file's path was previously incorrectly shortened totmp~:Before:
After:
Also, because of the last-minute nature of the textual substitution of
~with$HOME, minimum column width calculation could have overestimated how many columns were necessary. This can be seen in the previousAbsolute Full pathexample above, e.g.:Before:
After:
Speed when ignoring unlisted buffers
To demonstrate speed improvements in the presence of many unlisted buffers, create a test directory with many small files:
Invoke Vim and search for
textin these files:Use Vim's profiling feature to measure timing:
Then invoke BufExplorer via
\be. By default, unlisted buffers are not displayed.Now stop profiling and exit Vim:
profile.logwill have timing measurements. Excerpts below focus on the total time of theBufExplorer()function.Before:
After:
Total BufExplorer time went down from 605 ms to 175 ms when ignoring unlisted buffers.
To test with unlisted buffers active, relaunch Vim, launch BufExplorer with
\be, pressuto display unlisted buffers, then quit BufExplorer viaq.Now repeat the
:grepmeasurements as above:Before:
After: