nvim-treesitter-textobjects icon indicating copy to clipboard operation
nvim-treesitter-textobjects copied to clipboard

feat: add Solidity language support

Open zeroaddresss opened this issue 1 year ago • 8 comments

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.scm file 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.

zeroaddresss avatar Aug 05 '24 15:08 zeroaddresss

Please run the query formatter from nvim-treesitter (./scripts/format-queries.lua in that repo) on the new file.

clason avatar Aug 05 '24 15:08 clason

Please run the query formatter from nvim-treesitter (./scripts/format-queries.lua in 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?

zeroaddresss avatar Aug 05 '24 21:08 zeroaddresss

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).

clason avatar Aug 05 '24 21:08 clason

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.)

clason avatar Aug 06 '24 09:08 clason

I'm sorry, but are you using ChatGPT for this? Your queries bear little relation to the actual grammar.

clason avatar Aug 06 '24 10:08 clason

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

zeroaddresss avatar Aug 06 '24 13:08 zeroaddresss

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

  1. start from scratch and add patterns one by one; :InspectTree is your friend;
  2. test locally (you can use the check-queries script 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.

clason avatar Aug 06 '24 13:08 clason

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

  1. start from scratch and add patterns one by one; :InspectTree is your friend;
  2. test locally (you can use the check-queries script 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.

zeroaddresss avatar Aug 06 '24 13:08 zeroaddresss