Skip to content

Latest commit

 

History

History
314 lines (176 loc) · 15.8 KB

README.md

File metadata and controls

314 lines (176 loc) · 15.8 KB

Solidity by Nomic Foundation

This extension adds language support for Solidity to Visual Studio Code, and provides editor integration for Hardhat projects, and experimental support for Foundry, Truffle and Ape projects. It supports:

Built by the Nomic Foundation. We’re hiring.


Table of Contents


Installation

Solidity by Nomic Foundation can be installed by using the Visual Studio Code Marketplace.

Some features (e.g. inline validation, quick fixes) are still experimental and are only enabled within a Hardhat project, this is a limitation that will be lifted with future releases.

Features

Code Completions

The solidity language server autocompletes references to existing symbols (e.g. contract instances, globally available variables and built-in types like arrays) and import directives (i.e. it autocompletes the path to the imported file).

Direct imports (those not starting with ./ or ../) are completed based on suggestions from ./node_modules.

Relative imports pull their suggestions from the file system based on the current solidity file's location.

Import completions

Natspec documentation completion is also supported

Natspec contract completions

Natspec function completions


Navigation

Move through your codebase with semantic navigation commands:

Go to Definition

Navigates to the definition of an identifier.

Go to Type Definition

Navigates to the type of an identifier.

Go to References

Shows all references of the identifier under the cursor.

Navigation


Renames

Rename the identifier under the cursor and all of its references:

Rename


Format document

Apply Solidity formatting to the current document, for all the configuration options see Formatting Configuration.

Reformat


Hover

Hovering the cursor over variables, function calls, errors and events will display a popup showing type and signature information:

Hover


Inline code validation (Diagnostics)

As code is edited, the solc compiler is run over the changes and any warnings or errors are displayed.

Diagnostic


Code Actions

Code actions, or quickfixes are refactorings suggested to resolve a solc warning or error.

A line with a warning/error that has a code action, will appear with small light bulb against it; clicking the light bulb will provide the option to trigger the code action.

Implement missing functions on interface

A contract that implements an interface, but is missing functions specified in the interface, will get a solidity(3656) error.

The matching code action Add missing functions from interface will determine which functions need to be implemented to satisfy the interface and add them as stubs to the body of the contract.

Implement interface

Constrain mutability

A function without a mutability keyword but which does not update contract state will show a solidity(2018) warning, with solc suggesting adding either the view or pure keyword depending on whether the function reads from state.

The matching code action Add view/pure modifier to function declaration resolves the warning by adding the keyword to the function signature.

Constrain Mutability

Adding virtual/override on inherited function signature

A function in an inheriting contract, that has the same name and parameters as a function in the base contract, causes solidity(4334) in the base contract function if it does not have the virtual keyword and solidity(9456) in the inheriting contract function if does not have the override keyword.

The Add virtual specifier to function definition and Add override specifier to function definition code actions appear against functions with these errors.

Virtual and Override

Adding public/private to function signature

A function without an accessibility keyword will cause the solidity(4937) error.

Two code actions will appear against a function with this error: Add public visibility to declaration and Add private visibility to declaration.

Public Private

Adding license identifier and pragma solidity version

When no license is specified on a contract, the solidity(1878) warning is raised by the compiler. Similarly, when no compiler version is specified with a pragma solidity statement, the compiler shows the solidity(3420) warning. There are code actions available for quick fixes.

Add license specifier

Add pragma solidity

Specifying data location for variables

Some types require you to specify a data location (memory, storage, calldata), depending on where they are defined. The available code actions allow the user to add, change or remove data locations depending on the error being raised.

Data location quickfix

Fix addresses checksum

The solidity compiler requires explicit addresses to be in the correct checksummed format. This quickfix transforms any address to the expected format.

Address checksum

Hardhat console auto-import

Hardhat's console.sol can be imported with this quickfix. Please note that this is only available on hardhat projects.

Console import


Project support

Some features are only fully enabled, if the Solidity file being worked on is part of a supported project (e.g. inline code validation).

We provide support for Hardhat projects and experimental support for Foundry, Truffle and Ape projects.

Project support is needed as Solidity delegates to the project framework to determine Solidity import resolution. The current project and its configuration needs to be understood to replicate this logic and provide language features based upon it.

If the Solidity file is not part of a project or the project cannot be determined, a best effort is made to resolve imports based only on relative imports.

Hardhat

Hardhat projects are detected by looking for a hardhat.config.{js,ts} file.

Inline validation (the display of compiler errors and warnings against the code) is based on your Hardhat configuration file. The version of the solc solidity compiler used for validation is set within this file, see the Hardhat documentation for more details.

Foundry (experimental)

Foundry projects are detected by looking for a foundry.toml file.

The version of the solc solidity compiler used for validation is set within the foundry.toml, see the Foundry documentation for more details.

Remappings are supported either from a remappings.txt file or as part of the foundry.toml.

Truffle (experimental)

Truffle projects are detected by the presence of a truffle.js or truffle-config.js file.

Solidity import resolution for Truffle supports relative paths, direct imports from the node_modules folder and Truffle direct imports (e.g. truffle/Assert.sol).

Ape (experimental)

Ape projects are detected by the presence of an ape-config.yaml file.

Remappings are supported when set within the ape-config.yaml file.

Monorepo Support

Monorepos are supported and can be opened as workspace folders. On opening a monorepo, it will be scanned to find all supported projects (i.e. Hardhat, Foundry, Truffle and Ape).

The project type and project config file that are being used when validating a Solidity file are shown in the Solidity section of the Status Bar:

Open Config

Commands

Compile project

When working on a Hardhat project, the command Hardhat: Compile project is available on the command palette. This will trigger a hardhat compile run.

Compile command

Clean artifacts

When working on a hardhat project, the command Hardhat: Clear cache and artifacts is present on the command palette. This will trigger a hardhat clean run.

Clean command

Flatten contract

When working on a solidity file inside a hardhat project, the command Hardhat: Flatten this file and its dependencies is present on the command palette and the context menu. This will trigger a hardhat flatten $FILE run, and will output the result in a new file tab.

Flatten command

Task provider

The extension is registered as a task provider for Hardhat projects, in which the build task is provided, running hardhat compile, and the test task, which runs hardhat test.

Formatting

Solidity by Nomic Foundation provides formatting support for .sol files, by leveraging prettier-plugin-solidity.

Note: if you currently have other solidity extensions installed, or have had previously, they may be set as your default formatter for solidity files.

To set Solidity by Nomic Foundation as your default formatter for solidity files:

  1. Within a Solidity file run the Format Document With command, either through the command palette, or by right clicking and selecting through the context menu:

Format Document With

  1. Select Configure Default Formatter...

Format Document With

  1. Select Solidity as the default formatter for solidity files

Format Document With

Formatting Configuration

Formatting can be configured to be provided by either prettier (the default) or forge.

Prettier

The default formatting rules that will be applied are taken from prettier-plugin-solidity, with the exception that explicitTypes are preserved (rather than forced).

To override the settings, add a prettierrc configuration file at the root of your project. Add a *.sol file override to the prettier configuration file and change from the defaults shown:

// .prettierrc.json
{
  "overrides": [
    {
      "files": "*.sol",
      "options": {
        "printWidth": 80,
        "tabWidth": 4,
        "useTabs": false,
        "singleQuote": false,
        "bracketSpacing": false,
        "explicitTypes": "preserve"
      }
    }
  ]
}

Forge

If forge is selected as the formatter under the configuration, then the forge fmt command is run on the open editor file to provide formatting. The forge fmt command determines the configuration based on the project's foundry.toml file.

The configuration options for forge fmt are available here.

Alternative editors

We currently distribute a vim.coc extension and a standalone language server that you can integrate with your editor of choice to have full Solidity language support.

Feedback, help and news

Hardhat Support Discord server: for questions and feedback.

Follow Hardhat on Twitter.