@@ -7,28 +7,31 @@ import chapi.domain.core.CodeDataStruct
7
7
import chapi.domain.core.CodeField
8
8
import chapi.domain.core.CodeImport
9
9
import chapi.domain.core.CodeProperty
10
+ import java.io.File
11
+
12
+ private const val PRIMARY_CONSTRUCTOR = " PrimaryConstructor"
10
13
11
14
/* *
12
- * listen to full identifier with complex type and sceneries, such as:
15
+ * listen to full identifier with a complex type and sceneries, such as:
13
16
* - call relationship
14
17
* - lambda expression
15
18
* - coroutine
16
19
*/
17
- open class KotlinFullIdentListener (fileName : String ) : KotlinBasicIdentListener(fileName) {
20
+ open class KotlinFullIdentListener (val fileName : String ) : KotlinBasicIdentListener(fileName) {
18
21
19
22
private val postClassHandler = mutableListOf< (CodeDataStruct ) -> Unit > ()
20
- private var VARIABLE_POOL : MutableMap <String , String > = mutableMapOf ()
23
+ private var variablePool : MutableMap <String , String > = mutableMapOf ()
21
24
22
25
override fun enterPropertyDeclaration (ctx : KotlinParser .PropertyDeclarationContext ? ) {
23
26
if (ctx!! .variableDeclaration() != null ) {
24
27
val varDecl = ctx.variableDeclaration()
25
28
val key = varDecl.simpleIdentifier().text
26
29
if (ctx.ASSIGNMENT () != null ) {
27
30
if (ctx.propertyDelegate() != null ) {
28
- VARIABLE_POOL [key] = ctx.propertyDelegate().text
31
+ variablePool [key] = ctx.propertyDelegate().text
29
32
}
30
33
if (ctx.expression() != null ) {
31
- VARIABLE_POOL [key] = ctx.expression().text
34
+ variablePool [key] = ctx.expression().text
32
35
}
33
36
}
34
37
@@ -58,16 +61,16 @@ open class KotlinFullIdentListener(fileName: String) : KotlinBasicIdentListener(
58
61
// if a argument/parameter type is not a string and not a number, it could be a variable
59
62
// so, try to load value from VARIABLE_POOL
60
63
if (typeType == " " ) {
61
- if (VARIABLE_POOL [value] != null ) {
62
- value = VARIABLE_POOL [value].toString()
64
+ if (variablePool [value] != null ) {
65
+ value = variablePool [value].toString()
63
66
64
67
// value contains with String
65
68
val matches = VAR_IN_STRING .find(value)
66
69
matches?.groups?.forEach {
67
70
if (it?.value != null ) {
68
71
val variable = it.value
69
72
val withoutVariable = variable.removePrefix(" $" )
70
- val pool = VARIABLE_POOL [withoutVariable]
73
+ val pool = variablePool [withoutVariable]
71
74
if (pool != null ) {
72
75
var poolText = pool
73
76
if (poolText.startsWith(" \" " ) && poolText.endsWith(" \" " )) {
@@ -85,6 +88,8 @@ open class KotlinFullIdentListener(fileName: String) : KotlinBasicIdentListener(
85
88
return CodeProperty (TypeType = typeType, TypeValue = value)
86
89
}
87
90
91
+ val testDir = listOf (" src" , " test" , " kotlin" ).joinToString(File .separator)
92
+
88
93
// correct the function info
89
94
private fun CodeCall.refineIfExistsCreator (): CodeCall {
90
95
// search the node in declared classes (for individual function)
@@ -109,12 +114,14 @@ open class KotlinFullIdentListener(fileName: String) : KotlinBasicIdentListener(
109
114
110
115
// return null to quick exit
111
116
private fun CodeCall.refineWithClass (it : CodeDataStruct ): CodeCall ? {
112
- if (FunctionName .isNotEmpty() && it.NodeName == FunctionName && FunctionName .first().isUpperCase()) {
113
- Type = CallType .CREATOR
114
- Package = it.Package
115
- NodeName = it.NodeName
116
- this .FunctionName = " PrimaryConstructor"
117
- return null
117
+ if (FunctionName [0 ].isUpperCase()) {
118
+ if (it.NodeName == FunctionName ) {
119
+ Type = CallType .CREATOR
120
+ Package = it.Package
121
+ NodeName = it.NodeName
122
+ this .FunctionName = PRIMARY_CONSTRUCTOR
123
+ return null
124
+ }
118
125
}
119
126
120
127
return this
@@ -126,10 +133,13 @@ open class KotlinFullIdentListener(fileName: String) : KotlinBasicIdentListener(
126
133
return null
127
134
}
128
135
136
+ // for Kotlin, if the function name is same as the import name, it's a creator
129
137
if (it.AsName == FunctionName && FunctionName [0 ].isUpperCase()) {
130
138
Package = it.Source
131
139
NodeName = FunctionName
132
- return null ;
140
+ FunctionName = PRIMARY_CONSTRUCTOR
141
+ Type = CallType .CREATOR
142
+ return null
133
143
}
134
144
135
145
if (it.AsName == FunctionName ) {
0 commit comments