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

LSP auto-completion of YAML frontmatter tags #144

Closed
mickael-menu opened this issue Jan 5, 2022 · 12 comments · Fixed by #146
Closed

LSP auto-completion of YAML frontmatter tags #144

mickael-menu opened this issue Jan 5, 2022 · 12 comments · Fixed by #146
Labels
feature request Something is missing

Comments

@mickael-menu
Copy link
Member

Hello!
Is it planned to add auto-complete on tags in the YAML frontmatter?
It works well when added with # but I use them only in the frontmatter.
Originally posted by @tissieres in #22 (comment)

@mickael-menu mickael-menu added the feature request Something is missing label Jan 5, 2022
@mickael-menu
Copy link
Member Author

LSP auto-completion needs a trigger character, which is easy with #hashtags and :colon:tags:, but not with the YAML frontmatter since there's no character distinguishing tags.

A possible workaround would be to use # for the trigger character, and if we detect that we are inside a YAML frontmatter, request the editor to remove it after insertion. Not super straightforward but feasible, I think this would be useful to have in zk.

@kabouzeid
Copy link
Contributor

This is not completely true. Completion is also automatically triggered when typing an identifier, and it can of course always be manually triggered by the user. See (https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#completionOptions) and (https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#completionTriggerKind).

I've just checked that Neovim will in fact send a complete request when starting to type a tag name (identifier) in the frontmatter. VSCode does the same. This is from the logs:

[DEBUG][2022-01-05 20:32:24] .../vim/lsp/rpc.lua:347	"rpc.send"	{  id = 36,  jsonrpc = "2.0",  method = "textDocument/completion",  params = {    context = {      triggerKind = 1    },    position = {      character = 7,      line = 2    },    textDocument = {      uri = "file:///home/karim/Documents/Zettelkasten/index.md"    }  }}

Notice that context.triggerKind is set to 1 in this case.

@mickael-menu
Copy link
Member Author

Ha that's interesting, do you know if there's a way to request a specific kind of completion item from the client?

@kabouzeid
Copy link
Contributor

I'm not sure I understand what you mean. The client can (and usually does) filter and sort the completion results locally according to user prefs. But how would that help here?

@mickael-menu
Copy link
Member Author

No I mean how do you know whether to complete tags or links (not a great example, but more completion items might be added later on) from the LSP server when receiving this?

Right now, I'm using the value of the triggerCharacter for this.

Or maybe the server is supposed to check the location of the caret to infer the kind of item to complete?

@kabouzeid
Copy link
Contributor

Or maybe the server is supposed to check the location of the caret to infer the kind of item to complete?

Yes :)

@mickael-menu
Copy link
Member Author

I think it would be pretty trivial to support for:

tags: [a, b]

But YAML bullet lists would require more work:

tags:
  - a
  - b

@kabouzeid
Copy link
Contributor

Kind of hacky, but relatively easy and might work very well:
On the server side, we could insert some random string at the completion position, then parse the yaml and see if the inserted random string would be parsed as a tag.

@mickael-menu
Copy link
Member Author

Yeah that should work fine.

@mickael-menu
Copy link
Member Author

Could you check this out #146? I implemented your idea @kabouzeid, it works well but requires the YAML frontmatter to be valid, so if you have a hanging [ or ", it won't auto-complete.

@mickael-menu
Copy link
Member Author

cc @tissieres

@tissieres
Copy link

Hi @mickael-menu
Just tested it and it works perfectly for me. BTW, I'm using a template with a valid frontmatter at the beginning:

tags: []

Thanks very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Something is missing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants