diff --git a/src-tauri/src/commands/ai/code_intel.rs b/src-tauri/src/commands/ai/code_intel.rs index 60b70b0..2d05771 100644 --- a/src-tauri/src/commands/ai/code_intel.rs +++ b/src-tauri/src/commands/ai/code_intel.rs @@ -131,8 +131,9 @@ const FUNCTION_VALUE_KINDS: &[&str] = &["arrow_function", "function", "generator /// /// variable_declarator 单看 kind 过宽:`let msg = "fn main() {}"` 这类普通声明也命中, /// contains_definition 会对几乎任何 JS/TS 文件误报提示。收窄为 `const fn = () => {}` -/// 形态(初始化值是函数/类表达式)才算定义,contains_definition / read_symbol / -/// outline 三处共用同一判定。 +/// 形态(初始化值本身是函数/类表达式)才算定义,contains_definition / read_symbol / +/// outline 三处共用同一判定。判定看 value 节点自身 kind 而非子树包含—— +/// `const x = arr.map(n => ...)` 子树含 arrow_function,但 x 不是函数定义。 fn is_definition_node(node: &Node<'_>) -> bool { if !DEFINITION_KINDS.contains(&node.kind()) { return false; @@ -141,25 +142,11 @@ fn is_definition_node(node: &Node<'_>) -> bool { return true; } match node.child_by_field_name("value") { - Some(v) => subtree_contains_kind(v, FUNCTION_VALUE_KINDS), + Some(v) => FUNCTION_VALUE_KINDS.contains(&v.kind()), None => false, } } -/// 子树内是否含任一目标 kind 节点 -fn subtree_contains_kind(node: Node<'_>, kinds: &[&str]) -> bool { - if kinds.contains(&node.kind()) { - return true; - } - let mut cursor = node.walk(); - for child in node.children(&mut cursor) { - if subtree_contains_kind(child, kinds) { - return true; - } - } - false -} - /// 递归遍历语法树,判断是否含任一定义节点(contains_definition 的遍历内核)。 fn tree_has_definition(node: &Node<'_>) -> bool { if is_definition_node(node) {