resolveCandidatePaths function

List<String> resolveCandidatePaths(
  1. String filePath, {
  2. List<String> workspaceRoots = const [],
  3. int parentLevels = 4,
})

Generate candidate paths for a package-relative file path.

Given a list of workspace root paths, produces candidates (in order):

  1. Normalized path relative to each workspace root
  2. Normalized path relative to parent directories (up to parentLevels)
  3. Absolute path (if applicable)
  4. 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;
}