Pretty-printing imports #
This module defines the following utilities for pretty-printing imports:
ImportGraph.Lean.Import.prettyandprettyHeaderfor printingArray Importas source blocks or headers, respectively.- These take an optional
Import.FormatBehaviorparameter to control import sorting and grouping. By default, imports are grouped by visibility (public/(private)/all), sorted by phase and module name, and visibility groups are separated by extra newlines.
- These take an optional
headerToImportRefs(WithWhitespace) to track source positions and comments around existing imports in sourceprettyWithSourceWhitespaceto pretty-print newImports while attaching comments from source header syntax. This allows us to reformat existing imports while preserving e.g.shakeannotations (and any other informative comments).- If comments cannot be carried over (or may no longer apply), this is (by default) explained in a comment shown below the import block.
mkImportSuggestionMessage, which creates a suggestion reformatting imports. This is used by#norm_imports.
Future Work #
- Some parts of this API only work in the module system. In general, we should also support non-modules.
Extends Import with stx : TSyntax `Lean.Parser.Module.import to allow reporting at the
given import and processing of whitespace.
- stx : Lean.TSyntax `Lean.Parser.Module.import
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
Equations
Equations
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
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
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
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
Uses Import.comparePretty. Not tagged as an instance by default.
Equations
Instances For
Uses ImportRef.comparePretty. Not tagged as an instance by default.
Equations
Instances For
Whitespace (including comments) surrounding an import.
- leading : String
The leading whitespace after regrouping whitespace so that
trailingdoes not include newlines. Includes exactly one newline of ASCII whitespace at the end ofleadingif there are non-whitespace characters in it (which is suitable for normalizedimportcomments), and no ASCII whitespace at the beginning. If there are no non-ASCII-whitespace characters inleading, it is empty. - trailing : String
The trailing whitespace after regrouping whitespace so that the
trailingwhitespace 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
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Formats a with the whitespace ws wrapped around it.
Equations
Instances For
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 (
publicfirst, then private (no token), thenall), then by phase (metaimports first), then alphabetically. - grouped
(splitMeta : Bool := false)
: Import.FormatBehavior
Groups imports by visibility (first
public, then private (no token), thenall) with an extra newline in between groups. Within groups, sorts first by themetatoken then alphabetically within groups. IfsplitMeta := true(default:false), also inserts an extra newline betweenmetaimports and non-metaimports.
Instances For
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
- ImportGraph.Lean.Import.sortPretty imports = imports.qsortOrd
Instances For
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
- ImportGraph.Lean.ImportRef.sortPretty imports = imports.qsortOrd
Instances For
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
- One or more equations did not get rendered due to their size.
- ImportGraph.Lean.Import.pretty imports ImportGraph.Lean.Import.FormatBehavior.sorted = Std.Format.joinSep (ImportGraph.Lean.Import.sortPretty imports).toList (Std.format "\n")
- ImportGraph.Lean.Import.pretty imports ImportGraph.Lean.Import.FormatBehavior.none = Std.Format.joinSep imports.toList (Std.format "\n")
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.
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.toImportmatches theimpexactly.- reviewWhitespace : Array (Lean.Name × Array Lean.Import × Array (ImportRef × Whitespace))
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
- One or more equations did not get rendered due to their size.
- ImportGraph.Lean.Import.instBEqFormatErrors.beq x✝¹ x✝ = false
Instances For
Equations
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- errs.isEmpty = (errs.multipleTrailing.isEmpty && errs.reviewWhitespace.isEmpty && errs.unusedWithComments.isEmpty)
Instances For
Assumes whitespace has been created with headerToImportRefsWithWhitespace.
Equations
- One or more equations did not get rendered due to their size.
Instances For
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
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
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.