Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -15,6 +15,8 @@
*/
package com.diffplug.spotless.extra.glue.jdt;

import static org.eclipse.jdt.core.util.CompilationUnitSorter.RELATIVE_ORDER;

import java.util.Comparator;
import java.util.List;
import java.util.StringTokenizer;
Expand Down Expand Up @@ -347,33 +349,18 @@ public int compare(BodyDeclaration bodyDeclaration1, BodyDeclaration bodyDeclara
}

private static int sortPreservedCategory(int category) {
switch (category) {
case STATIC_FIELDS_INDEX:
case STATIC_INIT_INDEX:
return STATIC_FIELDS_INDEX;
case FIELDS_INDEX:
case INIT_INDEX:
return FIELDS_INDEX;
default:
return category;
}
return switch (category) {
case STATIC_FIELDS_INDEX, STATIC_INIT_INDEX -> STATIC_FIELDS_INDEX;
case FIELDS_INDEX, INIT_INDEX -> FIELDS_INDEX;
default -> category;
};
}

private boolean isSortPreserved(BodyDeclaration bodyDeclaration) {
switch (bodyDeclaration.getNodeType()) {
case ASTNode.FIELD_DECLARATION:
case ASTNode.ENUM_CONSTANT_DECLARATION:
case ASTNode.INITIALIZER:
return true;
default:
return false;
}
}
private boolean isSortPreserved(BodyDeclaration bodyDeclaration) {return switch(bodyDeclaration.getNodeType()){case ASTNode.FIELD_DECLARATION,ASTNode.ENUM_CONSTANT_DECLARATION,ASTNode.INITIALIZER->true;default->false;};}

private int preserveRelativeOrder(BodyDeclaration bodyDeclaration1, BodyDeclaration bodyDeclaration2) {
int value1 = (Integer) bodyDeclaration1.getProperty(CompilationUnitSorter.RELATIVE_ORDER);
int value2 = (Integer) bodyDeclaration2.getProperty(CompilationUnitSorter.RELATIVE_ORDER);
return value1 - value2;
return (Integer) bodyDeclaration1.getProperty(RELATIVE_ORDER) -
(Integer) bodyDeclaration2.getProperty(RELATIVE_ORDER);
}

private int compareNames(BodyDeclaration bodyDeclaration1, BodyDeclaration bodyDeclaration2, String name1, String name2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package com.diffplug.spotless.extra;

import static com.diffplug.spotless.LineEnding.PLATFORM_NATIVE;
import static com.diffplug.spotless.LineEnding.UNIX;
import static com.diffplug.spotless.LineEnding.WINDOWS;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -91,10 +95,9 @@ public LazyAllTheSame(File projectDir, Supplier<Iterable<File>> toFormat) {
protected String calculateState() throws Exception {
var files = toFormat.get().iterator();
if (files.hasNext()) {
Runtime runtime = new RuntimeInit(projectDir).atRuntime();
return runtime.getEndingFor(files.next());
return new RuntimeInit(projectDir).atRuntime().getEndingFor(files.next());
} else {
return LineEnding.UNIX.str();
return UNIX.str();
}
}

Expand Down Expand Up @@ -300,15 +303,14 @@ public String getEndingFor(File file) {
}

private static String convertEolToLineEnding(String eol, File file) {
switch (eol.toLowerCase(Locale.ROOT)) {
case "lf":
return LineEnding.UNIX.str();
case "crlf":
return LineEnding.WINDOWS.str();
default:
LOGGER.warn(".gitattributes file has unspecified eol value: {} for {}, defaulting to platform native", eol, file);
return LineEnding.PLATFORM_NATIVE.str();
}
return switch (eol.toLowerCase(Locale.ROOT)) {
case "lf" -> UNIX.str();
case "crlf" -> WINDOWS.str();
default -> {
LOGGER.warn(".gitattributes file has unspecified eol value: {} for {}, defaulting to platform native", eol, file);
yield PLATFORM_NATIVE.str();
}
};
}

private LineEnding findDefaultLineEnding(Config config) {
Expand All @@ -318,12 +320,12 @@ private LineEnding findDefaultLineEnding(Config config) {
// autocrlf=true converts CRLF->LF during commit
// and converts LF->CRLF during checkout
// so CRLF is the default line ending
return LineEnding.WINDOWS;
return WINDOWS;
} else if (autoCRLF == AutoCRLF.INPUT) {
// autocrlf=input converts CRLF->LF during commit
// and does no conversion during checkout
// mostly used on Unix, so LF is the default encoding
return LineEnding.UNIX;
return UNIX;
} else if (autoCRLF == AutoCRLF.FALSE) {
// handle core.eol
EOL eol = config.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_EOL, EOL.NATIVE);
Expand All @@ -335,14 +337,11 @@ private LineEnding findDefaultLineEnding(Config config) {

/** Creates a LineEnding from an EOL. */
private static LineEnding fromEol(EOL eol) {
// @formatter:off
switch (eol) {
case CRLF: return LineEnding.WINDOWS;
case LF: return LineEnding.UNIX;
case NATIVE: return LineEnding.PLATFORM_NATIVE;
default: throw new IllegalArgumentException("Unknown eol " + eol);
}
// @formatter:on
return switch (eol) {
case CRLF -> WINDOWS;
case LF -> UNIX;
case NATIVE -> PLATFORM_NATIVE;
};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ private FormattingOptions createFormattingOptions() throws Exception {
case META -> Formatter.META_FORMAT;
case GOOGLE -> Formatter.GOOGLE_FORMAT;
case KOTLIN_LANG -> Formatter.KOTLINLANG_FORMAT;
default -> throw new IllegalStateException("Unknown formatting option " + style);
};

if (ktfmtFormattingOptions != null) {
Expand Down
Loading
Loading