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

Add support for region comments in Solidity #452

Closed
denyshorman opened this issue Apr 9, 2023 · 4 comments
Closed

Add support for region comments in Solidity #452

denyshorman opened this issue Apr 9, 2023 · 4 comments
Labels
status:needs-decision A decision needs to be made before more progress

Comments

@denyshorman
Copy link

Region comments are useful for organizing code and making it more readable by allowing developers to collapse and expand sections of code. This is especially useful for large files with many functions or blocks of code that are related but not necessarily sequential.

There are several use cases where region comments could be useful:

Contract sections: Solidity contracts can have multiple sections, such as state variables, constructors, functions, and modifiers. Using region comments to group these sections together would make it easier to navigate and understand the structure of a contract.

contract MyContract {
  //#region State variables
  uint public myVar;
  uint private myPrivateVar;
  //#endregion

  //#region Constructors
  constructor(uint _myVar) public {
    myVar = _myVar;
  }
  //#endregion

  //#region Public functions
  function getMyVar() public view returns (uint) {
    return myVar;
  }
  //#endregion

  //#region Modifiers
  modifier onlyOwner() {
    require(msg.sender == owner, "Only owner can call this function.");
    _;
  }
  //#endregion
}

Code blocks: Solidity code can be complex, and it's often helpful to break it down into smaller, more manageable sections. Region comments could be used to group related code blocks together, such as loops, conditionals, or function calls.

function myFunction() public {
  //#region Setup
  uint[] memory myArray = new uint[](10);
  for (uint i = 0; i < 10; i++) {
    myArray[i] = i;
  }
  //#endregion

  //#region Loop
  for (uint i = 0; i < myArray.length; i++) {
    if (myArray[i] % 2 == 0) {
      doSomething(myArray[i]);
    }
  }
  //#endregion
}

Regional comments are supported in multiple languages in VS Code, and I believe it would be beneficial to have support for region comments in Solidity as well. Solidity code can often be complex, with multiple sections and code blocks, and region comments would make it easier for developers to navigate and understand the code. The ability to collapse and expand sections of code would improve code readability and reduce the cognitive load on developers. Additionally, many developers are already familiar with using region comments in other languages, so having support for them in Solidity would make the development experience more consistent and intuitive.

@kanej
Copy link
Member

kanej commented Apr 20, 2023

This is interesting. We are interested in whether other users would find this useful, so leave a comment or a +1.

In terms of implementation, it involves a change to the language configuration, basically pulling this from the js/ts language config: https://github.com/microsoft/vscode/blob/a1eb9e2b48736c3f79697fb6618528d094318a93/extensions/javascript/javascript-language-configuration.json#L112

@kanej kanej added status:needs-decision A decision needs to be made before more progress and removed status:triaging labels Apr 20, 2023
@denyshorman
Copy link
Author

Yes, region comments can be a helpful tool for organizing, structuring, and reading Solidity code, but, unfortunately, they are not widely supported in major dev environments right now like IntelliJ, VS Code, Remix, etc, while other languages have good support for it. Developers who have used this functionality in other languages and are familiar with its benefits would likely want to embrace it in Solidity (example). Once Solidity IDEs add support for region comments, developers can adopt this practice and benefit from its advantages.

@scorpion9979
Copy link
Contributor

scorpion9979 commented Nov 3, 2023

I was looking into this myself. By performing a quick search on GitHub, we could see all Solidity code out there that integrate region comments. Also note that this feature is already supported in juanfranblanco's VSCode extension. I personally think region comments are a must have for any code formatting maxi, so I took the initiative by opening #526 to address this.

@kanej
Copy link
Member

kanej commented Aug 5, 2024

Region comments are now supported in the extension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:needs-decision A decision needs to be made before more progress
Projects
Status: Done
Development

No branches or pull requests

3 participants