Questions about a project, from the names its own index holds: two names, three in a sentence, eight.
(dir, sets)
| 236 | |
| 237 | /** Questions about a project, from the names its own index holds: two names, three in a sentence, eight. */ |
| 238 | function chooseQuestions(dir, sets) { |
| 239 | const db = new DatabaseSync(join(dir, '.codegraph', 'codegraph.db'), { readOnly: true }); |
| 240 | const rows = db |
| 241 | .prepare("SELECT name, file_path AS file, start_line AS line, end_line AS last FROM nodes WHERE kind NOT IN ('file','import','export','parameter') ORDER BY file_path, start_line, name") |
| 242 | .all(); |
| 243 | db.close(); |
| 244 | const defined = new Map(); |
| 245 | for (const row of rows) defined.set(row.name, (defined.get(row.name) ?? 0) + 1); |
| 246 | const asked = sets > 1 ? rows.filter((row) => !/(^|\/)(__tests__|tests?|fixtures?|node_modules|dist|target|vendor|docs?)\//.test(row.file) && !/\.(test|spec)\./.test(row.file)) : rows; |
| 247 | const eligible = asked.filter((row) => WRITTEN_AS_CODE.test(row.name) && /^[\p{L}\p{N}_$]+$/u.test(row.name) && row.name.length >= 3 && defined.get(row.name) <= 3); |
| 248 | const bodied = eligible.filter((row) => row.last > row.line); |
| 249 | const seen = new Set(); |
| 250 | const unique = (bodied.length >= 8 ? bodied : eligible).filter((row) => !seen.has(row.name) && seen.add(row.name)); |
| 251 | const questions = {}; |
| 252 | for (let set = 0; set < sets; set++) { |
| 253 | const chosen = []; |
| 254 | const take = Math.min(8, unique.length); |
| 255 | for (let at = 0; at < take; at++) { |
| 256 | const row = unique[(Math.floor((at * (unique.length - 1)) / Math.max(1, take - 1)) + set * 37) % unique.length]; |
| 257 | if (!chosen.some((each) => each.name === row.name)) chosen.push(row); |
| 258 | } |
| 259 | const named = (count) => chosen.slice(0, count).map((row) => [row.name, row.file, row.line]); |
| 260 | const key = (kind) => (sets > 1 ? `${kind}#${set}` : kind); |
| 261 | const names = chosen.map((row) => row.name); |
| 262 | if (names.length >= 2) questions[key('two')] = { query: `${names[0]} ${names[1]}`, names: named(2) }; |
| 263 | if (names.length >= 3) questions[key('three')] = { query: `how does ${names[0]} use ${names[1]} and ${names[2]}`, names: named(3) }; |
| 264 | if (names.length >= 8) questions[key('eight')] = { query: names.join(' '), names: named(8) }; |
| 265 | } |
| 266 | return questions; |
| 267 | } |
| 268 | |
| 269 | /** Ask each question; for each, which named definitions the answer shows and how long it is. */ |
| 270 | async function answersTo(dir, questions) { |