Documentation

ImportGraph.Imports.Pretty

Pretty-printing imports #

This module defines the following utilities for pretty-printing imports:

Future Work #

Extends Import with stx : TSyntax `Lean.Parser.Module.import to allow reporting at the given import and processing of whitespace.

Instances For

    Returns the module Ident (following (public)? (meta)? import (all)? of a given ImportRef. Returns .missing if the identifier somehow has a dangling dot (the parser should, however, never succeed in producing such syntax) or is otherwise malformed.

    Instances For
      def ImportGraph.Lean.headerToImportStx (header : Lean.TSyntax `Lean.Parser.Module.header) :
      Lean.TSyntaxArray `Lean.Parser.Module.import

      Destructures header syntax ((module)? (prelude)? $imports*) into an array of Imports together with the import syntax that gave rise to them. Creates an array of .missing when the syntax is malformed. See also headerToImports.

      Instances For
        def ImportGraph.Lean.getModule (header : Lean.TSyntax `Lean.Parser.Module.header) :

        Destructures header syntax ((module)? (prelude)? $imports*) into an array of Imports together with the import syntax that gave rise to them. See also headerToImports.

        Instances For
          def ImportGraph.Lean.headerToImportRefs (header : Lean.TSyntax `Lean.Parser.Module.header) :

          Destructures header syntax ((module)? (prelude)? $imports*) into an array of Imports together with the import syntax that gave rise to them. See also headerToImports.

          Instances For

            Considers imports with public to come first; then those without all; then those with meta; then compares the modules alphabetically.

            Instances For

              Considers imports with public to come first; then those without all; then those with meta; then compares the modules alphabetically; then compares starting source position, c considering those without a starting position to come first.

              Instances For
                @[instance_reducible]

                Uses Import.comparePretty. Not tagged as an instance by default.

                Instances For
                  @[instance_reducible]

                  Uses ImportRef.comparePretty. Not tagged as an instance by default.

                  Instances For

                    Whether two Array Imports contain the same imports when considered as a (multi)set.

                    Instances For

                      Whitespace (including comments) surrounding an import.

                      • leading : String

                        The leading whitespace after regrouping whitespace so that trailing does not include newlines. Includes exactly one newline of ASCII whitespace at the end of leading if there are non-whitespace characters in it (which is suitable for normalized import comments), and no ASCII whitespace at the beginning. If there are no non-ASCII-whitespace characters in leading, it is empty.

                      • trailing : String

                        The trailing whitespace after regrouping whitespace so that the trailing whitespace has no newlines. May have ASCII whitespace on the left.

                      Instances For
                        @[instance_reducible]

                        Sorts whitespace first by leading length, then trailing length, then alphabetical in each.

                        @[inline]

                        Formats a with the whitespace ws wrapped around it.

                        Instances For
                          @[inline]

                          Whitespace with empty strings for both the leading and trailing values.

                          Instances For
                            @[inline]

                            Whether both the leading and trailing fields of Whitespace are empty.

                            Instances For

                              Convert parsed header syntax into ImportRefs after regrouping leading and trailing whitespace so that trailing whitespace has no newlines, and extract the leading and trailing whitespace into a useful ASCII-whitespace normalized form in Whitespace.

                              Instances For

                                A configuration option guiding the behavior of import block pretty-printing functions (e.g ImportGraph.Lean.Import.pretty) which determines the order and grouping of imports.

                                • none : Import.FormatBehavior

                                  Does not sort or group imports at all.

                                • sorted : Import.FormatBehavior

                                  Sorts an array of imports first by visibility (public first, then private (no token), then all), then by phase (meta imports first), then alphabetically.

                                • grouped (splitMeta : Bool := false) : Import.FormatBehavior

                                  Groups imports by visibility (first public, then private (no token), then all) with an extra newline in between groups. Within groups, sorts first by the meta token then alphabetically within groups. If splitMeta := true (default: false), also inserts an extra newline between meta imports and non-meta imports.

                                Instances For
                                  @[inline]

                                  Sorts an array of imports first by visibility (public first, then private (no token), then all), then by phase (meta imports first), then alphabetically.

                                  Instances For
                                    @[inline]

                                    Sorts an array of imports first by visibility (public first, then private (no token), then all), then by phase (meta imports first), then alphabetically.

                                    Instances For
                                      @[inline]

                                      Pretty-print an array of Imports as a block of import statements (not including module and/ or prelude). The grouping and sorting behavior may be controlled by the formatAs argument.

                                      By default, this function groups imports by visibility (public, private (no token), or all) with an extra newline in between groups, and within groups, sorts first by the meta token then alphabetically. See Import.FormatBehavior for more details.

                                      Instances For

                                        Pretty-print an array of Imports as a full header, including te module and prelude tokens as given by isModule (default: true) and isPrelude (default: false).

                                        Instances For

                                          Descriptions of cases in which the import formatting procedure does not know how to proceed.

                                          • multipleTrailing : Array (Lean.Import × Array (ImportRef × String))

                                            If we have multiple versions of the same import, and each have their own nontrivial trailing whitespace, we don't necessarily know how to combine them. In this case, each ref.toImport matches the imp exactly.

                                          • If a module was originally imported in one manner with nontrivial trailing whitespace, but we now import it in a different manner (e.g. if it was imported multiple times with different modifiers, and we've normalized the imports), then the user should review the whitespace (which may be a shake annotation) to make sure it still makes sense. We also tell the user to review the leading whitespace, just in case.

                                          • unusedWithComments : Array (ImportRef × Whitespace)

                                            If we no longer use some imports that had nontrivial whitespace, record them here.

                                          Instances For

                                            Wraps the Whitespace associated with each import around it, and formats the array of imports as an import block according to formatAs.

                                            Instances For
                                              @[inline]

                                              Formats the modified imps and attaches whitespace from the corresponding import in sourceImps when doing so is unambiguous. Ambiguity encountered while assigning nontrivial whitespace is recorded in the returned Array Import.FormatError.

                                              If includeErrorsAsComment := true (the default), the errors are included as a source comment following the formatted import block.

                                              We assume sourceImps has been created by ImportGraph.headerToImportRefsWithWhitespace.

                                              Instances For
                                                def ImportGraph.Lean.Import.mkImportSuggestionMessage (ref : Lean.Syntax) (newImps : Array Lean.Import) (sourceImps : Array (ImportRef × Whitespace)) (formatAs : Import.FormatBehavior := FormatBehavior.grouped) (toCodeActionTitle? : Option (String → String) := some fun (x : String) => "Modify imports") (includeErrorsAsComment : Bool := true) :

                                                Create a message that suggests replacing sourceImps with newImps. Includes errors as a comment. Returns none if the suggestion is would not modify the source at all (including whitespace).

                                                Note that the resulting widget will show a diff view if the resulting errs : Import.FormatErrors satisfies errs.isEmpty or includeErrorsAsComment := false. Otherwise, if a comment is included, a try-this widget prefixed by an [apply] is shown. (This is because the repeated imports in errs don't behave well in the diff view.)

                                                ref is passed to Meta.Hint.mkSuggestionsMessage.

                                                Instances For