epwalsh / obsidian.nvim

Obsidian 🤝 Neovim
Apache License 2.0
3.3k stars 150 forks source link

For completion, default to links/aliases, make leading #/^ complete for anchors/blocks #589

Closed linduxed closed 1 month ago

linduxed commented 1 month ago

🚀 The feature, motivation and pitch

Since v3.7.0, the addition of support for anchor and block search has resulted in the default behavior of the completion to change.

Previously typing [[ would (after a configurable amount of characters) initiate search among the note names and their aliases, much like how Obsidian works. The recent addition of the support for anchors and blocks changes two things:

  1. The completions no longer resemble the default behavior of Obsidian, due to Obsidian requiring a prefix after [[ for initiating a completion different than note names and aliases ([[# for headings in the current note, [[## in all notes; [[^ for blocks in this note, and [[^^ in all notes).
  2. The amount of completions to process in the vault increases, meaning that the time before the completion menu pops up has increased notably.

While having the ability to get completion for anchors and blocks is useful, to both be more in line with the working of Obsidian, and to avoid the performance penalty of always completing for everything (notes, aliases, anchors, and blocks), would it be a good idea to introduce a similar kind of prefix detection (i.e. looking for something like [[#) to the completion triggering?

Alternatives

An alternative would be making it possible to disable the anchor and block completion, to go back to the previous way things worked. The README made me think that this should be possible by changing the wiki_link_func config opt, but (unless I've missed something) this does not seem to do the trick.

Additional context

No response

epwalsh commented 1 month ago

Hey @linduxed, I like the idea of only giving header/anchor link completion when the current prompt ends with # or ^. That seems perfectly reasonable to me and I think would regain the previous performance. I didn't know about Obsidian's [[## / [[^^ global search prompt, but that seems like a good feature to add here as well.

epwalsh commented 1 month ago

I like the idea of only giving header/anchor link completion when the current prompt ends with # or ^.

@linduxed I just opened a PR for this. Can you test this out?

linduxed commented 1 month ago

Hello @epwalsh! Thank you for the very quick response and code!

I checked out the PR locally and tested. Observations:

  1. The PR does indeed make it so that [[ only searches for note names.
  2. [[ does not show aliases in the completion. Not sure if this is expected or not.
  3. The searching for headers/anchors is either confusing to me as to how it should be used, or something is not working: a. Writing [[# does not seem to give any results. b. Writing [[## seems to give a short list of inaccurate results: I have a Purchases.md note with a ## Books heading, and this heading does not get found, while 3 other results that are totally unrelated get returned. The same 3 results seem to be returned for some other queries, so the text after [[## does not seem to matter. For a brief moment, however, there seems to be a flash of a long list of completion results that cover the screen, and maybe those results are good, but the flash is too quickly gone to tell.
linduxed commented 1 month ago

Oh, wait, my bad:

when the current prompt ends with # or ^.

I misunderstood. That seems to work. In that case, I guess my only relevant observation is the part about aliases not being shown.

linduxed commented 1 month ago

Further observations upon closer inspection:

  1. Now that I understand how to invoke the header functionality, the completions seem to contain false positives. Example: I have a note that's called Purchases.md with a header called ## Books, and a note called Purchase of apartment.md that does not have a header containing the word "books". Nonetheless, Purchase of apartment#books is one of the completions for [[purchase#books.
  2. Aliases are indeed there in the completion for [[, but they get listed in the completion as actual notes, and get inserted as actual note titles. Example: let's presume I have a note called Books.md with the alias To read. To read will be listed as a completion, and if one selects that, at least what one would expect that what would be inserted as text in the document would be [[Books|To read]], but instead [[To read]] is inserted.
linduxed commented 1 month ago

Question: how is one to reference headers in the same document with this PR, excluding writing out the name of the current note and then the header (i.e. in a note called Foo.md, writing [[Foo#?

linduxed commented 1 month ago

Would you want me to move some of the content in my previous messages to the PR thread, to have it all in context there?