Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,29 @@
import com.intellij.java.language.psi.JavaDirectoryService;
import com.intellij.java.language.psi.PsiJavaPackage;
import consulo.annotation.access.RequiredReadAction;
import consulo.application.ReadAction;
import consulo.compiler.Compiler;
import consulo.compiler.CompilerManager;
import consulo.compiler.action.CompileActionBase;
import consulo.compiler.localize.CompilerLocalize;
import consulo.compiler.resourceCompiler.ResourceCompiler;
import consulo.compiler.scope.FileSetCompileScope;
import consulo.compiler.scope.ModuleCompileScope;
import consulo.dataContext.DataContext;
import consulo.java.compiler.localize.JavaCompilerLocalize;
import consulo.language.editor.LangDataKeys;
import consulo.language.file.FileTypeManager;
import consulo.language.psi.PsiDirectory;
import consulo.language.psi.PsiElement;
import consulo.language.psi.PsiManager;
import consulo.localize.LocalizeValue;
import consulo.module.Module;
import consulo.module.content.ProjectFileIndex;
import consulo.module.content.ProjectRootManager;
import consulo.platform.base.localize.ActionLocalize;
import consulo.project.Project;
import consulo.ui.annotation.RequiredUIAccess;
import consulo.ui.ex.action.ActionPlaces;
import consulo.ui.ex.action.AnActionEvent;
import consulo.ui.ex.action.Presentation;
import consulo.util.collection.ContainerUtil;
import consulo.util.lang.StringUtil;
import consulo.util.lang.function.Condition;
import consulo.virtualFileSystem.VirtualFile;
import consulo.virtualFileSystem.fileType.FileType;
import jakarta.annotation.Nonnull;
Expand All @@ -55,164 +52,159 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;

public class ProcessAnnotationsAction extends CompileActionBase {
@RequiredUIAccess
@Override
protected void doAction(DataContext dataContext, Project project) {
final Module module = dataContext.getData(LangDataKeys.MODULE_CONTEXT);
final Condition<Compiler> filter = compiler -> {
// EclipseLink CanonicalModelProcessor reads input from output hence adding ResourcesCompiler
return compiler instanceof AnnotationProcessingCompiler || compiler instanceof ResourceCompiler;
};
if (module != null) {
CompilerManager.getInstance(project).make(new ModuleCompileScope(module, false, true), filter, null);
} else {
final FileSetCompileScope scope = getCompilableFiles(project, dataContext.getData(VirtualFile.KEY_OF_ARRAY));
if (scope != null) {
CompilerManager.getInstance(project).make(scope, filter, null);
}
public ProcessAnnotationsAction() {
super(JavaCompilerLocalize.actionRunAptText(), JavaCompilerLocalize.actionRunAptDescription(), null);
}
}

@RequiredUIAccess
@Override
public void update(@Nonnull AnActionEvent event) {
super.update(event);
Presentation presentation = event.getPresentation();
if (!presentation.isEnabled()) {
return;
}
presentation.setVisible(false);

Project project = event.getData(Project.KEY);
if (project == null) {
presentation.setEnabled(false);
return;
@Override
@RequiredUIAccess
protected void doAction(DataContext dataContext, Project project) {
Module module = dataContext.getData(LangDataKeys.MODULE_CONTEXT);
Predicate<Compiler> filter = compiler -> {
// EclipseLink CanonicalModelProcessor reads input from output hence adding ResourcesCompiler
return compiler instanceof AnnotationProcessingCompiler || compiler instanceof ResourceCompiler;
};
if (module != null) {
CompilerManager.getInstance(project).make(new ModuleCompileScope(module, false, true), filter, null);
}
else {
FileSetCompileScope scope = getCompilableFiles(project, dataContext.getData(VirtualFile.KEY_OF_ARRAY));
if (scope != null) {
CompilerManager.getInstance(project).make(scope, filter, null);
}
}
}

final JavaCompilerConfiguration compilerConfiguration = JavaCompilerConfiguration.getInstance(project);
@Override
public void update(@Nonnull AnActionEvent event) {
super.update(event);
Presentation presentation = event.getPresentation();
if (!presentation.isEnabled()) {
return;
}
presentation.setVisible(false);

final Module module = event.getData(Module.KEY);
final Module moduleContext = event.getData(LangDataKeys.MODULE_CONTEXT);
Project project = event.getData(Project.KEY);
if (project == null) {
presentation.setEnabled(false);
return;
}

if (module == null) {
presentation.setEnabled(false);
return;
}
final AnnotationProcessingConfiguration profile = compilerConfiguration.getAnnotationProcessingConfiguration(module);
if (!profile.isEnabled() || (!profile.isObtainProcessorsFromClasspath() && profile.getProcessors().isEmpty())) {
presentation.setEnabled(false);
return;
}
JavaCompilerConfiguration compilerConfiguration = JavaCompilerConfiguration.getInstance(project);

presentation.setVisible(true);
presentation.setTextValue(createPresentationText(""));
final FileSetCompileScope scope = getCompilableFiles(project, event.getData(VirtualFile.KEY_OF_ARRAY));
if (moduleContext == null && scope == null) {
presentation.setEnabled(false);
return;
}
Module module = event.getData(Module.KEY);
Module moduleContext = event.getData(LangDataKeys.MODULE_CONTEXT);

LocalizeValue elementDescription = LocalizeValue.empty();
if (moduleContext != null) {
elementDescription = CompilerLocalize.actionCompileDescriptionModule(moduleContext.getName());
} else {
PsiJavaPackage aPackage = null;
final Collection<VirtualFile> files = scope.getRootFiles();
if (files.size() == 1) {
final PsiDirectory directory = PsiManager.getInstance(project).findDirectory(files.iterator().next());
if (directory != null) {
aPackage = JavaDirectoryService.getInstance().getPackage(directory);
if (module == null) {
presentation.setEnabled(false);
return;
}
} else {
PsiElement element = event.getData(PsiElement.KEY);
if (element instanceof PsiJavaPackage javaPackage) {
aPackage = javaPackage;
AnnotationProcessingConfiguration profile =
ReadAction.compute(() -> compilerConfiguration.getAnnotationProcessingConfiguration(module));
if (!profile.isEnabled() || (!profile.isObtainProcessorsFromClasspath() && profile.getProcessors().isEmpty())) {
presentation.setEnabled(false);
return;
}
}

if (aPackage != null) {
String name = aPackage.getQualifiedName();
if (name.isEmpty()) {
//noinspection HardCodedStringLiteral
name = "<default>";
}
elementDescription = LocalizeValue.localizeTODO("'" + name + "'");
} else if (files.size() == 1) {
final VirtualFile file = files.iterator().next();
FileType fileType = file.getFileType();
if (CompilerManager.getInstance(project).isCompilableFileType(fileType)) {
elementDescription = LocalizeValue.localizeTODO("'" + file.getName() + "'");
} else {
if (!ActionPlaces.MAIN_MENU.equals(event.getPlace())) {
// the action should be invisible in popups for non-java files
presentation.setEnabled(true);
presentation.setVisible(true);
presentation.setTextValue(JavaCompilerLocalize.actionRunAptText());

FileSetCompileScope scope = ReadAction.compute(() -> getCompilableFiles(project, event.getData(VirtualFile.KEY_OF_ARRAY)));
if (moduleContext == null && scope == null) {
presentation.setEnabled(false);
presentation.setVisible(false);
return;
}
}
} else {
elementDescription = CompilerLocalize.actionCompileDescriptionSelectedFiles();
}
}

if (elementDescription == null) {
presentation.setEnabled(false);
return;
}

presentation.setTextValue(createPresentationText(elementDescription.get()));
presentation.setEnabled(true);
}
if (moduleContext != null) {
presentation.setTextValue(JavaCompilerLocalize.actionRunAptModuleText(trimName(moduleContext.getName())));
}
else {
PsiJavaPackage aPackage = null;
Collection<VirtualFile> files = scope.getRootFiles();
if (files.size() == 1) {
PsiDirectory directory = ReadAction.compute(() -> PsiManager.getInstance(project).findDirectory(files.iterator().next()));
if (directory != null) {
aPackage = JavaDirectoryService.getInstance().getPackage(directory);
}
}
else {
PsiElement element = event.getData(PsiElement.KEY);
if (element instanceof PsiJavaPackage javaPackage) {
aPackage = javaPackage;
}
}

private static LocalizeValue createPresentationText(final String elementDescription) {
int length = elementDescription.length();
String target = length > 23
? (StringUtil.startsWithChar(elementDescription, '\'') ? "'..." : "...") +
elementDescription.substring(length - 20, length)
: elementDescription;
return StringUtil.isEmpty(target)
? ActionLocalize.actionRunaptText()
: ActionLocalize.actionRunapt1Text(target);
}
if (aPackage != null) {
String name = aPackage.getQualifiedName();
presentation.setTextValue(
StringUtil.isNotEmpty(name)
? JavaCompilerLocalize.actionRunApt0Text(trimName(name))
: JavaCompilerLocalize.actionRunAptDefaultText()
);
}
else if (files.size() == 1) {
VirtualFile file = files.iterator().next();
FileType fileType = file.getFileType();
if (CompilerManager.getInstance(project).isCompilableFileType(fileType)) {
presentation.setTextValue(JavaCompilerLocalize.actionRunApt0Text(trimName(file.getName())));
}
else {
presentation.setEnabled(false);
// the action should be invisible in popups for non-java files
presentation.setVisible(ActionPlaces.MAIN_MENU.equals(event.getPlace()));
}
}
else {
presentation.setTextValue(JavaCompilerLocalize.actionRunAptSelectedFilesText());
}
}
}

@Nullable
@RequiredReadAction
private static FileSetCompileScope getCompilableFiles(Project project, VirtualFile[] files) {
if (files == null || files.length == 0) {
return null;
private static String trimName(String name) {
int length = name.length();
return length > 23 ? '…' + name.substring(length - 20, length) : name;
}
final PsiManager psiManager = PsiManager.getInstance(project);
final FileTypeManager typeManager = FileTypeManager.getInstance();
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
final CompilerManager compilerManager = CompilerManager.getInstance(project);
final List<VirtualFile> filesToCompile = new ArrayList<>();
final List<Module> affectedModules = new ArrayList<>();
for (final VirtualFile file : files) {
if (!fileIndex.isInSourceContent(file)) {
continue;
}
if (!file.isInLocalFileSystem()) {
continue;
}
if (file.isDirectory()) {
final PsiDirectory directory = psiManager.findDirectory(file);
if (directory == null || JavaDirectoryService.getInstance().getPackage(directory) == null) {
continue;

@Nullable
@RequiredReadAction
private static FileSetCompileScope getCompilableFiles(Project project, VirtualFile[] files) {
if (files == null || files.length == 0) {
return null;
}
} else {
FileType fileType = file.getFileType();
if (!(compilerManager.isCompilableFileType(fileType))) {
continue;
PsiManager psiManager = PsiManager.getInstance(project);
ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
CompilerManager compilerManager = CompilerManager.getInstance(project);
List<VirtualFile> filesToCompile = new ArrayList<>();
List<Module> affectedModules = new ArrayList<>();
for (VirtualFile file : files) {
if (!fileIndex.isInSourceContent(file)) {
continue;
}
if (!file.isInLocalFileSystem()) {
continue;
}
if (file.isDirectory()) {
PsiDirectory directory = psiManager.findDirectory(file);
if (directory == null || JavaDirectoryService.getInstance().getPackage(directory) == null) {
continue;
}
}
else {
FileType fileType = file.getFileType();
if (!(compilerManager.isCompilableFileType(fileType))) {
continue;
}
}
filesToCompile.add(file);
ContainerUtil.addIfNotNull(affectedModules, fileIndex.getModuleForFile(file));
}
}
filesToCompile.add(file);
ContainerUtil.addIfNotNull(affectedModules, fileIndex.getModuleForFile(file));
}
if (filesToCompile.isEmpty()) {
return null;
if (filesToCompile.isEmpty()) {
return null;
}
return new FileSetCompileScope(filesToCompile, affectedModules.toArray(new Module[affectedModules.size()]));
}
return new FileSetCompileScope(filesToCompile, affectedModules.toArray(new Module[affectedModules.size()]));
}
}
Loading
Loading