Skip to content

Add EditorConfig file - #180

Merged
jeremylong merged 1 commit into
package-url:masterfrom
dwalluck:editorconfig
Mar 17, 2025
Merged

Add EditorConfig file#180
jeremylong merged 1 commit into
package-url:masterfrom
dwalluck:editorconfig

Conversation

@dwalluck

Copy link
Copy Markdown
Contributor

No description provided.

@jeremylong

Copy link
Copy Markdown
Collaborator

While I agree we should add something like this. I think we are considering palantir-java-formatter (see #183).

@dwalluck

Copy link
Copy Markdown
ContributorAuthor

Editorconfig is on a lower level. I am not sure that any formatter plugin with necessarily handle everything so you may want to use both as they do different things. Thoughts @ppkarwasz?

Editorconfig can be used to check some of the formatting also. I am a contributor to both https://github.com/ec4j/editorconfig-maven-plugin. But, it is more meant to set the IDE settings for the developers and is not language-specific as it will make sure that the correct indentation level is set for various languages like JSON, Shell, and YAML, too, not just Java.

I don't have a strong opinion on which formatter plugin to choose. I have contributed to https://github.com/revelc/formatter-maven-plugin and I think that https://github.com/diffplug/spotless is one of the more popular ones.

@dwalluck

Copy link
Copy Markdown
ContributorAuthor

The goods news is that

  • No additional maven plugins are required to use editorconfig
  • Your IDE or editor may already support it with no additional configuration required
  • It is applied before editing. A formatter plugin may try to fix up the code after the fact, but cannot help you set the required newlines, trim whitespace, or add end of file markers before building

@dwalluck

Copy link
Copy Markdown
ContributorAuthor

I don't have a strong opinion on which formatter plugin to choose. I have contributed to https://github.com/revelc/formatter-maven-plugin and I think that https://github.com/diffplug/spotless is one of the more popular ones.

OK, palantir-java-format uses spotless.

@ppkarwaszppkarwasz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a .gitattributes file, so Windows users don't end up checking out CRLFs?

I usually use something like this:

# All text files with LF line endings
* text=auto eol=lf
# Maven Wrapper cmd script
/mvnw.cmd eol=crlf
# Maven Wrapper need LF line endings
/.mvn/wrapper/maven-wrapper.properties eol=lf

I don't know how Git detects which files are text, but this repo should not contain binary blobs, so it should be fine.

Comment thread.editorconfig Outdated
Comment on lines +13 to +15
[{*.json,*.sh,*.yml}]
indent_size = 2
tab_width = 2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add *.xml and *.yaml too? I find that XML looks better with 2 spaces of indentation. This is also what sortpom uses.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I can add .yaml, but so far, the files all use .yml. I am not sure if the 3-letter or 4-letter extension is preferred over the other.

About .xml, pom.xml was currently set to 4 spaces. I like the sortpom plugin, too, and it looks like 2 is the canonical number anyway.

It's easier, I guess, to just change the default to 2, and set 4 for .java, then.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, 6bf3a95 just defaults to 2.

@ppkarwasz

Copy link
Copy Markdown
Contributor

Editorconfig is on a lower level. I am not sure that any formatter plugin with necessarily handle everything so you may want to use both as they do different things. Thoughts @ppkarwasz?

In some repo I had to disable .editorconfig, because it conflicted with some other Spotless formatting rule, but I don't remember what exactly was the problem. In general I am OK with .editorconfig as long as it does not contain IDE-specific properties.

@dwalluck

Copy link
Copy Markdown
ContributorAuthor

In some repo I had to disable .editorconfig, because it conflicted with some other Spotless formatting rule, but I don't remember what exactly was the problem. In general I am OK with .editorconfig as long as it does not contain IDE-specific properties.

Does spotless support all of the same features? If so, it could fix any issues without an additional plugin.

@dwalluck

Copy link
Copy Markdown
ContributorAuthor

don't know how Git detects which files are text, but this repo should not contain binary blobs, so it should be fine.

I thought that this has more to do with the core.autocrlf setting.

I think that ideally all files should be treated as binary and it shouldn't be converting anything and files should be kept as-is. Windows files should stay crlf, java files should stay lf, etc.

@dwalluck

Copy link
Copy Markdown
ContributorAuthor

I think that ideally all files should be treated as binary and it shouldn't be converting anything and files should be kept as-is. Windows files should stay crlf, java files should stay lf, etc.

OK, see #190.

@ppkarwasz

Copy link
Copy Markdown
Contributor

In some repo I had to disable .editorconfig, because it conflicted with some other Spotless formatting rule, but I don't remember what exactly was the problem. In general I am OK with .editorconfig as long as it does not contain IDE-specific properties.

Does spotless support all of the same features? If so, it could fix any issues without an additional plugin.

I am not sure if all are supported, but there are some generic formatting steps and we can use the Prettier formatter for the rest.

@ppkarwasz

Copy link
Copy Markdown
Contributor

I think that ideally all files should be treated as binary and it shouldn't be converting anything and files should be kept as-is. Windows files should stay crlf, java files should stay lf, etc.

When Windows users create a new Java file, they might use CRLF. If we add a .gitattributes that specifies that all text files must use LF, those CRLFs will be converted as soon as Git touches the file.

@dwalluck

Copy link
Copy Markdown
ContributorAuthor

When Windows users create a new Java file, they might use CRLF. If we add a .gitattributes that specifies that all text files must use LF, those CRLFs will be converted as soon as Git touches the file.

I prefer to treat all text as binary (that's what I did in #190). This means git won't mess with text files. Otherwise, you have to specify every Windows file in the .gitattributes?

The formatter plugin could be used to check the line ending on a pull request, so does git even need to get involved? No one should be committing directly, except maybe a upon a new release.

@jeremylong
jeremylong merged commit 8925c06 into package-url:masterMar 17, 2025
@dwalluck
dwalluck deleted the editorconfig branch April 15, 2025 21:39
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@dwalluck@jeremylong@ppkarwasz