Skip to content

Commit

Permalink
feat(loc): add position for c#
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Mar 24, 2022
1 parent a47b668 commit 591de72
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand Down

0 comments on commit 591de72

Please # to comment.