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

Commit ec5a5ba

Browse files
committed
Merge pull request #69 from welovecoding/issue-68-cyclic
Fixes #68
2 parents 4128056 + 7f8de7d commit ec5a5ba

22 files changed

+155
-56
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.welovecoding.nbeditorconfig.config;
2+
3+
import java.util.logging.Level;
4+
5+
public class LoggerSettings {
6+
7+
public static final Level OPERATION_LOG_LEVEL = Level.INFO;
8+
public static final Level PROCESSOR_LOG_LEVEL = Level.INFO;
9+
public static final Level LISTENER_LOG_LEVEL = Level.INFO;
10+
public static final Level MAPPER_LOG_LEVEL = Level.INFO;
11+
12+
private LoggerSettings() {
13+
}
14+
15+
}

src/main/java/com/welovecoding/nbeditorconfig/config/Settings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public final class Settings {
66

77
public static final String MIME_TYPE = "text/x-editorconfig"; // NOI18N
88
public static final String EXTENSION = "editorconfig"; // NOI18N
9+
public static final String DEFAULT_FILE_NAME = ".editorconfig"; // NOI18N
910
public static final String RESOURCE_PATH = "com/welovecoding/nbeditorconfig"; // NOI18N
1011

1112
@StaticResource

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

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

3+
import static com.welovecoding.nbeditorconfig.config.LoggerSettings.LISTENER_LOG_LEVEL;
34
import java.util.Collections;
45
import java.util.logging.Level;
56
import java.util.logging.Logger;
@@ -24,7 +25,7 @@ public class EditorConfigChangeListener extends FileChangeAdapter {
2425
private final FileChangeListener subsequentFilesListener;
2526

2627
static {
27-
LOG.setLevel(Level.INFO);
28+
LOG.setLevel(LISTENER_LOG_LEVEL);
2829
}
2930

3031
public EditorConfigChangeListener(Project project, FileObject editorConfigFileObject) {

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.welovecoding.nbeditorconfig.listener;
22

3+
import static com.welovecoding.nbeditorconfig.config.LoggerSettings.LISTENER_LOG_LEVEL;
34
import com.welovecoding.nbeditorconfig.processor.EditorConfigProcessor;
45
import com.welovecoding.nbeditorconfig.processor.SmartSkip;
6+
import com.welovecoding.nbeditorconfig.processor.WriteEditorAction;
7+
import com.welovecoding.nbeditorconfig.processor.WriteStringToFileAction;
58
import java.util.logging.Level;
69
import java.util.logging.Logger;
710
import org.netbeans.api.project.Project;
@@ -24,7 +27,7 @@ public class FileChangeListener extends FileChangeAdapter {
2427
private final FileObject editorConfigFileObject;
2528

2629
static {
27-
LOG.setLevel(Level.INFO);
30+
LOG.setLevel(LISTENER_LOG_LEVEL);
2831
}
2932

3033
public FileChangeListener(Project project, FileObject editorConfigFileObject) {
@@ -47,17 +50,22 @@ public void fileChanged(FileEvent event) {
4750

4851
LOG.log(Level.INFO, "[EC for {0}] File content changed: {1}", new Object[]{editorConfigFileObject.getPath(), path});
4952

50-
if (applyRulesToFile(event)) {
51-
try {
52-
new EditorConfigProcessor().applyRulesToFile(DataObject.find(event.getFile()));
53-
} catch (DataObjectNotFoundException ex) {
54-
Exceptions.printStackTrace(ex);
55-
} catch (Exception ex) {
56-
Exceptions.printStackTrace(ex);
53+
if (!event.firedFrom(new WriteEditorAction()) && !event.firedFrom(new WriteStringToFileAction())) {
54+
if (applyRulesToFile(event)) {
55+
try {
56+
new EditorConfigProcessor().applyRulesToFile(DataObject.find(event.getFile()));
57+
} catch (DataObjectNotFoundException ex) {
58+
Exceptions.printStackTrace(ex);
59+
} catch (Exception ex) {
60+
Exceptions.printStackTrace(ex);
61+
}
62+
} else {
63+
LOG.log(Level.INFO, "[EC for {0}] Rules will not be applied to: {1}", new Object[]{editorConfigFileObject.getPath(), path});
5764
}
5865
} else {
59-
LOG.log(Level.INFO, "[EC for {0}] Rules will not be applied to: {1}", new Object[]{editorConfigFileObject.getPath(), path});
66+
LOG.log(Level.INFO, "[EC for {0}] Rules will not be applied to: {1} - Change triggered by EditorConfig plugin", new Object[]{editorConfigFileObject.getPath(), path});
6067
}
68+
6169
}
6270

6371
private boolean applyRulesToFile(FileEvent event) {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.welovecoding.nbeditorconfig.listener;
22

3-
import com.welovecoding.nbeditorconfig.config.Settings;
3+
import static com.welovecoding.nbeditorconfig.config.LoggerSettings.LISTENER_LOG_LEVEL;
4+
import static com.welovecoding.nbeditorconfig.config.Settings.DEFAULT_FILE_NAME;
5+
import static com.welovecoding.nbeditorconfig.config.Settings.EXTENSION;
46
import com.welovecoding.nbeditorconfig.processor.SmartSkip;
57
import java.util.logging.Level;
68
import java.util.logging.Logger;
@@ -12,7 +14,7 @@ public class ListenerAttacher {
1214
private static final Logger LOG = Logger.getLogger(ListenerAttacher.class.getName());
1315

1416
static {
15-
LOG.setLevel(Level.INFO);
17+
LOG.setLevel(LISTENER_LOG_LEVEL);
1618
}
1719

1820
/**
@@ -35,7 +37,7 @@ public static void attachListeners(FileObject file, Project project) {
3537
}
3638
}
3739
} else {
38-
if (file.getExt().equals(Settings.EXTENSION)) {
40+
if (file.getExt().equals(EXTENSION) || file.getName().equals(DEFAULT_FILE_NAME)) {
3941
file.addFileChangeListener(new EditorConfigChangeListener(project, file));
4042
LOG.log(Level.INFO, "\u00ac Found EditorConfig: {0}", file.getPath());
4143
} else {

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

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

3+
import static com.welovecoding.nbeditorconfig.config.LoggerSettings.LISTENER_LOG_LEVEL;
34
import java.util.logging.Level;
45
import java.util.logging.Logger;
56
import org.netbeans.api.project.Project;
@@ -17,7 +18,7 @@ public class ProjectChangeListener extends FileChangeAdapter {
1718
private final Project project;
1819

1920
static {
20-
LOG.setLevel(Level.INFO);
21+
LOG.setLevel(LISTENER_LOG_LEVEL);
2122
}
2223

2324
public ProjectChangeListener(Project project) {

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

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

3+
import static com.welovecoding.nbeditorconfig.config.LoggerSettings.LISTENER_LOG_LEVEL;
34
import java.util.logging.Level;
45
import java.util.logging.Logger;
56
import org.netbeans.api.project.Project;
@@ -32,7 +33,7 @@ public class ProjectHookLookup implements LookupProvider {
3233
private static final Logger LOG = Logger.getLogger(ProjectHookLookup.class.getName());
3334

3435
static {
35-
LOG.setLevel(Level.INFO);
36+
LOG.setLevel(LISTENER_LOG_LEVEL);
3637
}
3738

3839
@Override

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

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

3+
import static com.welovecoding.nbeditorconfig.config.LoggerSettings.LISTENER_LOG_LEVEL;
34
import java.util.logging.Level;
45
import java.util.logging.Logger;
56
import org.netbeans.api.project.Project;
@@ -12,7 +13,7 @@ public class ProjectOpenCloseListener extends ProjectOpenedHook {
1213
private Project project;
1314

1415
static {
15-
LOG.setLevel(Level.INFO);
16+
LOG.setLevel(LISTENER_LOG_LEVEL);
1617
}
1718

1819
public ProjectOpenCloseListener() {

src/main/java/com/welovecoding/nbeditorconfig/mapper/EditorConfigPropertyMapper.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.welovecoding.nbeditorconfig.mapper;
22

3+
import com.welovecoding.nbeditorconfig.config.LoggerSettings;
34
import com.welovecoding.nbeditorconfig.io.model.MappedCharset;
45
import com.welovecoding.nbeditorconfig.io.model.SupportedCharsets;
5-
import com.welovecoding.nbeditorconfig.model.MappedEditorConfig;
66
import com.welovecoding.nbeditorconfig.model.EditorConfigConstant;
7+
import com.welovecoding.nbeditorconfig.model.MappedEditorConfig;
78
import java.io.File;
89
import java.util.ArrayList;
910
import java.util.HashMap;
@@ -18,6 +19,10 @@ public class EditorConfigPropertyMapper {
1819

1920
private static final Logger LOG = Logger.getLogger(EditorConfigPropertyMapper.class.getName());
2021

22+
static {
23+
LOG.setLevel(LoggerSettings.MAPPER_LOG_LEVEL);
24+
}
25+
2126
public static synchronized MappedEditorConfig createEditorConfig(String filePath) {
2227
return createEditorConfig(filePath, null);
2328
}
@@ -133,12 +138,12 @@ protected static synchronized String getFileMark(String editorConfigCharset) {
133138
*
134139
* @param ecCharset String value for these charsets: "latin1", "utf-8",
135140
* "utf-8-bom", "utf-16be" or "utf-16le"
136-
*
141+
*
137142
* @return the mapped charset
138143
*/
139144
protected static synchronized MappedCharset mapCharset(String ecCharset) {
140145
MappedCharset charset;
141-
146+
142147
if (ecCharset == null) {
143148
return SupportedCharsets.UTF_8;
144149
}

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package com.welovecoding.nbeditorconfig.processor;
22

3-
import com.welovecoding.nbeditorconfig.io.exception.FileAccessException;
3+
import static com.welovecoding.nbeditorconfig.config.LoggerSettings.PROCESSOR_LOG_LEVEL;
44
import com.welovecoding.nbeditorconfig.io.model.MappedCharset;
5-
import com.welovecoding.nbeditorconfig.io.writer.StyledDocumentWriter;
65
import com.welovecoding.nbeditorconfig.mapper.EditorConfigPropertyMapper;
76
import com.welovecoding.nbeditorconfig.model.EditorConfigConstant;
87
import com.welovecoding.nbeditorconfig.model.MappedEditorConfig;
@@ -24,6 +23,8 @@
2423
import org.netbeans.modules.editor.indent.spi.CodeStylePreferences;
2524
import org.openide.cookies.EditorCookie;
2625
import org.openide.filesystems.FileObject;
26+
import org.openide.filesystems.FileSystem.AtomicAction;
27+
import org.openide.filesystems.FileUtil;
2728
import org.openide.loaders.DataObject;
2829
import org.openide.text.NbDocument;
2930
import org.openide.util.Lookup;
@@ -32,11 +33,14 @@
3233
public class EditorConfigProcessor {
3334

3435
private static final Logger LOG = Logger.getLogger(EditorConfigProcessor.class.getName());
35-
public static final Level OPERATION_LOG_LEVEL = Level.INFO;
3636

3737
private String filePath;
3838
private final String projectPath;
3939

40+
static {
41+
LOG.setLevel(PROCESSOR_LOG_LEVEL);
42+
}
43+
4044
public EditorConfigProcessor() {
4145
// Project nbProject = OpenProjects.getDefault().getMainProject();
4246
Lookup lookup = Utilities.actionsGlobalContext();
@@ -248,8 +252,8 @@ private void updateChangesInEditorWindow(final FileInfo info) {
248252
Runnable runner = () -> {
249253
NbDocument.runAtomic(cookie.getDocument(), () -> {
250254
try {
251-
StyledDocumentWriter.writeWithEditorKit(info);
252-
} catch (FileAccessException ex) {
255+
FileUtil.runAtomicAction((AtomicAction) new WriteEditorAction(info));
256+
} catch (IOException ex) {
253257
LOG.log(Level.SEVERE, ex.getMessage());
254258
}
255259
});
@@ -270,7 +274,10 @@ private void updateChangesInFile(FileInfo info) {
270274
LOG.log(Level.INFO, "Write content (with all rules applied) to file: {0}",
271275
info.getFileObject().getPath());
272276

273-
WriteStringToFileTask task = new WriteStringToFileTask(info);
274-
task.run();
277+
try {
278+
FileUtil.runAtomicAction((AtomicAction) new WriteStringToFileAction(info));
279+
} catch (IOException ex) {
280+
LOG.log(Level.SEVERE, ex.getMessage());
281+
}
275282
}
276283
}

0 commit comments

Comments
 (0)