View Javadoc
1   /*
2    * Copyright (c) 2020 bahlef.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v2.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v20.html
7    * Contributors:
8    * markiewb - initial API and implementation and/or initial documentation
9    * bahlef
10   */
11  package de.funfried.netbeans.plugins.external.formatter.javascript.eclipse;
12  
13  import java.util.Map;
14  import java.util.Objects;
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 de.funfried.netbeans.plugins.external.formatter.FormatJob;
29  import de.funfried.netbeans.plugins.external.formatter.FormatterService;
30  import de.funfried.netbeans.plugins.external.formatter.javascript.base.AbstractJavascriptFormatterService;
31  import de.funfried.netbeans.plugins.external.formatter.javascript.eclipse.ui.EclipseJavascriptFormatterOptionsPanel;
32  import de.funfried.netbeans.plugins.external.formatter.ui.options.FormatterOptionsPanel;
33  import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
34  
35  /**
36   * Eclipse implementation of the {@link AbstractJavascriptFormatterService}.
37   *
38   * @author bahlef
39   */
40  @NbBundle.Messages({
41  		"FormatterName=Eclipse Javascript Code Formatter"
42  })
43  @ServiceProvider(service = FormatterService.class, position = 1000)
44  public class EclipseJavascriptFormatterService extends AbstractJavascriptFormatterService {
45  	/** The ID of this formatter service. */
46  	public static final String ID = "eclipse-javascript-formatter";
47  
48  	/** * The {@link EclipseJavascriptFormatterWrapper} implementation. */
49  	private final EclipseJavascriptFormatterWrapper formatter = new EclipseJavascriptFormatterWrapper();
50  
51  	/**
52  	 * {@inheritDoc}
53  	 */
54  	@NonNull
55  	@Override
56  	public String getDisplayName() {
57  		return NbBundle.getMessage(EclipseJavascriptFormatterService.class, "FormatterName");
58  	}
59  
60  	/**
61  	 * {@inheritDoc}
62  	 */
63  	@NonNull
64  	@Override
65  	public String getId() {
66  		return ID;
67  	}
68  
69  	/**
70  	 * {@inheritDoc}
71  	 */
72  	@Override
73  	public FormatterOptionsPanel createOptionsPanel(Project project) {
74  		return new EclipseJavascriptFormatterOptionsPanel(project);
75  	}
76  
77  	/**
78  	 * {@inheritDoc}
79  	 */
80  	@CheckForNull
81  	@Override
82  	public Integer getContinuationIndentSize(Document document) {
83  		if (document == null) {
84  			return null;
85  		}
86  
87  		Integer ret = null;
88  
89  		Preferences preferences = Settings.getActivePreferences(document);
90  		if (isUseFormatterIndentationSettings(preferences)) {
91  			String value = getEclipseFormatterProperty(preferences, document, "org.eclipse.wst.jsdt.core.formatter.continuation_indentation");
92  			if (value != null) {
93  				ret = Integer.valueOf(value);
94  
95  				Integer indentSize = getIndentSize(document);
96  				if (indentSize != null) {
97  					ret *= indentSize;
98  				}
99  			}
100 		}
101 
102 		return ret;
103 	}
104 
105 	/**
106 	 * {@inheritDoc}
107 	 */
108 	@CheckForNull
109 	@Override
110 	public Integer getIndentSize(Document document) {
111 		if (document == null) {
112 			return null;
113 		}
114 
115 		Integer ret = null;
116 
117 		Preferences preferences = Settings.getActivePreferences(document);
118 		if (isUseFormatterIndentationSettings(preferences)) {
119 			String tabChar = getEclipseFormatterProperty(preferences, document, "org.eclipse.wst.jsdt.core.formatter.tabulation.char");
120 			if (Objects.equals(tabChar, "mixed")) {
121 				String value = getEclipseFormatterProperty(preferences, document, "org.eclipse.wst.jsdt.core.formatter.indentation.size");
122 				if (value != null) {
123 					ret = Integer.valueOf(value);
124 				}
125 			} else {
126 				ret = getSpacesPerTab(document);
127 			}
128 
129 		}
130 
131 		return ret;
132 	}
133 
134 	/**
135 	 * {@inheritDoc}
136 	 */
137 	@CheckForNull
138 	@Override
139 	public Integer getRightMargin(Document document) {
140 		if (document == null) {
141 			return null;
142 		}
143 
144 		Integer ret = null;
145 
146 		String value = getEclipseFormatterProperty(null, document, "org.eclipse.wst.jsdt.core.formatter.lineSplit");
147 		if (value != null) {
148 			ret = Integer.valueOf(value);
149 		}
150 
151 		return ret;
152 	}
153 
154 	/**
155 	 * {@inheritDoc}
156 	 */
157 	@Override
158 	protected FormatJob getFormatJob(StyledDocument document, Pair<Integer, Integer> changedElement) {
159 		return new EclipseFormatJob(document, formatter, changedElement);
160 	}
161 
162 	/**
163 	 * {@inheritDoc}
164 	 */
165 	@CheckForNull
166 	@Override
167 	public Integer getSpacesPerTab(Document document) {
168 		if (document == null) {
169 			return null;
170 		}
171 
172 		Integer ret = null;
173 
174 		Preferences preferences = Settings.getActivePreferences(document);
175 		if (isUseFormatterIndentationSettings(preferences)) {
176 			if (!isExpandTabToSpaces(document, preferences) && preferences.getBoolean(Settings.OVERRIDE_TAB_SIZE, true)) {
177 				ret = preferences.getInt(Settings.OVERRIDE_TAB_SIZE_VALUE, 4);
178 			} else {
179 				String value = getEclipseFormatterProperty(preferences, document, "org.eclipse.wst.jsdt.core.formatter.tabulation.size");
180 				if (value != null) {
181 					ret = Integer.valueOf(value);
182 				}
183 			}
184 		}
185 
186 		return ret;
187 	}
188 
189 	/**
190 	 * Reads the configuration value of the Eclipse formatter configuration from a given
191 	 * {@link Document} for the given {@code  key}.
192 	 *
193 	 * @param preferences the {@link Preferences} of the {@link Document} if already loaded
194 	 *        or {@code null} to read the preferences of the given {@link Document}
195 	 * @param document the {@link Document} where to read the value from
196 	 * @param key the key of the value which should be read
197 	 *
198 	 * @return the configuration value of the Eclipse formatter configuration from a given
199 	 *         {@link Document} for the given {@code  key}
200 	 */
201 	private String getEclipseFormatterProperty(Preferences preferences, Document document, String key) {
202 		if (document == null) {
203 			return null;
204 		}
205 
206 		if (preferences == null) {
207 			preferences = Settings.getActivePreferences(document);
208 		}
209 
210 		String formatterFile = EclipseJavascriptFormatterSettings.getEclipseFormatterFile(preferences, document);
211 		String formatterProfile = preferences.get(EclipseJavascriptFormatterSettings.ACTIVE_PROFILE, "");
212 
213 		Map<String, String> config = EclipseFormatterConfig.parseConfig(formatterFile, formatterProfile);
214 
215 		return config.getOrDefault(key, null);
216 	}
217 
218 	/**
219 	 * {@inheritDoc}
220 	 */
221 	@CheckForNull
222 	@Override
223 	public Boolean isExpandTabToSpaces(Document document) {
224 		if (document == null) {
225 			return null;
226 		}
227 
228 		return isExpandTabToSpaces(document, Settings.getActivePreferences(document));
229 	}
230 
231 	private Boolean isExpandTabToSpaces(Document document, Preferences preferences) {
232 		if (document == null || preferences == null) {
233 			return null;
234 		}
235 
236 		Boolean ret = null;
237 
238 		if (isUseFormatterIndentationSettings(preferences)) {
239 			String value = getEclipseFormatterProperty(preferences, document, "org.eclipse.wst.jsdt.core.formatter.tabulation.char");
240 			if (value != null) {
241 				ret = Objects.equals(value, "space");
242 			}
243 		}
244 
245 		return ret;
246 	}
247 
248 	/**
249 	 * Returns {@code true} if using the formatter indentation settings from the external
250 	 * formatter is activated, otherwise {@code false}.
251 	 *
252 	 * @param prefs the {@link Preferences} where to check
253 	 *
254 	 * @return {@code true} if using the formatter indentation settings from the external
255 	 *         formatter is activated, otherwise {@code false}
256 	 */
257 	private boolean isUseFormatterIndentationSettings(Preferences prefs) {
258 		return prefs.getBoolean(Settings.ENABLE_USE_OF_INDENTATION_SETTINGS, true);
259 	}
260 
261 	/**
262 	 * {@inheritDoc}
263 	 */
264 	@Override
265 	@CheckForNull
266 	public Boolean organizeImports(StyledDocument document, boolean afterFixImports) throws BadLocationException {
267 		return null;
268 	}
269 }