use crate::types::Language;
use crate::types::SymbolType;
pub(crate) mod classify;
pub(crate) mod names;
mod special_forms;
use classify::{classify_node, container_body_kinds};
use names::extract_name;
use special_forms::*;
#[derive(Debug, Clone)]
pub struct RawSymbol {
pub name: Option<String>,
pub symbol_type: SymbolType,
pub start_byte: usize,
pub end_byte: usize,
pub start_row: usize,
pub end_row: usize,
}
impl RawSymbol {
fn at(node: &tree_sitter::Node<'_>, name: Option<String>, symbol_type: SymbolType) -> Self {
Self {
name,
symbol_type,
start_byte: node.start_byte(),
end_byte: node.end_byte(),
start_row: node.start_position().row,
end_row: node.end_position().row,
}
}
}
pub fn extract_symbols(tree: &tree_sitter::Tree, source: &[u8], lang: Language) -> Vec<RawSymbol> {
let mut symbols = Vec::new();
let mut cursor = tree.root_node().walk();
collect_symbols_cursor(&mut cursor, source, lang, &mut symbols, 1);
symbols.sort_by_key(|s| s.start_byte);
symbols
}
pub fn extract_rst_section_titles(tree: &tree_sitter::Tree, source: &[u8]) -> Vec<(u32, String)> {
fn walk(node: tree_sitter::Node<'_>, source: &[u8], out: &mut Vec<(u32, String)>) {
if node.kind() == "title"
&& let Ok(text) = node.utf8_text(source)
{
let title = text.split_whitespace().collect::<Vec<_>>().join(" ");
if !title.is_empty() {
out.push((node.start_position().row as u32, title));
}
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
walk(child, source, out);
}
}
let mut titles = Vec::new();
walk(tree.root_node(), source, &mut titles);
titles.sort_by_key(|(row, _)| *row);
titles.dedup_by(|a, b| a.0 == b.0);
titles
}
fn collect_symbols_cursor(
cursor: &mut tree_sitter::TreeCursor,
source: &[u8],
lang: Language,
symbols: &mut Vec<RawSymbol>,
depth: usize,
) {
if depth > 6 {
return;
}
if !cursor.goto_first_child() {
return;
}
loop {
collect_symbols(cursor.node(), source, lang, symbols, depth);
if !cursor.goto_next_sibling() {
break;
}
}
cursor.goto_parent();
}
fn collect_symbols(
node: tree_sitter::Node<'_>,
source: &[u8],
lang: Language,
symbols: &mut Vec<RawSymbol>,
depth: usize,
) {
if depth > 6 {
return;
}
if !node.is_named() {
return;
}
let kind = node.kind();
if lang == Language::Go && kind == "type_declaration" {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "type_spec" {
let sym_type = refine_go_type_spec(&child, source);
let name = extract_name(&child, source);
symbols.push(RawSymbol::at(&child, name, sym_type));
}
}
return;
}
if lang == Language::R
&& kind == "binary_operator"
&& let Some(rhs) = node.child_by_field_name("rhs")
&& rhs.kind() == "function_definition"
{
let name = extract_name(&node, source);
symbols.push(RawSymbol::at(&node, name, SymbolType::Function));
return;
}
if (lang == Language::TypeScript || lang == Language::JavaScript)
&& matches!(kind, "lexical_declaration" | "variable_declaration")
&& let Some(sym) = extract_js_function_binding(&node, source)
{
symbols.push(sym);
return;
}
if lang == Language::Lua
&& matches!(kind, "assignment_statement" | "variable_declaration")
&& let Some(sym) = extract_lua_function_binding(&node, source)
{
symbols.push(sym);
return;
}
if lang == Language::Zig && kind == "variable_declaration" {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "struct_declaration" {
let name = extract_name(&node, source);
symbols.push(RawSymbol::at(&node, name, SymbolType::Struct));
return;
}
}
}
if lang == Language::Scheme
&& kind == "list"
&& let Some(sym) = extract_scheme_define(&node, source)
{
symbols.push(sym);
return;
}
if lang == Language::Racket
&& kind == "list"
&& let Some(sym) = extract_racket_define(&node, source)
{
symbols.push(sym);
return;
}
if lang == Language::Clojure
&& kind == "list_lit"
&& let Some(sym) = extract_lisp_define(&node, source, CLOJURE_DEFINE_KINDS)
{
symbols.push(sym);
return;
}
if lang == Language::CommonLisp
&& kind == "defun"
&& let Some(sym) = extract_commonlisp_defun(&node, source)
{
symbols.push(sym);
return;
}
if lang == Language::CommonLisp
&& kind == "list_lit"
&& let Some(sym) = extract_lisp_define(&node, source, COMMONLISP_DEFINE_KINDS)
{
symbols.push(sym);
return;
}
if lang == Language::Elixir
&& kind == "call"
&& let Some(target) = node.child_by_field_name("target")
&& let Ok(text) = target.utf8_text(source)
{
let sym_type = match text {
"defmodule" => Some(SymbolType::Module),
"def" | "defp" | "defmacro" => Some(SymbolType::Function),
_ => None,
};
if let Some(st) = sym_type {
let name = extract_elixir_name(&node, source);
symbols.push(RawSymbol::at(&node, name, st));
if text == "defmodule"
&& let Some(do_block) = get_elixir_do_block(&node)
{
let mut cursor = do_block.walk();
collect_symbols_cursor(&mut cursor, source, lang, symbols, depth + 1);
}
return;
}
}
if let Some(mut sym_type) = classify_node(lang, kind) {
if lang == Language::Hcl && kind == "block" {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "identifier" {
if let Ok(text) = child.utf8_text(source) {
sym_type = match text {
"resource" | "data" => SymbolType::Struct,
"variable" | "output" => SymbolType::TypeAlias,
"module" => SymbolType::Module,
_ => SymbolType::Struct,
};
}
break;
}
}
}
if (lang == Language::Kotlin || lang == Language::Swift) && kind == "class_declaration" {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
let ckind = child.kind();
if ckind == "enum" || ckind == "enum_class_body" {
sym_type = SymbolType::Enum;
} else if ckind == "interface" {
sym_type = SymbolType::Interface;
} else if ckind == "struct" {
sym_type = SymbolType::Struct;
}
}
}
if lang == Language::Rust && (kind == "impl_item" || kind == "trait_item") {
extract_rust_block_methods(node, source, lang, symbols, sym_type);
return;
}
if lang == Language::Bash && kind == "function_definition" {
extract_bash_case_items(&node, source, symbols);
return;
}
if lang == Language::Rust && kind == "mod_item" {
let name = extract_name(&node, source);
symbols.push(RawSymbol::at(&node, name, sym_type));
if let Some(body) = node.child_by_field_name("body") {
let mut cursor = body.walk();
collect_symbols_cursor(&mut cursor, source, lang, symbols, depth + 1);
}
return;
}
if lang == Language::Python && kind == "class_definition" {
extract_python_class_methods(node, source, symbols);
return;
}
if lang == Language::ObjectiveC
&& (kind == "class_interface"
|| kind == "class_implementation"
|| kind == "category_interface"
|| kind == "category_implementation")
{
let name = extract_name(&node, source);
symbols.push(RawSymbol::at(&node, name, sym_type));
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if let Some(child_sym) = classify_node(lang, child.kind()) {
let child_name = extract_name(&child, source);
symbols.push(RawSymbol::at(&child, child_name, child_sym));
}
if child.kind() == "implementation_definition" {
let mut inner = child.walk();
for inner_child in child.children(&mut inner) {
if let Some(inner_sym) = classify_node(lang, inner_child.kind()) {
let inner_name = extract_name(&inner_child, source);
symbols.push(RawSymbol::at(&inner_child, inner_name, inner_sym));
}
}
}
}
return;
}
if container_body_kinds(lang).contains(&kind) {
let name = extract_name(&node, source);
symbols.push(RawSymbol::at(&node, name, sym_type));
let mut cursor = node.walk();
collect_symbols_cursor(&mut cursor, source, lang, symbols, depth + 1);
return;
}
if (lang == Language::CSharp
|| lang == Language::Php
|| lang == Language::Dart
|| lang == Language::TypeScript
|| lang == Language::JavaScript
|| lang == Language::Java)
&& (kind == "class_declaration"
|| kind == "class_definition"
|| kind == "interface_declaration"
|| kind == "enum_declaration")
{
extract_general_class_methods(node, source, lang, symbols, sym_type);
return;
}
if lang == Language::Protobuf && (kind == "service" || kind == "service_definition") {
let name = extract_name(&node, source);
symbols.push(RawSymbol::at(&node, name, sym_type));
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "service_body" || child.kind() == "block" {
let mut inner_cursor = child.walk();
for inner_child in child.children(&mut inner_cursor) {
if (inner_child.kind() == "rpc"
|| inner_child.kind() == "rpc_definition"
|| inner_child.kind() == "rpc_declaration")
&& let Some(rpc_type) = classify_node(lang, inner_child.kind())
{
let end_byte = inner_child.end_byte();
let end_row = inner_child.end_position().row;
let rpc_name = extract_name(&inner_child, source);
symbols.push(RawSymbol {
name: rpc_name,
symbol_type: rpc_type,
start_byte: inner_child.start_byte(),
end_byte,
start_row: inner_child.start_position().row,
end_row,
});
}
}
} else if (child.kind() == "rpc"
|| child.kind() == "rpc_definition"
|| child.kind() == "rpc_declaration")
&& let Some(rpc_type) = classify_node(lang, child.kind())
{
let end_byte = child.end_byte();
let end_row = child.end_position().row;
let rpc_name = extract_name(&child, source);
symbols.push(RawSymbol {
name: rpc_name,
symbol_type: rpc_type,
start_byte: child.start_byte(),
end_byte,
start_row: child.start_position().row,
end_row,
});
}
}
return;
}
let mut end_byte = node.end_byte();
let mut end_row = node.end_position().row;
if lang == Language::Dart
&& (kind == "function_signature" || kind == "method_signature")
&& let Some(next_sibling) = node.next_sibling()
&& next_sibling.kind() == "function_body"
{
end_byte = next_sibling.end_byte();
end_row = next_sibling.end_position().row;
}
let name = extract_name(&node, source);
symbols.push(RawSymbol {
name,
symbol_type: sym_type,
start_byte: node.start_byte(),
end_byte,
start_row: node.start_position().row,
end_row,
});
return;
}
let mut cursor = node.walk();
collect_symbols_cursor(&mut cursor, source, lang, symbols, depth + 1);
}
#[cfg(test)]
mod container_tests;
#[cfg(test)]
mod tests;