Declaration/import context needed to interpret a selected source file. This returns paths only; repository adapters own recorded-byte retrieval.
(file: &SourceAnalysis, paths: &BTreeSet<String>)
| 100 | /// Declaration/import context needed to interpret a selected source file. |
| 101 | /// This returns paths only; repository adapters own recorded-byte retrieval. |
| 102 | pub fn context_paths(file: &SourceAnalysis, paths: &BTreeSet<String>) -> BTreeSet<String> { |
| 103 | let mut result = BTreeSet::new(); |
| 104 | if file.language == Language::Rust { |
| 105 | let mut ancestor = Path::new(&file.path).parent(); |
| 106 | while let Some(directory) = ancestor { |
| 107 | for candidate in [ |
| 108 | directory.join("lib.rs"), |
| 109 | directory.join("main.rs"), |
| 110 | directory.join("mod.rs"), |
| 111 | directory.with_extension("rs"), |
| 112 | ] { |
| 113 | let candidate = candidate.to_string_lossy().into_owned(); |
| 114 | if paths.contains(&candidate) { |
| 115 | result.insert(candidate); |
| 116 | } |
| 117 | } |
| 118 | ancestor = directory.parent().filter(|parent| *parent != directory); |
| 119 | } |
| 120 | } |
| 121 | for import in &file.imports { |
| 122 | if let Some(module) = &import.script_module { |
| 123 | result.extend(script_candidates(&file.path, module, paths)); |
| 124 | continue; |
| 125 | } |
| 126 | if file.language != Language::Rust { |
| 127 | continue; |
| 128 | } |
| 129 | let mut base = Path::new(&file.path) |
| 130 | .parent() |
| 131 | .unwrap_or_else(|| Path::new("")) |
| 132 | .to_path_buf(); |
| 133 | if !matches!( |
| 134 | Path::new(&file.path) |
| 135 | .file_name() |
| 136 | .and_then(|name| name.to_str()), |
| 137 | Some("lib.rs" | "main.rs" | "mod.rs") |
| 138 | ) { |
| 139 | base.push(Path::new(&file.path).file_stem().unwrap_or_default()); |
| 140 | } |
| 141 | for part in &import.scope { |
| 142 | base.push(part); |
| 143 | } |
| 144 | let mut target = import.target.as_slice(); |
| 145 | match target.first().map(String::as_str) { |
| 146 | Some("crate") => { |
| 147 | let mut root = PathBuf::new(); |
| 148 | for component in Path::new(&file.path).components() { |
| 149 | root.push(component.as_os_str()); |
| 150 | if component.as_os_str() == "src" { |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | base = root; |
| 155 | target = &target[1..]; |
| 156 | } |
| 157 | Some("self") => target = &target[1..], |
| 158 | Some("super") => { |
| 159 | while target.first().is_some_and(|part| part == "super") { |
no test coverage detected