Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ serde = { version = "^1.0", features = ["derive"] }
termcolor = "^1.2"
walkdir = "^2.3"

tree-sitter = "=0.25.3"
tree-sitter = "=0.26.5"
tree-sitter-java = "=0.23.5"
tree-sitter-kotlin-ng = "1.1.0"
tree-sitter-kotlin-codanna = "0.3.9"
tree-sitter-typescript = "=0.23.2"
tree-sitter-javascript = "=0.23.1"
tree-sitter-python = "=0.23.6"
tree-sitter-rust = "=0.23.2"
tree-sitter-javascript = "=0.25.0"
tree-sitter-python = "=0.25.0"
tree-sitter-rust = "=0.24.0"
tree-sitter-preproc = { path = "./tree-sitter-preproc", version = "=0.20.3" }
tree-sitter-ccomment = { path = "./tree-sitter-ccomment", version = "=0.20.3" }
tree-sitter-mozcpp = { path = "./tree-sitter-mozcpp", version = "=0.20.4" }
tree-sitter-mozjs = { path = "./tree-sitter-mozjs", version = "=0.20.3" }

[dev-dependencies]
insta = { version = "1.29.0", features = ["yaml", "json", "redactions"] }
insta = { version = "1.46.1", features = ["yaml", "json", "redactions"] }
pretty_assertions = "^1.3"

[profile.dev.package.insta]
Expand Down
12 changes: 6 additions & 6 deletions enums/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ edition = "2024"

[dependencies]
clap = { version = "^4.0", features = ["derive"] }
askama = "^0.14"
askama = "^0.15"

tree-sitter = "=0.25.3"
tree-sitter = "=0.26.5"
tree-sitter-java = "=0.23.5"
tree-sitter-kotlin-ng = "1.1.0"
tree-sitter-kotlin-codanna = "0.3.9"
tree-sitter-typescript = "=0.23.2"
tree-sitter-javascript = "=0.23.1"
tree-sitter-python = "=0.23.6"
tree-sitter-rust = "=0.23.2"
tree-sitter-javascript = "=0.25.0"
tree-sitter-python = "=0.25.0"
tree-sitter-rust = "=0.24.0"
tree-sitter-preproc = { path = "../tree-sitter-preproc", version = "=0.20.3" }
tree-sitter-ccomment = { path = "../tree-sitter-ccomment", version = "=0.20.3" }
tree-sitter-mozcpp = { path = "../tree-sitter-mozcpp", version = "=0.20.4" }
Expand Down
2 changes: 1 addition & 1 deletion enums/src/languages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use tree_sitter::Language;
mk_langs!(
// 1) Name for enum
// 2) tree-sitter function to call to get a Language
(Kotlin, tree_sitter_kotlin_ng),
(Kotlin, tree_sitter_kotlin_codanna),
(Java, tree_sitter_java),
(Rust, tree_sitter_rust),
(Cpp, tree_sitter_cpp),
Expand Down
2 changes: 1 addition & 1 deletion enums/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ macro_rules! mk_get_language {
( $( ($camel:ident, $name:ident) ),* ) => {
pub fn get_language(lang: &Lang) -> Language {
match lang {
Lang::Kotlin => tree_sitter_kotlin_ng::LANGUAGE.into(),
Lang::Kotlin => tree_sitter_kotlin_codanna::language().into(),
Lang::Java => tree_sitter_java::LANGUAGE.into(),
Lang::Typescript => tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
Lang::Tsx => tree_sitter_typescript::LANGUAGE_TSX.into(),
Expand Down
153 changes: 153 additions & 0 deletions src/asttools.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
//! AST traversal and analysis utilities.
//!
//! This module provides helper functions and macros for traversing
//! and analyzing the AST tree structure.

use crate::node::Node;

/// Gets an ancestor at a specific level above the current node.
///
/// # Arguments
/// * `node` - The starting node
/// * `level` - How many levels up to traverse (0 returns the node itself)
///
/// # Returns
/// The ancestor node at the specified level, or None if the tree isn't deep enough.
///
/// # Example
/// ```ignore
/// // Get the grandparent (2 levels up)
/// if let Some(grandparent) = get_parent(&node, 2) {
/// println!("Grandparent kind: {}", grandparent.kind());
/// }
/// ```
pub fn get_parent<'a>(node: &Node<'a>, level: usize) -> Option<Node<'a>> {
let mut level = level;
let mut current = *node;
while level != 0 {
current = current.parent()?;
level -= 1;
}
Some(current)
}

/// Traverses a tree passing from children to children in search of a specific
/// token or series of tokens.
///
/// # Arguments
/// * `node` - The starting node
/// * `token_list` - A slice of predicates, each matching a level of descent
///
/// # Returns
/// The final node after following the token path, or None if any token wasn't found.
///
/// # Example
/// ```ignore
/// // Find: node -> child matching pred1 -> grandchild matching pred2
/// let result = traverse_children(&node, &[
/// |id| id == SomeToken::Foo as u16,
/// |id| id == SomeToken::Bar as u16,
/// ]);
/// ```
pub fn traverse_children<'a, F>(node: &Node<'a>, token_list: &[F]) -> Option<Node<'a>>
where
F: Fn(u16) -> bool,
{
let mut current = *node;
'outer: for token in token_list {
for child in current.children() {
if token(child.kind_id()) {
current = child;
continue 'outer;
}
}
// Token not found at this level
return None;
}
Some(current)
}

/// Checks if a node has specific ancestors in sequence.
///
/// This macro checks if the node's ancestors match a specific pattern,
/// where the first pattern(s) are immediate ancestors and the last pattern
/// is the final ancestor to match.
///
/// # Example
/// ```ignore
/// // Check if node is inside a function inside a class
/// let is_method = has_ancestors!(node, Class | Struct, Function);
/// ```
#[macro_export]
macro_rules! has_ancestors {
($node:expr, $( $typs:pat_param )|*, $( $typ:pat_param ),+) => {{
let mut res = false;
loop {
let mut node = *$node;
$(
if let Some(parent) = node.parent() {
match parent.kind_id().into() {
$typ => {
node = parent;
},
_ => {
break;
}
}
} else {
break;
}
)*
if let Some(parent) = node.parent() {
match parent.kind_id().into() {
$( $typs )|+ => {
res = true;
},
_ => {}
}
}
break;
}
res
}};
}

/// Counts specific ancestors matching a pattern until a stop condition.
///
/// This macro traverses up the tree counting ancestors that match the given
/// patterns, stopping when it encounters an ancestor matching the stop pattern.
///
/// # Example
/// ```ignore
/// // Count nested if statements until we hit a function boundary
/// let nesting = count_specific_ancestors!(node, If | ElseIf, Function | Method);
/// ```
#[macro_export]
macro_rules! count_specific_ancestors {
($node:expr, $checker:ty, $( $typs:pat_param )|*, $( $stops:pat_param )|*) => {{
let mut count = 0;
let mut node = *$node;
while let Some(parent) = node.parent() {
match parent.kind_id().into() {
$( $typs )|* => {
if !<$checker>::is_else_if(&parent) {
count += 1;
}
},
$( $stops )|* => break,
_ => {}
}
node = parent;
}
count
}};
}

#[cfg(test)]
mod tests {
#[test]
fn test_get_parent_level_zero() {
// Level 0 should return the same node
// (actual test would need a real node)
}
}
55 changes: 40 additions & 15 deletions src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,39 +663,64 @@ impl Checker for RustCode {
}

impl Checker for KotlinCode {
fn is_comment(_: &Node) -> bool {
false
fn is_comment(node: &Node) -> bool {
matches!(
node.kind_id().into(),
Kotlin::LineComment | Kotlin::MultilineComment
)
}

fn is_useful_comment(_: &Node, _: &[u8]) -> bool {
false
}

fn is_func_space(_: &Node) -> bool {
false
fn is_func_space(node: &Node) -> bool {
matches!(
node.kind_id().into(),
Kotlin::SourceFile | Kotlin::ClassDeclaration
)
}

fn is_func(_: &Node) -> bool {
false
fn is_func(node: &Node) -> bool {
node.kind_id() == Kotlin::FunctionDeclaration
}

fn is_closure(_: &Node) -> bool {
false
fn is_closure(node: &Node) -> bool {
node.kind_id() == Kotlin::LambdaLiteral
}

fn is_call(_: &Node) -> bool {
false
fn is_call(node: &Node) -> bool {
node.kind_id() == Kotlin::CallExpression
}

fn is_non_arg(_: &Node) -> bool {
false
fn is_non_arg(node: &Node) -> bool {
matches!(
node.kind_id().into(),
Kotlin::LPAREN
| Kotlin::COMMA
| Kotlin::RPAREN
| Kotlin::PIPEPIPE
| Kotlin::UnaryExpression
)
}

fn is_string(_: &Node) -> bool {
false
fn is_string(node: &Node) -> bool {
// StringLiteral covers both single-line and multi-line strings in this grammar
// StringContent captures the text content within strings
matches!(
node.kind_id().into(),
Kotlin::StringLiteral | Kotlin::StringContent
)
}

fn is_else_if(_: &Node) -> bool {
fn is_else_if(node: &Node) -> bool {
if node.kind_id() != Kotlin::IfExpression {
return false;
}
if let Some(parent) = node.parent() {
return parent.kind_id() == Kotlin::Else;
}

false
}

Expand Down
50 changes: 49 additions & 1 deletion src/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,4 +575,52 @@ impl Getter for JavaCode {
}
}

impl Getter for KotlinCode {}
impl Getter for KotlinCode {
fn get_space_kind(node: &Node) -> SpaceKind {
use Kotlin::*;

let typ = node.kind_id().into();
match typ {
ClassDeclaration => SpaceKind::Class,
FunctionDeclaration | Constructor | AnnotatedLambda => SpaceKind::Function,
SourceFile => SpaceKind::Unit,
_ => SpaceKind::Unknown,
}
}

fn get_op_type(node: &Node) -> HalsteadType {
use Kotlin::*;

let typ = node.kind_id();

match typ.into() {
// Operator: function calls
CallExpression
// Operator: control flow
| If | Else | When | Try | Catch | Throw | For | While | Continue | Break | Do | Finally
// Operator: keywords
| Return | Abstract | Final | Super | This
// Operator: brackets and comma and terminators (separators)
| SEMI | COMMA | COLONCOLON | LBRACE | LBRACK | LPAREN | RBRACE | RBRACK | RPAREN | DOTDOT | DOT
// Operator: operators
| EQ | LT | GT | BANG | QMARKCOLON | AsQMARK | COLON // no grammar for lambda operator ->
| EQEQ | LTEQ | GTEQ | BANGEQ | AMPAMP | PIPEPIPE | PLUSPLUS | DASHDASH
| PLUS | DASH | STAR | SLASH | PERCENT
| PLUSEQ | DASHEQ | STAREQ | SLASHEQ | PERCENTEQ => {
HalsteadType::Operator
}
// Operands: variables, constants, literals
// StringLiteral covers both line strings and multi-line strings in this grammar
RealLiteral | IntegerLiteral | HexLiteral | BinLiteral | CharacterLiteralToken1 | UniCharacterLiteralToken1
| LiteralConstant | StringLiteral | StringContent | LambdaLiteral | FunctionLiteral
| ObjectLiteral | UnsignedLiteral | LongLiteral | BooleanLiteral | CharacterLiteral => {
HalsteadType::Operand
},
_ => {
HalsteadType::Unknown
},
}
}

get_operator!(Kotlin);
}
2 changes: 1 addition & 1 deletion src/langs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ mk_langs!(
"kotlin",
KotlinCode,
KotlinParser,
tree_sitter_kotlin_ng,
tree_sitter_kotlin_codanna,
[kt, kts],
["kotlin"]
),
Expand Down
Loading