Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

feat: enhance ScalaParser — correct NodeKinds, signature, visibility, CONTAINS edges

riley02279c94d555a273
diff --git a/memory-layer/src/main/scala/ix/memory/ingestion/parsers/ScalaParser.scala b/memory-layer/src/main/scala/ix/memory/ingestion/parsers/ScalaParser.scala
index 9d0be11c..2d4db4fa 100644
--- a/memory-layer/src/main/scala/ix/memory/ingestion/parsers/ScalaParser.scala
+++ b/memory-layer/src/main/scala/ix/memory/ingestion/parsers/ScalaParser.scala
@@ -72,7 +72,7 @@ class ScalaParser extends Parser {
val endLine = findBraceBlockEnd(lines, idx).getOrElse(lineNum)
entities = entities :+ ParsedEntity(
name = name,
- kind = NodeKind.Class,
+ kind = NodeKind.Trait,
attrs = Map("scala_kind" -> Json.fromString("trait"), "language" -> Json.fromString("scala")),
lineStart = lineNum,
lineEnd = endLine
@@ -89,7 +89,7 @@ class ScalaParser extends Parser {
val endLine = findBraceBlockEnd(lines, idx).getOrElse(lineNum)
entities = entities :+ ParsedEntity(
name = name,
- kind = NodeKind.Class,
+ kind = NodeKind.Object,
attrs = Map("scala_kind" -> Json.fromString("object"), "language" -> Json.fromString("scala")),
lineStart = lineNum,
lineEnd = endLine
@@ -126,31 +126,33 @@ class ScalaParser extends Parser {
val funcName = m.group(1)
val endLine = findBraceBlockEnd(lines, idx).getOrElse(lineNum)
- // Build summary from the first line of the signature, truncated to 120 chars
- val sigLine = trimmed.take(120)
- val summary = sigLine
+ val signature = extractSignature(trimmed)
+ val visibility = extractVisibility(trimmed)
+
+ // Determine enclosing type
+ val enclosing = typeRanges.find { case (_, start, end) =>
+ lineNum > start && lineNum <= end
+ }
+
+ val (kind, edgePredicate, edgeSrc) = enclosing match {
+ case Some((typeName, _, _)) => (NodeKind.Method, "CONTAINS", typeName)
+ case None => (NodeKind.Function, "DEFINES", fileName)
+ }
entities = entities :+ ParsedEntity(
name = funcName,
- kind = NodeKind.Function,
+ kind = kind,
attrs = Map(
- "language" -> Json.fromString("scala"),
- "summary" -> Json.fromString(summary)
+ "language" -> Json.fromString("scala"),
+ "summary" -> Json.fromString(signature),
+ "signature" -> Json.fromString(signature),
+ "visibility" -> Json.fromString(visibility)
),
lineStart = lineNum,
lineEnd = endLine
)
- // Determine enclosing type
- val enclosing = typeRanges.find { case (_, start, end) =>
- lineNum > start && lineNum <= end
- }
- enclosing match {
- case Some((typeName, _, _)) =>
- relationships = relationships :+ ParsedRelationship(typeName, funcName, "DEFINES")
- case None =>
- relationships = relationships :+ ParsedRelationship(fileName, funcName, "DEFINES")
- }
+ relationships = relationships :+ ParsedRelationship(edgeSrc, funcName, edgePredicate)
}
}
@@ -217,4 +219,21 @@ class ScalaParser extends Parser {
if (foundOpen) Some(lines.length) else None
}
+
+ /** Extract a clean signature from a trimmed line, stripping trailing body markers. */
+ private def extractSignature(trimmedLine: String): String = {
+ val cleaned = trimmedLine
+ .replaceAll("""\s*=\s*\{?\s*$""", "")
+ .replaceAll("""\s*\{\s*$""", "")
+ .trim
+ cleaned.take(120)
+ }
+
+ /** Detect visibility from a trimmed line (after stripping `override`). */
+ private def extractVisibility(trimmedLine: String): String = {
+ val withoutOverride = trimmedLine.replaceFirst("""^\s*override\s+""", "").trim
+ if (withoutOverride.startsWith("private")) "private"
+ else if (withoutOverride.startsWith("protected")) "protected"
+ else "public"
+ }
}
diff --git a/memory-layer/src/test/scala/ix/memory/ingestion/parsers/ScalaParserSpec.scala b/memory-layer/src/test/scala/ix/memory/ingestion/parsers/ScalaParserSpec.scala
index 93c1e863..eac7c275 100644
--- a/memory-layer/src/test/scala/ix/memory/ingestion/parsers/ScalaParserSpec.scala
+++ b/memory-layer/src/test/scala/ix/memory/ingestion/parsers/ScalaParserSpec.scala
@@ -45,40 +45,43 @@ class ScalaParserSpec extends AnyFlatSpec with Matchers {
file.get.attrs.get("language") shouldBe Some(Json.fromString("scala"))
}
- it should "extract trait definitions" in {
+ it should "extract trait definitions with Trait kind" in {
val result = parser.parse("ConfidenceScorer.scala", sampleCode)
- val traits = result.entities.filter(e =>
- e.kind == NodeKind.Class &&
- e.attrs.get("scala_kind").contains(Json.fromString("trait"))
- )
+ val traits = result.entities.filter(_.kind == NodeKind.Trait)
traits.map(_.name) should contain("ConfidenceScorer")
}
- it should "extract class definitions" in {
+ it should "extract class definitions with Class kind" in {
val result = parser.parse("ConfidenceScorer.scala", sampleCode)
- val classes = result.entities.filter(e =>
- e.kind == NodeKind.Class &&
- e.attrs.get("scala_kind").contains(Json.fromString("class"))
- )
+ val classes = result.entities.filter(_.kind == NodeKind.Class)
classes.map(_.name) should contain("ConfidenceScorerImpl")
}
- it should "extract object definitions" in {
+ it should "extract object definitions with Object kind" in {
val result = parser.parse("ConfidenceScorer.scala", sampleCode)
- val objects = result.entities.filter(e =>
- e.kind == NodeKind.Class &&
- e.attrs.get("scala_kind").contains(Json.fromString("object"))
- )
+ val objects = result.entities.filter(_.kind == NodeKind.Object)
objects.map(_.name) should contain("ConfidenceScorerImpl")
}
- it should "extract method definitions with line ranges" in {
+ it should "extract contained defs as Method kind" in {
val result = parser.parse("ConfidenceScorer.scala", sampleCode)
- val methods = result.entities.filter(_.kind == NodeKind.Function)
+ val methods = result.entities.filter(_.kind == NodeKind.Method)
methods.map(_.name) should contain allOf ("score", "computeBase", "apply")
- val computeBase = methods.find(_.name == "computeBase").get
- computeBase.lineStart should be > 0
- computeBase.lineEnd should be >= computeBase.lineStart
+ }
+
+ it should "extract top-level defs as Function kind" in {
+ val code =
+ """def topLevel(x: Int): Int = x + 1
+ |
+ |class Foo {
+ | def bar(): Unit = ()
+ |}
+ |""".stripMargin
+ val result = parser.parse("TopLevel.scala", code)
+ val functions = result.entities.filter(_.kind == NodeKind.Function)
+ functions.map(_.name) should contain("topLevel")
+ val methods = result.entities.filter(_.kind == NodeKind.Method)
+ methods.map(_.name) should contain("bar")
}
it should "extract import statements" in {
@@ -87,12 +90,20 @@ class ScalaParserSpec extends AnyFlatSpec with Matchers {
imports.map(_.name) should contain allOf ("cats.effect.IO", "ix.memory.model")
}
- it should "create DEFINES relationships" in {
+ it should "create DEFINES relationships from file to types" in {
val result = parser.parse("ConfidenceScorer.scala", sampleCode)
val defines = result.relationships.filter(_.predicate == "DEFINES")
defines.map(r => (r.srcName, r.dstName)) should contain allOf (
("ConfidenceScorer.scala", "ConfidenceScorer"),
- ("ConfidenceScorer.scala", "ConfidenceScorerImpl"),
+ ("ConfidenceScorer.scala", "ConfidenceScorerImpl")
+ )
+ }
+
+ it should "create CONTAINS relationships from types to methods" in {
+ val result = parser.parse("ConfidenceScorer.scala", sampleCode)
+ val contains = result.relationships.filter(_.predicate == "CONTAINS")
+ contains.map(r => (r.srcName, r.dstName)) should contain allOf (
+ ("ConfidenceScorerImpl", "score"),
("ConfidenceScorerImpl", "computeBase")
)
}
@@ -103,12 +114,27 @@ class ScalaParserSpec extends AnyFlatSpec with Matchers {
imports should not be empty
}
- it should "store method signature as summary" in {
+ it should "store method signature attr" in {
val result = parser.parse("ConfidenceScorer.scala", sampleCode)
val method = result.entities.find(_.name == "computeBase").get
- val summary = method.attrs.get("summary").flatMap(_.asString)
- summary shouldBe defined
- summary.get should include("computeBase")
+ val sig = method.attrs.get("signature").flatMap(_.asString)
+ sig shouldBe defined
+ sig.get should include("computeBase")
+ }
+
+ it should "store visibility attr for private defs" in {
+ val result = parser.parse("ConfidenceScorer.scala", sampleCode)
+ val method = result.entities.find(_.name == "computeBase").get
+ val vis = method.attrs.get("visibility").flatMap(_.asString)
+ vis shouldBe Some("private")
+ }
+
+ it should "default visibility to public" in {
+ val result = parser.parse("ConfidenceScorer.scala", sampleCode)
+ val method = result.entities.find(e => e.name == "score" && e.kind == NodeKind.Method)
+ method shouldBe defined
+ val vis = method.get.attrs.get("visibility").flatMap(_.asString)
+ vis shouldBe Some("public")
}
it should "not store raw file content" in {
@@ -120,10 +146,21 @@ class ScalaParserSpec extends AnyFlatSpec with Matchers {
it should "handle case classes" in {
val code = """case class Foo(bar: String, baz: Int)""".stripMargin
val result = parser.parse("Foo.scala", code)
- val classes = result.entities.filter(e =>
- e.kind == NodeKind.Class &&
- e.attrs.get("scala_kind").contains(Json.fromString("case_class"))
- )
+ val classes = result.entities.filter(_.kind == NodeKind.Class)
classes.map(_.name) should contain("Foo")
+ val foo = classes.find(_.name == "Foo").get
+ foo.attrs.get("scala_kind") shouldBe Some(Json.fromString("case_class"))
+ }
+
+ it should "preserve scala_kind attr on traits" in {
+ val result = parser.parse("ConfidenceScorer.scala", sampleCode)
+ val tr = result.entities.find(_.name == "ConfidenceScorer").get
+ tr.attrs.get("scala_kind") shouldBe Some(Json.fromString("trait"))
+ }
+
+ it should "preserve scala_kind attr on objects" in {
+ val result = parser.parse("ConfidenceScorer.scala", sampleCode)
+ val obj = result.entities.find(e => e.name == "ConfidenceScorerImpl" && e.kind == NodeKind.Object).get
+ obj.attrs.get("scala_kind") shouldBe Some(Json.fromString("object"))
}
}