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

Function dedentDocstring

src/extraction/languages/python.ts:96–116  ·  view source on GitHub ↗

* A docstring is indented to its definition, so every line after the first * carries that indentation. Strip the common prefix (PEP 257's rule: the first * line is exempt because it starts right after the opening quotes) and drop * blank edges, so the stored prose reads the same as a comment-deri

(raw: string)

Source from the content-addressed store, hash-verified

94 * blank edges, so the stored prose reads the same as a comment-derived one.
95 */
96function dedentDocstring(raw: string): string {
97 const lines = raw.replace(/\r\n?/g, '\n').split('\n').map((line) => {
98 let column = 0;
99 return Array.from(line, (char) => {
100 const width = char === '\t' ? 8 - column % 8 : 1;
101 column += width;
102 return char === '\t' ? ' '.repeat(width) : char;
103 }).join('');
104 });
105 const rest = lines.slice(1).filter((l) => l.trim().length > 0);
106 const indent = rest.length === 0
107 ? 0
108 : minOf(rest.map((l) => l.length - l.trimStart().length));
109 const out = [
110 lines[0]?.trim() ?? '',
111 ...lines.slice(1).map((l) => l.slice(indent).trimEnd()),
112 ];
113 while (out.length > 0 && out[0]!.trim() === '') out.shift();
114 while (out.length > 0 && out[out.length - 1]!.trim() === '') out.pop();
115 return out.join('\n');
116}

Callers 1

python.tsFile · 0.85

Calls 4

minOfFunction · 0.90
mapMethod · 0.80
fromMethod · 0.80
joinMethod · 0.45

Tested by

no test coverage detected