Future _parseMainDart()

in packages/snippets/lib/flutter_sample_editor.dart [178:230]


  Future<void> _parseMainDart(
      {required Map<String, List<String>> sections,
      required List<String> sectionOrder}) async {
    final List<String> mainDartLines = await mainDart.readAsLines();
    final RegExp sectionMarkerRe = RegExp(
        r'^([/]{2}\*) ((?<direction>\S)\3{7}) (?<name>[-a-zA-Z0-9]+).*$');
    String? currentSection;
    int firstTrailingEmpty = -1;
    sections.clear();
    sectionOrder.clear();
    for (String line in mainDartLines) {
      final RegExpMatch? match = sectionMarkerRe.firstMatch(line);
      if (match != null) {
        if (match.namedGroup('direction')! == '\u25bc' /* ▼ */) {
          // Start of section, initialize it.
          currentSection = match.namedGroup('name');
          sections[currentSection!] ??= <String>[];
          sectionOrder.add(currentSection);
        } else {
          // End of a section
          // Remove any blank lines at the end
          if (firstTrailingEmpty >= 0 &&
              firstTrailingEmpty < sections[currentSection]!.length) {
            sections[currentSection]!.removeRange(
                firstTrailingEmpty, sections[currentSection]!.length);
          }
          // Remove the section if it's empty.
          if (sections[currentSection]!.isEmpty) {
            sections.remove(currentSection);
          }
          currentSection = null;
          firstTrailingEmpty = -1;
        }
      } else {
        if (currentSection != null) {
          if (currentSection == 'description') {
            // Strip comment markers off of description lines.
            line = line.replaceFirst(RegExp(r'^\s*///? ?'), '');
          }
          // Skip empty lines at the beginning of a section.
          final bool isEmpty = line.trim().isEmpty;
          if (sections[currentSection]!.isEmpty && isEmpty) {
            continue;
          }
          sections[currentSection]!.add(line);
          if (!isEmpty) {
            // If we find a non-empty line, reset the trailing empty index.
            firstTrailingEmpty = sections[currentSection]!.length;
          }
        }
      }
    }
  }