Giter VIP home page Giter VIP logo

prettier-plugin-solidity's Introduction

prettier-plugin-solidity

Telegram Twitter Follow

A Prettier plugin for automatically formatting your Solidity code.

If you like this project, please consider contributing to our Gitcoin grant!

Installation and usage

Install both prettier and prettier-plugin-solidity:

npm install --save-dev prettier prettier-plugin-solidity

Run prettier in your contracts:

npx prettier --write 'contracts/**/*.sol'

You can add a script for running prettier on all your contracts:

"prettier": "prettier --write 'contracts/**/*.sol'"

Or you can use it as part of your linting to check that all your code is prettified:

"lint": "prettier --list-different 'contracts/**/*.sol'"

Who's using it?

These are some of the projects using Prettier Solidity:

Integrations

Vim

To integrate this plugin with vim, first install vim-prettier. These instructions assume you are using vim-plug. Add this to your configuration:

Plug 'prettier/vim-prettier', {
  \ 'do': 'yarn install && yarn add prettier-plugin-solidity',
  \ 'branch': 'release/1.x',
  \ 'for': [
    \ 'javascript',
    \ 'typescript',
    \ 'css',
    \ 'less',
    \ 'scss',
    \ 'json',
    \ 'graphql',
    \ 'markdown',
    \ 'vue',
    \ 'lua',
    \ 'php',
    \ 'python',
    \ 'ruby',
    \ 'html',
    \ 'swift',
    \ 'solidity'] }

We modified the do instruction to also install this plugin. Then you'll have to configure the plugin to always use the version installed in the vim plugin's directory:

let g:prettier#exec_cmd_path = '~/.vim/bundle/vim-prettier/node_modules/.bin/prettier'

To check that everything is working, open a solidity file and run :Prettier.

If you also want to autoformat every time you write the buffer, add these lines:

let g:prettier#autoformat = 0
autocmd BufWritePre *.sol Prettier

Now Prettier will be run every time the file is saved.

VSCode

VSCode is not familiar with the solidity language, so solidity support needs to be installed.

code --install-extension JuanBlanco.solidity

Having done that you should proceed to install prettier-vscode.

code --install-extension esbenp.prettier-vscode

To interact with 3rd party plugins, prettier-vscode will look in the project's npm modules, so you'll need to have prettier and prettier-plugin-solidity in your package.json

npm install --save-dev prettier prettier-plugin-solidity

As a final check, make sure that VSCode is configured to format files on save.

You'll notice now that prettier is formatting every time the files are saved but the indentation is using 2 spaces instead of 4. This has been reported and in the meantime you can use the following configuration in your .prettierrc file:

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

Note: When you install the npm package prettier in your project and create a .prettierrc file (which wasn't in your project before this), your VSCode's default settings or rules in settings.json are ignored (prettier/prettier-vscode#1079).

If you want a different configuration for your javascript and solidity files, you can add an overrides property to your .prettierrc.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. All existing test and coverage must pass (npm run test:all), if coverage drops below 100% add missing tests.
  5. Push to the branch (git push origin feature/fooBar)
  6. Create a new Pull Request

License

Distributed under the MIT license. See LICENSE for more information.

prettier-plugin-solidity's People

Contributors

benignmop avatar dependabot-preview[bot] avatar dependabot[bot] avatar dependencies-bot avatar dependencies[bot] avatar fvictorio avatar jablko avatar janther avatar mattiaerre avatar maxsam4 avatar minggas avatar ritikm avatar svechinsky avatar yhuard avatar zemse avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.