Adding a custom language adapter
Any language with a tree-sitter Python binding can be supported in three steps:
# 1. Create your adapter (e.g. my_adapters/kotlin.py)
from trailhead.services.indexing.adapters.base import LanguageAdapter, _node_text, _complexity
from trailhead.services.indexing.graph import PropertyGraph, Vertex
from pathlib import Path
class KotlinAdapter(LanguageAdapter):
extensions = frozenset({".kt", ".kts"})
@classmethod
def is_available(cls) -> bool:
try:
import tree_sitter_kotlin # noqa: F401
return True
except ImportError:
return False
def parse(self, path: Path, graph: PropertyGraph) -> Vertex:
import tree_sitter_kotlin as tskotlin
from tree_sitter import Language, Parser
source = path.read_bytes()
language = Language(tskotlin.language())
parser = Parser(language)
tree = parser.parse(source)
module_v = graph.add_vertex("module", name=path.stem, path=str(path))
# ... walk tree and add vertices/edges ...
return module_v
@classmethod
def matches(cls, path: Path) -> bool:
# Optional: decline files whose extension is shared with other formats.
return True
def finalize(self, graph: PropertyGraph) -> None:
# Optional: resolve cross-file edges after all files have been parsed.
return None
# 2. Register it at startup (e.g. in your app's __init__ or conftest)
from trailhead.services.indexing.adapters import register
register(KotlinAdapter())
# 3. Done — th index, serve, and query all pick it up automatically.
If your adapter shares an extension with other formats, override matches()
to decide whether a given file really belongs to you. If you need cross-file
relationships, override finalize() and resolve them after the full
directory walk.
What each adapter should produce:
| Vertex label | Meaning | Required properties |
|---|---|---|
module |
one per source file | name, path |
class |
class / struct / interface / trait | name, path, line |
function |
function / method | name, path, line, source, complexity |
external |
imported module name | name |
Edges: defines (module→class, module→function), has_method
(class→function), imports (module→external), calls (function→function).
See the Python API reference for the exact
LanguageAdapter contract and the PropertyGraph/Vertex/Edge data model.