use crate::getter::node_text;
use crate::lang_helpers::case_arms::kotlin_when_entry_is_else;
use crate::lang_helpers::doc_comment::kotlin_doc;
use crate::role_table;
use crate::roles::control_flow::else_if_prev_sibling_gate;
use crate::{Ancestors, Kotlin, Node, Search};
fn modifier_was_written_as_one<'a>(
node: &Node<'a>,
_code: &[u8],
ancestors: Ancestors<'a, '_>,
) -> bool {
let host = ancestors
.iter(node)
.map(|(ancestor, _)| ancestor)
.find(|ancestor| {
let kind = ancestor.kind();
!(kind.ends_with("_modifier") || kind == "type_modifiers" || kind == "user_type")
});
host.is_some_and(|host| !host.is_error()) && !next_token_is_inserted(node, ancestors)
}
fn next_token_is_inserted<'a>(node: &Node<'a>, ancestors: Ancestors<'a, '_>) -> bool {
std::iter::once((*node, ancestors))
.chain(ancestors.iter(node))
.find_map(|(level, level_ancestors)| {
level_ancestors
.siblings_after(&level)
.find(|sibling| !sibling.as_tree_sitter().is_extra())
})
.is_some_and(|mut next| {
while let Some(first) = next.child(0) {
next = first;
}
next.start_byte() == next.end_byte()
})
}
fn eager_infix(node: &Node<'_>, code: &[u8]) -> bool {
node.children()
.filter(|child| !child.as_tree_sitter().is_extra())
.nth(1)
.filter(|name| name.kind_id() == Kotlin::Identifier as u16)
.and_then(|name| node_text(code, &name))
.is_some_and(|name| matches!(name, "and" | "or" | "xor"))
}
fn is_case_entry(node: &Node<'_>, _code: &[u8]) -> bool {
!kotlin_when_entry_is_else(node)
}
fn class_or_interface(node: &Node<'_>, _code: &[u8]) -> crate::SpaceKind {
if node.first_child(|id| id == Kotlin::Interface).is_some() {
crate::SpaceKind::Interface
} else {
crate::SpaceKind::Class
}
}
else_if_prev_sibling_gate!(Kotlin, Else);
crate::roles::byte_gate!(opens_with_kdoc_marker, kotlin_doc);
role_table! {
lang: Kotlin;
Call { owns [CallExpression] }
StringLit { owns [StringLiteral, MultilineStringLiteral] wraps [Interpolation] }
Comparison {
owns [
BinaryExpression(holds_payer),
IsExpression, InExpression,
]
pays [
EQEQ, EQEQEQ, BANGEQ, BANGEQEQ, LTEQ, GTEQ,
LT under BinaryExpression, GT under BinaryExpression,
Is under IsExpression, BANGis under IsExpression,
In under InExpression, BANGin under InExpression,
]
project "operator"
}
ShortCircuit {
owns [BinaryExpression(holds_payer)]
pays [AMPAMP, PIPEPIPE]
project "operator"
}
EagerLogical { owns [InfixExpression(text_gated_by eager_infix no_chain)] }
Ternary { absent "Kotlin has no ternary operator; `if` is an expression" }
NullSafe {
owns [
BinaryExpression(holds_payer),
NavigationExpression(holds_payer),
]
pays [QMARKCOLON, QMARKDOT]
project "operator"
excludes [BANGBANG "`a!!` throws on null rather than branching; an operator, no decision"]
}
CaseArm { owns [WhenEntry(gated_by is_case_entry no_chain)] }
Guard { absent "Kotlin 2.x's `when` guards are not in the pinned grammar; `if` is an expression" }
Branch { owns [IfExpression] }
Switch { owns [WhenExpression] }
Loop { owns [ForStatement, WhileStatement, DoWhileStatement] }
Catch { owns [CatchBlock] }
ElseIf { owns [IfExpression(gated_by follows_else_keyword)] pays [] }
Exit { owns [ReturnExpression, ThrowExpression] }
Modifier {
owns [
Enum(gated_by modifier_was_written_as_one),
Sealed(gated_by modifier_was_written_as_one),
Annotation2(gated_by modifier_was_written_as_one),
Data(gated_by modifier_was_written_as_one),
Inner(gated_by modifier_was_written_as_one),
Value(gated_by modifier_was_written_as_one),
Public(gated_by modifier_was_written_as_one),
Private(gated_by modifier_was_written_as_one),
Protected(gated_by modifier_was_written_as_one),
Internal(gated_by modifier_was_written_as_one),
Abstract(gated_by modifier_was_written_as_one),
Final(gated_by modifier_was_written_as_one),
Open(gated_by modifier_was_written_as_one),
Override(gated_by modifier_was_written_as_one),
Lateinit(gated_by modifier_was_written_as_one),
Const(gated_by modifier_was_written_as_one),
Tailrec(gated_by modifier_was_written_as_one),
Operator(gated_by modifier_was_written_as_one),
Infix(gated_by modifier_was_written_as_one),
Inline(gated_by modifier_was_written_as_one),
External(gated_by modifier_was_written_as_one),
Suspend(gated_by modifier_was_written_as_one),
Vararg(gated_by modifier_was_written_as_one),
Noinline(gated_by modifier_was_written_as_one),
Crossinline(gated_by modifier_was_written_as_one),
ReificationModifier(gated_by modifier_was_written_as_one),
Out(gated_by modifier_was_written_as_one),
Expect(gated_by modifier_was_written_as_one),
Actual(gated_by modifier_was_written_as_one),
Companion,
]
}
DeclKeyword { owns [Class, Interface, Fun, Object, Val, Var, Typealias] }
Primitive { absent "Kotlin's built-in types are ordinary type names (`Int`, `String`)" }
FuncOpener {
owns [
FunctionDeclaration(callable opens Function),
SecondaryConstructor(callable opens Function),
Getter(opens Function),
Setter(opens Function),
AnonymousInitializer(opens Function),
]
}
Closure { owns [LambdaLiteral, AnonymousFunction] }
Container {
owns [
SourceFile(opens Unit),
ClassDeclaration(opens_by class_or_interface no_chain),
ObjectDeclaration(opens Class),
CompanionObject(opens Class),
ObjectLiteral(opens Class),
]
}
Member { owns [PropertyDeclaration] }
Comment { owns [LineComment, BlockComment] }
DocComment { owns [BlockComment(text_gated_by opens_with_kdoc_marker no_chain)] }
Docstring { absent "Kotlin documents with KDoc comments, not string literals" }
DocTarget {
owns [
ClassDeclaration,
ObjectDeclaration,
CompanionObject,
FunctionDeclaration,
PropertyDeclaration,
TypeAlias,
SecondaryConstructor,
EnumEntry,
Parameter,
ClassParameter,
]
}
DocRunOpener { absent "Dokka attaches every KDoc to a declaration; none documents by its own content" }
DocRunDirective { absent "Dokka reads no directive comment" }
DocBodyResidual { owns [Getter, Setter, AnonymousInitializer] }
}
#[cfg(test)]
mod tests {
use super::*;
use crate::KotlinCode;
use crate::test_support::for_each_node_with_chain;
#[cfg(feature = "kotlin")]
#[test]
fn eager_infix_is_told_by_its_name() {
let source = b"fun f(a: Boolean, b: Boolean, x: Int) { val c = a and b; val d = a or b; val e = a xor b; val s = x shl 1; val p = 1 to 2; val m = a myInfix b; val n = (a && b) xor a; val o = a and /* c */ b and a; val q = (a) to and }\n";
let (mut owned, mut paid) = (Vec::new(), Vec::new());
for_each_node_with_chain::<KotlinCode>(source, |node, chain| {
let ancestors = Ancestors::known(chain);
let text = || node.utf8_text(source).expect("fixture is UTF-8").to_owned();
if eager_logical::owns(node, source, ancestors) {
owned.push(text());
}
if eager_logical::pays_at_leaf(node, source, ancestors) {
paid.push(text());
}
});
assert_eq!(
owned,
[
"a and b",
"a or b",
"a xor b",
"(a && b) xor a",
"a and /* c */ b and a",
"a and /* c */ b",
]
);
assert_eq!(paid, owned);
assert_eq!(eager_logical::PAYERS, &[] as &[u16]);
}
}