Skip to content

Commit 011abe1

Browse files
authored
Merge pull request #193 from funfried/release/1.14.x
2 parents ddd9fdb + db8ae28 commit 011abe1

File tree

7 files changed

+42
-25
lines changed

7 files changed

+42
-25
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
<dependency>
148148
<groupId>io.spring.javaformat</groupId>
149149
<artifactId>spring-javaformat-formatter</artifactId>
150-
<version>0.0.29</version>
150+
<version>0.0.30</version>
151151
</dependency>
152152

153153
<!-- Google Formatter -->
@@ -701,7 +701,7 @@
701701
<artifactId>maven-surefire-plugin</artifactId>
702702
<configuration>
703703
<argLine>
704-
${argLine} -Xmx1024m
704+
-Xmx1024m
705705
--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
706706
--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
707707
--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED

src/main/java/de/funfried/netbeans/plugins/external/formatter/AbstractFormatJob.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import javax.swing.text.StyledDocument;
2121

2222
import org.apache.commons.collections4.CollectionUtils;
23-
import org.apache.commons.lang3.StringUtils;
2423
import org.apache.commons.lang3.mutable.MutableInt;
2524
import org.apache.commons.lang3.tuple.Pair;
2625
import org.netbeans.api.annotations.common.NonNull;
@@ -154,7 +153,15 @@ protected boolean setFormattedCode(String code, String formattedContent) throws
154153
* @return The content of the {@code document}
155154
*/
156155
protected String getCode() {
157-
return StringUtils.trim(DocumentUtilities.getText(document).toString());
156+
try {
157+
// returns the actual content of the document
158+
return document.getText(0, document.getLength());
159+
} catch (BadLocationException ex) {
160+
log.log(Level.WARNING, "Could not fetch text, falling back to utility method", ex);
161+
162+
// returns only the trimmed content of the document
163+
return DocumentUtilities.getText(document).toString();
164+
}
158165
}
159166

160167
/**

src/main/java/de/funfried/netbeans/plugins/external/formatter/java/eclipse/EclipseJavaFormatterService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public Integer getContinuationIndentSize(Document document) {
9595

9696
Integer indentSize = getIndentSize(document);
9797
if (indentSize != null) {
98-
ret = Integer.valueOf(value) * indentSize;
98+
ret *= indentSize;
9999
}
100100
}
101101
}

src/main/java/de/funfried/netbeans/plugins/external/formatter/java/google/GoogleJavaFormatterWrapper.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ public final class GoogleJavaFormatterWrapper {
4040
* Formats the given {@code code} with the given configurations and returns
4141
* the formatted code.
4242
*
43-
* @param code the unformatted code
44-
* @param codeStyle the code {@link JavaFormatterOptions.Style} to use,
45-
* if {@code null} the {@link JavaFormatterOptions.Style#GOOGLE}
46-
* style will be used
43+
* @param code the unformatted code
44+
* @param codeStyle the code {@link JavaFormatterOptions.Style} to use,
45+
* if {@code null} the {@link JavaFormatterOptions.Style#GOOGLE}
46+
* style will be used
4747
* @param changedElements a {@link SortedSet} containing ranges as {@link Pair}
48-
* objects defining the offsets which should be formatted
48+
* objects defining the offsets which should be formatted
4949
*
5050
* @return the formatted code
5151
*

src/main/java/de/funfried/netbeans/plugins/external/formatter/java/spring/SpringJavaFormatterService.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import de.funfried.netbeans.plugins.external.formatter.java.spring.ui.SpringJavaFormatterOptionsPanel;
3535
import de.funfried.netbeans.plugins.external.formatter.ui.options.FormatterOptionsPanel;
3636
import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
37-
import io.spring.javaformat.formatter.Formatter;
37+
import io.spring.javaformat.formatter.eclipse.EclipseCodeFormatter;
3838

3939
/**
4040
* Spring implementation of the {@link AbstractJavaFormatterService}.
@@ -95,11 +95,16 @@ public Integer getContinuationIndentSize(Document document) {
9595

9696
Preferences preferences = Settings.getActivePreferences(document);
9797
if (isUseFormatterIndentationSettings(preferences)) {
98-
String propKey = "org.eclipse.jdt.core.formatter.continuation_indentation";
98+
String propKey = "core.formatter.continuation_indentation";
9999
String prop = this.getSpringFormatterProperty(propKey);
100100
if (prop != null) {
101101
try {
102102
ret = Integer.parseInt(prop);
103+
104+
Integer indentSize = getIndentSize(document);
105+
if (indentSize != null) {
106+
ret *= indentSize;
107+
}
103108
} catch (NumberFormatException ex) {
104109
log.log(Level.WARNING, "Property '" + propKey + "' is not an integer: " + prop, ex);
105110
}
@@ -127,14 +132,19 @@ public Integer getIndentSize(Document document) {
127132

128133
Preferences preferences = Settings.getActivePreferences(document);
129134
if (isUseFormatterIndentationSettings(preferences)) {
130-
String propKey = "org.eclipse.jdt.core.formatter.indentation.size";
131-
String prop = this.getSpringFormatterProperty(propKey);
132-
if (prop != null) {
133-
try {
134-
ret = Integer.parseInt(prop);
135-
} catch (NumberFormatException ex) {
136-
log.log(Level.WARNING, "Property '" + propKey + "' is not an integer: " + prop, ex);
135+
String tabChar = getSpringFormatterProperty("core.formatter.tabulation.char");
136+
if (Objects.equals(tabChar, "mixed")) {
137+
String propKey = "core.formatter.indentation.size";
138+
String prop = this.getSpringFormatterProperty(propKey);
139+
if (prop != null) {
140+
try {
141+
ret = Integer.parseInt(prop);
142+
} catch (NumberFormatException ex) {
143+
log.log(Level.WARNING, "Property '" + propKey + "' is not an integer: " + prop, ex);
144+
}
137145
}
146+
} else {
147+
ret = getSpacesPerTab(document);
138148
}
139149

140150
if (ret == null) {
@@ -157,7 +167,7 @@ public Integer getRightMargin(Document document) {
157167

158168
Integer ret = 120;
159169

160-
String propKey = "org.eclipse.jdt.core.formatter.lineSplit";
170+
String propKey = "core.formatter.lineSplit";
161171
String prop = this.getSpringFormatterProperty(propKey);
162172
if (prop != null) {
163173
try {
@@ -195,7 +205,7 @@ public Integer getSpacesPerTab(Document document) {
195205
if (preferences.getBoolean(Settings.OVERRIDE_TAB_SIZE, true)) {
196206
ret = preferences.getInt(Settings.OVERRIDE_TAB_SIZE_VALUE, 4);
197207
} else {
198-
String propKey = "org.eclipse.jdt.core.formatter.tabulation.size";
208+
String propKey = "core.formatter.tabulation.size";
199209
String prop = this.getSpringFormatterProperty(propKey);
200210
if (prop != null) {
201211
try {
@@ -225,7 +235,7 @@ public Integer getSpacesPerTab(Document document) {
225235
*/
226236
private String getSpringFormatterProperty(String key) {
227237
Properties props = new Properties();
228-
try (InputStream is = Formatter.class.getResourceAsStream("formatter.prefs")) {
238+
try (InputStream is = EclipseCodeFormatter.class.getResourceAsStream("formatter.prefs")) {
229239
props.load(is);
230240
} catch (IOException ex) {
231241
log.log(Level.SEVERE, "Could not read internal Spring formatter configuration", ex);
@@ -248,7 +258,7 @@ public Boolean isExpandTabToSpaces(Document document) {
248258

249259
Preferences preferences = Settings.getActivePreferences(document);
250260
if (isUseFormatterIndentationSettings(preferences)) {
251-
String propKey = "org.eclipse.jdt.core.formatter.tabulation.size";
261+
String propKey = "core.formatter.tabulation.char";
252262
String prop = this.getSpringFormatterProperty(propKey);
253263
if (prop != null) {
254264
ret = Objects.equals(prop, "space");

src/main/java/de/funfried/netbeans/plugins/external/formatter/javascript/eclipse/EclipseJavascriptFormatterService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public Integer getContinuationIndentSize(Document document) {
9393

9494
Integer indentSize = getIndentSize(document);
9595
if (indentSize != null) {
96-
ret = Integer.valueOf(value) * indentSize;
96+
ret *= indentSize;
9797
}
9898
}
9999
}

src/test/java/de/funfried/netbeans/plugins/external/formatter/java/spring/SpringJavaFormatterServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void testFormat() throws Exception {
5858
Assert.assertNotNull(instance.createOptionsPanel(null));
5959
Assert.assertEquals((long) 120L, (long) instance.getRightMargin(document));
6060

61-
Assert.assertEquals((long) 2L, (long) instance.getContinuationIndentSize(document));
61+
Assert.assertEquals((long) 8L, (long) instance.getContinuationIndentSize(document));
6262
Assert.assertEquals((long) 4L, (long) instance.getIndentSize(document));
6363
Assert.assertEquals((long) 4L, (long) instance.getSpacesPerTab(document));
6464
Assert.assertFalse(instance.isExpandTabToSpaces(document));

0 commit comments

Comments
 (0)