Skip to content

Commit 489cd80

Browse files
committed
feat: add rules param.name, param.type
1 parent 0cb6bb6 commit 489cd80

19 files changed

+296
-106
lines changed

idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/core/ClassExportRuleKeys.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ object ClassExportRuleKeys {
9999
BooleanRuleMode.ANY
100100
)
101101

102+
val PARAM_NAME: RuleKey<String> = SimpleRuleKey(
103+
"param.name",
104+
StringRuleMode.SINGLE
105+
)
106+
107+
val PARAM_TYPE: RuleKey<String> = SimpleRuleKey(
108+
"param.type",
109+
StringRuleMode.SINGLE
110+
)
111+
102112
val PARAM_DEFAULT_VALUE: RuleKey<String> = SimpleRuleKey(
103113
"param.default.value",
104114
StringRuleMode.MERGE_DISTINCT

idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/core/ExportContext.kt

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import com.itangcent.intellij.jvm.element.ExplicitParameter
1313
import kotlin.reflect.KClass
1414

1515
interface ExportContext : Extensible {
16+
/**
17+
* the parent context, allowing navigation up the context hierarchy.
18+
*/
1619
fun parent(): ExportContext?
1720

1821
/**
@@ -23,22 +26,45 @@ interface ExportContext : Extensible {
2326
fun psi(): PsiElement
2427
}
2528

29+
/**
30+
* Extends ExportContext for contexts dealing with variables (methods or parameters).
31+
*/
2632
interface VariableExportContext : ExportContext {
2733

34+
/**
35+
* the name of the variable.
36+
*/
2837
fun name(): String
2938

39+
/**
40+
* the type of the variable, which may be null if the type is not resolved.
41+
*/
3042
fun type(): DuckType?
3143

44+
/**
45+
* the explicit element representation of the variable.
46+
*/
3247
fun element(): ExplicitElement<*>
48+
49+
/**
50+
* Sets a resolved name for the variable, typically used for renaming.
51+
*/
52+
fun setResolvedName(name: String)
3353
}
3454

3555
//region kits of ExportContext
3656

57+
/**
58+
* find specific contexts by type.
59+
*/
3760
@Suppress("UNCHECKED_CAST")
3861
fun <T : ExportContext> ExportContext.findContext(condition: KClass<T>): T? {
3962
return findContext { condition.isInstance(it) } as? T
4063
}
4164

65+
/**
66+
* find specific contexts by condition.
67+
*/
4268
fun ExportContext.findContext(condition: (ExportContext) -> Boolean): ExportContext? {
4369
var exportContext: ExportContext? = this
4470
while (exportContext != null) {
@@ -50,39 +76,58 @@ fun ExportContext.findContext(condition: (ExportContext) -> Boolean): ExportCont
5076
return null
5177
}
5278

53-
fun <T> ExportContext.findExt(attr: String): T? {
54-
var exportContext: ExportContext? = this
55-
while (exportContext != null) {
56-
exportContext.getExt<T>(attr)?.let { return it }
57-
exportContext = exportContext.parent()
58-
}
59-
return null
60-
}
61-
6279
//endregion
6380

81+
/**
82+
* Base context with no parent, typically used for top-level classes.
83+
*/
6484
abstract class RootExportContext :
6585
SimpleExtensible(), ExportContext {
6686
override fun parent(): ExportContext? {
6787
return null
6888
}
6989
}
7090

91+
/**
92+
* General purpose context implementation with a specified parent context.
93+
*/
7194
abstract class AbstractExportContext(private val parent: ExportContext) :
72-
SimpleExtensible(), ExportContext {
95+
SimpleExtensible(), VariableExportContext {
96+
97+
private var resolvedName: String? = null
98+
7399
override fun parent(): ExportContext? {
74100
return this.parent
75101
}
102+
103+
/**
104+
* Returns the name of the element.
105+
*
106+
* @return the element name.
107+
*/
108+
override fun name(): String {
109+
return resolvedName ?: element().name()
110+
}
111+
112+
override fun setResolvedName(name: String) {
113+
this.resolvedName = name
114+
}
76115
}
77116

117+
/**
118+
* Context specifically for a class
119+
*/
78120
class ClassExportContext(val cls: PsiClass) : RootExportContext() {
79121
override fun psi(): PsiClass {
80122
return cls
81123
}
82124
}
83125

126+
/**
127+
* Context for a method, containing specifics about the method being exported.
128+
*/
84129
class MethodExportContext(
85-
private val parent: ExportContext,
130+
parent: ExportContext,
86131
private val method: ExplicitMethod
87132
) : AbstractExportContext(parent), VariableExportContext {
88133

@@ -113,19 +158,27 @@ class MethodExportContext(
113158
}
114159
}
115160

116-
class ParameterExportContext(
117-
private val parent: ExportContext,
118-
private val parameter: ExplicitParameter
119-
) : AbstractExportContext(parent), VariableExportContext {
161+
/**
162+
* Context for a parameter, containing specifics about the parameter being exported.
163+
*/
164+
interface ParameterExportContext : VariableExportContext {
120165

121-
/**
122-
* Returns the name of the element.
123-
*
124-
* @return the element name.
125-
*/
126-
override fun name(): String {
127-
return parameter.name()
128-
}
166+
override fun element(): ExplicitParameter
167+
168+
override fun psi(): PsiParameter
169+
}
170+
171+
fun ParameterExportContext(
172+
parent: ExportContext,
173+
parameter: ExplicitParameter
174+
): ParameterExportContext {
175+
return ParameterExportContextImpl(parent, parameter)
176+
}
177+
178+
class ParameterExportContextImpl(
179+
parent: ExportContext,
180+
private val parameter: ExplicitParameter
181+
) : AbstractExportContext(parent), ParameterExportContext {
129182

130183
/**
131184
* Returns the type of the variable.
@@ -145,18 +198,30 @@ class ParameterExportContext(
145198
}
146199
}
147200

201+
/**
202+
* retrieve ClassExportContext based on the current context.
203+
*/
148204
fun ExportContext.classContext(): ClassExportContext? {
149205
return this.findContext(ClassExportContext::class)
150206
}
151207

208+
/**
209+
* retrieve MethodExportContext based on the current context.
210+
*/
152211
fun ExportContext.methodContext(): MethodExportContext? {
153212
return this.findContext(MethodExportContext::class)
154213
}
155214

215+
/**
216+
* retrieve ParameterExportContext based on the current context.
217+
*/
156218
fun ExportContext.paramContext(): ParameterExportContext? {
157219
return this.findContext(ParameterExportContext::class)
158220
}
159221

222+
/**
223+
* Searches for an extended property, first locally then up the context hierarchy.
224+
*/
160225
fun <T> ExportContext.searchExt(attr: String): T? {
161226
this.getExt<T>(attr)?.let { return it }
162227
this.parent()?.searchExt<T>(attr)?.let { return it }

0 commit comments

Comments
 (0)