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

Commit

Permalink
Fixed issue #43 & cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Yserz committed Mar 23, 2015
1 parent efe999c commit 6223888
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public final class Settings {

public static final String MIME_TYPE = "text/x-editorconfig"; // NOI18N
public static final String EXTENSION = "editorconfig"; // NOI18N
public static final String RESOURCE_PATH = "com/welovecoding/nbeditorconfig"; // NOI18N

@StaticResource
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import org.openide.filesystems.FileRenameEvent;

/**
* http://bits.netbeans.org/dev/javadoc/
* This kind of listener is attached to editorconfig files within a project.
* When this listener is attached to an editorconfig a FileChangeListener will
* be attached to all files in the folder with the editorconfig and subsequent
* ones. http://bits.netbeans.org/dev/javadoc/
*/
public class EditorConfigChangeListener extends FileChangeAdapter {

Expand All @@ -20,40 +23,39 @@ public class EditorConfigChangeListener extends FileChangeAdapter {
private final FileObject editorConfigFileObject;
private final FileChangeListener subsequentFilesListener;

static {
LOG.setLevel(Level.INFO);
}

public EditorConfigChangeListener(Project project, FileObject editorConfigFileObject) {
this.project = project;
this.editorConfigFileObject = editorConfigFileObject;

LOG.log(Level.INFO, "Attached EditorConfigChangeListener to: {0}", editorConfigFileObject.getPath());
this.subsequentFilesListener = new FileChangeListener(project, editorConfigFileObject);
editorConfigFileObject.getParent().addRecursiveListener(subsequentFilesListener);
}

@Override
public void fileAttributeChanged(FileAttributeEvent event) {
super.fileAttributeChanged(event);
LOG.log(Level.INFO, "Attribute changed: {0}", event.getFile().getPath());
}

@Override
public void fileRenamed(FileRenameEvent event) {
super.fileRenamed(event);
LOG.log(Level.INFO, "Renamed file: {0}", event.getFile().getPath());
// immediately apply editorconfig
propagateChanges();
}

@Override
public void fileDeleted(FileEvent event) {
super.fileDeleted(event);
LOG.log(Level.INFO, "Deleted file: {0}", event.getFile().getPath());
//TODO processDeletedEditorConfig
//TODO processDeletedFolderWhichMayContainsFoldersWithListeners -> remove them
event.getFile().getParent().removeRecursiveListener(subsequentFilesListener);
event.getFile().removeFileChangeListener(this);
}

@Override
public void fileChanged(FileEvent event) {
super.fileChanged(event);
LOG.log(Level.INFO, "EditorConfigs content changed: {0}", event.getFile().getPath());

propagateChanges();
}

private void propagateChanges() {
for (FileObject fo : Collections.list(editorConfigFileObject.getParent().getChildren(true))) {
LOG.log(Level.INFO, "Updating subsequent file: {0}", fo.getPath());
subsequentFilesListener.fileChanged(new FileEvent(fo));
Expand All @@ -63,7 +65,7 @@ public void fileChanged(FileEvent event) {
@Override
public void fileFolderCreated(FileEvent event) {
super.fileFolderCreated(event);
LOG.log(Level.INFO, "Created folder: {0}", event.getFile().getPath());
LOG.log(Level.FINE, "Created folder: {0}", event.getFile().getPath());
//TODO search for editor-configs and attach listeners
}

Expand All @@ -76,7 +78,18 @@ public void fileFolderCreated(FileEvent event) {
@Override
public void fileDataCreated(FileEvent event) {
super.fileDataCreated(event);
LOG.log(Level.INFO, "fileDataCreated: {0}", event.getFile().getPath());
LOG.log(Level.FINE, "fileDataCreated: {0}", event.getFile().getPath());
}

@Override
public void fileAttributeChanged(FileAttributeEvent event) {
super.fileAttributeChanged(event);
LOG.log(Level.FINE, "Attribute changed: {0}", event.getFile().getPath());
}

@Override
public void fileRenamed(FileRenameEvent event) {
super.fileRenamed(event);
LOG.log(Level.FINE, "Renamed file: {0}", event.getFile().getPath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,31 @@ public class FileChangeListener extends FileChangeAdapter {

private static final Logger LOG = Logger.getLogger(FileChangeListener.class.getSimpleName());
private final Project project;
private final FileObject editorConfigFileObject;

public FileChangeListener(Project project, FileObject editorConfigFileObject) {
this.project = project;
LOG.log(Level.INFO, "Attached FileChangeListener to: {0}", editorConfigFileObject.getParent().getPath());
static {
LOG.setLevel(Level.INFO);
}

@Override
public void fileAttributeChanged(FileAttributeEvent event) {
super.fileAttributeChanged(event);
LOG.log(Level.INFO, "Attribute changed: {0}", event.getFile().getPath());
}

@Override
public void fileRenamed(FileRenameEvent event) {
super.fileRenamed(event);
LOG.log(Level.INFO, "Renamed file: {0}", event.getFile().getPath());
public FileChangeListener(Project project, FileObject editorConfigFileObject) {
this.project = project;
this.editorConfigFileObject = editorConfigFileObject;
LOG.log(Level.INFO, "[EC for {0}] Attached FileChangeListener to: {1}", new Object[]{editorConfigFileObject.getPath(), editorConfigFileObject.getParent().getPath()});
}

@Override
public void fileDeleted(FileEvent event) {
super.fileDeleted(event);
LOG.log(Level.INFO, "Deleted file: {0}", event.getFile().getPath());
//TODO processDeletedEditorConfig
//TODO processDeletedFolderWhichMayContainsFoldersWithListeners -> remove them
LOG.log(Level.INFO, "[EC for {0}] Deleted file: {1}", new Object[]{editorConfigFileObject.getPath(), event.getFile().getPath()});
event.getFile().removeRecursiveListener(this);
}

@Override
public void fileChanged(FileEvent event) {
super.fileChanged(event);
String path = event.getFile().getPath();

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

if (applyRulesToFile(event)) {
try {
Expand All @@ -63,7 +56,7 @@ public void fileChanged(FileEvent event) {
Exceptions.printStackTrace(ex);
}
} else {
LOG.log(Level.INFO, "Rules will not be applied to: {0}", path);
LOG.log(Level.INFO, "[EC for {0}] Rules will not be applied to: {1}", new Object[]{editorConfigFileObject.getPath(), path});
}
}

Expand All @@ -85,7 +78,7 @@ private boolean applyRulesToFile(FileEvent event) {
@Override
public void fileFolderCreated(FileEvent event) {
super.fileFolderCreated(event);
LOG.log(Level.INFO, "Created folder: {0}", event.getFile().getPath());
LOG.log(Level.FINE, "[EC for {0}] Created folder: {1}", new Object[]{editorConfigFileObject.getPath(), event.getFile().getPath()});
//TODO search for editor-configs and attach listeners
}

Expand All @@ -99,8 +92,19 @@ public void fileFolderCreated(FileEvent event) {
public void fileDataCreated(FileEvent event) {
super.fileDataCreated(event);
FileObject primaryFile = event.getFile();
LOG.log(Level.INFO, "Added new file to project: {0} (MIME type: {1})",
new Object[]{primaryFile.getPath(), primaryFile.getMIMEType()});
LOG.log(Level.FINE, "[EC for {0}] Added new file to project: {1} (MIME type: {2})",
new Object[]{editorConfigFileObject.getPath(), primaryFile.getPath(), primaryFile.getMIMEType()});
}

@Override
public void fileAttributeChanged(FileAttributeEvent event) {
super.fileAttributeChanged(event);
LOG.log(Level.FINE, "[EC for {0}] Attribute changed: {1}", new Object[]{editorConfigFileObject.getPath(), event.getFile().getPath()});
}

@Override
public void fileRenamed(FileRenameEvent event) {
super.fileRenamed(event);
LOG.log(Level.FINE, "[EC for {0}] Renamed file: {1}", new Object[]{editorConfigFileObject.getPath(), event.getFile().getPath()});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.welovecoding.nbeditorconfig.listener;

import com.welovecoding.nbeditorconfig.config.Settings;
import com.welovecoding.nbeditorconfig.processor.SmartSkip;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.netbeans.api.project.Project;
import org.openide.filesystems.FileObject;

/**
*
* @author Michael Koppen
*/
public class ListenerAttacher {

private static final Logger LOG = Logger.getLogger(ListenerAttacher.class.getSimpleName());

static {
LOG.setLevel(Level.INFO);
}

/**
* Recursively attach listeners to folders containing a .editorconfig file.
*
* @param root
* @param project
*/
public static void attachListeners(FileObject file, Project project) {
if (project.getProjectDirectory().equals(file)) {
file.addRecursiveListener(new ProjectChangeListener(project));
}

if (file.isFolder()) {
if (SmartSkip.skipDirectory(file)) {
LOG.log(Level.INFO, "\u00ac Skipped directory: {0}", file.getPath());
} else {
for (FileObject subFile : file.getChildren()) {
attachListeners(subFile, project);
}
}
} else {
if (file.getExt().equals(Settings.EXTENSION)) {
file.addFileChangeListener(new EditorConfigChangeListener(project, file));
LOG.log(Level.INFO, "\u00ac Found EditorConfig: {0}", file.getPath());
} else {
LOG.log(Level.FINE, "\u00ac No EditorConfig Found: {0}", file.getPath());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,55 @@ public class ProjectChangeListener extends FileChangeAdapter {
private final Project project;

static {
LOG.setLevel(Level.OFF);
LOG.setLevel(Level.INFO);
}

public ProjectChangeListener(Project project) {
this.project = project;
}

@Override
public void fileFolderCreated(FileEvent event) {
super.fileFolderCreated(event);
LOG.log(Level.INFO, "PROJECTLISTENER: Created folder: {0}", event.getFile().getPath());
ListenerAttacher.attachListeners(event.getFile(), project);
}

/**
* Method is triggered when content has changed and it's possible to display
* content in NetBeans. Method is also triggered when project will be opened.
*
* @param event Event for listening on filesystem changes
*/
@Override
public void fileDataCreated(FileEvent event) {
super.fileDataCreated(event);
LOG.log(Level.INFO, "PROJECTLISTENER: fileDataCreated: {0}", event.getFile().getPath());
ListenerAttacher.attachListeners(event.getFile(), project);
}

@Override
public void fileAttributeChanged(FileAttributeEvent event) {
super.fileAttributeChanged(event);
LOG.log(Level.INFO, "PROJECTLISTENER: Attribute changed: {0}", event.getFile().getPath());
LOG.log(Level.FINE, "PROJECTLISTENER: Attribute changed: {0}", event.getFile().getPath());
}

@Override
public void fileRenamed(FileRenameEvent event) {
super.fileRenamed(event);
LOG.log(Level.INFO, "PROJECTLISTENER: Renamed file: {0}", event.getFile().getPath());
LOG.log(Level.FINE, "PROJECTLISTENER: Renamed file: {0}", event.getFile().getPath());
}

@Override
public void fileDeleted(FileEvent event) {
super.fileDeleted(event);
LOG.log(Level.INFO, "PROJECTLISTENER: Deleted file: {0}", event.getFile().getPath());
LOG.log(Level.FINE, "PROJECTLISTENER: Deleted file: {0}", event.getFile().getPath());
}

@Override
public void fileChanged(FileEvent event) {
super.fileChanged(event);
LOG.log(Level.INFO, "PROJECTLISTENER: File content changed: {0}", event.getFile().getPath());
}

@Override
public void fileFolderCreated(FileEvent event) {
super.fileFolderCreated(event);
LOG.log(Level.INFO, "PROJECTLISTENER: Created folder: {0}", event.getFile().getPath());
}

/**
* Method is triggered when content has changed and it's possible to display
* content in NetBeans. Method is also triggered when project will be opened.
*
* @param event Event for listening on filesystem changes
*/
@Override
public void fileDataCreated(FileEvent event) {
super.fileDataCreated(event);
LOG.log(Level.INFO, "PROJECTLISTENER: fileDataCreated: {0}", event.getFile().getPath());
LOG.log(Level.FINE, "PROJECTLISTENER: File content changed: {0}", event.getFile().getPath());
}

}
Loading

0 comments on commit 6223888

Please sign in to comment.