Skip to content
This repository was archived by the owner on Apr 10, 2021. It is now read-only.

Commit 080d28d

Browse files
Chris2011bennycode
authored andcommitted
Trigger reformatting document only when NetBeans option is enabled (#108)
1 parent e95a328 commit 080d28d

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@
251251
<version>2.5</version>
252252
<type>jar</type>
253253
</dependency>
254+
<dependency>
255+
<groupId>org.netbeans.api</groupId>
256+
<artifactId>org-netbeans-modules-editor-util</artifactId>
257+
<version>RELEASE82</version>
258+
</dependency>
254259
</dependencies>
255260

256261
<build>

src/main/java/com/welovecoding/nbeditorconfig/io/writer/StyledDocumentWriter.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
import java.util.List;
2020
import java.util.logging.Level;
2121
import java.util.logging.Logger;
22+
import java.util.prefs.Preferences;
2223
import javax.swing.SwingUtilities;
2324
import javax.swing.text.BadLocationException;
2425
import javax.swing.text.Caret;
2526
import javax.swing.text.EditorKit;
2627
import javax.swing.text.StyledDocument;
2728
import org.netbeans.api.editor.mimelookup.MimeLookup;
2829
import org.netbeans.api.editor.mimelookup.MimePath;
30+
import org.netbeans.api.editor.settings.SimpleValueNames;
31+
import org.netbeans.lib.editor.util.swing.DocumentUtilities;
2932
import org.netbeans.modules.editor.indent.api.Reformat;
3033
import org.openide.cookies.EditorCookie;
3134
import org.openide.filesystems.FileLock;
@@ -157,16 +160,28 @@ public void run() {
157160

158161
// Reformat code (to apply ident size & styles)
159162
// 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+
}
170185
}
171186
} catch (BadLocationException | IOException ex) {
172187
LOG.log(Level.SEVERE, "Document could not be saved: {0}", ex.getMessage());

0 commit comments

Comments
 (0)