Skip to content

Add EditorConfig for more consistent coding styles #3278

Closed

Description

@seisman

EditorConfig homepage: https://editorconfig.org/

EditorConfig helps maintain consistent coding styles for multiple developers working on the same project across various editors and IDEs.

We already have ruff to format our Python codes, but we also have many non-Python files. For these non-Python files, it would be great if we could also remove trailing whitespace (xref #935), and end lines with \n.

This can be done by adding the .editorconfig file and some editors (https://editorconfig.org/#pre-installed) work without any other installations while other editors (https://editorconfig.org/#download) will work after installing a plugin.

Activity

  1. added
    discussions
    on May 29, 2024
  2. seisman commented on Jun 2, 2024

    @seisman
    MemberAuthor

    Ping @GenericMappingTools/pygmt-maintainers for comments.

  3. weiji14 commented on Jun 2, 2024

    @weiji14
    Member

    For maintainers, I think most of us are aware of the style rules already, so it doesn't seem necesary. For casual contributors who might just edit files using the GitHub GUI (which is super prone to whitespace issues), I don't think an editorconfig would help?

  4. seisman commented on Jun 2, 2024

    @seisman
    MemberAuthor

    For maintainers, I think most of us are aware of the style rules already, so it doesn't seem necesary.

    Yes, but we sometimes still make mistakes. For example, currently, the repository contains two files (./examples/projections/README.txt and ./doc/changes.md) that have trailing whitespaces:

    $ find . -type f ! -name '*.png' ! -name '*.jpg' ! -path '*/.dvc/*' ! -path '*/.git/*' -exec grep -l '[[:blank:]]$' {} +
    

    For casual contributors who might just edit files using the GitHub GUI (which is super prone to whitespace issues), I don't think an editorconfig would help?

    GitHub has native support of EditorConfig since 2015 (editorconfig/editorconfig.github.com#48) but as I understand it, it can't remove trailing whitespaces. So contributors may still accidentally commit trailing whitespaces to the repository, but if .editorconfig exists, the trailing whitespace can be automatically removed when any maintainers or contributors edit the file using a local text editor (depends on if a plugin is need/installed or not).

  5. yvonnefroehlich commented on Jun 3, 2024

    @yvonnefroehlich
    Member

    GitHub has native support of EditorConfig since 2015 (editorconfig/editorconfig.github.com#48) but as I understand it, it can't remove trailing whitespaces. So contributors may still accidentally commit trailing whitespaces to the repository, but if .editorconfig exists, the trailing whitespace can be automatically removed when any maintainers or contributors edit the file using a local text editor (depends on if a plugin is need/installed or not).

    Hm. I am somehow unsure here. Maybe we can make a POC PR for this? And see if it works as expected and helps us / we need it?
    It looks like this tool can also check the spelling (see speeling_language at the end of the table at https://spec.editorconfig.org/#supported-pairs).

    Yes, but we sometimes still make mistakes. For example, currently, the repository contains two files (./examples/projections/README.txt and ./doc/changes.md) that have trailing whitespaces:

    So, these two files should be corrected, or?

  6. seisman commented on Jun 3, 2024

    @seisman
    MemberAuthor

    GitHub has native support of EditorConfig since 2015 (editorconfig/editorconfig.github.com#48) but as I understand it, it can't remove trailing whitespaces. So contributors may still accidentally commit trailing whitespaces to the repository, but if .editorconfig exists, the trailing whitespace can be automatically removed when any maintainers or contributors edit the file using a local text editor (depends on if a plugin is need/installed or not).

    Hm. I am somehow unsure here. Maybe we can make a POC PR for this? And see if it works as expected and helps us / we need it?

    The https://github.com/editorconfig/editorconfig.github.com repository contains the .editorconfig file. I've forked it and tried to add some trailing whitespaces. PR https://github.com/seisman/editorconfig.github.com/pull/1 shows that the trailing whitespaces are added, so it means GitHub doesn't support trim_trailing_whitespace.

    image

    Yes, but we sometimes still make mistakes. For example, currently, the repository contains two files (./examples/projections/README.txt and ./doc/changes.md) that have trailing whitespaces:

    So, these two files should be corrected, or?

    Yes, but let's keep these two files for a while so that we can test if .editorconfig or other solutions works.

  7. seisman commented on Jun 4, 2024

    @seisman
    MemberAuthor

    I just tried to add a .editorconfig file locally. Here is the content:

    root = true
    
    [*]
    end_of_line = lf
    charset = utf-8
    trim_trailing_whitespace = true
    insert_final_newline = true
    

    Then I added a few lines to the examples/projections/README.txt file and expected to see that the extra trailing whitespace at line 5 would be trimmed automatically when saving, but nothing happened.

    I'm using vim 9.1 but as per the documentation (https://github.com/editorconfig/editorconfig-vim#bundled-versions), vim has native support for editorconfig for Vim 9.0.1799 and above.

  8. weiji14 commented on Jun 4, 2024

    @weiji14
    Member

    If we're talking about enforcing no trailing whitespaces, maybe we should revisit #1593 and add a .pre-commit-config.yaml instead? We can set autofix_prs=false to disable the autofixing of pre-commit.ci by default to address your concerns about the automatic fixes not being friendly for git-newbies.

  9. seisman commented on Jun 4, 2024

    @seisman
    MemberAuthor

    Yes, I was re-reading that issue too. I think what we care about most are trailing whitespace and leading tabs.

    I guess we have two different solutions:

    1. Enable the pre-commit CI service
    2. Extend the "Style Checks" workflow and "/format" slash command

    For solution 2, to find files with trailing whitespace:

    $ git grep -I -n -e ' \+$'
    doc/changes.md:10:* 🚀 Almost all module wrappers (with a few exceptions) now use in-memory GMT *virtual files* instead of intermediate temporary files to improve performance ([#2730](https://github.com/GenericMappingTools/pygmt/issues/2730))
    doc/changes.md:12:* Adopt [SPEC 0](https://scientific-python.org/specs/spec-0000/) policy for minimum supported versions of GMT, Python, and other core dependencies
    examples/projections/README.txt:5:one you want to use in all plotting methods. The projection is specified by a one-letter
    

    To find files with leading tabs:

    $ git grep -I -n -P '^\t' -- ':!**/Makefile' ':!Makefile'
    doc/_static/style.css:52:       margin-bottom: 80px;
    
  10. seisman commented on Jun 4, 2024

    @seisman
    MemberAuthor

    I guess we have two different solutions:

    1. Enable the pre-commit CI service
    2. Extend the "Style Checks" workflow and "/format" slash command

    After thinking twice, I think pre-commmit CI service is better.

  11. weiji14 commented on Jun 4, 2024

    @weiji14
    Member

    Ok, I'll close this issue and re-open #1593 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussions

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions