idea java 自动编译_Intellij IDEA Java类不能自动编译

使用Reformat and Compile插件(受Alexandre DuBreuil的Save Actions插件启发):

[https://plugins.jetbrains.com/plugin/8231?pr=idea_ce]

目前我只提供一个jar文件,但这是代码中最重要的部分:

private final static Set documentsToProcess = new HashSet();

private static VirtualFile[] fileToCompile = VirtualFile.EMPTY_ARRAY;

// The plugin extends FileDocumentManagerAdapter.

// beforeDocumentSaving calls reformatAndCompile

private static void reformatAndCompile(

@NotNull final Project project,

@NotNull final Document document,

@NotNull final PsiFile psiFile) {

documentsToProcess.add(document);

if (storage.isEnabled(Action.compileFile) && isDocumentActive(project, document)) {

fileToCompile = isFileCompilable(project, psiFile.getVirtualFile());

}

ApplicationManager.getApplication().invokeLater(new Runnable() {

@Override

public void run() {

if (documentsToProcess.contains(document)) {

documentsToProcess.remove(document);

if (storage.isEnabled(Action.optimizeImports)

|| storage.isEnabled(Action.reformatCode)) {

CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {

@Override

public void run() {

if (storage.isEnabled(Action.optimizeImports)) {

new OptimizeImportsProcessor(project, psiFile)

.run();

}

if (storage.isEnabled(Action.reformatCode)) {

new ReformatCodeProcessor(

project,

psiFile,

null,

ChangeListManager

.getInstance(project)

.getChange(psiFile.getVirtualFile()) != null)

.run();

}

ApplicationManager.getApplication().runWriteAction(new Runnable() {

@Override

public void run() {

CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(psiFile);

}

});

}

});

}

}

if (fileToCompile.length > 0) {

if (documentsToProcess.isEmpty()) {

compileFile(project, fileToCompile);

fileToCompile = VirtualFile.EMPTY_ARRAY;

}

} else if (storage.isEnabled(Action.makeProject)) {

if (documentsToProcess.isEmpty()) {

makeProject(project);

}

} else {

saveFile(project, document, psiFile.getVirtualFile());

}

}

}, project.getDisposed());

}

private static void makeProject(@NotNull final Project project) {

ApplicationManager.getApplication().invokeLater(new Runnable() {

@Override

public void run() {

CompilerManager.getInstance(project).make(null);

}

}, project.getDisposed());

}

private static void compileFile(

@NotNull final Project project,

@NotNull final VirtualFile[] files) {

ApplicationManager.getApplication().invokeLater(new Runnable() {

@Override

public void run() {

CompilerManager.getInstance(project).compile(files, null);

}

}, project.getDisposed());

}

private static void saveFile(

@NotNull final Project project,

@NotNull final Document document,

@NotNull final VirtualFile file) {

ApplicationManager.getApplication().invokeLater(new Runnable() {

@Override

public void run() {

final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();

if (fileDocumentManager.isFileModified(file)) {

fileDocumentManager.saveDocument(document);

}

}

}, project.getDisposed());

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值