1
2
3
4
5
6
7
8
9
10
11 package de.funfried.netbeans.plugins.external.formatter.javascript.eclipse;
12
13 import java.util.SortedSet;
14 import java.util.TreeSet;
15 import java.util.prefs.Preferences;
16
17 import javax.swing.text.StyledDocument;
18
19 import org.apache.commons.lang3.tuple.Pair;
20 import org.netbeans.api.annotations.common.NonNull;
21
22 import de.funfried.netbeans.plugins.external.formatter.eclipse.AbstractEclipseFormatJob;
23 import de.funfried.netbeans.plugins.external.formatter.exceptions.CannotLoadConfigurationException;
24 import de.funfried.netbeans.plugins.external.formatter.exceptions.ConfigReadException;
25 import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
26 import de.funfried.netbeans.plugins.external.formatter.exceptions.ProfileNotFoundException;
27 import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
28
29
30
31
32
33
34
35
36 class EclipseFormatJob extends AbstractEclipseFormatJob {
37
38 private final EclipseJavascriptFormatterWrapper formatter;
39
40
41
42
43
44
45
46
47 EclipseFormatJob(StyledDocument document, EclipseJavascriptFormatterWrapper formatter, Pair<Integer, Integer> changedElement) {
48 super(document, singletonSortedSet(changedElement));
49
50 this.formatter = formatter;
51 }
52
53
54
55
56 @Override
57 protected String getFormattedContent(Preferences pref, String formatterFile, String formatterProfile, String code)
58 throws ConfigReadException, ProfileNotFoundException, CannotLoadConfigurationException, FormattingFailedException {
59 return formatter.format(formatterFile, formatterProfile, code, getLineFeed(pref), changedElements != null ? changedElements.first() : null);
60 }
61
62
63
64
65 @Override
66 protected String getFormatterFile(Preferences pref) {
67 return EclipseJavascriptFormatterSettings.getEclipseFormatterFile(pref, document);
68 }
69
70
71
72
73 @Override
74 protected String getFormatterProfile(Preferences pref) {
75 return pref.get(EclipseJavascriptFormatterSettings.ACTIVE_PROFILE, "");
76 }
77
78
79
80
81 @Override
82 protected String getLineFeed(Preferences pref) {
83 String lineFeedSetting = pref.get(EclipseJavascriptFormatterSettings.LINEFEED, "");
84 return Settings.getLineFeed(lineFeedSetting, System.getProperty("line.separator"));
85 }
86
87
88
89
90 @NonNull
91 @Override
92 protected String getNotificationMessageForEclipseFormatterConfigurationFileType(String formatterFile, String formatterProfile) {
93 String msg = "";
94 if (EclipseJavascriptFormatterSettings.isWorkspaceMechanicFile(formatterFile)) {
95
96 msg = String.format("Using %s", formatterFile);
97 } else if (EclipseJavascriptFormatterSettings.isXMLConfigurationFile(formatterFile)) {
98
99 msg = String.format("Using profile '%s' from %s", formatterProfile, formatterFile);
100 } else if (EclipseJavascriptFormatterSettings.isProjectSetting(formatterFile)) {
101
102 msg = String.format("Using %s", formatterFile);
103 }
104
105 return msg;
106 }
107
108
109
110
111
112
113
114
115
116
117 private static SortedSet<Pair<Integer, Integer>> singletonSortedSet(Pair<Integer, Integer> pair) {
118 if (pair == null) {
119 return null;
120 }
121
122 SortedSet<Pair<Integer, Integer>> sortedSet = new TreeSet<>();
123 sortedSet.add(pair);
124
125 return sortedSet;
126 }
127 }