code style: Add EditorConfig file #21123

pull kiminuo wants to merge 1 commits into bitcoin:master from kiminuo:feature/editorconfig changing 1 files +26 −0
  1. kiminuo commented at 8:07 am on February 9, 2021: contributor

    Motivation

    Developers are supposed to follow Coding style. However, from time to time a PR is created and then its author is asked to change tabs to spaces, for example.

    Introducing an .editorconfig file can mitigate these formatting issues.

    User story

    A contributor wants to create a new PR. She clones Bitcoin Core repo, opens her editor, the editor loads .editorconfig rules and writes her patch with correct formatting rules. Less Coding Style issues is then discovered in the PR review process and thus less CI runs are needed.

    What is EditorConfig file?

    https://editorconfig.org provides very well and concise explanation:

    What is EditorConfig?

    EditorConfig helps maintain consistent coding styles for multiple developers working on the same project across various editors and IDEs. The EditorConfig project consists of a file format for defining coding styles and a collection of text editor plugins that enable editors to read the file format and adhere to defined styles. EditorConfig files are easily readable and they work nicely with version control systems.

    Support

    .editorconfig is supported by many IDEs and text editors. Sometimes, the support is out of the box and sometimes a plugin is needed. However, for example, VS Code detects .editorconfig presence and automatically offers you to install the missing plugin.

    See https://editorconfig.org/#pre-installed for details on support. To name a few:

    • Visual Studio (out of the box)
    • VS Code (plugin)
    • JetBrains IDEs (IntelliJ IDEA, PyCharm, etc.) (out of the box)
    • Sublime Text (plugin)
    • Emacs (plugin)
    • Vim (plugin)

    Not supported (AFAIK):

    My editor does not support .editorconfig

    Then nothing really changes for you.

    .editorconfig vs .clang-format

    As explained here:

    Note that Visual Studio also supports EditorConfig, which works in a similar way. ClangFormat, however, has a much larger variety of style options than EditorConfig, including some very C++ specific rules that can be set, and it is already used by C++ developers today.

    Having both .editorconfig and .clang-format in a project, may not always work correctly though, I think. As some editors may have a plugin for .editorconfig and a plugin for clang-formatter which may not work correctly in unison. In VS Code & Visual Studio EditorConfig takes precedence over other settings.

    Possible issues

    Your editor may change formatting for some 3rd party library if you edit the code. A solution for this would be to make EditorConfig rules more specific (include only certain paths). I’m not sure if it is an issue in practice.

    Testing

    Add some trailing whitespace to a Python file and save the file. You should see that the trailing whitespace is removed.

    Possible future work

    It would be great to define rules for Makefiles. This would be good start:

    0# Makefiles
    1[Makefile,*.am]
    2indent_style = tab
    3trim_trailing_whitespace = true
    

    I don’t know makefiles in this project good enough to propose something reasonable. If this PR is well received, it would be great to add it in this PR.

    Also, there are actually many different file extensions and so the proposed .editorconfig file can be probably improved very much:

    0Get-ChildItem -Recurse | % {$_.Extension.ToLower()} | sort | unique
    
      0.1
      1.ac
      2.adoc
      3.am
      4.bash-completion
      5.bat
      6.bmp
      7.c
      8.cc
      9.cert
     10.cfg
     11.clang_complete
     12.clang-format
     13.cmake
     14.cmd
     15.cnf
     16.com
     17.conf
     18.cpp
     19.css
     20.csv
     21.doxyfile
     22.dtd
     23.empty
     24.exe
     25.exp
     26.gci
     27.gitattributes
     28.github
     29.gitignore
     30.gitmodules
     31.guess
     32.h
     33.hex
     34.hpp
     35.html
     36.icns
     37.ico
     38.idb
     39.ilk
     40.in
     41.include
     42.ini
     43.init
     44.ipp
     45.jam
     46.js
     47.json
     48.lastbuildstate
     49.lib
     50.list
     51.log
     52.m
     53.m4
     54.md
     55.mk
     56.mm
     57.moc
     58.obj
     59.openrc
     60.openrcconf
     61.patch
     62.pc
     63.pdb
     64.pl
     65.plist
     66.png
     67.po
     68.pro
     69.py
     70.python-version
     71.qbk
     72.qm
     73.qml
     74.qrc
     75.raw
     76.rb
     77.rc
     78.recipe
     79.res
     80.s
     81.sage
     82.sass
     83.scm
     84.scss
     85.service
     86.sgml
     87.sh
     88.sln
     89.spec
     90.sub
     91.supp
     92.svg
     93.targets
     94.td
     95.tlog
     96.ts
     97.tx
     98.txt
     99.ui
    100.user
    101.v2
    102.vcxproj
    103.verbatim
    104.vscode
    105.xml
    106.xpm
    107.xsl
    108.y
    109.yapf
    110.yml
    111.yy
    

    Fixes #21092

  2. kiminuo force-pushed on Feb 9, 2021
  3. in .editorconfig:10 in 470e36ebc1 outdated
     9+
    10+# Shell scripts
    11+[*.sh]
    12+indent_style = space
    13+indent_size = 4
    14+trim_trailing_whitespace = true
    


    MarcoFalke commented at 8:12 am on February 9, 2021:
    Couldn’t this be moved to [*] for all files?

    kiminuo commented at 8:15 am on February 9, 2021:
    It can. It was done with the purpose to make the first version very easy to review.

    kiminuo commented at 8:24 am on February 9, 2021:
    Fixed
  4. in .editorconfig:18 in 470e36ebc1 outdated
    13+indent_size = 4
    14+trim_trailing_whitespace = true
    15+
    16+# Python files
    17+[*.py]
    18+indent_style = space
    


    MarcoFalke commented at 8:12 am on February 9, 2021:
    same

    kiminuo commented at 8:24 am on February 9, 2021:
    Fixed
  5. in .editorconfig:9 in 470e36ebc1 outdated
    0@@ -0,0 +1,36 @@
    1+# This is the top-most EditorConfig file.
    2+root = true
    3+
    4+# For all files.
    5+[*]
    6+charset = utf-8
    7+end_of_line = lf
    8+insert_final_newline = false
    


    MarcoFalke commented at 8:13 am on February 9, 2021:
    Pretty sure this is true for our project?

    kiminuo commented at 8:23 am on February 9, 2021:
    Fixed
  6. kiminuo force-pushed on Feb 9, 2021
  7. kiminuo force-pushed on Feb 9, 2021
  8. MarcoFalke commented at 8:26 am on February 9, 2021: member
    shouldn’t harm, didn’t test, cursory review ACK a36f64de0f0509ab79e2c2f7e4a37f48f05b0333
  9. vincenzopalazzo commented at 11:13 am on February 9, 2021: none

    shouldn’t harm, didn’t test

    I tested, it looks good to me and to my emacs conf.

    ack https://github.com/bitcoin/bitcoin/commit/a36f64de0f0509ab79e2c2f7e4a37f48f05b0333

  10. laanwj commented at 1:05 pm on February 9, 2021: member

    Tested ACK with editorconfig-vim.

    It would be great to define rules for Makefiles. This would be good start:

    I think makefiles would be one of the things where this is most useful. My editor already defaults to bitcoin’s cpp indentation settings so for me personally this makes very little difference :slightly_smiling_face:

    It’s makefiles where I always have to manually switch to tabs instead of spaces to avoid the dreaded ‘missing separator’ error.

    I would suggest to add:

    0# Makefiles
    1[*.am]
    2indent_style = tab
    3trim_trailing_whitespace = true
    4
    5[Makefile.*.include]
    6indent_style = tab
    7trim_trailing_whitespace = true
    

    There is no reason to ever edit the generated Makefile. Also, the ‘,’ syntax to specify multiple globs doesn’t seem to work in the vim plugin.

  11. kiminuo commented at 1:49 pm on February 9, 2021: contributor

    @laanwj

    This works on my machine:

    0# Makefiles
    1[{*.am,Makefile.*.include}]
    2indent_style = tab
    

    (trim_trailing_whitespace = true is already specified in [*] section.)

    Could you please test? If it does not work, I would go with two sections.

  12. laanwj commented at 2:04 pm on February 9, 2021: member

    Could you please test? If it does not work, I would go with two sections.

    Can confirm that does work !

    For configure.ac I think we want a 2-spaces indent style. It’s very inconsistent though, in some places we use 4, there’s even some uses of tabs, but 2 seems most common.

  13. kiminuo force-pushed on Feb 9, 2021
  14. in .editorconfig:33 in e4fe31d5ca outdated
    28+# Autoconf scripts
    29+[configure.ac]
    30+indent_size = 2
    31+
    32+# C++ files
    33+[*.{h,cpp}]
    


    lontivero commented at 11:29 pm on February 9, 2021:

    The ident_size is common to all the source code files defined in this editor config file. It could be combined:

    0# Source code files
    1[*.{h,cpp,py,sh}]
    

    kiminuo commented at 7:00 am on February 10, 2021:
    Yes, thanks
  15. kiminuo force-pushed on Feb 10, 2021
  16. Add EditorConfig file. 7a135d57b2
  17. kiminuo force-pushed on Feb 10, 2021
  18. laanwj commented at 8:29 am on February 10, 2021: member

    Tested re-ACK 7a135d57b2ac17477b25d5046a3bec57eac3ab30

    Travis error has nothing to do with the change here.

  19. MarcoFalke commented at 12:03 pm on February 10, 2021: member
    Approach ACK 7a135d57b2ac17477b25d5046a3bec57eac3ab30
  20. lontivero commented at 12:18 pm on February 10, 2021: contributor
    Ack 7a135d57b2ac17477b25d5046a3bec57eac3ab30
  21. laanwj merged this on Feb 10, 2021
  22. laanwj closed this on Feb 10, 2021

  23. kiminuo deleted the branch on Feb 10, 2021
  24. sidhujag referenced this in commit 0cfee49446 on Feb 11, 2021
  25. PastaPastaPasta referenced this in commit 3cba474d6a on Jun 27, 2021
  26. PastaPastaPasta referenced this in commit 64efa9ee97 on Jun 28, 2021
  27. PastaPastaPasta referenced this in commit 4fcfb96261 on Jun 29, 2021
  28. PastaPastaPasta referenced this in commit b70e88b420 on Jul 1, 2021
  29. PastaPastaPasta referenced this in commit 54d505044d on Jul 1, 2021
  30. PastaPastaPasta referenced this in commit 33b761c4ec on Jul 15, 2021
  31. PastaPastaPasta referenced this in commit a9c314feba on Jul 16, 2021
  32. DrahtBot locked this on Aug 18, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2025-01-15 12:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me