Skip to content

Commit

Permalink
Formatting escape hatch (#81806)
Browse files Browse the repository at this point in the history
Thanks to https://bugs.eclipse.org/bugs/show_bug.cgi?id=574437,
we've run into a situation where Spotless is incorrectly formatting
a particular piece of syntax (due the underlying Eclipse bug). We
were able to turn off formatting of this syntax using `// @Formatter:off`
and `// @Formatter:on`, but there was a further problem. We configure
IntelliJ to use the Eclipse formatter plugin, but this doesn't
respect the `@formatter` tags since these are set at the Spotless
level, not the Eclipse formatter level. Note that these tags aren't
set in the Eclipse formatter config, because there we use `// tag::`
and `// end::` in order to avoid reformatting docs snippets, which
have a much narrower line width.

What a mess.

So, to get around all this, drop the `@formatter` tags and tweak
our custom `SnippetLengthCheck` Checkstyle rule so that
`// tag:noformat` regions are not subject to the narrower line length
check, but are still exempt from formatting.
  • Loading branch information
pugnascotia committed Dec 16, 2021
1 parent d5222bd commit 358cd80
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 29 deletions.
4 changes: 0 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space

ij_formatter_off_tag = @formatter:off
ij_formatter_on_tag = @formatter:on
ij_formatter_tags_enabled = false

[*.gradle]
ij_continuation_indent_size = 2
indent_size = 2
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ Please follow these formatting guidelines:
* Wildcard imports (`import foo.bar.baz.*`) are forbidden and will cause
the build to fail.
* If *absolutely* necessary, you can disable formatting for regions of code
with the `// @formatter:off` and `// @formatter:on` directives, but
with the `// tag::noformat` and `// end::noformat` directives, but
only do this where the benefit clearly outweighs the decrease in formatting
consistency.
* Note that Javadoc and block comments i.e. `/* ... */` are not formatted,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
/**
* Checks the snippets included in the docs aren't too wide to fit on
* the page.
* <p>
* Regions contained in the special <code>noformat</code> tag are exempt from the length
* check. This region is also exempt from automatic formatting.
*/
public class SnippetLengthCheck extends AbstractFileSetCheck {
private static final Pattern START = Pattern.compile("^( *)//\\s*tag::(.+?)\\s*$", Pattern.MULTILINE);
private static final Pattern START = Pattern.compile("^( *)//\\s*tag::(?!noformat)(.+?)\\s*$", Pattern.MULTILINE);
private int max;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ public void apply(Project project) {

java.target("src/**/*.java");

// Use `@formatter:off` and `@formatter:on` to toggle formatting - ONLY IF STRICTLY NECESSARY
java.toggleOffOn("@formatter:off", "@formatter:on");

java.removeUnusedImports();

// We enforce a standard order for imports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void setWeight(final double weight) {
this.weight = weight;
}

// @formatter:off
// tag::noformat
/**
* If the {@link GraphExploreRequest#useSignificance(boolean)} is true (the default)
* this statistic is available.
Expand All @@ -163,12 +163,12 @@ public void setWeight(final double weight) {
* href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html"
* >the significant_terms aggregation</a>)
*/
// @formatter:on
// end::noformat
public long getBg() {
return bg;
}

// @formatter:off
// tag::noformat
/**
* If the {@link GraphExploreRequest#useSignificance(boolean)} is true (the default)
* this statistic is available.
Expand All @@ -178,7 +178,7 @@ public long getBg() {
* href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html"
* >the significant_terms aggregation</a>)
*/
// @formatter:on
// end::noformat
public long getFg() {
return fg;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public HttpEntity getEntity() {
* Length of RFC 1123 format (with quotes and leading space), used in
* matchWarningHeaderPatternByPrefix(String).
*/
// @formatter:off
// tag::noformat
private static final int WARNING_HEADER_DATE_LENGTH = 0
+ 1
+ 1
Expand All @@ -131,7 +131,7 @@ public HttpEntity getEntity() {
+ 2 + 1 + 2 + 1 + 2 + 1
+ 3
+ 1;
// @formatter:on
// end::noformat

/**
* Tests if a string matches the RFC 7234 specification for warning headers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ public void testFieldIsntWrittenOutTwice() throws Exception {
// you need to add an additional index with no fields in order to trigger this (or potentially a shard)
// so that there is an UnmappedTerms in the list to reduce.
createIndex("foo_1");
// @formatter:off
// tag::noformat
XContentBuilder builder = jsonBuilder().startObject()
.startObject("properties")
.startObject("@timestamp")
Expand All @@ -463,17 +463,17 @@ public void testFieldIsntWrittenOutTwice() throws Exception {
.endObject()
.endObject()
.endObject();
// @formatter:on
// end:noformat
assertAcked(client().admin().indices().prepareCreate("foo_2").addMapping("doc", builder).get());
// @formatter:off
// tag:noformat
XContentBuilder docBuilder = jsonBuilder().startObject()
.startObject("license")
.field("partnumber", "foobar")
.field("count", 2)
.endObject()
.field("@timestamp", "2018-07-08T08:07:00.599Z")
.endObject();
// @formatter:on
// end:noformat
client().prepareIndex("foo_2", "doc").setSource(docBuilder).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();

client().admin().indices().prepareRefresh();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract class AbstractHyperLogLog extends AbstractCardinalityAlgorithm {
private static final int BIAS_K = 6;

// these static tables come from the appendix of the paper
// @formatter:off
// tag::noformat
private static final double[][] RAW_ESTIMATE_DATA = {
// precision 4
{ 11, 11.717, 12.207, 12.7896, 13.2882, 13.8204, 14.3772, 14.9342, 15.5202, 16.161, 16.7722, 17.4636, 18.0396, 18.6766, 19.3566,
Expand Down Expand Up @@ -689,7 +689,7 @@ public abstract class AbstractHyperLogLog extends AbstractCardinalityAlgorithm {
-404.317000000039, -528.898999999976, -506.621000000043, -513.205000000075, -479.351000000024, -596.139999999898,
-527.016999999993, -664.681000000099, -680.306000000099, -704.050000000047, -850.486000000034, -757.43200000003,
-713.308999999892, } };
// @formatter:on
// end::noformat

private static final long[] THRESHOLDS = new long[] {
10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void setWeight(final double weight) {
this.weight = weight;
}

// @formatter:off
// tag::noformat
/**
* If the {@link GraphExploreRequest#useSignificance(boolean)} is true (the default)
* this statistic is available.
Expand All @@ -177,12 +177,12 @@ public void setWeight(final double weight) {
* href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html"
* >the significant_terms aggregation</a>)
*/
// @formatter:on
// end::noformat
public long getBg() {
return bg;
}

// @formatter:off
// tag::noformat
/**
* If the {@link GraphExploreRequest#useSignificance(boolean)} is true (the default)
* this statistic is available.
Expand All @@ -192,7 +192,7 @@ public long getBg() {
* href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html"
* >the significant_terms aggregation</a>)
*/
// @formatter:on
// end::noformat
public long getFg() {
return fg;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

public final class DataTypes {

// @formatter:off
// tag::noformat
public static final DataType UNSUPPORTED = new DataType("UNSUPPORTED", null, 0, false, false, false);

public static final DataType NULL = new DataType("null", 0, false, false, false);
Expand Down Expand Up @@ -48,7 +48,7 @@ public final class DataTypes {
// complex types
public static final DataType OBJECT = new DataType("object", 0, false, false, false);
public static final DataType NESTED = new DataType("nested", 0, false, false, false);
//@formatter:on
//end::noformat

private static final Collection<DataType> TYPES = Arrays.asList(
UNSUPPORTED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

public class SqlDataTypes {

// @formatter:off
// tag::noformat
// date-only, time-only
public static final DataType DATE = new DataType("DATE", null, Long.BYTES, false, false, true);
public static final DataType TIME = new DataType("TIME", null, Long.BYTES, false, false, true);
Expand Down Expand Up @@ -88,7 +88,7 @@ public class SqlDataTypes {
public static final DataType GEO_SHAPE = new DataType("geo_shape", Integer.MAX_VALUE, false, false, false);
public static final DataType GEO_POINT = new DataType("geo_point", Double.BYTES * 2, false, false, false);
public static final DataType SHAPE = new DataType("shape", Integer.MAX_VALUE, false, false, false);
// @formatter:on
// end::noformat

private static final Map<String, DataType> ODBC_TO_ES = new HashMap<>(mapSize(38));

Expand Down

0 comments on commit 358cd80

Please sign in to comment.