1
2
3
4
5
6
7
8
9
10 package de.funfried.netbeans.plugins.external.formatter.java.eclipse;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Objects;
16 import java.util.SortedSet;
17
18 import org.apache.commons.collections4.CollectionUtils;
19 import org.apache.commons.lang3.tuple.Pair;
20 import org.eclipse.jdt.core.ToolFactory;
21 import org.eclipse.jdt.core.formatter.CodeFormatter;
22 import org.eclipse.jface.text.Document;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.IRegion;
25 import org.eclipse.jface.text.Region;
26 import org.eclipse.text.edits.TextEdit;
27 import org.netbeans.api.annotations.common.CheckForNull;
28 import org.netbeans.api.annotations.common.NonNull;
29
30 import de.funfried.netbeans.plugins.external.formatter.exceptions.CannotLoadConfigurationException;
31 import de.funfried.netbeans.plugins.external.formatter.exceptions.ConfigReadException;
32 import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
33 import de.funfried.netbeans.plugins.external.formatter.exceptions.ProfileNotFoundException;
34
35
36
37
38
39
40 public final class EclipseJavaFormatterWrapper {
41
42 private static final int FORMATTER_OPTS = CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS ;
43
44
45
46
47 EclipseJavaFormatterWrapper() {
48 }
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 @CheckForNull
69 public String format(String formatterFile, String formatterProfile, String code, String lineFeed, String sourceLevel, SortedSet<Pair<Integer, Integer>> changedElements)
70 throws ConfigReadException, ProfileNotFoundException, CannotLoadConfigurationException, FormattingFailedException {
71 if (code == null) {
72 return null;
73 }
74
75 int codeLength = code.length();
76
77 List<IRegion> regions = new ArrayList<>();
78 if (changedElements == null) {
79 regions.add(new Region(0, codeLength));
80 } else if (!CollectionUtils.isEmpty(changedElements)) {
81 for (Pair<Integer, Integer> changedElement : changedElements) {
82 int length = (changedElement.getRight() - changedElement.getLeft()) + 1;
83 if (length > codeLength) {
84 length = codeLength;
85 }
86
87 regions.add(new Region(changedElement.getLeft(), length));
88 }
89 } else {
90
91 return code;
92 }
93
94 Map<String, String> allConfig = EclipseFormatterConfig.parseConfig(formatterFile, formatterProfile, sourceLevel);
95
96 CodeFormatter formatter = ToolFactory.createCodeFormatter(allConfig, ToolFactory.M_FORMAT_EXISTING);
97
98
99 return format(formatter, code, regions.toArray(IRegion[]::new), lineFeed);
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 @CheckForNull
117 private String format(@NonNull CodeFormatter formatter, @NonNull String code, @NonNull IRegion[] regions, String lineFeed) throws FormattingFailedException, IllegalArgumentException {
118 String formattedCode = null;
119
120 try {
121 TextEdit te = formatter.format(FORMATTER_OPTS, code, regions, 0, lineFeed);
122 if (te != null && te.getChildrenSize() > 0) {
123 IDocument dc = new Document(code);
124 te.apply(dc);
125
126 formattedCode = dc.get();
127
128 if (Objects.equals(code, formattedCode)) {
129 return null;
130 }
131 }
132 } catch (IllegalArgumentException ex) {
133 throw ex;
134 } catch (Exception ex) {
135 throw new FormattingFailedException(ex);
136 }
137
138 return formattedCode;
139 }
140 }