Skip to content

Avoid non-portable glob pattern in wiki#fzf#pages().#248

Merged
lervag merged 1 commit intolervag:masterfrom
broken-pen:fix-fzf-pages
Oct 11, 2022
Merged

Avoid non-portable glob pattern in wiki#fzf#pages().#248
lervag merged 1 commit intolervag:masterfrom
broken-pen:fix-fzf-pages

Conversation

@broken-pen
Copy link
Copy Markdown

When g:wiki_filetypes contains more than one file type, the function wiki#fzf#pages() only shows files that are in a subdirectory of the wiki root (but no deeper).

The reason seems to be the glob pattern that the function generates. If g:wiki_filetypes == ['md'], the pattern looks like this:

**/*.md

If g:wiki_filetypes == ['md', 'wiki], the pattern looks like this:

**/*.{md,wiki}

This makes use of brace-expansion. The vim help page for wildcards has the following to say:

Which wildcards are supported depends on the system.
These are the common ones: […]

It then lists some wildcards, but notably not brace-expansion.

While on my system (Ubuntu 22.04), Neovim 0.7.2 seems to support brace-expansion, it does so in an extremely fickle manner:

glob('*.md')          " Finds md files at the root
glob('**.md')         " Finds md files at the root
glob('**/*.md')       " Finds md files in whole tree, including root
glob('*.{md,wiki}')    " Finds md and wiki files at the root
glob('**.{md,wiki}')   " Finds md and wiki files at the root
glob('**/*.{md,wiki}') " Finds md and wiki files *one* directory deep

It appears that ** is treated exactly like * except in certain circumstances. The presence of brace-expansion seems to disable these circumstances.

While it stands to reason that this is a bug, the child has already fallen into the well. This commit proposes to avoid the buggy glob pattern and instead to perform one glob per file type, and combine the results. This may well cause some slowdown for large and nested wikis, but I can't think of a better solution.

I've had a sweep through all occurrences of globpath() and it seems none outside of wiki#fzf#pages() use brace-expansion.

Please let me know if there's anything you'd like me to change or if there's another solution you'd like to see pursued. I'm not particularly attached to this one.

When `g:wiki_filetypes` contains more than one file type, the function
`wiki#fzf#pages()` only shows files that are in a subdirectory of the
wiki root (but no deeper).

The reason seems to be the glob pattern that the function generates. If
`g:wiki_filetypes == ['md']`, the pattern looks like this:

    **/*.md

If `g:wiki_filetypes == ['md', 'wiki]`, the pattern looks like this:

    **/*.{md,wiki}

This makes use of *brace-expansion*. The vim help page for `wildcards`
has the following to say:

> Which wildcards are supported depends on the system.
> These are the common ones: […]

It then lists some wildcards, but notably not brace-expansion.

While on my system (Ubuntu 22.04), Neovim 0.7.2 seems to support
brace-expansion, it does so in an extremely fickle manner:

```vim
glob('*.md')          " Finds md files at the root
glob('**.md')         " Finds md files at the root
glob('**/*.md')       " Finds md files in whole tree, including root
glob('*.{md,wiki}')    " Finds md and wiki files at the root
glob('**.{md,wiki}')   " Finds md and wiki files at the root
glob('**/*.{md,wiki}') " Finds md and wiki files *one* directory deep
```

It appears that `**` is treated exactly like `*` except in certain
circumstances. The presence of brace-expansion seems to disable these
circumstances.

While it stands to reason that this is a bug, the child has already
fallen into the well. This commit proposes to avoid the buggy glob
pattern and instead to perform one glob per file type, and combine the
results. This may well cause some slowdown for large and nested wikis,
but I can't think of a better solution.

I've had a sweep through all occurrences of `globpath()` and it seems
none outside of `wiki#fzf#pages()` use brace-expansion.
Copy link
Copy Markdown
Owner

@lervag lervag left a comment

Choose a reason for hiding this comment

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

Thanks, this looks very good to me! I'll make the minor adjustment mentioned in the review and merge immediately.

return l:pages
endfunction

let s:slash = exists('+shellslash') && !&shellslash ? '\' : '/'
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This seems duplicated (see line 28). I propose to remove line 28.

lervag added a commit that referenced this pull request Oct 11, 2022
@lervag lervag merged commit 3d9c25b into lervag:master Oct 11, 2022
@broken-pen broken-pen deleted the fix-fzf-pages branch October 12, 2022 07:56
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