Re: [PATCH v3] Add .editorconfig file for basic formatting

From: Miguel Ojeda
Date: Fri May 26 2023 - 17:16:43 EST


On Mon, May 8, 2023 at 10:59 AM Íñigo Huguet <ihuguet@xxxxxxxxxx> wrote:
>
> Originally I sampled manually, but I have crafted a script to collect
> more data. It's not 100% reliable, but good to get an idea. It reads
> the leading whitespaces and if >80% of the lines have one kind of
> indentation, it considers that it's the one used in that file. The
> results, filtered to show only the relevant ones, are pasted at the
> end.

This is useful -- thanks a lot for working on collecting it!

> These are some personal conclusions from the script's results:
> - .py: although the official and most widely used style in the
> community is 4-space indentation, in Linux tree many files use tabs.
> What should we do here? 4-space is the clear standard for python...

Yeah, this is the kind of thing that worries me and why I asked --
what do editors do when they have the config saying it is 4-spaces,
but the file is tabs? Do they adjust, do they convert the entire file,
or do they simply start mixing indentation styles? Does the
`.editorconfig` spec say anything about it? For instance, here is an
issue about this sort of problem:

https://github.com/editorconfig/editorconfig-vscode/issues/329

If the rule could be applied only to new files, then it would be
fairly easy to decide, given the majority uses 4-spaces and it nicely
aligns with PEP 8, Black, etc. But unless we are quite sure we are not
annoying developers, I would avoid specifying anything in these cases.

In some cases (e.g. few files), you may be able to propose to
normalize the indentation style treewide for that extension.

> - .rb: only one file in the whole tree
> - .pm: only 3 files in the whole tree

I guess you could also ignore extensions without many matches in order
to simplify -- they can always be added later, ideally by their
maintainers.

> - Files with many different indentations, better not to specify them:
> rst, cocci, tc, xsl, manual pages
> - Files that we should specify, tab indented: awk, dts, dtsi, dtso, s, S
> - Files that we might specify, with preference for tab indenting but
> not 100% clear: sh, bash, pl
> - Files in tools/perf/scripts/*/bin/*: there is no clear formatting
> for any file type, only for .py files that are tab-indented. To get
> these results I've run my script from tools/perf/scripts directory.

If all Python tab-indented files are in a given folder, then would it
be possible to provide an `.editorconfig` for that folder, and then
4-spaces for the global one? i.e. splitting the problem across folders
may be a solution (within reason, of course, i.e. as long as we don't
fill the kernel with `.editorconfig` files... :)

> I'm only aware of Clang and Rust formatter configs in Linux tree, and
> I think this complies with them. Do you know about any other?

There is `scripts/checkpatch.pl`, which I guess may be counted as one
since one can fix what it complains about manually (and I think it has
some "fix in place" support too).

There is also `Documentation/devicetree/bindings/.yamllint`.

In addition, some may be using formatters in a default config? e.g.
Black for some of the Python scripts.

Cheers,
Miguel