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: add Solidity language support #665

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

zeroaddresss
Copy link

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 zeroaddresss changed the title Add Solidity language support feat: add Solidity language support Aug 5, 2024
@clason
Copy link
Collaborator

clason commented Aug 5, 2024

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

@zeroaddresss
Copy link
Author

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?

@clason
Copy link
Collaborator

clason commented Aug 5, 2024

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


; Conditionals
(if_statement
consequence: (_
Copy link
Collaborator

Choose a reason for hiding this comment

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

Where are you getting this node from? It's not in the grammar.

@clason clason marked this pull request as draft August 6, 2024 09:23
@clason
Copy link
Collaborator

clason commented Aug 6, 2024

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
Copy link
Collaborator

clason commented Aug 6, 2024

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

@zeroaddresss
Copy link
Author

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

@clason
Copy link
Collaborator

clason commented Aug 6, 2024

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.

@zeroaddresss
Copy link
Author

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.

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.

2 participants