resolveCandidatePaths function
Generate candidate paths for a package-relative file path.
Given a list of workspace root paths, produces candidates (in order):
- Normalized path relative to each workspace root
- Normalized path relative to parent directories (up to
parentLevels) - Absolute path (if applicable)
- Original un-normalized path (fallback)
Returns relative candidate strings; the caller (TS shell) converts them to URIs.
Implementation
List<String> resolveCandidatePaths(
String filePath, {
List<String> workspaceRoots = const [],
int parentLevels = 4,
}) {
final normalized = normalizePath(filePath);
final candidates = <String>[];
for (final root in workspaceRoots) {
// Direct: workspace root + normalized path
candidates.add('$root/$normalized');
// Walk up parent directories.
var parent = root;
for (var i = 0; i < parentLevels; i++) {
final lastSlash = parent.lastIndexOf('/');
if (lastSlash <= 0) {
break;
}
parent = parent.substring(0, lastSlash);
candidates.add('$parent/$normalized');
}
}
// Absolute path fallback.
if (normalized.startsWith('/')) {
candidates.add(normalized);
}
// Try original un-normalized path if it differs.
if (normalized != filePath) {
for (final root in workspaceRoots) {
candidates.add('$root/$filePath');
}
}
return candidates;
}