refactor GeneratedSourcesFilter and GeneratedFileEditingNotificationProvider

GitOrigin-RevId: 87a1b24680dc64e073e3becd8ac5317a0f54438d
This commit is contained in:
Vladimir Krivosheev
2024-07-25 15:41:04 +02:00
committed by intellij-monorepo-bot
parent 194a81a881
commit bb0a02eda6
3 changed files with 68 additions and 77 deletions

View File

@@ -3278,12 +3278,15 @@ f:com.intellij.openapi.fileEditor.ex.FileEditorWithProvider
com.intellij.openapi.fileEditor.ex.StructureViewFileEditorProvider
- a:getStructureViewBuilder(com.intellij.openapi.project.Project,com.intellij.openapi.vfs.VirtualFile):com.intellij.ide.structureView.StructureViewBuilder
a:com.intellij.openapi.roots.GeneratedSourcesFilter
- sf:Companion:com.intellij.openapi.roots.GeneratedSourcesFilter$Companion
- sf:EP_NAME:com.intellij.openapi.extensions.ExtensionPointName
- <init>():V
- getNotificationText(com.intellij.openapi.vfs.VirtualFile,com.intellij.openapi.project.Project):java.lang.String
- getOriginalElements(com.intellij.psi.PsiElement):java.util.List
- a:isGeneratedSource(com.intellij.openapi.vfs.VirtualFile,com.intellij.openapi.project.Project):Z
- s:isGeneratedSourceByAnyFilter(com.intellij.openapi.vfs.VirtualFile,com.intellij.openapi.project.Project):Z
- sf:isGeneratedSourceByAnyFilter(com.intellij.openapi.vfs.VirtualFile,com.intellij.openapi.project.Project):Z
f:com.intellij.openapi.roots.GeneratedSourcesFilter$Companion
- f:isGeneratedSourceByAnyFilter(com.intellij.openapi.vfs.VirtualFile,com.intellij.openapi.project.Project):Z
com.intellij.openapi.ui.InputValidatorEx
- com.intellij.openapi.ui.InputValidator
- canClose(java.lang.String):Z

View File

@@ -1,34 +1,27 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.openapi.roots;
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.openapi.roots
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.NlsContexts
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiElement
import java.util.Collections;
import java.util.List;
abstract class GeneratedSourcesFilter {
companion object {
@JvmField
val EP_NAME: ExtensionPointName<GeneratedSourcesFilter> = ExtensionPointName("com.intellij.generatedSourcesFilter")
public abstract class GeneratedSourcesFilter {
public static final ExtensionPointName<GeneratedSourcesFilter> EP_NAME = ExtensionPointName.create("com.intellij.generatedSourcesFilter");
public static boolean isGeneratedSourceByAnyFilter(@NotNull VirtualFile file, @NotNull Project project) {
return ReadAction.compute(() -> {
if (project.isDisposed() || !file.isValid()) return false;
for (GeneratedSourcesFilter filter : EP_NAME.getExtensionList()) {
if (filter.isGeneratedSource(file, project)) {
return true;
}
@JvmStatic
fun isGeneratedSourceByAnyFilter(file: VirtualFile, project: Project): Boolean {
return ApplicationManager.getApplication().runReadAction<Boolean, RuntimeException> {
!project.isDisposed && file.isValid && EP_NAME.extensionList.any { it.isGeneratedSource(file, project) }
}
return false;
});
}
}
public abstract boolean isGeneratedSource(@NotNull VirtualFile file, @NotNull Project project);
abstract fun isGeneratedSource(file: VirtualFile, project: Project): Boolean
/**
* Returns all elements that have been processed by a code generator to derive the given element.
@@ -36,16 +29,12 @@ public abstract class GeneratedSourcesFilter {
* @param element the generated element
* @return a list of original elements. An empty result indicates that the element is not considered to be generated by the filter.
*/
public @NotNull List<? extends PsiElement> getOriginalElements(@NotNull PsiElement element) {
return Collections.emptyList();
}
open fun getOriginalElements(element: PsiElement): List<PsiElement> = emptyList()
/**
* The method is called only if {@link #isGeneratedSource} returns {@code true}.
* The method is called only if [.isGeneratedSource] returns `true`.
*
* @return a text to be shown in the editor notification panel or {@code null} for the default text
* @return a text to be shown in the editor notification panel or `null` for the default text
*/
public @NlsContexts.LinkLabel @Nullable String getNotificationText(@NotNull VirtualFile file, @NotNull Project project) {
return null;
}
open fun getNotificationText(file: VirtualFile, project: Project): @NlsContexts.LinkLabel String? = null
}

View File

@@ -1,48 +1,47 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide;
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.ide
import com.intellij.lang.LangBundle;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.GeneratedSourcesFilter;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.EditorNotificationPanel;
import com.intellij.ui.EditorNotificationProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.intellij.lang.LangBundle
import com.intellij.openapi.fileEditor.FileEditor
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.GeneratedSourcesFilter
import com.intellij.openapi.util.NlsContexts
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.ui.EditorNotificationPanel
import com.intellij.ui.EditorNotificationProvider
import java.util.function.Function
import javax.swing.JComponent
import javax.swing.*;
import java.util.function.Function;
final class GeneratedFileEditingNotificationProvider implements EditorNotificationProvider, DumbAware {
@Override
public @Nullable Function<? super @NotNull FileEditor, ? extends @Nullable JComponent> collectNotificationData(@NotNull Project project,
@NotNull VirtualFile file) {
if (!GeneratedSourceFileChangeTracker.getInstance(project).isEditedGeneratedFile(file)) return null;
String notificationText = getText(file, project);
return fileEditor -> {
EditorNotificationPanel panel = new EditorNotificationPanel(fileEditor, EditorNotificationPanel.Status.Warning);
panel.setText(notificationText);
return panel;
};
}
private static @NlsContexts.LinkLabel @NotNull String getText(@NotNull VirtualFile file, @NotNull Project project) {
if (!project.isDisposed() && file.isValid()) {
for (GeneratedSourcesFilter filter : GeneratedSourcesFilter.EP_NAME.getExtensionList()) {
if (!filter.isGeneratedSource(file, project)) {
continue;
}
String text = filter.getNotificationText(file, project);
if (text != null) {
return text;
}
}
internal class GeneratedFileEditingNotificationProvider : EditorNotificationProvider, DumbAware {
override fun collectNotificationData(
project: Project,
file: VirtualFile
): Function<in FileEditor, out JComponent?>? {
if (!GeneratedSourceFileChangeTracker.getInstance(project).isEditedGeneratedFile(file)) {
return null
}
val notificationText = getText(file, project)
return Function { fileEditor ->
val panel = EditorNotificationPanel(fileEditor, EditorNotificationPanel.Status.Warning)
panel.text = notificationText
panel
}
return LangBundle.message("link.label.generated.source.files");
}
}
private fun getText(file: VirtualFile, project: Project): @NlsContexts.LinkLabel String {
if (project.isDisposed || !file.isValid) return LangBundle.message("link.label.generated.source.files")
for (filter in GeneratedSourcesFilter.EP_NAME.extensionList) {
if (!filter.isGeneratedSource(file, project)) {
continue
}
val text = filter.getNotificationText(file, project)
if (text != null) {
return text
}
}
return LangBundle.message("link.label.generated.source.files")
}