ability to provide a hyperlink link by generated source filter

GitOrigin-RevId: 5ffa10ac1772d7d36feb053779092c24cc420f2f
This commit is contained in:
Vladimir Krivosheev
2024-07-25 16:04:18 +02:00
committed by intellij-monorepo-bot
parent bb0a02eda6
commit 3bd32f51f1
2 changed files with 36 additions and 14 deletions

View File

@@ -5,8 +5,10 @@ import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.extensions.ExtensionPointName import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.openapi.project.Project import com.intellij.openapi.project.Project
import com.intellij.openapi.util.NlsContexts import com.intellij.openapi.util.NlsContexts
import com.intellij.openapi.util.NlsSafe
import com.intellij.openapi.vfs.VirtualFile import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import org.jetbrains.annotations.ApiStatus
abstract class GeneratedSourcesFilter { abstract class GeneratedSourcesFilter {
companion object { companion object {
@@ -37,4 +39,19 @@ abstract class GeneratedSourcesFilter {
* @return a text to be shown in the editor notification panel or `null` for the default text * @return a text to be shown in the editor notification panel or `null` for the default text
*/ */
open fun getNotificationText(file: VirtualFile, project: Project): @NlsContexts.LinkLabel String? = null open fun getNotificationText(file: VirtualFile, project: Project): @NlsContexts.LinkLabel String? = null
@ApiStatus.Experimental
@ApiStatus.Internal
open fun getNotification(file: VirtualFile, project: Project): @NlsContexts.LinkLabel GeneratedSourceFilterNotification? {
return GeneratedSourceFilterNotification(text = getNotificationText(file, project) ?: return null, actions = emptyList())
}
} }
@ApiStatus.Experimental
@ApiStatus.Internal
data class GeneratedSourceFilterNotification(@NlsSafe val text: String, val actions: List<GeneratedSourceFilterHyperLinkAction>)
@ApiStatus.Experimental
@ApiStatus.Internal
data class GeneratedSourceFilterHyperLinkAction(@NlsSafe val text: String, val link: String)

View File

@@ -5,8 +5,8 @@ import com.intellij.lang.LangBundle
import com.intellij.openapi.fileEditor.FileEditor import com.intellij.openapi.fileEditor.FileEditor
import com.intellij.openapi.project.DumbAware import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.Project import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.GeneratedSourceFilterNotification
import com.intellij.openapi.roots.GeneratedSourcesFilter import com.intellij.openapi.roots.GeneratedSourcesFilter
import com.intellij.openapi.util.NlsContexts
import com.intellij.openapi.vfs.VirtualFile import com.intellij.openapi.vfs.VirtualFile
import com.intellij.ui.EditorNotificationPanel import com.intellij.ui.EditorNotificationPanel
import com.intellij.ui.EditorNotificationProvider import com.intellij.ui.EditorNotificationProvider
@@ -16,32 +16,37 @@ import javax.swing.JComponent
internal class GeneratedFileEditingNotificationProvider : EditorNotificationProvider, DumbAware { internal class GeneratedFileEditingNotificationProvider : EditorNotificationProvider, DumbAware {
override fun collectNotificationData( override fun collectNotificationData(
project: Project, project: Project,
file: VirtualFile file: VirtualFile,
): Function<in FileEditor, out JComponent?>? { ): Function<in FileEditor, out JComponent?>? {
if (!GeneratedSourceFileChangeTracker.getInstance(project).isEditedGeneratedFile(file)) { if (!GeneratedSourceFileChangeTracker.getInstance(project).isEditedGeneratedFile(file)) {
return null return null
} }
val notificationText = getText(file, project) val notification = getNotification(file, project)
return Function { fileEditor -> return Function { fileEditor ->
val panel = EditorNotificationPanel(fileEditor, EditorNotificationPanel.Status.Warning) val panel = EditorNotificationPanel(fileEditor, EditorNotificationPanel.Status.Warning)
panel.text = notificationText panel.text = notification.text
for (action in notification.actions) {
panel.createActionLabel(action.text) {
BrowserUtil.browse(action.link)
}
}
panel panel
} }
} }
} }
private fun getText(file: VirtualFile, project: Project): @NlsContexts.LinkLabel String { private fun getNotification(file: VirtualFile, project: Project): GeneratedSourceFilterNotification {
if (project.isDisposed || !file.isValid) return LangBundle.message("link.label.generated.source.files") if (!project.isDisposed && file.isValid) {
for (filter in GeneratedSourcesFilter.EP_NAME.extensionList) { for (filter in GeneratedSourcesFilter.EP_NAME.extensionList) {
if (!filter.isGeneratedSource(file, project)) { if (!filter.isGeneratedSource(file, project)) {
continue continue
} }
val text = filter.getNotificationText(file, project) filter.getNotification(file, project)?.let {
if (text != null) { return it
return text }
} }
} }
return LangBundle.message("link.label.generated.source.files") return GeneratedSourceFilterNotification(text = LangBundle.message("link.label.generated.source.files"), actions = emptyList())
} }