generatePreferredFilepaths function

FigGenerator generatePreferredFilepaths({
  1. required List<String> names,
  2. int matchPriority = 75,
})

Equivalent to the "filepaths" template, but boosts the priority of files that match one of the names provided.

Implementation

FigGenerator generatePreferredFilepaths({
  required List<String> names,
  int matchPriority = 75,
}) {
  final namesSet = names.toSet();
  return FigGenerator(
    template: "filepaths",
    filterTemplateSuggestions: (List<FigSuggestion> paths) {
      // In Dart, we might receive dynamic or List<FigSuggestion>.
      // The spec says filterTemplateSuggestions is dynamic.
      // If we implement it as a callback in runtime:
      for (final path in paths) {
        if (path.nameSingle != null && namesSet.contains(path.nameSingle)) {
          // We can't modify FigSuggestion as it is const (in my previous thought, but let's check spec.dart).
          // FigSuggestion has final fields. So we must copy it.
          // But wait, the previous spec.dart showed `final int priority;`.
          // So we need to create a new suggestion.
          // However, TS modifies it in place: `path.priority = matchPriority`.
          // Since we can't modify, we map.
          // But `filterTemplateSuggestions` signature in TS expects mutation or return.
          // In Dart, we should probably return a new list.
        }
      }
      return paths.map((path) {
        if (path.nameSingle != null && namesSet.contains(path.nameSingle)) {
          return FigSuggestion(
            name: path.name,
            displayName: path.displayName,
            description: path.description,
            icon: path.icon,
            priority: matchPriority,
            insertValue: path.insertValue,
            replaceValue: path.replaceValue,
            type: path.type,
            hidden: path.hidden,
            isDangerous: path.isDangerous,
            deprecated: path.deprecated,
            previewComponent: path.previewComponent,
            loadSpec: path.loadSpec,
          );
        }
        return path;
      }).toList();
    },
  );
}