Extract a function definition.
(
&self,
node: &Node,
source: &str,
file_path: &str,
in_impl: Option<&str>,
)
| 145 | |
| 146 | /// Extract a function definition. |
| 147 | fn extract_function( |
| 148 | &self, |
| 149 | node: &Node, |
| 150 | source: &str, |
| 151 | file_path: &str, |
| 152 | in_impl: Option<&str>, |
| 153 | ) -> Option<Entity> { |
| 154 | let name_node = node.child_by_field_name("name")?; |
| 155 | let name = self.node_text(&name_node, source); |
| 156 | |
| 157 | let kind = if in_impl.is_some() { |
| 158 | // Check if it takes &self / &mut self / self — then it's a method |
| 159 | let has_self = node |
| 160 | .child_by_field_name("parameters") |
| 161 | .map(|params| { |
| 162 | let mut cursor = params.walk(); |
| 163 | let children: Vec<_> = params.children(&mut cursor).collect(); |
| 164 | children.iter().any(|c| { |
| 165 | c.kind() == "self_parameter" || self.node_text(c, source).contains("self") |
| 166 | }) |
| 167 | }) |
| 168 | .unwrap_or(false); |
| 169 | |
| 170 | if has_self { |
| 171 | EntityKind::Method |
| 172 | } else { |
| 173 | // Associated function (no self parameter) |
| 174 | EntityKind::Function |
| 175 | } |
| 176 | } else { |
| 177 | EntityKind::Function |
| 178 | }; |
| 179 | |
| 180 | let line = node.start_position().row as u32 + 1; |
| 181 | let end_line = node.end_position().row as u32 + 1; |
| 182 | |
| 183 | let exported = self.is_pub(node); |
| 184 | let signature = self.build_function_signature(node, source); |
| 185 | |
| 186 | let mut entity = Entity::new(name, kind, file_path, line, end_line); |
| 187 | if let Some(sig) = signature { |
| 188 | entity = entity.with_signature(sig); |
| 189 | } |
| 190 | if exported { |
| 191 | entity.exported = true; |
| 192 | } |
| 193 | |
| 194 | Some(entity) |
| 195 | } |
| 196 | |
| 197 | /// Extract a struct definition. |
| 198 | fn extract_struct(&self, node: &Node, source: &str, file_path: &str) -> Option<Entity> { |
no test coverage detected