code: String lookupName(ClassName className) {
      // If the top level simple name is masked by a current type variable, use the canonical name.
      String topLevelSimpleName = className.topLevelClassName().simpleName();
      if (currentTypeVariables.contains(topLevelSimpleName)) {
        return className.canonicalName;
      }
  
      // Find the shortest suffix of className that resolves to className. This uses both local type
      // names (so `Entry` in `Map` refers to `Map.Entry`). Also uses imports.
      boolean nameResolved = false;
      for (ClassName c = className; c != null; c = c.enclosingClassName()) {
        ClassName resolved = resolve(c.simpleName());
        nameResolved = resolved != null;
  
        if (resolved != null && Objects.equals(resolved.canonicalName, c.canonicalName)) {
          int suffixOffset = c.simpleNames().size() - 1;
          return join(".", className.simpleNames().subList(
              suffixOffset, className.simpleNames().size()));
        }
      }
  
      // If the name resolved but wasn't a match, we're stuck with the fully qualified name.
      if (nameResolved) {
        return className.canonicalName;
      }
  
      // If the class is in the same package, we're done.
      if (Objects.equals(packageName, className.packageName())) {
        referencedNames.add(topLevelSimpleName);
        return join(".", className.simpleNames());
      }
  
      // We'll have to use the fully-qualified name. Mark the type as importable for a future pass.
      if (!javadoc) {
        importableType(className);
      }
  
      return className.canonicalName;
    }
docs: Returns the best name to identify {@code className} with in the current context. This uses the
  available imports and the current scope to find the shortest name available. It does not honor
  names visible due to inheritance.