Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate formatter-maven-plugin for CI builds #822

Closed
ansell opened this issue May 4, 2017 · 15 comments
Closed

Investigate formatter-maven-plugin for CI builds #822

ansell opened this issue May 4, 2017 · 15 comments
Assignees

Comments

@ansell
Copy link
Contributor

ansell commented May 4, 2017

Investigate adding formatter-maven-plugin to CI builds to validate formatting and fail CI builds (after compile/tests complete) if it has not been applied:

http://code.revelc.net/formatter-maven-plugin/formatter-maven-plugin/plugin-info.html

This will help with issues where the user has accidentally forgot to apply the formatting before submitting their pull request.

@ansell
Copy link
Contributor Author

ansell commented May 4, 2017

In particular, what are peoples thoughts on exporting the formatter configuration as an artifact to Maven Central and putting it on a different build number (similar to some parent pom systems where they only use 1/2/3/4/etc.) so it can be consistently used and tracked separate to the rest of RDF4J?

That is the recommended method for using the formatter-maven-plugin with multi module projects:

http://code.revelc.net/formatter-maven-plugin/formatter-maven-plugin/examples.html#Multimodule_Configuration

@barthanssens
Copy link
Contributor

Related to #790

@abrokenjester
Copy link
Contributor

Fwiw I have previoulsy briefly looked at using the maven checkstyle plugin for this purpose, but got mixed results. It's easy enough to set up but I had a hard time getting it to play nice with the Eclipse built-in code formatter. This plugin looks interesting though, and more immediately suited to our purpose. Link to github repo: https://github.com/revelc/formatter-maven-plugin

@abrokenjester
Copy link
Contributor

I had a bit of a play with this formatter and I really like it. Easy to integrate in our build process, and also plays nice with Eclipse's internal formatter.

@hmottestad
Copy link
Contributor

@jeenbroekstra I tested the formatter on fluent APIs like .stream() and it's a bit aggressive:

return m
    .stream()
    .map(st -> st.getObject())
    .filter(o -> o instanceof Literal)
    .map(l -> (Literal) l)
    .findAny();

Gets converted to a single line, because it's shorter than 120 characters!

I strongly prefer the multiline version of fluent apis, unless there is a particular need for using a one-liner (eg. in an IF condition).

@abrokenjester
Copy link
Contributor

The problem with that is that that's not a distinction the formatter can make. I can configure it to forcible always turn qualified invocations like this into multiline statements (even if < 120 chars) but I can't make an exception for "when it is part of an IF-condition". The upshot is that if I change this, we also get this:

if (book.stringValue()
				.equals("http://example.org/book/book6")) {
			assertEquals("Harry Potter and the Half-Blood Prince", b.getValue("title")
					.stringValue());
		} else if (book.stringValue()
				.equals("http://example.org/book/book7")) {
			assertEquals("Harry Potter and the Deathly Hallows", b.getValue("title")
					.stringValue());

instead of:

		if (book.stringValue().equals("http://example.org/book/book6")) {
			assertEquals("Harry Potter and the Half-Blood Prince", b.getValue("title").stringValue());
		} else if (book.stringValue().equals("http://example.org/book/book7")) {
			assertEquals("Harry Potter and the Deathly Hallows", b.getValue("title").stringValue());

@abrokenjester
Copy link
Contributor

I believe your particular example is an edge case: it's a fluent call that is just under 120 chars. If you feel that having it as a oneliner is less readable, you of course always have the option to use slightly longer variable names. Not only will that improve general readability, but it will also push it past the line length limit, so it will automatically get reformatted.

@hmottestad
Copy link
Contributor

Is there not a way to make it one-way. >120 always become multi line but <120 are ignored?

@hmottestad
Copy link
Contributor

So that if I make it multiline for readability, then it stays multiline.

@abrokenjester
Copy link
Contributor

abrokenjester commented Mar 12, 2019

I'll do one tweak though: the current settings would reformat as follows:

	return model.stream()
			.map(st -> st.getObject())
			.filter(o -> o instanceof Literal)
			.map(l -> (Literal) l)
			.findAny();

I'll change it so it becomes:

	return model
                            .stream()
                            .map(st -> st.getObject())
                            .filter(o -> o instanceof Literal)
                            .map(l -> (Literal) l)
                            .findAny();

(ignore the slightly too large indentation, that's a copy/paste issue)

@hmottestad
Copy link
Contributor

Shame it can’t handle that edge case. Using longer variable names is quite the hack to bypass the auto formatted.

@abrokenjester
Copy link
Contributor

Is there not a way to make it one-way. >120 always become multi line but <120 are ignored?

Afraid not. Or, well, there is, but enabling that would mean that the formatter never joins up two lines anymore. Which rather defeats the purpose.

This kind of thing is always a compromise.

@abrokenjester
Copy link
Contributor

Shame it can’t handle that edge case. Using longer variable names is quite the hack to bypass the auto formatted.

I don't really see it as a hack. If you are aiming for increased readability of your code, longer variable names are exactly a means to that end. And for what it's worth, I actually think that

return m.stream().map(st -> st.getObject()).filter(o -> o instanceof Literal).map(l -> (Literal) l).findAny();

still reads just fine.

@abrokenjester
Copy link
Contributor

Ah, I'm afraid I can't make the change I promised above either - it results in some rather weird side effects, for example:

                           Arrays.asList(new ListBindingSet(bindingNames,
                                           SimpleValueFactory.getInstance().createLiteral("1", XMLSchema.STRING))));

becomes:

                           Arrays
                                           .asList(new ListBindingSet(bindingNames,
                                                           SimpleValueFactory.getInstance().createLiteral("1", XMLSchema.STRING))));

@hmottestad
Copy link
Contributor

That’s fine.

abrokenjester pushed a commit that referenced this issue Mar 15, 2019
formatter no longer joins lines back up if deliberately split
abrokenjester pushed a commit that referenced this issue Aug 22, 2019
abrokenjester pushed a commit that referenced this issue Aug 22, 2019
abrokenjester pushed a commit that referenced this issue Aug 22, 2019
* develop:
  temporary switch back to old inferencer to avoid test failure
  fix compilation failure due to merge issues
  one-time pass of formatter with line-rejoining enabled
  one-time pass with line-rejoining enabled
  Update Jackson dependency
  formatter no longer joins lines back up if deliberately split
  Revert "GH-1240 formatted with eclipse conventions"
  Revert "fixed overlooked file formatting"
  fixed overlooked file formatting
  GH-1240 GH-822 updated readme and contributing guidelines
  GH-1240 formatted with eclipse conventions
  GH-822 configured formatter plugin with new code conventions

Signed-off-by: Håvard Ottestad <hmottestad@gmail.com>

# Conflicts:
#	inferencer/src/main/java/org/eclipse/rdf4j/sail/inferencer/fc/SchemaCachingRDFSInferencer.java
#	inferencer/src/main/java/org/eclipse/rdf4j/sail/inferencer/fc/SchemaCachingRDFSInferencerConnection.java
#	sail-base/src/main/java/org/eclipse/rdf4j/sail/base/SailSourceBranch.java
#	sail-spin/src/main/java/org/eclipse/rdf4j/sail/spin/SpinSail.java
#	sail-spin/src/main/java/org/eclipse/rdf4j/sail/spin/SpinSailConnection.java
#	sail-spin/src/test/java/org/eclipse/rdf4j/sail/spin/benchmarks/BasicBenchmarks.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants