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

review: feat: Make sniper printer infer indentation style of compilation unit #3717

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Deque;
import java.util.List;

import org.apache.commons.lang3.tuple.Pair;
import spoon.OutputType;
import spoon.SpoonException;
import spoon.compiler.Environment;
Expand All @@ -35,6 +36,7 @@
import spoon.support.sniper.internal.CollectionSourceFragment;
import spoon.support.sniper.internal.ElementPrinterEvent;
import spoon.support.sniper.internal.ElementSourceFragment;
import spoon.support.sniper.internal.IndentationDetector;
import spoon.support.sniper.internal.ModificationStatus;
import spoon.support.sniper.internal.MutableTokenWriter;
import spoon.support.sniper.internal.PrinterEvent;
Expand Down Expand Up @@ -132,6 +134,12 @@ public void calculate(CtCompilationUnit compilationUnit, List<CtType<?>> types)

//use line separator of origin source file
setLineSeparator(detectLineSeparator(compilationUnit.getOriginalSourceCode()));

// use indentation style of origin source file for new elements
Pair<Integer, Boolean> indentationInfo = IndentationDetector.detectIndentation(compilationUnit);
mutableTokenWriter.setOriginSourceTabulationSize(indentationInfo.getLeft());
mutableTokenWriter.setOriginSourceUsesTabulations(indentationInfo.getRight());
Comment on lines +139 to +141
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The token writer is updated with indentation style for each CU to be printed.


runInContext(new SourceFragmentContextList(mutableTokenWriter,
compilationUnit,
Collections.singletonList(compilationUnit.getOriginalSourceFragment()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* SPDX-License-Identifier: (MIT OR CECILL-C)
*
* Copyright (C) 2006-2019 INRIA and contributors
*
* Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) of the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon.
*/
package spoon.support.sniper.internal;

import org.apache.commons.lang3.tuple.Pair;
import spoon.reflect.declaration.CtCompilationUnit;
import spoon.reflect.path.CtRole;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* Utility class for detecting the indentation style used in a compilation unit.
*/
public class IndentationDetector {

private IndentationDetector() {
}

/**
* Detect the indentation style of the given compilation unit as 1, 2 or 4 spaces or tabs.
*
nharrand marked this conversation as resolved.
Show resolved Hide resolved
* @param cu A compilation unit.
* @return A pair on the form (indentationSize, isTabs)
*/
public static Pair<Integer, Boolean> detectIndentation(CtCompilationUnit cu) {
List<ElementSourceFragment> typeFragments = cu.getOriginalSourceFragment()
.getGroupedChildrenFragments().stream()
.filter(fragment -> fragment instanceof CollectionSourceFragment)
.flatMap(fragment -> extractTypeFragments((CollectionSourceFragment) fragment).stream())
.collect(Collectors.toList());
return detectIndentation(typeFragments);
}

private static Pair<Integer, Boolean> detectIndentation(List<ElementSourceFragment> topLevelTypeFragments) {
List<String> wsPrecedingTypeMembers = new ArrayList<>();

for (ElementSourceFragment typeSource : topLevelTypeFragments) {
assert typeSource.getRoleInParent() == CtRole.DECLARED_TYPE;

List<SourceFragment> children = typeSource.getChildrenFragments();
for (int i = 0; i < children.size() - 1; i++) {
if (children.get(i) instanceof TokenSourceFragment
&& children.get(i + 1) instanceof ElementSourceFragment) {

TokenSourceFragment cur = (TokenSourceFragment) children.get(i);
ElementSourceFragment next = (ElementSourceFragment) children.get(i + 1);
if (cur.getType() == TokenType.SPACE && next.getRoleInParent() == CtRole.TYPE_MEMBER) {
wsPrecedingTypeMembers.add(cur.getSourceCode().replace("\n", ""));
}
}
}
}

return guessIndentationStyle(wsPrecedingTypeMembers);
}

private static Pair<Integer, Boolean> guessIndentationStyle(List<String> wsPrecedingTypeMembers) {
double avgIndent = wsPrecedingTypeMembers.stream()
.map(String::length)
.map(Double::valueOf)
.reduce((acc, next) -> (acc + next) / 2).orElse(1d);

double diff1 = Math.abs(1d - avgIndent);
double diff2 = Math.abs(2d - avgIndent);
double diff4 = Math.abs(4d - avgIndent);

int indentationSize;
if (diff1 > diff2) {
indentationSize = diff2 > diff4 ? 4 : 2;
} else {
indentationSize = 1;
}

boolean usesTabs = wsPrecedingTypeMembers.stream()
.filter(s -> s.contains("\t"))
.count() >= wsPrecedingTypeMembers.size() / 2;
return Pair.of(indentationSize, usesTabs);
}

private static List<ElementSourceFragment> extractTypeFragments(CollectionSourceFragment collection) {
return collection.getItems().stream()
.filter(fragment -> fragment instanceof ElementSourceFragment)
.map(fragment -> (ElementSourceFragment) fragment)
.filter(fragment -> fragment.getRoleInParent() == CtRole.DECLARED_TYPE)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,38 @@ public class MutableTokenWriter implements TokenWriter {
private final TokenWriter delegate;
private boolean muted = false;

// indentation style to use for new elements
private boolean originSourceUsesTabulations;
private int originSourceTabulationSize;

public MutableTokenWriter(Environment env) {
this.delegate = new DefaultTokenWriter(new PrinterHelper(env));
this.delegate = new DefaultTokenWriter(new SniperPrinterHelper(env));
originSourceUsesTabulations = true;
originSourceTabulationSize = 1;
}

private class SniperPrinterHelper extends PrinterHelper {
private final Environment env;

SniperPrinterHelper(Environment env) {
super(env);
this.env = env;
}

/**
* We override this method to use the correct style of indentation for new elements.
*/
@Override
protected void autoWriteTabs() {
int setTabulationSize = env.getTabulationSize();
env.useTabulations(originSourceUsesTabulations);
env.setTabulationSize(originSourceTabulationSize);

super.autoWriteTabs();

env.setTabulationSize(setTabulationSize);
env.useTabulations(true);
}
Comment on lines +35 to +56
Copy link
Collaborator Author

@slarse slarse Dec 3, 2020

Choose a reason for hiding this comment

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

This is the actual thing that overrides the printing of tabs with whatever indentation style is present in the source file. I'm not completely satisfied with this solution as it seems a bit hacky, but I couldn't figure out a better way to do it. Suggestions are welcome.

}

/**
Expand All @@ -40,6 +70,20 @@ public void setMuted(boolean muted) {
this.muted = muted;
}

/**
* @param originSourceUsesTabulations whether or not the origin source uses tabs for indentation.
*/
public void setOriginSourceUsesTabulations(boolean originSourceUsesTabulations) {
this.originSourceUsesTabulations = originSourceUsesTabulations;
}

/**
* @param originSourceTabulationSize the amount of indentation used in the origin source.
*/
public void setOriginSourceTabulationSize(int originSourceTabulationSize) {
this.originSourceTabulationSize = originSourceTabulationSize;
}

@Override
public TokenWriter writeSeparator(String token) {
if (isMuted()) {
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import spoon.SpoonException;
import spoon.refactoring.Refactoring;
import spoon.reflect.CtModel;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtConstructorCall;
import spoon.reflect.code.CtCodeSnippetExpression;
import spoon.reflect.code.CtExpression;
Expand Down Expand Up @@ -53,6 +54,7 @@
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
Expand Down Expand Up @@ -415,6 +417,49 @@ public void testAddedImportStatementPlacedOnSeparateLineInFileWithPackageStateme
testSniper("visibility.YamlRepresenter", addArrayListImport, assertImportsPrintedCorrectly);
}

@Test
public void testAddedElementsIndentedWithAppropriateIndentationStyle() {
// contract: added elements in a source file should be indented with the same style of
// indentation as the rest of the file

Consumer<CtType<?>> addElements = type -> {
Factory fact = type.getFactory();
fact.createField(type, new HashSet<>(), fact.Type().INTEGER_PRIMITIVE, "z", fact.createLiteral(3));
type.getMethod("sum").getBody()
.addStatement(0, fact.createCodeSnippetStatement("System.out.println(z);"));
};
BiConsumer<CtType<?>, String> assertTabs = (type, result) -> {
assertThat(result, containsString("\n\tint z = 3;"));
assertThat(result, containsString("\n\t\tSystem"));
};
BiConsumer<CtType<?>, String> assertTwoSpaces = (type, result) -> {
assertThat(result, containsString("\n int z = 3;"));
assertThat(result, containsString("\n System"));
};
BiConsumer<CtType<?>, String> assertFourSpaces = (type, result) -> {
assertThat(result, containsString("\n int z = 3;"));
assertThat(result, containsString("\n System"));
};

testSniper("indentation.Tabs", addElements, assertTabs);
testSniper("indentation.TwoSpaces", addElements, assertTwoSpaces);
testSniper("indentation.FourSpaces", addElements, assertFourSpaces);
}

@Test
public void testDefaultsToSingleTabIndentationWhenThereAreNoTypeMembers() {
// contract: if there are no type members in a compilation unit, the sniper printer defaults
// to indenting with 1 tab

Consumer<CtType<?>> addField = type -> {
Factory fact = type.getFactory();
fact.createField(type, new HashSet<>(), fact.Type().INTEGER_PRIMITIVE, "z", fact.createLiteral(3));
};
testSniper("indentation.NoTypeMembers", addField, (type, result) -> {
assertThat(result, containsString("\n\tint z = 3;"));
});
}

/**
* 1) Runs spoon using sniper mode,
* 2) runs `typeChanger` to modify the code,
Expand Down
10 changes: 10 additions & 0 deletions src/test/resources/indentation/FourSpaces.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package indentation;

public class FourSpaces {
private int x = 1;
private int y = 2;

public int sum() {
return x + y;
}
}
4 changes: 4 additions & 0 deletions src/test/resources/indentation/NoTypeMembers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package indentation;

public class NoTypeMembers {
}
10 changes: 10 additions & 0 deletions src/test/resources/indentation/Tabs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package indentation;

public class Tabs {
private int x = 1;
private int y = 2;

public int sum() {
return x + y;
}
}
10 changes: 10 additions & 0 deletions src/test/resources/indentation/TwoSpaces.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package indentation;

public class TwoSpaces {
private int x = 1;
private int y = 2;

public int sum() {
return x + y;
}
}