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
#downcase! directive to use with ruby injections #1190
Conversation
Thanks for the PR, I think we could do something more general like #812 |
@stsewd but isn't #812 capturing a node irrespective of case and this PR about processing a result of a capture? When we capture the string |
Both are about capturing a string from a node and matching it to a set of languages, for example the HTML parser would have an injection regex that's case-insensitive. But I agree we can just make it default to make it lowercase. |
query.add_directive('downcase!', function(match, _, bufnr, pred, metadata) | ||
local node = match[pred[2]] | ||
local text = query.get_node_text(node, bufnr) | ||
local language = string.lower(text) | ||
metadata.language = language | ||
end) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although I agree with the implementation, it may be better to make it a bit more generic, maybe by providing the key to set instead of just using language
this would make it look a bit like set, and may feel a little better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DerekStride vigoux is right. Could you add an argument that determines which metadata to downcase?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could even be a modifier for set! In a future PR (set! "language" @foo downcase!)
or directly apply on a key (set! "language" @foo) (downcase! "language")
to chain multiple functions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback, I liked the suggestion to allow the directives to be chained, and made the corresponding changes.
(#set! "language" @foo) (#downcase! "language")
Hehe you'll say I am asking many things, but I'd rather go for a mimic of the signature of the The reason is that we don't have any guarantees on the order of evaluation of the directives, and thus making them standalone is better. |
I assume you mean to mimic the difference of when there are 3 vs 2 arguments, like what's listed below: ["set!"] = function(_, _, _, pred, metadata)
if #pred == 4 then
-- (#set! @capture "key" "value")
metadata[pred[2]][pred[3]] = pred[4]
else
-- (#set! "key" "value")
metadata[pred[2]] = pred[3]
end Then we can support |
``` (#downcase! "language") ``` downcase! will ensure the metadata value for the specified key will be downcased. If the value is a node, it will downcase the text specified by the node. ``` (#downcase! @node "key") ``` You can also namespace the key with a specific capture, similar to how you can call `(#set! @node "key" "value")`
if #pred == 3 then | ||
-- (#downcase! @capture "key") | ||
key = pred[3] | ||
value = metadata[pred[2]][key] | ||
else | ||
-- (#downcase! "key") | ||
key = pred[2] | ||
value = metadata[key] | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vigoux I made the changes that we discussed, I think I found a bug with how set was implemented in neovim. I'll be opening a PR with a fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix is up here: neovim/neovim#14418
I think a better solution would be something more generic that could simply look up the language based on a set of rules. For example, looking it up by a table. Since we can configure the language through a directive we could make it look it up any way we want to, whether that's by matching a regex pattern, a simple table lookup, or a transformation function of the language. EDIT: Maybe we'll just save the more advanced stuff for another time... |
@vigoux Is the order of the directives not guaranteed? I have some work that relies on that... 😢 |
I have implemented what I mean in #1190 (comment) #1238. |
I'll dig in tree-sitter at some point, but I am not sure it is... |
@vigoux is this ready to merge? |
Yup, looks ready indeed. |
What
This is a follow-up to neovim/neovim#14152 which was superseded by neovim/neovim#14046 which allows setting the injected parser language on the metadata field of directives.
Add a
downcase!
directiveI moved the implementation of the
#downcase!
directive I proposed in neovim/neovim#14152 to this plugin as it seems to be a better home for it.Often times the text matched by an
@language
node may not exactly resemble the name of the parser for that language. More often than not the parser name will be lowercase, i.e.html
instead ofHTML
. In the below example ruby code a string constant is defined using ruby's heredoc syntax. The coding conventions for heredocs are to surround the string content with the name of the language in all uppercase letters ie.HTML
,SQL
,JAVASCRIPT
, etc.The
downcase!
directive is used in anqueries/ruby/injections.scm
file like so to convert theHTML
value in the@language
node to a valid lowercasehtml
name of the parser.Screenshot