(context: ResolutionContext)
| 686 | } |
| 687 | |
| 688 | function buildPythonModuleIndex(context: ResolutionContext): PythonModuleIndex { |
| 689 | const files = new Set<string>(); |
| 690 | const packageDirs = new Set<string>(); |
| 691 | const dirs = new Set<string>(['']); |
| 692 | for (const f of context.getAllFiles()) { |
| 693 | const norm = f.replace(/\\/g, '/'); |
| 694 | if (!PYTHON_MODULE_FILE.test(norm)) continue; |
| 695 | files.add(norm); |
| 696 | const dir = parentDir(norm); |
| 697 | if (norm === '__init__.py' || norm.endsWith('/__init__.py')) packageDirs.add(dir); |
| 698 | for (let d = dir; d && !dirs.has(d); d = parentDir(d)) dirs.add(d); |
| 699 | } |
| 700 | |
| 701 | // Where a service's roots may answer from: everywhere (null), only itself |
| 702 | // when a package encloses it, or the nearest enclosing directory named as |
| 703 | // non-project code. |
| 704 | const serviceReach = (service: string): string | null => { |
| 705 | if (packageDirs.has(service)) return service; |
| 706 | for (let a = parentDir(service); a; a = parentDir(a)) { |
| 707 | if (packageDirs.has(a)) return service; |
| 708 | if (PYTHON_NON_PROJECT_DIRS.has(a.slice(a.lastIndexOf('/') + 1))) return a; |
| 709 | } |
| 710 | return null; |
| 711 | }; |
| 712 | |
| 713 | const projectRoots = new Set<string>(['']); |
| 714 | const localRoots = new Map<string, string>(); |
| 715 | const addLocal = (root: string, reach: string): void => { |
| 716 | const prior = localRoots.get(root); |
| 717 | // Two claims on one root: the wider reach wins. |
| 718 | if (prior === undefined || isWithin(prior, reach)) localRoots.set(root, reach); |
| 719 | }; |
| 720 | for (const dir of packageDirs) { |
| 721 | let top = dir; |
| 722 | while (top && packageDirs.has(parentDir(top))) top = parentDir(top); |
| 723 | if (top) addLocal(parentDir(top), parentDir(top)); |
| 724 | } |
| 725 | for (const service of dirs) { |
| 726 | const hasConfig = |
| 727 | files.has(joinRel(service, 'setup.py')) || |
| 728 | context.fileExists(joinRel(service, 'pyproject.toml')) || |
| 729 | context.fileExists(joinRel(service, 'setup.cfg')); |
| 730 | if (!hasConfig && service !== '') continue; |
| 731 | const reach = service === '' ? null : serviceReach(service); |
| 732 | const roots = [service]; |
| 733 | const src = joinRel(service, 'src'); |
| 734 | if (dirs.has(src) && !packageDirs.has(src)) roots.push(src); |
| 735 | for (const declared of declaredPythonRoots(context, service)) { |
| 736 | if (dirs.has(declared)) roots.push(declared); |
| 737 | } |
| 738 | for (const root of roots) { |
| 739 | if (reach === null) projectRoots.add(root); |
| 740 | else addLocal(root, reach); |
| 741 | } |
| 742 | } |
| 743 | for (const root of projectRoots) localRoots.delete(root); |
| 744 | return { files, packageDirs, dirs, projectRoots, localRoots, headsByName: new Map() }; |
| 745 | } |
no test coverage detected