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

feat: definitions and references lsp hint #1016

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

rszyma
Copy link
Contributor

@rszyma rszyma commented May 6, 2024

Describe your changes. Use imperative present tense.

Add goto definition support for LSP consumers. Adding support for it directly in LSP server could maybe be doable, but unreliable (e.g. templates are complicating things) and so a lot of parser logic would need to be reimplemented for all edge cases, so I think this is better added directly to kanata-parser.

For now I've added definition and references for aliases. I'd like to wait for recieve some feeback (i.e. if this is a direction we want to take) before adding the rest (variables, layers, etc.).

Currently the code is working for aliases when paired with rszyma/vscode-kanata#32

Implementation notes:

I couldn't add lsp_hints to ParserState, because it would require changing & to &mut on s param in many places which would be really ugly, and even then, when trying to switch to &mut at some point I got an error regarding multiple borrows of &mut s at the same time. So instead I've added additional lsp_hints param to many functions.

Other done things:

  • Move lsp_hint_inactive_code from ParserState to IntermediateConfig

Other thoughts:

Perhaps for performance purposes LSP features could be behind a feature flag, but it doesn't seem like it causes any major performance degradation + parser code runs only once, so for simplicity I didn't introduce the flag.

Checklist 1

Go-to definition and references implemented for:

  • aliases
  • variables
  • virtual_keys
  • chord_groups
  • layers
  • includes
  • templates

Checklist 2

  • Add documentation to docs/config.adoc
    • Yes or N/A
  • Add example and basic docs to cfg_samples/kanata.kbd
    • Yes or N/A
  • Update error messages
    • Yes or N/A
  • Added tests, or did manual testing
    • Yes

@rszyma
Copy link
Contributor Author

rszyma commented May 6, 2024

Also this would enable semantic highlighting I think?

@jtroo
Copy link
Owner

jtroo commented May 6, 2024

I couldn't add lsp_hints to ParserState, because it would require changing & to &mut on s param in many places which would be really ugly, and even then, when trying to switch to &mut at some point I got an error regarding multiple borrows of &mut s at the same time. So instead I've added additional lsp_hints param to many functions.

Consider using RefCell for this; I think it will suit the purpose well enough.


Perhaps for performance purposes LSP features could be behind a feature flag, but it doesn't seem like it causes any major performance degradation + parser code runs only once, so for simplicity I didn't introduce the flag.

I think we should do it for this new code and retroactively do it for the inactive code hints too. Probably good to compartmentalize the bulk of the new stuff into a new module.

We could make the functions always be called, and then the function body can be conditionally compiled (with an empty body by default); and rely on dead code elimination to do the right thing. Or add conditional compilation at the call-site to guarantee the optimization.

.push_from_atom(atom);
vkey_expr
.atom(s.vars())
.expect("must be atom since we're matching atom")
Copy link
Owner

Choose a reason for hiding this comment

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

Fly-by comment

The way this new code works isn't right because matching an expr and finding an Atom might find you a $var that is actually a list.

You might want to add span_atom equivalent of span_list, which I haven't yet added because I didn't have the need for it.

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.

None yet

2 participants