View Javadoc
1   /*
2    * Copyright (c) 2021 Andreas Reichel <a href="mailto:andreas@manticore-projects.com">andreas@manticore-projects.com</a>
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    * bahlef - initial API and implementation and/or initial documentation
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   * JSQLFormatter implementation of the {@link FormatterService}.
40   *
41   * @author Andreas Reichel <a href="mailto:andreas@manticore-projects.com">andreas@manticore-projects.com</a>
42   */
43  @NbBundle.Messages({
44  		"FormatterName=JSQLFormatter"
45  })
46  @ServiceProvider(service = FormatterService.class, position = 500)
47  public class JSQLFormatterService implements FormatterService {
48  	/** The ID of this formatter service. */
49  	public static final String ID = "jsqlformatter";
50  
51  	/** * The {@link JSQLFormatterWrapper} implementation. */
52  	private final JSQLFormatterWrapper formatter = new JSQLFormatterWrapper();
53  
54  	/**
55  	 * {@inheritDoc}
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  	 * {@inheritDoc}
70  	 */
71  	@Override
72  	public List<MimeType> getSupportedMimeTypes() {
73  		return Collections.singletonList(MimeType.SQL);
74  	}
75  
76  	/**
77  	 * {@inheritDoc}
78  	 */
79  	@NonNull
80  	@Override
81  	public String getDisplayName() {
82  		return NbBundle.getMessage(JSQLFormatterService.class, "FormatterName");
83  	}
84  
85  	/**
86  	 * {@inheritDoc}
87  	 */
88  	@NonNull
89  	@Override
90  	public String getId() {
91  		return ID;
92  	}
93  
94  	/**
95  	 * {@inheritDoc}
96  	 */
97  	@Override
98  	public FormatterOptionsPanel createOptionsPanel(Project project) {
99  		return new JSQLFormatterOptionsPanel(project);
100 	}
101 
102 	/**
103 	 * {@inheritDoc}
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 	 * {@inheritDoc}
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 	 * {@inheritDoc}
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 	 * Returns the {@link FormatJob}.
157 	 *
158 	 * @param document the {@link StyledDocument} which should be formatted
159 	 * @param changedElements a {@link SortedSet} containing ranges as {@link Pair} objects that should be formatted
160 	 */
161 	protected FormatJob getFormatJob(StyledDocument document, SortedSet<Pair<Integer, Integer>> changedElements) {
162 		return new JSQLFormatterJob(document, formatter, changedElements);
163 	}
164 
165 	/**
166 	 * {@inheritDoc}
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 	 * {@inheritDoc}
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 	 * Returns {@code true} if using the formatter indentation settings from the external
205 	 * formatter is activated, otherwise {@code false}.
206 	 *
207 	 * @param prefs the {@link Preferences} where to check
208 	 *
209 	 * @return {@code true} if using the formatter indentation settings from the external
210 	 *         formatter is activated, otherwise {@code false}
211 	 */
212 	private boolean isUseFormatterIndentationSettings(Preferences prefs) {
213 		return prefs.getBoolean(Settings.ENABLE_USE_OF_INDENTATION_SETTINGS, true);
214 	}
215 
216 	/**
217 	 * {@inheritDoc}
218 	 */
219 	@Override
220 	@CheckForNull
221 	public Boolean organizeImports(StyledDocument document, boolean afterFixImports) throws BadLocationException {
222 		return null;
223 	}
224 }