diff --git a/java-compiler-impl/src/main/java/com/intellij/java/compiler/impl/actions/ProcessAnnotationsAction.java b/java-compiler-impl/src/main/java/com/intellij/java/compiler/impl/actions/ProcessAnnotationsAction.java index c63ca1e5e9..a0c9e38cab 100644 --- a/java-compiler-impl/src/main/java/com/intellij/java/compiler/impl/actions/ProcessAnnotationsAction.java +++ b/java-compiler-impl/src/main/java/com/intellij/java/compiler/impl/actions/ProcessAnnotationsAction.java @@ -21,24 +21,22 @@ 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; @@ -46,7 +44,6 @@ 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; @@ -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 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 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 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 = ""; - } - 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 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 filesToCompile = new ArrayList<>(); - final List 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 filesToCompile = new ArrayList<>(); + List 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()])); - } } \ No newline at end of file diff --git a/java-compiler-impl/src/main/resources/LOCALIZE-LIB/en_US/consulo.java.compiler.JavaCompilerLocalize.yaml b/java-compiler-impl/src/main/resources/LOCALIZE-LIB/en_US/consulo.java.compiler.JavaCompilerLocalize.yaml index 5bc032a7c8..1575b028aa 100644 --- a/java-compiler-impl/src/main/resources/LOCALIZE-LIB/en_US/consulo.java.compiler.JavaCompilerLocalize.yaml +++ b/java-compiler-impl/src/main/resources/LOCALIZE-LIB/en_US/consulo.java.compiler.JavaCompilerLocalize.yaml @@ -1,42 +1,54 @@ +action.run.apt.0.text: + text: Process ''{0}'' _Annotations +action.run.apt.default.text: + text: Process _Annotations +action.run.apt.description: + text: Run Annotation Processors on the selected scope +action.run.apt.module.text: + text: Process Module ''{0}'' _Annotations +action.run.apt.selected.files.text: + text: Process Selected Files _Annotations +action.run.apt.text: + text: Process _Annotations add.notnull.assertions: - text: Add @NotNull &assertions -eclipse.compiler.error.jar.not.found: - text: |- - Cannot start Eclipse compiler. - The file {0} not found. + text: Add @NotNull &assertions +compiler.eclipse.embedded.name: + text: Eclipse in-process compiler.eclipse.name: - text: Eclipse -eclipse.options.group.title: - text: Eclipse Options + text: Eclipse +compiler.javac.name: + text: Javac +eclipse.compiler.analyzing: + text: Analyzing {0} +eclipse.compiler.error.jar.not.found: + text: |- + Cannot start Eclipse compiler. + The file {0} not found. eclipse.compiler.parsing: - text: Parsing {0} + text: Parsing {0} eclipse.compiler.reading: - text: Reading classes -eclipse.compiler.analyzing: - text: Analyzing {0} -compiler.eclipse.embedded.name: - text: Eclipse in-process -generate.ant.build.title: - text: Generate Ant Build + text: Reading classes +eclipse.options.group.title: + text: Eclipse Options generate.ant.build.progress.message: - text: Generating ant build... -compiler.javac.name: - text: Javac + text: Generating ant build... +generate.ant.build.title: + text: Generate Ant Build javac.error.jdk.home.missing: - text: |- - Cannot find home directory ''{1}'' for JDK ''{0}''. - Update JDK configuration. + text: |- + Cannot find home directory ''{1}'' for JDK ''{0}''. + Update JDK configuration. javac.error.jdk.is.not.set.for.module: - text: |- - JDK is not set for module ''{0}''. - Update JDK configuration. + text: |- + JDK is not set for module ''{0}''. + Update JDK configuration. +javac.error.target.jdk.not.supported: + text: Compilation is not supported for JDK lower 1.5 javac.error.tools.jar.missing: - text: |- - Cannot obtain path javac classes for JDK {0}. - Update JDK configuration. + text: |- + Cannot obtain path javac classes for JDK {0}. + Update JDK configuration. javac.error.unknown.jdk.version: - text: |- - Cannot determine version for JDK {0}. - Update JDK configuration. -javac.error.target.jdk.not.supported: - text: Compilation is not supported for JDK lower 1.5 + text: |- + Cannot determine version for JDK {0}. + Update JDK configuration.