1
+ /* ******************************************************************************
2
+ * Copyright (c) 2025 Red Hat, Inc.
3
+ * Distributed under license by Red Hat, Inc. All rights reserved.
4
+ * This program is made available under the terms of the
5
+ * Eclipse Public License v2.0 which accompanies this distribution,
6
+ * and is available at http://www.eclipse.org/legal/epl-v20.html
7
+ *
8
+ * Contributors:
9
+ * Red Hat, Inc. - initial API and implementation
10
+ ******************************************************************************/
11
+ package com.redhat.devtools.intellij.kubernetes.editor.inlay.selector
12
+
13
+ import com.intellij.psi.PsiElement
14
+ import com.redhat.devtools.intellij.common.validation.KubernetesTypeInfo
15
+ import com.redhat.devtools.intellij.kubernetes.editor.util.areMatchingMatchExpressions
16
+ import com.redhat.devtools.intellij.kubernetes.editor.util.areMatchingMatchLabels
17
+ import com.redhat.devtools.intellij.kubernetes.editor.util.getKubernetesTypeInfo
18
+ import com.redhat.devtools.intellij.kubernetes.editor.util.getLabels
19
+ import com.redhat.devtools.intellij.kubernetes.editor.util.getTemplate
20
+ import com.redhat.devtools.intellij.kubernetes.editor.util.hasMatchExpressions
21
+ import com.redhat.devtools.intellij.kubernetes.editor.util.hasMatchLabels
22
+ import com.redhat.devtools.intellij.kubernetes.editor.util.hasSelector
23
+ import com.redhat.devtools.intellij.kubernetes.editor.util.isCronJob
24
+ import com.redhat.devtools.intellij.kubernetes.editor.util.isDaemonSet
25
+ import com.redhat.devtools.intellij.kubernetes.editor.util.isDeployment
26
+ import com.redhat.devtools.intellij.kubernetes.editor.util.isJob
27
+ import com.redhat.devtools.intellij.kubernetes.editor.util.isNetworkPolicy
28
+ import com.redhat.devtools.intellij.kubernetes.editor.util.isPersistentVolume
29
+ import com.redhat.devtools.intellij.kubernetes.editor.util.isPersistentVolumeClaim
30
+ import com.redhat.devtools.intellij.kubernetes.editor.util.isPod
31
+ import com.redhat.devtools.intellij.kubernetes.editor.util.isPodDisruptionBudget
32
+ import com.redhat.devtools.intellij.kubernetes.editor.util.isReplicaSet
33
+ import com.redhat.devtools.intellij.kubernetes.editor.util.isService
34
+ import com.redhat.devtools.intellij.kubernetes.editor.util.isStatefulSet
35
+
36
+ class SelectorFilter (private val selectingElement : PsiElement ) {
37
+
38
+ private val selectingElementType: KubernetesTypeInfo ? by lazy {
39
+ selectingElement.getKubernetesTypeInfo()
40
+ }
41
+
42
+ private val hasSelector: Boolean by lazy {
43
+ selectingElement.hasSelector()
44
+ }
45
+
46
+ private val hasMatchLabels: Boolean by lazy {
47
+ selectingElement.hasMatchLabels()
48
+ }
49
+
50
+ private val hasMatchExpressions: Boolean by lazy {
51
+ selectingElement.hasMatchExpressions()
52
+ }
53
+
54
+ fun filterMatching (toMatch : Collection <PsiElement >): Collection <PsiElement > {
55
+ if (! hasSelector) {
56
+ return emptyList()
57
+ }
58
+ return toMatch
59
+ .filter(::isMatching)
60
+ }
61
+
62
+ fun isMatching (element : PsiElement ): Boolean {
63
+ if (selectingElementType == null ) {
64
+ return false
65
+ }
66
+
67
+ val selectableType = element.getKubernetesTypeInfo() ? : return false
68
+ if (! isSelectable(selectableType)) {
69
+ return false
70
+ }
71
+
72
+ val labels = getLabels(selectableType, element, selectingElementType) ? : return false
73
+
74
+ return when {
75
+ hasMatchLabels && hasMatchExpressions ->
76
+ selectingElement.areMatchingMatchLabels(labels)
77
+ && selectingElement.areMatchingMatchExpressions(labels)
78
+
79
+ hasMatchLabels ->
80
+ selectingElement.areMatchingMatchLabels(labels)
81
+
82
+ hasMatchExpressions ->
83
+ selectingElement.areMatchingMatchExpressions(labels)
84
+
85
+ else -> false
86
+ }
87
+ }
88
+
89
+ private fun isSelectable (selectableType : KubernetesTypeInfo ): Boolean {
90
+ val selectingElementType = this .selectingElementType ? : return false
91
+ return when {
92
+ selectingElementType.isDeployment() ->
93
+ selectableType.isPod()
94
+ || selectableType.isDeployment() // can select deployment template
95
+
96
+ selectingElementType.isCronJob() ->
97
+ selectableType.isPod()
98
+ || selectableType.isCronJob() // template
99
+
100
+ selectingElementType.isDaemonSet() ->
101
+ selectableType.isPod()
102
+ || selectableType.isDaemonSet() // template
103
+
104
+ selectingElementType.isJob() ->
105
+ selectableType.isPod()
106
+ || selectableType.isJob() // template
107
+
108
+ selectingElementType.isReplicaSet() ->
109
+ selectableType.isPod()
110
+ || selectableType.isReplicaSet() // template
111
+
112
+ selectingElementType.isStatefulSet() ->
113
+ selectableType.isPod()
114
+ || selectableType.isStatefulSet() // template
115
+
116
+ selectingElementType.isNetworkPolicy()
117
+ || selectingElementType.isPodDisruptionBudget()
118
+ || selectingElementType.isService() ->
119
+ selectableType.isPod()
120
+
121
+ selectingElementType.isPersistentVolumeClaim() ->
122
+ selectableType.isPersistentVolume()
123
+ || selectableType.isPersistentVolumeClaim()
124
+
125
+ else ->
126
+ false
127
+ }
128
+ }
129
+
130
+ private fun getLabels (
131
+ selectableType : KubernetesTypeInfo ,
132
+ selectableElement : PsiElement ,
133
+ selectingElementType : KubernetesTypeInfo ?
134
+ ): PsiElement ? {
135
+ return when {
136
+ selectingElementType == null ->
137
+ null
138
+
139
+ (selectingElementType.isCronJob()
140
+ && selectableType.isCronJob())
141
+ || (selectingElementType.isDaemonSet()
142
+ && selectableType.isDaemonSet())
143
+ || (selectingElementType.isDeployment()
144
+ && selectableType.isDeployment())
145
+ || (selectingElementType.isJob()
146
+ && selectableType.isJob())
147
+ || (selectingElementType.isReplicaSet()
148
+ && selectableType.isReplicaSet())
149
+ || (selectingElementType.isStatefulSet()
150
+ && selectableType.isStatefulSet()) ->
151
+ selectableElement.getTemplate()
152
+
153
+ else ->
154
+ selectableElement.getLabels()
155
+ }
156
+ }
157
+ }
0 commit comments