#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
use super::*;
fn kotlin_leading_identifier_len(text: &str) -> usize {
let mut chars = text.char_indices();
let Some((_, first)) = chars.next() else {
return 0;
};
if first != '_' && !first.is_alphabetic() {
return 0;
}
let mut end = first.len_utf8();
for (idx, c) in chars {
if c == '_' || c.is_alphanumeric() {
end = idx + c.len_utf8();
} else {
break;
}
}
end
}
fn kotlin_is_short_interp_name<'a>(
node: &Node<'a>,
previous: impl FnOnce() -> Option<Node<'a>>,
code: &[u8],
) -> bool {
use Kotlin::{StringContent, StringContent2, StringContent3};
const STRING_CONTENT_KINDS: [u16; 3] = [
StringContent as u16,
StringContent2 as u16,
StringContent3 as u16,
];
STRING_CONTENT_KINDS.contains(&node.kind_id())
&& node
.utf8_text(code)
.is_some_and(|text| kotlin_leading_identifier_len(text) > 0)
&& previous().is_some_and(|prev| {
STRING_CONTENT_KINDS.contains(&prev.kind_id()) && prev.utf8_text(code) == Some("$")
})
}
fn kotlin_short_interp_operand_bytes<'a>(node: &Node, code: &'a [u8]) -> &'a [u8] {
let start = node.start_byte();
let end = node.end_byte();
let full = &code[start..end];
match std::str::from_utf8(full) {
Ok(text) => {
let prefix_len = kotlin_leading_identifier_len(text);
if prefix_len > 0 {
&full[..prefix_len]
} else {
full
}
}
Err(_) => full,
}
}
fn kotlin_string_has_interp(node: &Node, code: &[u8]) -> bool {
let mut previous = None;
node.children().any(|child| {
let found = crate::roles::kotlin::string_lit::WRAPS.contains(&child.kind_id())
|| kotlin_is_short_interp_name(&child, || previous, code);
previous = Some(child);
found
})
}
const GENERIC_ANGLES: GenericAngleKinds = GenericAngleKinds {
lists: &[
Kotlin::TypeArguments as u16,
Kotlin::TypeParameters as u16,
Kotlin::SuperExpression as u16,
],
openers: &[Kotlin::LT as u16],
closers: &[Kotlin::GT as u16],
is_misparse: never_misparsed,
};
impl Getter for KotlinCode {
fn get_func_space_name<'a, 'tree>(
node: &Node<'tree>,
code: &'a [u8],
ancestors: Ancestors<'tree, '_>,
) -> Option<&'a str> {
match node.kind_id().into() {
Kotlin::Getter => Some("<get>"),
Kotlin::Setter => Some("<set>"),
Kotlin::AnonymousInitializer => Some("<init>"),
Kotlin::PrimaryConstructor => ancestors
.parent(node)
.and_then(|class| crate::getter::default_func_space_name(&class, code, ancestors)),
_ => crate::getter::default_func_space_name(node, code, ancestors),
}
}
impl_byte_less_space_kind!(kotlin);
fn get_space_kind_with_code<'a>(
node: &Node<'a>,
code: &[u8],
ancestors: Ancestors<'a, '_>,
) -> SpaceKind {
crate::roles::kotlin::opens_space(node, code, ancestors)
}
keyword_roles!(kotlin);
paying_roles!(kotlin);
fn get_op_type<'a>(node: &Node<'a>, ancestors: Ancestors<'a, '_>) -> TokenRole {
use Kotlin::*;
if GENERIC_ANGLES.is_closer(node, ancestors) {
return TokenRole::Unknown;
}
let kind: Kotlin = node.kind_id().into();
let accessor = match kind {
Get => Some(Kotlin::Getter),
Set => Some(Setter),
_ => None,
};
if let Some(accessor) = accessor {
return if ancestors.parent_has_kind(node, accessor as u16) {
TokenRole::Operator
} else {
TokenRole::Unknown
};
}
match kind {
If | Else | When | For | While | Do | Try | Catch | Finally | Throw | Return
| ReturnAT
| In | Is | BANGis | BANGin
| As | AsQMARK | Constructor | Init | Where | By
| SEMI | COMMA | COLONCOLON | DOT | LBRACE | LBRACK | LPAREN
| EQ | PLUS | DASH | STAR | SLASH | PERCENT
| PLUSEQ | DASHEQ | STAREQ | SLASHEQ | PERCENTEQ
| PLUSPLUS | DASHDASH
| LT | GT
| BANG | BANGBANG
| QMARK
| DOTDOT | DOTDOTLT | DASHGT | COLON => TokenRole::Operator,
Identifier | NumberLiteral | FloatLiteral | CharacterLiteral | Label | This
| Super | ThisAT | SuperAT => TokenRole::Operand,
StringLiteral | MultilineStringLiteral => {
Self::string_operand_type(node, crate::roles::kotlin::string_lit::WRAPS)
}
_ => TokenRole::Unknown,
}
}
fn get_op_type_with_code<'a>(
node: &Node<'a>,
code: &[u8],
ancestors: Ancestors<'a, '_>,
) -> TokenRole {
use Kotlin::*;
match node.kind_id().into() {
StringContent | StringContent2 | StringContent3
if kotlin_is_short_interp_name(node, || ancestors.previous_sibling(node), code) =>
{
TokenRole::Operand
}
StringLiteral | MultilineStringLiteral => {
if kotlin_string_has_interp(node, code) {
TokenRole::Unknown
} else {
TokenRole::Operand
}
}
_ => keyword_or_op_type::<Self>(node, code, ancestors),
}
}
fn get_operand_id<'a>(
node: &Node<'a>,
code: &'a [u8],
ancestors: Ancestors<'a, '_>,
) -> &'a [u8] {
if kotlin_is_short_interp_name(node, || ancestors.previous_sibling(node), code) {
kotlin_short_interp_operand_bytes(node, code)
} else {
&code[node.start_byte()..node.end_byte()]
}
}
fn get_operator_spelling<'a>(
node: &Node<'a>,
ancestors: Ancestors<'a, '_>,
) -> Option<&'static str> {
GENERIC_ANGLES.opener_spelling(node, ancestors)
}
get_operator!(Kotlin);
}