autoCompleting method

List<String> autoCompleting(
  1. String errorText,
  2. String stateName
)

Match wid to width errorText will be match

Implementation

List<String> autoCompleting(String errorText, String stateName) {
  assert(_runtimeContext != null);
  var matches = <Match>[];
  var warnedToUseSplitAutoCompleting = false;

  /// step1: find matches;
  _runtimeContext![stateName]?.whereType<JParse>().forEach((jparse) {
    if (jparse.isConst && jparse is! GroupJParse) {
      matches.addAll(enumAllMatches(errorText, jparse.constants!));
    } else if (jparse is GroupJParse &&
        jparse.groupDTokens.any((dtoken) => dtoken?.isEnum ?? false)) {
      // print(element.pattern);
      var constantRules = jparse.groupDTokens
          .map<List<String>>((dtoken) => _enumAllGiven(dtoken, stateName))
          .toList(growable: false);
      List<List<String>?>? templates;

      if (jparse.groupDTokens.any((dtoken) => dtoken is Templates)) {
        templates = jparse.groupDTokens.map((dtoken) {
          if (dtoken is Templates) {
            if (dtoken.vars != null) {
              if (warnedToUseSplitAutoCompleting == false) {
                warnedToUseSplitAutoCompleting = true;
              }
              return [...dtoken.templates]..removeWhere((template) =>
                  template.replaceAll(r'\$', '').contains(r'$'));
            }
            return dtoken.templates;
          }
          return null;
        }).toList(growable: false);
      }
      matches.addAll(
          enumAllMatches(errorText, constantRules, templates: templates));
    }
  });

  /// step2: sort
  /// 从左到右 -> 优先
  /// start 小 ->
  /// input.length 小 ->
  matches.sort((left, right) {
    if (left.start < right.start) {
      return -1;
    } else if (left.input.length < right.input.length) {
      return -1;
    }
    return 1;
  });

  if (warnedToUseSplitAutoCompleting == true) {
    print(
        '\nWarning:Template Variable Injection only work in splitAutoCompleting!\n'
        r'All templates with symbol($) will be ignored');
  }

  /// step3: 用 Set 去重
  return (<String>{}..addAll(matches.map<String>((m) => m.input))).toList();
}