From 591de721b59ed6ee4089e984ef696238f12b76c2 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Thu, 24 Mar 2022 23:28:08 +0800 Subject: [PATCH] feat(loc): add position for c# --- .../ast/csharpast/CSharpFullIdentListener.kt | 14 ++++++++++++- .../csharpast/CSharpFullIdentListenerTest.kt | 21 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/chapi-ast-csharp/src/main/kotlin/chapi/ast/csharpast/CSharpFullIdentListener.kt b/chapi-ast-csharp/src/main/kotlin/chapi/ast/csharpast/CSharpFullIdentListener.kt index 23e147af..ea9ab853 100644 --- a/chapi-ast-csharp/src/main/kotlin/chapi/ast/csharpast/CSharpFullIdentListener.kt +++ b/chapi-ast-csharp/src/main/kotlin/chapi/ast/csharpast/CSharpFullIdentListener.kt @@ -5,6 +5,7 @@ import chapi.ast.antlr.CSharpParser.Class_typeContext import chapi.ast.antlr.CSharpParser.Type_declarationContext import chapi.domain.core.* import chapi.infra.Stack +import org.antlr.v4.runtime.ParserRuleContext class CSharpFullIdentListener(val fileName: String) : CSharpAstListener() { private var currentStruct: CodeDataStruct = CodeDataStruct(); @@ -83,7 +84,8 @@ class CSharpFullIdentListener(val fileName: String) : CSharpAstListener() { override fun enterClass_definition(ctx: CSharpParser.Class_definitionContext?) { val className = ctx!!.identifier().text val codeDataStruct = CodeDataStruct( - NodeName = className + NodeName = className, + Position = buildPosition(ctx) ) val classMemberDeclarations = ctx.class_body().class_member_declarations() @@ -295,6 +297,16 @@ class CSharpFullIdentListener(val fileName: String) : CSharpAstListener() { return modifiers } + + fun buildPosition(ctx: ParserRuleContext): CodePosition { + val position = CodePosition() + position.StartLine = ctx.start.line + position.StartLinePosition = ctx.start.charPositionInLine + position.StopLine = ctx.stop.line + position.StopLinePosition = ctx.stop.charPositionInLine + return position + } + fun getNodeInfo(): CodeContainer { return codeContainer } diff --git a/chapi-ast-csharp/src/test/kotlin/chapi/ast/csharpast/CSharpFullIdentListenerTest.kt b/chapi-ast-csharp/src/test/kotlin/chapi/ast/csharpast/CSharpFullIdentListenerTest.kt index 212b81c3..a8d963f0 100644 --- a/chapi-ast-csharp/src/test/kotlin/chapi/ast/csharpast/CSharpFullIdentListenerTest.kt +++ b/chapi-ast-csharp/src/test/kotlin/chapi/ast/csharpast/CSharpFullIdentListenerTest.kt @@ -223,6 +223,27 @@ namespace Chapi { assertEquals(2, structs[0].Functions.size) } + + @Test + fun shouldIdentClassPosition() { + val code = """ +using System; + +namespace Chapi { + public class Chapi { + public Chapi() {} + public Chapi(long id) {} + } +} +""" + val codeContainer = CSharpAnalyser().analysis(code, "ChapiController.cs") + val structs = codeContainer.Containers[0].DataStructures + assertEquals(structs.size, 1) + + assertEquals(structs[0].Position.StartLine, 5) + assertEquals(structs[0].Position.StopLine, 8) + } + @Test fun shouldIdentProperty() { val code = """