paint-brush
What Is EditorConfig And Why You Should Use Itby@dm8typrogrammer
574 reads
574 reads

What Is EditorConfig And Why You Should Use It

by The Mighty ProgrammerApril 16th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

EditorConfig is an INI¹ format based configuration system that let you establish project level coding standard. It allows configuring: indentation size, indentation. size, line width and more. It helps in reducing the effort required to bring each team member to the consistent coding standards by automatically importing and applying the configuration to. IDE. EditorConfig saves your time by: Whenever there are changes in coding standards,. supported IDEs automatically apply changes in. coding. standards, and then there is manual work; It is one of an invisible pain point; All of your team members might not be using the same.
featured image - What Is EditorConfig And Why You Should Use It
The Mighty Programmer HackerNoon profile picture

Editor Config is an INI¹ format based configuration system that let you establish project level coding standard; It allows configuring: indentation style, indentation size, line width and more. It helps in reducing the effort required to bring each team member to the consistent coding standards by automatically importing and applying the configuration to IDE.

What is EditorConfig solving?

Whenever we work in a team, we set up some ground rules to work efficiently; in the programming world, those ground rules are coding standards.

Coding standards may include:

Level 0 — Universal file configuration: indentation style, line width, line ending, indentation size.

Level 1 — Programming languages based configuration: block style, comment style, method naming, class naming.

Level 2 — Project Level Workflow configuration: the process regarding development to deployment. It is more of the management side.

Traditional Model of Coding Standard

Many IDEs provide a mechanism for code formatting configuration. This configuration can be exported to a file in an IDE specific file.

A team member configures; distributes configuration file within the team to avoid rework by other team members. They import the configuration file into their IDE; thus, the code standard is shared and applied.

The above exercise brings up:

  • Manual Work: Any change in the code standard; all the process of reimporting has to be repeated. It involves manual work and bringing each one IDE on the same page is difficult. Coding standard itself is a long-discussed collective agreement, and then there is manual work; It is one of an invisible pain point.
  • IDE Inflexibility: All of your team members might not be using the same IDE. Consequently, you have to write and maintain multiple configuration files for IDEs being in use, or all your team members have to use the same variant of IDE.

EditorConfig saves your time by:

  • Enabling Auto Import: Whenever there are changes in coding standards, commit the corresponding rules in .editorconfig file and supported IDEs automatically apply changes.
  • IDE flexibility: EditorConfig is available to many IDEs. Some IDE inherently support EditorConfig; some might require external plugins. Check the IDE documentation for details.

In action

You create a file named 

.editorconfig
 in your project root. The file is collections of rules; each rule is simple key-value pair separated by 
=
.

The first rule you write:

root = true

It is a declaration that the current file is root 

.editorconfig
 file.

By design, editor config engine searches for 

.editorconfig
 file in the current working directory and its all parents’ directories till it finds.editorconfig with 
root = true
 and merge the configurations of all found .editorconfig files.

Apply rules to files

You can apply rules to all files or a set of files using glob patterns². The following snippet applies rules to all the files in the project:

# apply rules to all files
[*] # * means all
indent_style = space
indent_size = 2
line_width = 110
charset = utf-8

Overriding Rules

EdiorConfig files are read from top to bottom. Consequently, the latest rule became final applicable rule; this mechanism allows overriding properties.

Consider, you want to override Indention size (

indent_size
) to
4
for yamlfiles and keeping all the rest rules.

[*.yml]
indent_size = 4

YAML files would be constrained by all rules (defined in the previous section under

[*]
 ) and with overridden Indention size.

Rules to Specific File

Rules can also be targeted to a specific file by addressing it with its name enclosed 

[{}]
 .

# Rules only applicable to kconfig.yml
[{kconfig.yml}] 
indent_size = 2

Merging All

By combining all discussed concepts, a 

.editorconfig
 file looks like:

root = true
[*]
indent_style = space
indent_size = 2
line_width = 110
# apply setting to yaml files
[*.yml]
indent_size = 4   #override the size 2 for 4
# Rules only applicable to kconfig.yml
[{kconfig.yml}] 
indent_size = 2
# Level-1 config
[*.java]
curly_bracket_next_line = true

Other Useful Rules

| Rules                   | Description                   |
|-------------------------|-------------------------------|
| end_of_line             | line separator                |
| max_line_length         | line width                    |
| spaces_around_operators | space around binary operators |
| indent_brace_style      | L1- block brace style         |

Check out the official document for all the supported rules:

https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties

IDEs specific rules

IDEs also have been started supporting their specific rules. IntelliJIdea introduces rules prefixed by 

ij
.

ij_visual_guides
ij_formatter_off_tag
ij_formatter_on_tag
ij_formatter_tags_enabled
ij_wrap_on_typing
ij_continuation_indent_size
ij_smart_tabs
ij_java_blank_lines_after_imports
.
.
more

Visual Studio also supports specific rules.

dotnet_style_qualification_for_field
dotnet_style_require_accessibility_modifiers
dotnet_style_collection_initializer
dotnet_style_coalesce_expression
.
.
.
.
If you are using IDE specific rules, you are giving away IDE flexibility benefit 🙇

Takeaways

EditorConfig saves your team time by automatically importing configuration. It helps in reducing the effort to bring each team member to the same definition of coding standards whenever there is a change in code standards.

Reference

1. INI Format

https://en.wikipedia.org/wiki/INI_file

Footnotes

2. GlobPattern: A algebraic way to specify a set of files.

Previously published at https://themightyprogrammer.dev/article/editor-config