Created
August 17, 2012 06:31
-
-
Save siosio/3376430 to your computer and use it in GitHub Desktop.
package-info.javaを一括で作るプラグインのコード
This file contains 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 siosio.createpackageinfo; | |
import com.intellij.openapi.actionSystem.AnAction; | |
import com.intellij.openapi.actionSystem.AnActionEvent; | |
import com.intellij.openapi.actionSystem.DataContext; | |
import com.intellij.openapi.actionSystem.PlatformDataKeys; | |
import com.intellij.openapi.application.ApplicationManager; | |
import com.intellij.openapi.project.Project; | |
import com.intellij.openapi.roots.ProjectFileIndex; | |
import com.intellij.openapi.roots.ProjectRootManager; | |
import com.intellij.openapi.vfs.VirtualFile; | |
import com.intellij.psi.JavaDirectoryService; | |
import com.intellij.psi.JavaPsiFacade; | |
import com.intellij.psi.PsiDirectory; | |
import com.intellij.psi.PsiElement; | |
import com.intellij.psi.PsiElementFactory; | |
import com.intellij.psi.PsiFile; | |
import com.intellij.psi.PsiManager; | |
import com.intellij.psi.PsiPackage; | |
import com.intellij.psi.PsiPackageStatement; | |
import com.intellij.psi.javadoc.PsiDocComment; | |
public class CreatePackageInfoAction extends AnAction { | |
/** テンプレート */ | |
private static final String TEMPLATE = "/**\n" | |
+ " * #package#パッケージ。\n" | |
+ " *\n" | |
+ " * <pre>\n" | |
+ " * // TODO パッケージ内容の詳細を記述してください\n" | |
+ " * </pre>\n" | |
+ " * \n" | |
+ " */"; | |
/** ファイル名 */ | |
private static final String PACKAGE_INFO_FILE_NAME = "package-info.java"; | |
/** {@inheritDoc} */ | |
public void actionPerformed(AnActionEvent e) { | |
Project project = e.getProject(); | |
if (project == null) { | |
return; | |
} | |
ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex(); | |
PsiManager instance = PsiManager.getInstance(project); | |
// 選択されているファイルをリスト | |
DataContext context = e.getDataContext(); | |
VirtualFile[] files = PlatformDataKeys.VIRTUAL_FILE_ARRAY.getData(context); | |
if (files == null) { | |
return; | |
} | |
for (VirtualFile file : files) { | |
// ソース配下以外のディレクトリは対象外 | |
if (!fileIndex.isInSource(file) || !file.isDirectory()) { | |
continue; | |
} | |
PsiDirectory directory = instance.findDirectory(file); | |
if (directory == null) { | |
continue; | |
} | |
// package-info.javaの作成 | |
createPackageInfo(project, directory); | |
} | |
} | |
@Override | |
public void update(AnActionEvent e) { | |
Project project = e.getProject(); | |
if (project == null) { | |
return; | |
} | |
ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex(); | |
PsiManager instance = PsiManager.getInstance(project); | |
// 選択されているファイルをリスト | |
DataContext context = e.getDataContext(); | |
VirtualFile[] files = PlatformDataKeys.VIRTUAL_FILE_ARRAY.getData(context); | |
if (files == null) { | |
return; | |
} | |
for (VirtualFile file : files) { | |
// ソース配下以外のディレクトリは対象外 | |
if (fileIndex.isInSource(file) && file.isDirectory()) { | |
e.getPresentation().setEnabled(true); | |
return; | |
} | |
} | |
e.getPresentation().setEnabled(false); | |
} | |
private void createPackageInfo(final Project project, final PsiDirectory directory) { | |
if (directory.findFile("package-info.java") != null) { | |
return; | |
} | |
PsiPackage psiPackage = JavaDirectoryService.getInstance().getPackage(directory); | |
if (psiPackage == null) { | |
return; | |
} | |
final String packageText = psiPackage.getQualifiedName(); | |
if (!packageText.isEmpty()) { | |
ApplicationManager.getApplication().runWriteAction(new Runnable() { | |
@Override | |
public void run() { | |
createPackageInfoFile(project, directory, packageText); | |
} | |
}); | |
} | |
for (PsiDirectory subDirectory : directory.getSubdirectories()) { | |
createPackageInfo(project, subDirectory); | |
} | |
} | |
private void createPackageInfoFile(Project project, PsiDirectory directory, String packageText) { | |
// package-info.javaを作成 | |
PsiFile packageInfo = directory.createFile(PACKAGE_INFO_FILE_NAME); | |
PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project); | |
// JavaDoc要素を追加 | |
PsiDocComment comment = elementFactory.createDocCommentFromText(TEMPLATE.replaceAll("#package#", packageText)); | |
// package宣言を追加 | |
PsiPackageStatement statement = elementFactory.createPackageStatement(packageText); | |
// package-info.javaに生成した要素を追加 | |
PsiElement addElement = packageInfo.add(statement); | |
packageInfo.addBefore(comment, addElement); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment