weineng96 · 2022-12-05 15:25
In Swift, when an RGB value is provided, xcode will render the actual colour of the RGB value on the side.
Would this be possible to be implemented in tree sitter? Currently at work, we have a key-value mapping of an enum integers (eg Inventory(69420) to object names (Eg "Iphone 15 SuperPro"). I'm hoping to render the object name as a comment on the side (similiar to how git-lens work).
​
Would anyone be able to provide a pointer on how I should go about doing this?
Psychological-Ad7512 · 2022-12-05 16:17
You would need to do a few steps:
1. Create a tree-sitter query to extract the enum information from the specific file which has the mapping \[1\]
2. Create a tree-sitter query to match everything which looks like \`Inventory(xxx)\`. If you want to deal with aliasing, you'll have to do that yourself.
3. Create overlays for each item found in 2.
\[1\] https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries
eleven_cupfuls · 2022-12-05 18:51
Tree-sitter does not have cross-file semantic information like this. It operates on one file at a time and only has syntactic information about that one file. You can't recognize things like "this enum is defined in the current project", let alone look up its value. That would be a task for LSP.
That said, something like this should be doable, and in fact without Tree-sitter. For example, there is an existing package, [rainbox-mode](https://elpa.gnu.org/packages/rainbow-mode.html) that highlights color literals with their rendered color. There are other packages like [hl-todo](https://github.com/tarsius/hl-todo) that highlight keywords.
What you can do, then, is [add font lock keywords](https://www.gnu.org/software/emacs/manual/html_node/elisp/Search_002dbased-Fontification.html#Search_002dbased-Fontification) for the Swift files in your project. Those keywords would use a function (like `rainbow-mode` does) and add an overlay with whatever the value is. Not necessarily simple, but definitely possible.
weineng96 · 2022-12-05 23:04
Hmm, this seems like string matching, which isnt exactly what I want.
For eg, I would like a way to match
AddInventory(69420, 123), which adds 123 to the quantity of iphone 15.
Ideally, i would like to see AddInventory(69420 "iphone15", 123) in the code.
Ive not thought deeply into this problem yet (hack day is on Friday), but I would think some kind of AST matching should work.
Do you know if some kind of LSP solution will work?
weineng96 · 2022-12-05 23:04
Will poke around tonight! Thank you for looking!
weineng96 · 2022-12-05 23:15
Hmm, this seems like string matching too. Do you know if some kind of AST parsing to detect the struct will work?
eleven_cupfuls · 2022-12-06 00:15
Sorry, I'm not quite sure I'm following this anymore. You have integer literals that you want to map to enum case names? Or where did the enum come in?
Yes, the LSP server (sourcekit-lsp) will (potentially) have that kind of information, but I unfortunately don't know how to get at it or apply it. It does sound like a fun hack day project!
But the most straightforward way to make this work in Emacs is going to be copying the mapping you want into Elisp code, and applying the information from there.
Again, unless the mapping is defined in the same file, Tree-sitter isn't going to be able to help at all. And even then, it's not likely. A syntax tree doesn't help with interpreting the code, knowing the values of things. It doesn't have any _semantic_ information, like "this function takes an `Inventory`", or "this function is actually a struct initializer". Even for a literal, where there's some inferrable information, it doesn't know whether your integer literal is a `UInt64` vs. an `Int`.
You can do matching in queries to special case things, e.g.:
(function_call name: (identifier) @name
(.eq? @name "AddInventory")) @some-special-highlight-rule
but that's about as far as it goes.
[deleted] · 2022-12-06 11:15
[deleted]
Psychological-Ad7512 · 2022-12-06 14:38
What do you mean? tree sitter parses the AST; there are regexp matchers, but for string nodes within an AST (think method names etc).