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

Update IndentStep to allow leading space on multi-line comments #1072

Merged
merged 5 commits into from Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 multi-line 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 multi-line 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 multi-line 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");
}
}