Skip to content

Commit

Permalink
Update IndentStep to allow leading space on multi-line comments (#1072
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nedtwigg committed Jan 7, 2022
2 parents 5d37686 + d5c3d65 commit 542546c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -10,6 +10,8 @@ This document is intended for Spotless developers.
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Fixed
* Update IndentStep to allow leading space on multiline comments ([#1072](https://github.com/diffplug/spotless/pull/1072)).

## [2.21.1] - 2022-01-06
### Changed
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -100,6 +100,9 @@ String format(String raw) {
++contentStart;
}

// detect potential multi-line comments
boolean mightBeMultiLineComment = (contentStart < raw.length()) && (raw.charAt(contentStart) == '*');

// add the leading space in a canonical way
if (numSpaces > 0) {
switch (state.type) {
Expand All @@ -112,6 +115,9 @@ String format(String raw) {
for (int i = 0; i < numSpaces / state.numSpacesPerTab; ++i) {
builder.append('\t');
}
if (mightBeMultiLineComment && (numSpaces % state.numSpacesPerTab == 1)) {
builder.append(' ');
}
break;
default:
throw new IllegalArgumentException("Unexpected enum " + state.type);
Expand Down
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).

## [Unreleased]
### Fixed
* Update IndentStep to allow leading space on multiline comments ([#1072](https://github.com/diffplug/spotless/pull/1072)).

## [6.1.1] - 2022-01-06
### Fixed
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Fixed
* Update IndentStep to allow leading space on multiline comments ([#1072](https://github.com/diffplug/spotless/pull/1072)).

## [2.19.0] - 2022-01-06
### Added
Expand Down
13 changes: 11 additions & 2 deletions testlib/src/main/resources/indent/IndentedWithSpace.test
Expand Up @@ -2,8 +2,13 @@ package com.github.youribonnaffe.gradle.format;

import java.util.function.Function;


/**
* Test class.
*/
public class Java8Test {
/**
* Test method.
*/
public void doStuff() throws Exception {
Function<String, Integer> example = Integer::parseInt;
example.andThen(val -> {
Expand All @@ -21,7 +26,11 @@ public class Java8Test {
throw new Exception();
}
}


/** Test enum
* with weirdly formatted javadoc
* which IndentStep should not change
*/
public enum SimpleEnum {
A, B, C;
}
Expand Down
13 changes: 11 additions & 2 deletions testlib/src/main/resources/indent/IndentedWithTab.test
Expand Up @@ -2,8 +2,13 @@ package com.github.youribonnaffe.gradle.format;

import java.util.function.Function;


/**
* Test class.
*/
public class Java8Test {
/**
* Test method.
*/
public void doStuff() throws Exception {
Function<String, Integer> example = Integer::parseInt;
example.andThen(val -> {
Expand All @@ -21,7 +26,11 @@ public class Java8Test {
throw new Exception();
}
}


/** Test enum
* with weirdly formatted javadoc
* which IndentStep should not change
*/
public enum SimpleEnum {
A, B, C;
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.ResourceHarness;
import com.diffplug.spotless.SerializableEqualityTester;
import com.diffplug.spotless.StepHarness;

class IndentStepTest extends ResourceHarness {
@Test
Expand Down Expand Up @@ -82,4 +83,13 @@ protected FormatterStep create() {
}
}.testEquals();
}

@Test
void allowLeadingSpaceIfMultiLineComment() throws Exception {
StepHarness.forStep(IndentStep.Type.TAB.create(4))
.testUnaffected("* test")
.testUnaffected(" * test")
.testUnaffected("\t* test")
.test(" * test", "\t* test");
}
}

0 comments on commit 542546c

Please sign in to comment.