|
19 | 19 | import java.util.List;
|
20 | 20 | import java.util.logging.Level;
|
21 | 21 | import java.util.logging.Logger;
|
| 22 | +import java.util.prefs.Preferences; |
22 | 23 | import javax.swing.SwingUtilities;
|
23 | 24 | import javax.swing.text.BadLocationException;
|
24 | 25 | import javax.swing.text.Caret;
|
25 | 26 | import javax.swing.text.EditorKit;
|
26 | 27 | import javax.swing.text.StyledDocument;
|
27 | 28 | import org.netbeans.api.editor.mimelookup.MimeLookup;
|
28 | 29 | import org.netbeans.api.editor.mimelookup.MimePath;
|
| 30 | +import org.netbeans.api.editor.settings.SimpleValueNames; |
| 31 | +import org.netbeans.lib.editor.util.swing.DocumentUtilities; |
29 | 32 | import org.netbeans.modules.editor.indent.api.Reformat;
|
30 | 33 | import org.openide.cookies.EditorCookie;
|
31 | 34 | import org.openide.filesystems.FileLock;
|
@@ -157,16 +160,28 @@ public void run() {
|
157 | 160 |
|
158 | 161 | // Reformat code (to apply ident size & styles)
|
159 | 162 | // TODO: Do this only if CodeStylePreferences have been changed
|
160 |
| - Reformat reformat = Reformat.get(cookie.getDocument()); |
161 |
| - reformat.lock(); |
162 |
| - try { |
163 |
| - reformat.reformat(0, cookie.getDocument().getLength()); |
164 |
| - } catch (BadLocationException ex) { |
165 |
| - LOG.log(Level.SEVERE, "AutoFormat on document not possible: {0}", ex.getMessage()); |
166 |
| - } finally { |
167 |
| - reformat.unlock(); |
168 |
| - // Save document after reformat |
169 |
| - cookie.saveDocument(); |
| 163 | + |
| 164 | + Preferences prefs = MimeLookup.getLookup(DocumentUtilities.getMimeType(cookie.getDocument())).lookup(Preferences.class); |
| 165 | + |
| 166 | + if (prefs.getBoolean(SimpleValueNames.ON_SAVE_USE_GLOBAL_SETTINGS, true)) { |
| 167 | + prefs = MimeLookup.getLookup(MimePath.EMPTY).lookup(Preferences.class); |
| 168 | + } |
| 169 | + |
| 170 | + String policy = prefs.get(SimpleValueNames.ON_SAVE_REFORMAT, null); |
| 171 | + |
| 172 | + if (!"never".equals(policy)) { |
| 173 | + Reformat reformat = Reformat.get(cookie.getDocument()); |
| 174 | + reformat.lock(); |
| 175 | + |
| 176 | + try { |
| 177 | + reformat.reformat(0, cookie.getDocument().getLength()); |
| 178 | + } catch (BadLocationException ex) { |
| 179 | + LOG.log(Level.SEVERE, "AutoFormat on document not possible: {0}", ex.getMessage()); |
| 180 | + } finally { |
| 181 | + reformat.unlock(); |
| 182 | + // Save document after reformat |
| 183 | + cookie.saveDocument(); |
| 184 | + } |
170 | 185 | }
|
171 | 186 | } catch (BadLocationException | IOException ex) {
|
172 | 187 | LOG.log(Level.SEVERE, "Document could not be saved: {0}", ex.getMessage());
|
|
0 commit comments