fromEnvironment static method

CompletionState? fromEnvironment([
  1. Map<String, String>? environmentOverride
])

Creates a CompletionState from the environment variables set by the shell script.

Implementation

static CompletionState? fromEnvironment([
  Map<String, String>? environmentOverride,
]) {
  final environment = environmentOverride ?? Platform.environment;
  final compCword = environment['COMP_CWORD'];
  final compPoint = environment['COMP_POINT'];
  final line = environment['COMP_LINE'];

  if (compCword == null || compPoint == null || line == null) {
    return null;
  }

  final cwordInt = int.tryParse(compCword);
  final pointInt = int.tryParse(compPoint);

  if (cwordInt == null || pointInt == null) {
    return null;
  }

  final args = line.trimLeft().split(' ').skip(1);

  if (pointInt < line.length) {
    // Do not complete when the cursor is not at the end of the line
    return null;
  }

  if (args.isNotEmpty && args.take(args.length - 1).contains('--')) {
    // Do not complete if there is an argument terminator in the middle of
    // the sentence
    return null;
  }

  return CompletionState(
    cword: cwordInt,
    point: pointInt,
    line: line,
    args: args,
  );
}