Created
October 29, 2012 03:00
-
-
Save masanobuimai/3971240 to your computer and use it in GitHub Desktop.
FileStatusBarPluginをカスタマイズして,使っている改行コードを表示するようにしたもの。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.siyeh.filestatusbar; | |
import com.intellij.openapi.components.ProjectComponent; | |
import com.intellij.openapi.fileEditor.FileEditorManager; | |
import com.intellij.openapi.fileEditor.FileEditorManagerEvent; | |
import com.intellij.openapi.fileEditor.FileEditorManagerListener; | |
import com.intellij.openapi.project.Project; | |
import com.intellij.openapi.util.io.FileUtil; | |
import com.intellij.openapi.vfs.VirtualFile; | |
import com.intellij.openapi.wm.StatusBar; | |
import com.intellij.openapi.wm.WindowManager; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.Reader; | |
public class FileStatusBarPlugin implements ProjectComponent, FileEditorManagerListener { | |
private final Project project; | |
public FileStatusBarPlugin(Project project) { | |
super(); | |
this.project = project; | |
} | |
public void projectOpened() { | |
final FileEditorManager fileEditorManager = | |
FileEditorManager.getInstance(project); | |
fileEditorManager.addFileEditorManagerListener(this); | |
} | |
public void projectClosed() { | |
final FileEditorManager fileEditorManager = | |
FileEditorManager.getInstance(project); | |
fileEditorManager.removeFileEditorManagerListener(this); | |
} | |
public String getComponentName() { | |
return "FileStatusBar"; | |
} | |
public void initComponent() { | |
} | |
public void disposeComponent() { | |
} | |
public void fileOpened(FileEditorManager fileEditorManager, VirtualFile virtualFile) { | |
populateStatusBar(virtualFile); | |
} | |
public void fileClosed(FileEditorManager fileEditorManager, VirtualFile virtualFile) { | |
} | |
public void selectionChanged(FileEditorManagerEvent event) { | |
final VirtualFile virtualFile = event.getNewFile(); | |
populateStatusBar(virtualFile); | |
} | |
private void populateStatusBar(VirtualFile virtualFile) { | |
final WindowManager windowManager = WindowManager.getInstance(); | |
final StatusBar statusBar = windowManager.getStatusBar(project); | |
if (virtualFile == null) { | |
statusBar.setInfo(""); | |
} | |
else { | |
final String path = virtualFile.getPath(); | |
final File file = new File(path); | |
final String fileName = file.getName(); | |
if (file.exists()) { | |
final long length = file.length(); | |
final boolean isBinary = virtualFile.getFileType().isBinary(); | |
final String linebreak = isBinary ? "binary" | |
: getLineBreak(file, virtualFile.getCharset().displayName()); | |
String output = String.format("%s, %,d byte(s), [%s], %tY-%<tm-%<td %<tH:%<tM:%<tS", fileName, length, linebreak, file.lastModified()); | |
statusBar.setInfo(output); | |
} | |
else { | |
statusBar.setInfo(""); | |
} | |
} | |
} | |
private String getLineBreak(File file, String enc) { | |
String lineBreak = "???"; | |
try { | |
Reader reader = new InputStreamReader(new FileInputStream(file), enc); | |
// とりあえず 1024byte 読み込んで改行コードをチェックする | |
char[] chars = FileUtil.loadText(reader, 1024); | |
reader.close(); | |
for (int i = 0; i < chars.length; i++) { | |
if ('\n' == chars[i]) { | |
lineBreak = "unix"; | |
break; | |
} | |
else if ('\r' == chars[i]) { | |
if (i + 1 < chars.length && '\n' == chars[i + 1]) { | |
lineBreak = "windows"; | |
break; | |
} | |
else { | |
lineBreak = "mac"; | |
break; | |
} | |
} | |
} | |
} | |
catch (IOException ignore) {} | |
return lineBreak; | |
} | |
} |
Author
masanobuimai
commented
Oct 29, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment