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

Commit 7205cd2

Browse files
committed
Pretty raw fix for #87
1 parent 83602c6 commit 7205cd2

File tree

6 files changed

+81
-67
lines changed

6 files changed

+81
-67
lines changed

nb-configuration.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,12 @@
1717
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapAnnotations>WRAP_IF_LONG</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapAnnotations>
1818
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.sortMembersInGroups>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.sortMembersInGroups>
1919
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.keepGettersAndSettersTogether>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.keepGettersAndSettersTogether>
20+
<de-markiewb-netbeans-plugins-eclipse-formatter.preserveBreakPoints>true</de-markiewb-netbeans-plugins-eclipse-formatter.preserveBreakPoints>
21+
<de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterActiveProfile/>
22+
<de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterLocation/>
23+
<de-markiewb-netbeans-plugins-eclipse-formatter.showNotifications>false</de-markiewb-netbeans-plugins-eclipse-formatter.showNotifications>
24+
<de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterEnabled>false</de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterEnabled>
25+
<de-markiewb-netbeans-plugins-eclipse-formatter.enableFormatAsSaveAction>false</de-markiewb-netbeans-plugins-eclipse-formatter.enableFormatAsSaveAction>
26+
<de-markiewb-netbeans-plugins-eclipse-formatter.useProjectSettings>true</de-markiewb-netbeans-plugins-eclipse-formatter.useProjectSettings>
2027
</properties>
2128
</project-shared-configuration>

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

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.List;
1919
import java.util.logging.Level;
2020
import java.util.logging.Logger;
21+
import javax.swing.SwingUtilities;
2122
import javax.swing.text.BadLocationException;
2223
import javax.swing.text.Caret;
2324
import javax.swing.text.EditorKit;
@@ -30,6 +31,7 @@
3031
import org.openide.loaders.DataObject;
3132
import org.openide.util.Lookup;
3233
import org.openide.filesystems.FileObject;
34+
import org.openide.text.NbDocument;
3335
import org.openide.util.Utilities;
3436

3537
public class StyledDocumentWriter {
@@ -109,62 +111,69 @@ public static void writeOnFileWithLines(File file, Charset charset, List<String>
109111
}
110112

111113
public static void writeWithEditorKit(FileInfo info)
112-
throws FileAccessException {
114+
throws FileAccessException, IOException {
115+
113116
EditorCookie cookie = info.getCookie();
117+
StyledDocument openedDocument = cookie.openDocument();
114118
EditorKit kit = getEditorKit(info.getDataObject());
115-
StyledDocument document = null;
116-
int caretPosition;
117-
118-
try {
119-
document = cookie.openDocument();
120-
} catch (IOException ex) {
121-
throw new FileAccessException("Document could not be loaded: " + ex.getMessage());
122-
}
123119

124120
try (InputStream is = new ByteArrayInputStream(info.getContentAsBytes())) {
125121
// Backup caret position
126-
Caret caret = info.getCaret();
127-
caretPosition = info.getCurrentCaretPosition();
128-
129-
// Wipe document
130-
document.remove(0, document.getLength());
131-
132-
LOG.log(Level.INFO, "Write to \"is\": {0}", is);
133-
LOG.log(Level.INFO, "Write to \"document\": {0}", document);
134-
135-
// Read input stream into the document (which is a "write" operation)
136-
kit.read(is, document, 0);
137-
cookie.saveDocument();
138-
info.getFileObject().setAttribute(ENCODING_SETTING, info.getCharset().name());
139-
140-
// Reset caret positon
141-
caretPosition -= info.getCaretOffset();
142-
if (caretPosition < document.getLength()) {
143-
LOG.log(Level.INFO, "Moving caret position to: {0} / {1}",
144-
new Object[]{caretPosition, document.getLength()});
145-
caret.setDot(caretPosition);
122+
final Caret caret = info.getCaret();
123+
if (caret == null) {
124+
LOG.log(Level.WARNING, "Could not get Caret");
125+
return;
146126
}
147-
} catch (BadLocationException | IOException ex) {
148-
throw new FileAccessException("Document could not be saved: " + ex.getMessage());
149-
}
150-
151-
// Reformat code (to apply ident size & styles)
152-
// TODO: Do this only if CodeStylePreferences have been changed
153-
Reformat reformat = Reformat.get(document);
154-
reformat.lock();
155-
156-
try {
157-
reformat.reformat(0, document.getLength());
158-
} catch (BadLocationException ex) {
159-
LOG.log(Level.SEVERE, "AutoFormat on document not possible: {0}", ex.getMessage());
160-
} finally {
161-
reformat.unlock();
162-
// Save document after reformat
163-
try {
164-
cookie.saveDocument();
165-
} catch (IOException ex) {
166-
throw new FileAccessException("Document could not be saved: " + ex.getMessage());
127+
int caretPosition = info.getCurrentCaretPosition();
128+
Runnable runner = () -> {
129+
NbDocument.runAtomic(openedDocument, () -> {
130+
try {
131+
// Wipe document
132+
cookie.getDocument().remove(0, cookie.getDocument().getLength());
133+
134+
LOG.log(Level.INFO, "Write to \"is\": {0}", is);
135+
LOG.log(Level.INFO, "Write to \"document\": {0}", cookie.getDocument());
136+
137+
// Read input stream into the document (which is a "write" operation)
138+
kit.read(is, cookie.getDocument(), 0);
139+
cookie.saveDocument();
140+
141+
info.getFileObject().setAttribute(ENCODING_SETTING, info.getCharset().name());
142+
143+
// Reset caret positon
144+
// caretPosition -= info.getCaretOffset();
145+
if (caretPosition < cookie.getDocument().getLength()) {
146+
LOG.log(Level.INFO, "Moving caret position from {0} to: {1} / {2}",
147+
new Object[]{info.getCaretOffset(), caretPosition, cookie.getDocument().getLength()});
148+
caret.setDot(caretPosition);
149+
}
150+
151+
// Reformat code (to apply ident size & styles)
152+
// TODO: Do this only if CodeStylePreferences have been changed
153+
Reformat reformat = Reformat.get(cookie.getDocument());
154+
reformat.lock();
155+
try {
156+
reformat.reformat(0, cookie.getDocument().getLength());
157+
} catch (BadLocationException ex) {
158+
LOG.log(Level.SEVERE, "AutoFormat on document not possible: {0}", ex.getMessage());
159+
} finally {
160+
reformat.unlock();
161+
// Save document after reformat
162+
cookie.saveDocument();
163+
}
164+
} catch (BadLocationException | IOException ex) {
165+
LOG.log(Level.SEVERE, "Document could not be saved: {0}", ex.getMessage());
166+
}
167+
});
168+
};
169+
170+
if (SwingUtilities.isEventDispatchThread()) {
171+
runner.run();
172+
} else {
173+
SwingUtilities.invokeLater(runner);
167174
}
175+
} catch (IOException ex) {
176+
LOG.log(Level.SEVERE, "Could not load content of document: {0}", ex.getMessage());
168177
}
169178
}
170179

src/main/java/com/welovecoding/nbeditorconfig/listener/FileChangeListener.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ public void fileChanged(FileEvent event) {
5252
LOG.log(Level.INFO, "[EC for {0}] File content changed: {1}", new Object[]{editorConfigFileObject.getPath(), path});
5353

5454
if (!event.firedFrom(new WriteEditorAction()) && !event.firedFrom(new WriteStringToFileAction())) {
55-
if (applyRulesToFile(event)) {
55+
if (isCandidateForProcessing(event)) {
5656
try {
57+
LOG.log(Level.INFO, "[EC for {0}] Applying rules");
5758
editorConfigProcessor.applyRulesToFile(DataObject.find(event.getFile()));
5859
} catch (DataObjectNotFoundException ex) {
5960
Exceptions.printStackTrace(ex);
@@ -69,7 +70,7 @@ public void fileChanged(FileEvent event) {
6970

7071
}
7172

72-
private boolean applyRulesToFile(FileEvent event) {
73+
private boolean isCandidateForProcessing(FileEvent event) {
7374
FileObject file = event.getFile();
7475

7576
boolean applyRules = false;

src/main/java/com/welovecoding/nbeditorconfig/processor/EditorConfigProcessor.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818
import java.util.logging.Logger;
1919
import java.util.prefs.BackingStoreException;
2020
import java.util.prefs.Preferences;
21-
import javax.swing.SwingUtilities;
2221
import org.netbeans.api.project.Project;
2322
import org.netbeans.modules.editor.indent.spi.CodeStylePreferences;
2423
import org.openide.cookies.EditorCookie;
2524
import org.openide.filesystems.FileObject;
2625
import org.openide.filesystems.FileSystem.AtomicAction;
2726
import org.openide.filesystems.FileUtil;
2827
import org.openide.loaders.DataObject;
29-
import org.openide.text.NbDocument;
3028
import org.openide.util.Lookup;
3129
import org.openide.util.Utilities;
3230

@@ -67,8 +65,10 @@ public EditorConfigProcessor() {
6765
* @param dataObject Object that represents the file which was recognized by
6866
* an EditorConfig rule
6967
*/
70-
public synchronized void applyRulesToFile(DataObject dataObject) {
68+
public void applyRulesToFile(DataObject dataObject) {
69+
LOG.log(Level.INFO, "Getting Primary File");
7170
FileObject primaryFile = dataObject.getPrimaryFile();
71+
LOG.log(Level.INFO, "Getting Path");
7272
filePath = primaryFile.getPath();
7373

7474
LOG.log(Level.INFO, "Apply rules to file: {0} (MIME type: {1})",
@@ -102,6 +102,8 @@ public synchronized void applyRulesToFile(DataObject dataObject) {
102102
LOG.log(Level.INFO, "Flush file changes for: {0}", filePath);
103103
flushFile(info);
104104
}
105+
LOG.log(Level.INFO, "Flush style changes for: {0}", filePath);
106+
flushStyles(info);
105107
}
106108

107109
protected FileInfo excuteOperations(DataObject dataObject, MappedEditorConfig config) {
@@ -246,21 +248,11 @@ private MappedEditorConfig readRulesForFile(String filePath) {
246248
private void updateChangesInEditorWindow(final FileInfo info) {
247249
LOG.log(Level.INFO, "Update changes in Editor window for: {0}", info.getPath());
248250

249-
final EditorCookie cookie = info.getCookie();
250-
Runnable runner = () -> {
251-
NbDocument.runAtomic(cookie.getDocument(), () -> {
252251
try {
253252
FileUtil.runAtomicAction((AtomicAction) new WriteEditorAction(info));
254253
} catch (IOException ex) {
255254
LOG.log(Level.SEVERE, ex.getMessage());
256255
}
257-
});
258-
};
259-
if (SwingUtilities.isEventDispatchThread()) {
260-
runner.run();
261-
} else {
262-
SwingUtilities.invokeLater(runner);
263-
}
264256
}
265257

266258
/**

src/main/java/com/welovecoding/nbeditorconfig/processor/FileInfo.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.nio.charset.Charset;
55
import java.util.logging.Level;
66
import java.util.logging.Logger;
7+
import javax.swing.JEditorPane;
78
import javax.swing.SwingUtilities;
89
import javax.swing.text.Caret;
910
import org.openide.cookies.EditorCookie;
@@ -43,7 +44,12 @@ public FileInfo(DataObject dataObject) {
4344
public Caret getCaret() {
4445
Runnable runner = () -> {
4546
NbDocument.runAtomic(cookie.getDocument(), () -> {
46-
currentCaret = cookie.getOpenedPanes()[0].getCaret();
47+
JEditorPane pane = cookie.getOpenedPanes()[0];
48+
if (pane != null) {
49+
currentCaret = pane.getCaret();
50+
} else {
51+
LOG.log(Level.SEVERE, "Could not get JEditorPane for Document");
52+
}
4753
});
4854
};
4955
if (SwingUtilities.isEventDispatchThread()) {

src/main/java/com/welovecoding/nbeditorconfig/processor/WriteEditorAction.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.welovecoding.nbeditorconfig.processor;
22

3-
import com.welovecoding.nbeditorconfig.io.exception.FileAccessException;
43
import com.welovecoding.nbeditorconfig.io.writer.StyledDocumentWriter;
54
import java.util.logging.Level;
65
import java.util.logging.Logger;
@@ -24,7 +23,7 @@ public WriteEditorAction(FileInfo info) {
2423
public void run() {
2524
try {
2625
StyledDocumentWriter.writeWithEditorKit(info);
27-
} catch (FileAccessException ex) {
26+
} catch (Exception ex) {
2827
LOG.log(Level.SEVERE, ex.getMessage());
2928
}
3029
}

0 commit comments

Comments
 (0)