Skip to content

Commit

Permalink
Fixed module-info formatting in eclipse-jdt.
Browse files Browse the repository at this point in the history
  • Loading branch information
fvgh committed Oct 4, 2021
1 parent 0c34b39 commit b2c5cd9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
5 changes: 5 additions & 0 deletions _ext/eclipse-jdt/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `4.8.0`).

## [Unreleased]
### Changed
* Bumped minimum supported Eclipse JDT core version to 3.27.0
* `format` interface requires source file information to distinguish module-info from compilation unit. Old interface marked as deprecated.
### Fixed
* Fixed module-info formatting. Previous versions did not recognized content and skipped formatting.

## [4.8.0] - 2018-07-19
* Initial release!
2 changes: 1 addition & 1 deletion _ext/eclipse-jdt/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ description=Eclipse's JDT formatter bundled for Spotless
VER_JAVA=11

# Compile
VER_ECLIPSE_JDT_CORE=[3.12.0,4.0.0[
VER_ECLIPSE_JDT_CORE=[3.13.0,4.0.0[
VER_SPOTLESS_ECLISPE_BASE=[3.5.0,4.0.0[
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2021 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 @@ -15,11 +15,13 @@
*/
package com.diffplug.spotless.extra.eclipse.java;

import java.io.File;
import java.util.Properties;

import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jdt.internal.compiler.env.IModule;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.text.edits.TextEdit;
Expand Down Expand Up @@ -53,8 +55,17 @@ public void activatePlugins(SpotlessEclipsePluginConfig config) {
}
}

/** @deprecated Use {@link #format(String, File)} instead. */
public String format(String raw) throws Exception {
TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, raw, 0, raw.length(), 0, SpotlessEclipseFramework.LINE_DELIMITER);
return format(raw, new File(""));
}

/** Formatting Java string, distinguishing module-info and compilation unit by file name */
public String format(String raw, File file) throws Exception {
int kind = (file.getName().equals(IModule.MODULE_INFO_JAVA) ? CodeFormatter.K_MODULE_INFO
: CodeFormatter.K_COMPILATION_UNIT) | CodeFormatter.F_INCLUDE_COMMENTS;

TextEdit edit = codeFormatter.format(kind, raw, 0, raw.length(), 0, SpotlessEclipseFramework.LINE_DELIMITER);
if (edit == null) {
throw new IllegalArgumentException("Invalid java syntax for formatting.");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,24 @@ void htmlPreTag() throws Throwable {
assertEquals(formatted, format(unformatted), "Failed to create internal code formatter. See Spotless issue #191");
}

@Test
void moduleInfo() throws Throwable {
config.clear();
config.setProperty(
DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_MODULE_STATEMENTS,
DefaultCodeFormatterConstants.createAlignmentValue(true, DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE, DefaultCodeFormatterConstants.INDENT_BY_ONE));
String formatted = getTestResource("java/eclipse/ModuleInfoFormatted.test");
String unformatted = getTestResource("java/eclipse/ModuleInfoUnformatted.test");
assertEquals(formatted, format(unformatted, "whatever/module-info.java"), "Jvm9 module info not formatted.");
}

private String format(final String input) throws Exception {
return format(input, "");
}

private String format(final String input, final String fileName) throws Exception {
EclipseJdtFormatterStepImpl formatter = new EclipseJdtFormatterStepImpl(config);
return formatter.format(input);
return formatter.format(input, new File(fileName));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module foo.bar {
provides test.Service
with
foo.bar.Service1,
foo.bar.Service2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module foo.bar
{
provides test.Service
with foo.bar.Service1, foo.bar.Service2;
}

0 comments on commit b2c5cd9

Please # to comment.