| 75 | } |
| 76 | |
| 77 | function pythonFacts(lines: readonly string[], name?: string): CodeFacts | undefined { |
| 78 | const definition = new RegExp(`^\\s*(?:async\\s+)?(?:def|class)\\s+${name ? `${escape(name)}\\b` : '\\w'}`); |
| 79 | let start = lines.findIndex(line => definition.test(line)); |
| 80 | if (start < 0) start = lines.findIndex(line => /^\s*(?:async\s+)?(?:def|class)\s+\w/.test(line)); |
| 81 | if (start < 0) return undefined; |
| 82 | const base = indentOf(lines[start]); |
| 83 | const shift = (line: string) => line.slice(Math.min(base, indentOf(line))); |
| 84 | const header = statement(lines, start); |
| 85 | const isClass = /^class\s/.test(header.text); |
| 86 | const inner = lines.slice(header.end + 1).find(line => line.trim() && !line.trim().startsWith('#')); |
| 87 | const pad = ' '.repeat(Math.max(4, inner ? indentOf(inner) - base : 4)); |
| 88 | const out = [bounded(header.text), ...docstring(lines, header.end, pad)]; |
| 89 | const bodyStart = docstringEnd(lines, header.end) + 1; |
| 90 | const body = lines.slice(bodyStart).filter(line => line.trim() && !line.trim().startsWith('#') && indentOf(line) > base); |
| 91 | if (!isClass) { |
| 92 | const shown = body.length <= SHORT_BODY ? body.map(line => bounded(shift(line).trimEnd())) : []; |
| 93 | const room = Math.max(0, CODE_FACT_LINES - out.length); |
| 94 | return { lines: [...out, ...shown.slice(0, room)], more: body.length - Math.min(shown.length, room), unit: 'lines', fence: 'python' }; |
| 95 | } |
| 96 | // Members at the class's own body indent: assignments first, then methods with their decorators. |
| 97 | const memberIndent = inner ? indentOf(inner) : base + 4; |
| 98 | const assignments: string[][] = [], methods: string[][] = []; |
| 99 | let decorators: string[] = []; |
| 100 | for (let index = bodyStart; index < lines.length; index++) { |
| 101 | const line = lines[index]; |
| 102 | if (!line.trim() || line.trim().startsWith('#')) continue; |
| 103 | if (indentOf(line) <= base) break; |
| 104 | if (indentOf(line) !== memberIndent) continue; |
| 105 | const { text, end } = statement(lines, index); |
| 106 | if (text.startsWith('@')) { decorators.push(`${pad}${bounded(text)}`); index = end; continue; } |
| 107 | if (/^(?:async\s+)?(?:def|class)\s/.test(text)) methods.push([...decorators, `${pad}${bounded(text)}`]); |
| 108 | else if (/^[A-Za-z_]\w*(?:\s*:[^=]+)?\s*=[^=]/.test(text)) assignments.push([`${pad}${bounded(text)}`]); |
| 109 | decorators = []; |
| 110 | index = end; |
| 111 | } |
| 112 | let more = 0; |
| 113 | for (const member of [...assignments, ...methods]) { |
| 114 | if (out.length + member.length <= CODE_FACT_LINES) out.push(...member); else more++; |
| 115 | } |
| 116 | return { lines: out, more, unit: 'members', fence: 'python' }; |
| 117 | } |
| 118 | |
| 119 | /** The line that declares `name`, or the first line of code; without its opening brace. */ |
| 120 | function definitionLine(lines: readonly string[], name: string | undefined, fence: string): CodeFacts | undefined { |