Hardhat for Visual Studio Code is a VS Code extension that adds language support for Solidity and provides editor integration for Hardhat projects. Integrations for other tools are coming in the near future.
Hardhat for Visual Studio code adds the following features:
- Code completion
- Go to definition, type definition and references
- Symbol renames
- Solidity code formatting
- Inline code validation from compiler errors/warnings for Hardhat projects
- Hover help for variables, function calls, errors, events etc.
- Code actions (quickfixes) suggested from compiler errors/warnings for Hardhat projects
Hardhat for Visual Studio Code 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.
This extension should work without any configuration. To learn more about how it works with Hardhat projects, please read this section. If formatting functionality isn't working, or you have previously configured another Solidity formatter, please see the formatting section.
Hardhat for Visual Studio Code 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
../) are completed based on suggestions from
Relative imports pull their suggestions from the file system based on the current solidity file's location.
Move through your codebase with semantic navigation commands:
Navigates to the definition of an identifier.
Navigates to the type of an identifier.
Shows all references of the identifier under the cursor.
Rename the identifier under the cursor and all of its references:
Apply solidity formatting to the current document.
The formatting configuration can be overriden through a
.prettierrc file, see Formatting Configuration.
Hovering the cursor over variables, function calls, errors and events will display a popup showing type and signature information:
As code is edited, Hardhat for Visual Studio Code runs the solc compiler over the changes and displays any warnings or errors it finds.
This feature is only available in solidity files that are part of a Hardhat project, as Hardhat is used for import resolution, see Hardhat Projects for details.
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.
A contract that implements an interface, but is missing functions specified in the interface, will get a
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.
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
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.
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
The Add virtual specifier to function definition and Add override specifier to function definition code actions appear against functions with these errors.
A function without an accessibility keyword will cause the
Two code actions will appear against a function with this error: Add public visibility to declaration and Add private visibility to declaration.