Refine .editorconfig For R Core Style: A How-To Guide
A recurring annoyance for both contributors and R Core developers is that whitespace and other style changes can be introduced as a result of the contributor's IDE settings. An EditorConfig file is intended to address this exact issue. Guys, think of it like a universal style guide for your code editor, ensuring consistency across different IDEs and developers.
@krlmlr shared a draft .editorconfig
file in Bug 18757. This initial draft was a great starting point, but it needed some refinement and testing to ensure it truly met the needs of the R Core style.
I think a useful contribution would be to refine the .editorconfig
and test it with Editorconfig-Checker to identify any conflicts between the configuration and the R sources. This may help to refine the .editorconfig
further and assess the impact of any unresolvable conflicts. By using EditorConfig-Checker, we can automatically identify any discrepancies between the .editorconfig
file and the actual source code, making the refinement process much more efficient and accurate. This ensures that the rules we set are actually being followed and helps us catch any potential issues early on.
Here is an updated version of @krlmlr' .editorconfig
:
# EditorConfig: https://EditorConfig.org
# Don't search for .editorconfig files in parent directories (may conflict)
root = true
# Global settings:
[*]
# Unix-style newlines with a newline ending every file
end_of_line = lf
insert_final_newline = true
# Set default charset
charset = utf-8
# In an ideal world, the setting below would be true, but we want to remove the existing
# spaces at EOL first before turning that on.
trim_trailing_whitespace = false
# DESCRIPTION is special, don't remove trailing spaces after :
[DESCRIPTION]
trim_trailing_whitespace = false
# C files: indentation = 4 spaces; every 8 spaces in indentation replaced with tab
[*.c]
indent_style = tab
indent_size = 4
tab_width = 8
# R files: indentation = 4 spaces (always); any tab should be displayed as 8 spaces
[*.R]
indent_style = space
indent_size = 4
tab_width = 8
# Rd files: indentation = 2 spaces (always)
[*.Rd]
indent_style = space
indent_size = 2
# Makefiles: Tab indentation (no size specified)
[Makefile]
indent_style = tab
This updated .editorconfig
file includes specific settings for various file types commonly found in R projects, such as C files, R files, Rd files, and Makefiles. The settings cover aspects like indentation style, indentation size, tab width, end-of-line characters, and whether to trim trailing whitespace. Each of these settings plays a crucial role in maintaining a consistent coding style across the project. For example, specifying indent_style = space
and indent_size = 4
for R files ensures that all R code uses 4 spaces for indentation, which is a common convention in the R community. Similarly, setting end_of_line = lf
ensures that all files use Unix-style line endings, which can prevent issues when collaborating on different operating systems. By carefully configuring these settings, we can create a .editorconfig
file that helps enforce a consistent and professional coding style throughout the R project. The goal is to make the code more readable, maintainable, and collaborative-friendly.
The indentation rules for R and C files are consistent with @mmaechler's Comment 1 in the bug report. I have tested this with Vim and Emacs. Unfortunately, the C configuration doesn't work in VS Code or Positron, as VS Code cannot be set to replace spaces with tabs (see https://github.com/Microsoft/vscode/issues/42740, https://github.com/Microsoft/vscode/issues/5394). In that case, the best we can do is use VS Code settings directly to get as close as possible to the right configuration - I'll open another issue about that. This is a bit of a bummer, but we can work around it by using VS Code's built-in settings to achieve a similar result. It's important to remember that while .editorconfig
is a powerful tool, it's not a silver bullet, and sometimes we need to use other methods to ensure consistency across all environments.
Extension Task: R Dev Guide Integration
An extension to this task would be to add a section to the R Dev Guide, sharing the .editorconfig
and how to use it Vim and Emacs. It could go in a new chapter "IDE Setup". This is where things get really cool! Imagine having a dedicated section in the R Dev Guide that walks developers through setting up their IDEs to align with the R Core style. This would be a huge win for consistency and collaboration. By including the .editorconfig
file and instructions on how to use it with popular editors like Vim and Emacs, we can make it incredibly easy for developers to contribute high-quality, well-formatted code to the R project. This not only improves the overall look and feel of the codebase but also reduces the amount of time spent on style-related reviews, allowing developers to focus on the more important aspects of their contributions. The "IDE Setup" chapter could cover topics such as installing the EditorConfig plugin, configuring editor-specific settings, and troubleshooting common issues. By providing a comprehensive guide, we can empower developers to create a consistent and professional coding environment, ultimately benefiting the entire R community.
While there is no .editorconfig
in the R sources, it is best to put it in a directory one level out from the source repository. E.g. you might have, e.g., svn/.editorconfig
, then check out to svn/R-devel
or similar. This is a neat trick to avoid accidentally committing the .editorconfig
file to the repository itself. It keeps things clean and organized!
Emacs
I did not need to install anything, just add (editorconfig-mode 1)
to my init.el
. Emacs users, you're in luck! It's super easy to get EditorConfig working in Emacs. Just a simple line in your init.el
file, and you're good to go. This seamless integration makes Emacs a fantastic choice for R development, especially when combined with the power of EditorConfig.
Vim
Install the EditorConfig plugin as a Vim8 plugin: https://github.com/editorconfig/editorconfig-vim#install-as-vim8-plugin (although I have Vim 9.1 on my mac, the EditorConfig plugin wasn't bundled in, you can check via :scriptnames). Vim users, don't worry, you're not left out! Installing the EditorConfig plugin is a breeze, and once it's set up, you'll have all the benefits of consistent coding styles in your favorite editor. Even if you're rocking the latest Vim version, it's worth checking if the plugin is already bundled, but if not, the installation process is straightforward.