Skip to content

Commit 6e70eac

Browse files
committed
feat(rust): make pass for first function calls
1 parent 2503fc9 commit 6e70eac

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

chapi-ast-rust/src/main/kotlin/chapi/ast/rustast/RustFullIdentListener.kt

+13-7
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ class RustFullIdentListener(val fileName: String) : RustAstBaseListener(fileName
2727
}
2828
}
2929
}
30-
31-
println("localVars: $localVars")
3230
}
3331

3432
override fun enterCallExpression(ctx: RustParser.CallExpressionContext?) {
@@ -43,17 +41,25 @@ class RustFullIdentListener(val fileName: String) : RustAstBaseListener(fileName
4341
}
4442

4543
override fun enterMethodCallExpression(ctx: RustParser.MethodCallExpressionContext?) {
46-
val functionName = ctx?.expression()?.text
47-
val split = functionName?.split("::")
44+
val instanceVar = ctx?.expression()?.text
45+
val nodeName = localVars.getOrDefault(instanceVar ?: "", instanceVar)
46+
47+
val functionName = lookupFunctionName(ctx?.pathExprSegment())
48+
4849
// todo: handle method call
4950
functionInstance.FunctionCalls += CodeCall(
50-
Package = split?.dropLast(1)?.joinToString("::") ?: "",
51-
NodeName = split?.dropLast(1)?.joinToString("::") ?: "",
52-
FunctionName = split?.last() ?: "",
51+
Package = nodeName ?: "",
52+
NodeName = nodeName ?: "",
53+
OriginNodeName = instanceVar ?: "",
54+
FunctionName = functionName,
5355
Parameters = buildParameters(ctx?.callParams()),
5456
)
5557
}
5658

59+
private fun lookupFunctionName(pathExprSegmentContext: RustParser.PathExprSegmentContext?): String {
60+
return pathExprSegmentContext?.pathIdentSegment()?.identifier()?.text ?: ""
61+
}
62+
5763
private fun buildParameters(callParamsContext: RustParser.CallParamsContext?) = callParamsContext?.expression()?.map {
5864
CodeProperty(
5965
TypeValue = it.text,

chapi-ast-rust/src/test/kotlin/chapi/ast/rustast/RustFullIdentListenerTest.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ class RustFullIdentListenerTest {
201201

202202
val codeContainer = RustAnalyser().analysis(code, "test.rs")
203203
val codeDataStruct = codeContainer.DataStructures[0]
204-
assertEquals(1, codeDataStruct.Functions.size)
205204
val functionCalls = codeDataStruct.Functions[0].FunctionCalls
205+
206206
assertEquals(1, functionCalls.size)
207207
assertEquals("Point", functionCalls[0].NodeName)
208208
assertEquals("new", functionCalls[0].FunctionName)
@@ -221,8 +221,11 @@ class RustFullIdentListenerTest {
221221

222222
val codeContainer = RustAnalyser().analysis(code, "test.rs")
223223
val codeDataStruct = codeContainer.DataStructures[0]
224-
assertEquals(1, codeDataStruct.Functions.size)
225224
val functionCalls = codeDataStruct.Functions[0].FunctionCalls
226225
assertEquals(2, functionCalls.size)
226+
227+
assertEquals("crate::Point", functionCalls[1].NodeName)
228+
assertEquals("print", functionCalls[1].FunctionName)
229+
assertEquals("p", functionCalls[1].OriginNodeName)
227230
}
228231
}

chapi-domain/src/main/kotlin/chapi/domain/core/CodeCall.kt

+14-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ enum class CallType(val calltype: String) {
77
FIELD("field"),
88
LAMBDA("lambda"),
99
ARROW("arrow"),
10+
1011
// for Kotlin,
1112
CREATOR("creator"),
1213
FUNCTION("function"),
@@ -22,17 +23,24 @@ enum class CallType(val calltype: String) {
2223
@Serializable
2324
data class CodeCall(
2425
var Package: String = "",
25-
// for Java, it can be CreatorClass, lambda
26-
// for TypeScript, can be anonymous function, arrow function
26+
/**
27+
* for Java, it can be CreatorClass, lambda
28+
* for TypeScript, can be anonymous function, arrow function
29+
*/
2730
var Type: CallType = CallType.FUNCTION,
28-
// for Class/DataStruct, it's ClassName
29-
// for Function, it's empty
31+
/**
32+
* for Class/DataStruct, it's ClassName
33+
* for Function, it's empty
34+
*/
3035
var NodeName: String = "",
3136
var FunctionName: String = "",
3237
var Parameters: List<CodeProperty> = listOf(),
3338
var Position: CodePosition = CodePosition(),
34-
// like "v1.Group", the v1 will be the Receiver
35-
// since 2.0.0-Beta.9
39+
/**
40+
* like "v1.Group", the v1 will be the OriginNodeName
41+
*
42+
* @since 2.0.0-Beta.9
43+
*/
3644
var OriginNodeName: String = "",
3745
) {
3846

0 commit comments

Comments
 (0)