nvim-treesitter-textobjects
nvim-treesitter-textobjects copied to clipboard
feat: add Solidity language support
This PR adds textobjects support for the Solidity programming language. It introduces a new textobjects.scm file specifically for Solidity, enabling textobject functionalities when working with Solidity.
Changes introduced:
- Added a new
textobjects.scmfile for Solidity - Implemented textobjects for Solidity-specific constructs such as contracts, interfaces, libraries, and structs
- Included standard textobjects like functions, loops, conditionals, and assignments
- Adapted existing textobjects to fit Solidity's syntax and structure
Textobjects implemented:
- function.inner/outer
- class.inner/outer (for contracts, interfaces, libraries, and structs)
- loop.inner/outer
- conditional.inner/outer
- call.inner/outer
- block.inner/outer
- parameter.inner/outer
- comment.inner/outer
- assignment.inner/outer/lhs/rhs
- return.inner/outer
- number.inner
- attribute.inner/outer (for natspec comments)
- statement.outer
Testing:
I have tested these textobjects with various Solidity code snippets to ensure they work as expected. However, further testing by the community would be appreciated to catch any edge cases or improvements needed.
Notes:
- This implementation follows the guidelines provided in the project's README regarding query names and textobject structure.
- Some standard textobjects (like regex) were omitted as they are not applicable to Solidity.
Please run the query formatter from nvim-treesitter (./scripts/format-queries.lua in that repo) on the new file.
Please run the query formatter from nvim-treesitter (
./scripts/format-queries.luain that repo) on the new file.
I did, and also made two more commits but some tests are still failing. Do you mind looking into it please?
Or are you possibly targeting a different parser? We use https://github.com/JoranHonig/tree-sitter-solidity/blob/master/grammar.js (which has no consequence field).
Please check your queries against the grammar. If you install the solidity parser with nvim-treesitter and open the query in a recent Neovim, the query linter should show you which nodes and patterns are invalid.
Mark the PR as ready for review when the query file check passes. (You can ignore the Tests for now; they're flaky.)
I'm sorry, but are you using ChatGPT for this? Your queries bear little relation to the actual grammar.
I'm sorry, but are you using ChatGPT for this? Your queries bear little relation to the actual grammar.
I've tried to ask Claude but unfortunately it does not help much. My initial thought was to grab ecma's code (which is used for javascript queries) and apply some changes to fit Solidity grammar rules. This is because Solidity syntax has lots of common patterns with js. Also, it's my first time dealing with scm so I'm getting my hands dirty with some trial and error
No, that will not work at all. You need to look at the Solidity grammar specification I linked and work directly from that; you can't copy (even partially) queries from other languages.
Trial and error is fine but
- start from scratch and add patterns one by one;
:InspectTreeis your friend; - test locally (you can use the
check-queriesscript locally, and as long as a parser and query is found anywhere on runtimepath, the textobjects should just work for testing).
Please only push your changes once you are confident that they are correct; then we can help finetune.
No, that will not work at all. You need to look at the Solidity grammar specification I linked and work directly from that; you can't copy (even partially) queries from other languages.
Trial and error is fine but
- start from scratch and add patterns one by one;
:InspectTreeis your friend;- test locally (you can use the
check-queriesscript locally, and as long as a parser and query is found anywhere on runtimepath, the textobjects should just work for testing).Please only push your changes once you are confident that they are correct; then we can help finetune.
Thank you for the guideline. Will try to dive deeper into scm patterns and implement the textobjects from scratch as you suggested. Will mark this PR as ready for review once I'm confident with my queries.