1
2
3
4
5
6
7
8
9
10 package de.funfried.netbeans.plugins.external.formatter.sql.jsqlformatter;
11
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.SortedSet;
15 import java.util.prefs.Preferences;
16
17 import javax.swing.text.BadLocationException;
18 import javax.swing.text.Document;
19 import javax.swing.text.StyledDocument;
20
21 import org.apache.commons.lang3.tuple.Pair;
22 import org.netbeans.api.annotations.common.CheckForNull;
23 import org.netbeans.api.annotations.common.NonNull;
24 import org.netbeans.api.project.Project;
25 import org.openide.util.NbBundle;
26 import org.openide.util.lookup.ServiceProvider;
27
28 import com.manticore.jsqlformatter.JSQLFormatter;
29
30 import de.funfried.netbeans.plugins.external.formatter.FormatJob;
31 import de.funfried.netbeans.plugins.external.formatter.FormatterService;
32 import de.funfried.netbeans.plugins.external.formatter.MimeType;
33 import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
34 import de.funfried.netbeans.plugins.external.formatter.sql.jsqlformatter.ui.JSQLFormatterOptionsPanel;
35 import de.funfried.netbeans.plugins.external.formatter.ui.options.FormatterOptionsPanel;
36 import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
37
38
39
40
41
42
43 @NbBundle.Messages({
44 "FormatterName=JSQLFormatter"
45 })
46 @ServiceProvider(service = FormatterService.class, position = 500)
47 public class JSQLFormatterService implements FormatterService {
48
49 public static final String ID = "jsqlformatter";
50
51
52 private final JSQLFormatterWrapper formatter = new JSQLFormatterWrapper();
53
54
55
56
57 @Override
58 public boolean format(StyledDocument document, SortedSet<Pair<Integer, Integer>> changedElements) throws BadLocationException, FormattingFailedException {
59 if (!canHandle(document)) {
60 throw new FormattingFailedException("The file type '" + MimeType.getMimeTypeAsString(document) + "' is not supported");
61 }
62
63 getFormatJob(document, changedElements).format();
64
65 return true;
66 }
67
68
69
70
71 @Override
72 public List<MimeType> getSupportedMimeTypes() {
73 return Collections.singletonList(MimeType.SQL);
74 }
75
76
77
78
79 @NonNull
80 @Override
81 public String getDisplayName() {
82 return NbBundle.getMessage(JSQLFormatterService.class, "FormatterName");
83 }
84
85
86
87
88 @NonNull
89 @Override
90 public String getId() {
91 return ID;
92 }
93
94
95
96
97 @Override
98 public FormatterOptionsPanel createOptionsPanel(Project project) {
99 return new JSQLFormatterOptionsPanel(project);
100 }
101
102
103
104
105 @CheckForNull
106 @Override
107 public Integer getContinuationIndentSize(Document document) {
108 if (document == null) {
109 return null;
110 }
111
112 Integer width = null;
113
114 Preferences preferences = Settings.getActivePreferences(document);
115 if (isUseFormatterIndentationSettings(preferences)) {
116 width = preferences.getInt(JSQLFormatter.FormattingOption.INDENT_WIDTH.toString(), JSQLFormatter.getIndentWidth());
117 }
118
119 return width;
120 }
121
122
123
124
125 @CheckForNull
126 @Override
127 public Integer getIndentSize(Document document) {
128 if (document == null) {
129 return null;
130 }
131
132 Integer width = null;
133
134 Preferences preferences = Settings.getActivePreferences(document);
135 if (isUseFormatterIndentationSettings(preferences)) {
136 width = preferences.getInt(JSQLFormatter.FormattingOption.INDENT_WIDTH.toString(), JSQLFormatter.getIndentWidth());
137 }
138
139 return width;
140 }
141
142
143
144
145 @CheckForNull
146 @Override
147 public Integer getRightMargin(Document document) {
148 if (document == null) {
149 return null;
150 }
151
152 return 120;
153 }
154
155
156
157
158
159
160
161 protected FormatJob getFormatJob(StyledDocument document, SortedSet<Pair<Integer, Integer>> changedElements) {
162 return new JSQLFormatterJob(document, formatter, changedElements);
163 }
164
165
166
167
168 @CheckForNull
169 @Override
170 public Integer getSpacesPerTab(Document document) {
171 if (document == null) {
172 return null;
173 }
174
175 Integer width = null;
176
177 Preferences preferences = Settings.getActivePreferences(document);
178 if (isUseFormatterIndentationSettings(preferences)) {
179 width = preferences.getInt(JSQLFormatter.FormattingOption.INDENT_WIDTH.toString(), JSQLFormatter.getIndentWidth());
180 }
181
182 return width;
183 }
184
185
186
187
188 @CheckForNull
189 @Override
190 public Boolean isExpandTabToSpaces(Document document) {
191 if (document == null) {
192 return null;
193 }
194
195 Preferences preferences = Settings.getActivePreferences(document);
196 if (isUseFormatterIndentationSettings(preferences)) {
197 return Boolean.TRUE;
198 }
199
200 return null;
201 }
202
203
204
205
206
207
208
209
210
211
212 private boolean isUseFormatterIndentationSettings(Preferences prefs) {
213 return prefs.getBoolean(Settings.ENABLE_USE_OF_INDENTATION_SETTINGS, true);
214 }
215
216
217
218
219 @Override
220 @CheckForNull
221 public Boolean organizeImports(StyledDocument document, boolean afterFixImports) throws BadLocationException {
222 return null;
223 }
224 }