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
    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      Equations
      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.

        Equations
        • One or more equations did not get rendered due to their size.
        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.

          Equations
          • One or more equations did not get rendered due to their size.
          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.

            Equations
            • One or more equations did not get rendered due to their size.
            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.

              Equations
              • One or more equations did not get rendered due to their size.
              Instances For

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

                Equations
                • One or more equations did not get rendered due to their size.
                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.

                  Equations
                  Instances For

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

                    Equations
                    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
                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For
                          Equations
                          Instances For
                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For
                              @[instance_reducible]

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

                              Equations
                              • One or more equations did not get rendered due to their size.
                              @[instance_reducible]
                              Equations
                              • One or more equations did not get rendered due to their size.
                              @[inline]

                              Formats a with the whitespace ws wrapped around it.

                              Equations
                              Instances For
                                @[inline]

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

                                Equations
                                Instances For
                                  @[inline]

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

                                  Equations
                                  Instances For
                                    @[instance_reducible]
                                    Equations
                                    • One or more equations did not get rendered due to their size.

                                    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.

                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    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.

                                        Equations
                                        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.

                                          Equations
                                          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.

                                            Equations
                                            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).

                                              Equations
                                              • One or more equations did not get rendered due to their size.
                                              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
                                                  Equations
                                                  Instances For
                                                    Equations
                                                    • One or more equations did not get rendered due to their size.
                                                    Instances For

                                                      Assumes whitespace has been created with headerToImportRefsWithWhitespace.

                                                      Equations
                                                      • One or more equations did not get rendered due to their size.
                                                      Instances For
                                                        @[instance_reducible]
                                                        Equations
                                                        • One or more equations did not get rendered due to their size.

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

                                                        Equations
                                                        • One or more equations did not get rendered due to their size.
                                                        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.

                                                          Equations
                                                          • One or more equations did not get rendered due to their size.
                                                          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.

                                                            Equations
                                                            • One or more equations did not get rendered due to their size.
                                                            Instances For