findCanonicalLibraryFor method

Library findCanonicalLibraryFor(
  1. Element e
)

Tries to find a top level library that references this element.

Implementation

Library findCanonicalLibraryFor(Element e) {
  assert(allLibrariesAdded);
  var searchElement = e;
  if (e is PropertyAccessorElement) {
    searchElement = e.variable;
  }
  if (e is GenericFunctionTypeElement) {
    searchElement = e.enclosingElement;
  }

  if (_canonicalLibraryFor.containsKey(e)) {
    return _canonicalLibraryFor[e];
  }
  _canonicalLibraryFor[e] = null;
  for (var library in publicLibraries) {
    if (library.modelElementsMap.containsKey(searchElement)) {
      for (var modelElement in library.modelElementsMap[searchElement]) {
        if (modelElement.isCanonical) {
          return _canonicalLibraryFor[e] = library;
        }
      }
    }
  }
  return _canonicalLibraryFor[e];
}