buildFileIndex creates a multi-key index for fast import resolution
(files []FileInfo, goModule string)
| 104 | |
| 105 | // buildFileIndex creates a multi-key index for fast import resolution |
| 106 | func buildFileIndex(files []FileInfo, goModule string) *fileIndex { |
| 107 | idx := &fileIndex{ |
| 108 | byExact: make(map[string][]string), |
| 109 | bySuffix: make(map[string][]string), |
| 110 | byDir: make(map[string][]string), |
| 111 | goPkgs: make(map[string][]string), |
| 112 | } |
| 113 | |
| 114 | for _, f := range files { |
| 115 | path := f.Path |
| 116 | dir := filepath.Dir(path) |
| 117 | if dir == "." { |
| 118 | dir = "" |
| 119 | } |
| 120 | |
| 121 | // Index by directory |
| 122 | idx.byDir[dir] = append(idx.byDir[dir], path) |
| 123 | |
| 124 | // Index by exact path (without extension for fuzzy matching) |
| 125 | idx.byExact[path] = append(idx.byExact[path], path) |
| 126 | noExt := strings.TrimSuffix(path, filepath.Ext(path)) |
| 127 | idx.byExact[noExt] = append(idx.byExact[noExt], path) |
| 128 | |
| 129 | // Index by all path suffixes (for nested package resolution) |
| 130 | // e.g., "llm-server/app/core/config.py" indexed as: |
| 131 | // - "app/core/config.py" |
| 132 | // - "core/config.py" |
| 133 | // - "config.py" |
| 134 | parts := strings.Split(path, string(filepath.Separator)) |
| 135 | for i := 1; i < len(parts); i++ { |
| 136 | suffix := strings.Join(parts[i:], string(filepath.Separator)) |
| 137 | idx.bySuffix[suffix] = append(idx.bySuffix[suffix], path) |
| 138 | // Also without extension |
| 139 | noExt := strings.TrimSuffix(suffix, filepath.Ext(suffix)) |
| 140 | idx.bySuffix[noExt] = append(idx.bySuffix[noExt], path) |
| 141 | } |
| 142 | |
| 143 | // Go package index |
| 144 | if strings.HasSuffix(path, ".go") && goModule != "" { |
| 145 | pkgPath := goModule |
| 146 | if dir != "" { |
| 147 | pkgPath = goModule + "/" + dir |
| 148 | } |
| 149 | idx.goPkgs[pkgPath] = append(idx.goPkgs[pkgPath], path) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return idx |
| 154 | } |
| 155 | |
| 156 | // fuzzyResolve converts an import path to actual file paths using universal matching |
| 157 | // No language-specific switch - relies on pattern matching against file index |
no outgoing calls