(source: { text: string; path: string; kind: 'file' | 'selection'; startLine: number; endLine: number }, language: 'en' | 'de')
| 161 | * is and how long, a module's docstring, its definitions, and the code facts of a single marked |
| 162 | * symbol (B3). `names` are the definitions' names, by which a question may ask about it. */ |
| 163 | export function codeSourceFacts(source: { text: string; path: string; kind: 'file' | 'selection'; startLine: number; endLine: number }, language: 'en' | 'de'): { summary: string[]; code?: string; names: string[] } { |
| 164 | const words = chatRound3Text[language]; |
| 165 | const fence = FENCES[extension(source.path)] ?? ''; |
| 166 | const kind = words.codeKind(LANGUAGES[fence] ?? ''); |
| 167 | const name = `\`${(source.path.split('/').pop() ?? source.path).replace(/`/g, "'")}\``; |
| 168 | const lines = source.text.replace(/\r\n?/g, '\n').replace(/\n$/, '').split('\n'); |
| 169 | const defined = source.text.trim() ? definitionsOf(lines, fence) : []; |
| 170 | const names = defined.map(item => item.split(' ').pop()!); |
| 171 | const listed = (where: 'file' | 'marked') => defined.length ? [words.definitions(defined.length, defined.slice(0, DEFINITIONS).map(item => `\`${item}\``), where)] : []; |
| 172 | if (source.kind === 'file') { |
| 173 | const doc = fence === 'python' ? moduleDocstring(lines) : undefined; |
| 174 | return { summary: [words.outlineHeading(name, kind, lines.length), ...doc ? [words.moduleDocstring(bounded(doc))] : [], ...listed('file')], names }; |
| 175 | } |
| 176 | const marked = words.marked(source.startLine, source.endLine, name, kind); |
| 177 | // One marked class or function: its own lines say what it declares (B2). |
| 178 | const first = lines.find(line => line.trim() && !/^\s*(?:#|\/\/|@)/.test(line)); |
| 179 | const single = defined.length === 1 && first !== undefined && (fence === 'python' ? PYTHON_DEFINITION : DEFINITION).test(first.trim()); |
| 180 | const facts = single ? codeFacts(source, { name: names[0] }) : undefined; |
| 181 | return { summary: [marked, ...single ? [] : listed('marked')], ...facts ? { code: codeFactsMarkdown(facts, language) } : {}, names }; |
| 182 | } |
| 183 | |
| 184 | /** "In the source:" with the lines as a code block and what did not fit, in the language of the question. */ |
| 185 | export function codeFactsMarkdown(facts: CodeFacts, language: 'en' | 'de'): string { |
no test coverage detected