reloadWorkingFile method

Future<void> reloadWorkingFile({
  1. bool notify = true,
})

Re-parses the working file, and attempts to set the current sample and element to the updated sample and element, if they exist.

Implementation

Future<void> reloadWorkingFile({bool notify = true}) async {
  if (_workingFile == null) {
    assert(_workingFileWatcher == null);
    // No working file, nothing to reload.
    return;
  }
  // Only reload if the contents have actually changed.
  final String contents = await workingFile!.readAsString();
  if (_workingFileDigest != null &&
      _computeDigest(contents) == _workingFileDigest!) {
    return;
  }
  await _loadWorkingFile(contents: contents);
  if (_currentElement != null && _elements != null) {
    // Try to find the old element.
    final Iterable<SourceElement> oldMatches = _elements!.where(
        (SourceElement element) =>
            element.elementName == _currentElement!.elementName);
    if (oldMatches.isNotEmpty) {
      _currentElement = oldMatches.first;
    } else {
      throw SnippetException(
          'Unable to find element ${_currentElement!.elementName} during reload.');
    }
  } else {
    _currentElement = null;
    _currentSample = null;
  }
  if (_currentElement != null && _currentSample != null) {
    final Iterable<CodeSample> samples = _currentElement!.samples
        .where((CodeSample sample) => sample.index == _currentSample!.index);
    if (samples.length != 1) {
      throw SnippetException(
          'Unable to find sample ${_currentSample!.index} on ${_currentElement!.elementName} during reload.');
    }
    _currentSample = samples.single;
  } else {
    _currentSample = null;
  }
  if (notify) {
    notifyListeners();
  }
}