zk-org / zk

A plain text note-taking assistant
https://zk-org.github.io/zk/
GNU General Public License v3.0
1.63k stars 118 forks source link

Markdown links render as wikilinks when inserted via completion #436

Closed tjex closed 1 month ago

tjex commented 1 month ago

Check if applicable

Describe the bug

No doubt related / see also #435 .

Despite using wikilink syntax to trigger completion ([[), when selecting a link from the lsp link completion menu, the link should be rendered as a regular markdown link provided the user has link-format = "markdown" in their config.toml.

This however only happens for me if I select an initial completion option.

If I type, to filter the list, and select an option, the link is inserted as a regular wiki link.

Here a starting point: https://github.com/zk-org/zk/blob/725126575d00130c2fa423d36c89f0fff328a318/internal/adapter/lsp/server.go#L660

How to reproduce?

  1. have link-format = "markdown" set in config
  2. open a note
  3. type [[ and accept a completion

Link should render as a markdown link (and it does for me)

On a separate line:

  1. type [[
  2. filter for a different note by typing a note title
  3. accept a completion option

Link should render as markdown link (and it does not for me, it renders as a wikilink)

zk configuration

na

Environment

zk 0.14.1
system: Darwin 23.4.0 arm64
WhyNotHugo commented 1 month ago

I cannot reproduce this; I trigger completion with [[, but the link is inserted in markdown format for me.

Using the LSP via neovim + nvim-cmp.

tjex commented 1 month ago

But what if you filter the list, and then accept a completion option?

i.e, this:

  1. type [[
  2. filter for a different note by typing a note title
  3. accept a completion option

Link should render as markdown link (and it does not for me, it renders as a wikilink)

WhyNotHugo commented 1 month ago

Following those steps, the link renders as markdown for me.

Accepting/confirming the completion replaces the placeholder with a markdown link for me.


If instead of accepting the first choice, I pick another, the placeholder text is replaced as soon as I pick another.

tjex commented 1 month ago

Here an asciicast: https://asciinema.org/a/4lvriIlQLHMCKF33ms155QAs8

So it's actually a slightly different step (second and last link attempt in video):

  1. type [[
  2. start typing some text of a title
  3. trigger completion
  4. select completion

Link renders as wikilink.

@WhyNotHugo can you see if that can be reproduced on your end? I also just dissabled my autopairs plugin, in case that was causing issues. But the behaviour is the same.

WhyNotHugo commented 1 month ago

What completion plugin is this?

When you do the second (failing) insertion, what mapping do you use? What function does this call? I have an impression that you may not be properly triggering the completion and that this is an IDE configuration issue.

Personally, I use nvim-cmp, and I mapped C-o to cmp.mapping.confirm({ select = true }),, which will auto-select the first entry and insert that.

tjex commented 1 month ago

So after typing a few chars, I hit cmp.mapping.complete() (to bring up the pop up) and then cmp.mapping.confirm({ select = true }).

So yeah, I'm also using nvim-cmp. But maybe you spot something amiss? I'll also check through your dotfiles and see. It's a really annoying issue... 🫨

nvim-cmp conf

tjex commented 1 month ago

Hmm, you don't even have a keybind for cmd.mapping.complete() 🧐 https://git.sr.ht/~whynothugo/dotfiles/tree/main/item/home/.config/nvim/init.lua#L64

I also don't see a setting for autocomplete. What is this sorcery; How do you invoke the pop-up? haha

tjex commented 1 month ago

ugh, ok I (half) figured it out.

When I'm typing, and open completion, the only completion results I get are plain text completion results, and not lsp files.

Even in a (freshly saved) blank document (where the text "untitled 2") is not present, cmp still supplies me with a completion buffer text option, "untitled 2".

Stranger still, after typing unt|, the lsp completion result for the file (that will actually correctly insert a markdown link) disappears! In other words, after I type, I seemingly only get text results, or the lsp / cmp is getting the type of the completion result incorrect 🤷‍♂️

I guess this is what was throwing me off, I had no reason to expect a text completion when the only text in the document was # Untitled 1, so I overlooked the little Tt symbol in the cmp popup.. As I dont have buffer as a completion source. I guess nvim_lsp completion source grabs all text from other files? Or maybe headings, caus its seeing a markdown document...

It's still behaving strangely though. But it needs more investigating from my end.

In other, other words

  2 # Untitled 1
  1
3   [[]]
~  ╭────────────────────╮
~  │ Untitled 1 󰈇 [LSP] │ <- lsp file
~  │ Untitled 2 󰈇 [LSP] │ <- lsp file
~  │ {{title}}  󰉿 [LSP] │ <- why it pics this up, i dno.
~  │ Untitled 2 󰉿 [LSP] │ <- buffer text (that doesn't exist in this buffer 🤷‍♂️)
~  ╰────────────────────╯
  2 # Untitled 1
  1
3   [[un]]
~    ╭────────────────────╮
~    │ Untitled 2 󰉿 [LSP] │ <- the buffer text option, _not_ the lsp file option.
~    ╰────────────────────╯
tjex commented 1 month ago

...Right. Got it:

incorrect:

            sources = cmp.config.sources({
                { name = "nvim_lsp" },
                { name = "luasnip" },
                -- { name = "nvim_lua", keyword_length = 6 },
                { name = "path" },
                { name = "buffer" },
            }),

correct:

            sources = cmp.config.sources({
                { name = "nvim_lsp" },
                { name = "luasnip" },
                -- { name = "nvim_lua", keyword_length = 6 },
                { name = "path" },
            }, {
                { name = "buffer" },
            }),

This slight change was meaning that my completion results were disappearing when I began to type.. As I never have autocomplete on, I never considered this strange, as I always hit complete() manually, to invoke the pop-up.

If I do type (whereby the lsp file and text completions stay in place), but then hit complete(), I still only get the text completion. This I think is the core source of the 'bug' then, in our implentation of completion / lsp somewhere.

Because I expect that after typing, that re-executing complete() which brings up the pop-up (so, not select()) that I should be shown the exact same results as when I finished typing.

Here asciinema showing this

In any case, will close this for now, as if there is a bug. It's definitely worth starting a new issue for it. This one got busy!! Thanks for your extra eyes Hugo.

WhyNotHugo commented 1 month ago

Hmm, you don't even have a keybind for cmd.mapping.complete() 🧐

Completion triggers automatically when I type.

I had no idea about this syntax using cmp.config.sources. I use { name = "buffer", priority = 1 }, which just keeps buffer suggestions at the lowest priority (e.g.: last in the list).

For me, the LSP only shows suggestions of type Reference. Not sure what 󰈇 and 󰉿 mean, but I only have one type of suggestions. Maybe my LSP is configured different? Can you confirm that you don't have some other LSP running with :LspInfo?

tjex commented 1 month ago

I also have marksman lsp running, but it makes no difference. If I dissable markdman, I don't get the Tt completion results when cmp triggers after typing [[. But, if I type some chars and then hit complete() I still only get one singular result with Tt symbol, when there was previously an lsp result to create the link.

I also didn't know about this syntax for cmp.config.sources, I'm quite sure I copy and pasted the config from setup when I first installed, so maybe it was a change at some point...

WhyNotHugo commented 4 weeks ago

I realised that I'm using [[ to trigger link completions even though I'm using markdown style links.

I configured ["<C-s>"] = cmp.mapping.complete(). If I press ctrl+S follows by some letters, I only get dictionary completion but no suggestions from the LSP.

I think that the LSP does not send any completions unless I have previously typed [[.

WhyNotHugo commented 3 weeks ago

The completion provider only yields results in the two preceding characters are [[[: https://github.com/zk-org/zk/blob/4a51e398ae4572b2a1338fd9017ceeff30e5ffda/internal/adapter/lsp/server.go#L647

This is why manually invoking completion yields no results.

WhyNotHugo commented 3 weeks ago

This is called from internal/adapter/lsp/server.go:204

I think when params.Context.TriggerKind == protocol.CompletionTriggerKindInvoked, we ought to always provide results.

WhyNotHugo commented 3 weeks ago

I'll open a separate issue.