hardhat-vscode icon indicating copy to clipboard operation
hardhat-vscode copied to clipboard

Add support for region comments in Solidity

Open denyshorman opened this issue 1 year ago • 3 comments

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.

denyshorman avatar Apr 09 '23 16:04 denyshorman