MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / body_docstring

Function body_docstring

codegraph-kernel/src/python.rs:26–72  ·  view source on GitHub ↗

Keep body-docstring selection and cleaning in sync with languages/python.ts.

(node: Node, src: &str)

Source from the content-addressed store, hash-verified

24
25// Keep body-docstring selection and cleaning in sync with languages/python.ts.
26fn body_docstring(node: Node, src: &str) -> Option<String> {
27 let body = if node.kind() == "module" { node } else { node.child_by_field_name("body")? };
28 let first = (0..body.named_child_count())
29 .filter_map(|i| body.named_child(i))
30 .find(|c| c.kind() != "comment")?;
31 if first.kind() != "expression_statement" {
32 return None;
33 }
34 if first.named_child_count() != 1 || (0..first.child_count())
35 .filter_map(|i| first.child(i)).any(|c| c.kind() == ",") {
36 return None;
37 }
38 let mut literal = first.named_child(0)?;
39 while literal.kind() == "parenthesized_expression" {
40 literal = (0..literal.named_child_count())
41 .filter_map(|i| literal.named_child(i))
42 .find(|c| c.kind() != "comment")?;
43 }
44 let strings = if literal.kind() == "concatenated_string" {
45 (0..literal.named_child_count()).filter_map(|i| literal.named_child(i))
46 .filter(|c| c.kind() != "comment").collect::<Vec<_>>()
47 } else {
48 vec![literal]
49 };
50 let mut raw = String::new();
51 for string in strings {
52 if string.kind() != "string" {
53 return None;
54 }
55 let start = (0..string.named_child_count()).filter_map(|i| string.named_child(i))
56 .find(|c| c.kind() == "string_start")?;
57 if src[start.byte_range()].bytes().any(|b| matches!(b, b'b' | b'B' | b'f' | b'F')) {
58 return None;
59 }
60 if !(0..string.named_child_count()).filter_map(|i| string.named_child(i))
61 .any(|c| c.kind() == "string_end") {
62 return None;
63 }
64 for i in 0..string.named_child_count() {
65 if let Some(content) = string.named_child(i).filter(|c| c.kind() == "string_content") {
66 raw.push_str(&src[content.byte_range()]);
67 }
68 }
69 }
70 let cleaned = dedent_docstring(&raw);
71 if cleaned.is_empty() { None } else { Some(cleaned) }
72}
73
74fn docstring_space(c: char) -> bool {
75 // Match JavaScript trim, including BOM but excluding NEXT LINE.

Callers 2

docstring_forFunction · 0.85
extractFunction · 0.85

Calls 2

dedent_docstringFunction · 0.85
bytesMethod · 0.80

Tested by

no test coverage detected