lean4-htt/tests/lean/run/3791.lean
Kyle Miller b56ad5a7d2
fix: apply newlines before and after comments when formatting syntax (#8626)
This PR closes #3791, making sure that the Syntax formatter inserts
whitespace before and after comments in the leading and trailing text of
Syntax to avoid having comments comment out any following syntax, and to
avoid comments' lexical syntax from being interpreted as being part of
another syntax. If the text contains newlines before or after any
comments, they are formatted as hard newlines rather than soft newlines.
For example, `--` comments will have a hard newline after them. Note:
metaprograms generating Syntax with comments should be sure to include
newlines at the ends of `--` comments.
2025-06-26 19:23:35 +00:00

165 lines
4.4 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Lean
/-!
# Formatter: preserve hard newlines around comments (#3791)
This is a regression test for https://github.com/leanprover/lean4/issues/3791
The issue was that the formatter was not preserving whether there were hard newlines
around comments in Syntax.
Note: one should turn off the sanitizer when pretty printing user syntax,
since the sanitizer clears comments from all identifiers.
-/
open Lean
deriving instance Repr for Std.Format.FlattenBehavior, Format
elab "#parse_cmd " update?:("!")? dbg?:("?")? s:str : command => Elab.Command.liftTermElabM do
withOptions (fun opts => opts.set pp.sanitizeNames.name false) do
let s := s.getString
let mut cmd ← ofExcept <| Parser.runParserCategory (← getEnv) `command s
if update?.isSome then
cmd := cmd.updateLeading
let fmt ← PrettyPrinter.ppCommand ⟨cmd⟩
if dbg?.isSome then
dbg_trace "{repr cmd}"; dbg_trace "{repr fmt}"
logInfo (toString f!"{fmt}")
elab "#parse_term " update?:("!")? dbg?:("?")? s:str : command => Elab.Command.liftTermElabM do
withOptions (fun opts => opts.set pp.sanitizeNames.name false) do
let s := s.getString
let mut term ← ofExcept <| Parser.runParserCategory (← getEnv) `term s
if update?.isSome then
term := term.updateLeading
let fmt ← PrettyPrinter.ppTerm ⟨term⟩
if dbg?.isSome then
dbg_trace "{repr term}"; dbg_trace "{repr fmt}"
logInfo (toString f!"{fmt}")
/-!
Newline before and after comment, puts hard newlines before and after.
("commented out" comes from #3791; formerly `1 = 1` was being commented out by the comment
in the output.)
-/
/--
info: theorem ohno :
-- commented out:
1 = 1 :=
trivial
-/
#guard_msgs in #parse_cmd "theorem ohno : \n\
-- commented out:\n\
1 = 1 := trivial"
/-!
No newline before but a newline after, puts hard newline after.
-/
/--
info: theorem ohno : /- commented out: -/
1 = 1 :=
trivial
-/
#guard_msgs in #parse_cmd "theorem ohno : /- commented out: -/\n\
1 = 1 := trivial"
/-!
Now newlines before or after, puts no newlines.
-/
/--
info: theorem ohno : /- commented out: -/ 1 = 1 :=
trivial
-/
#guard_msgs in #parse_cmd "theorem ohno : /- commented out: -/ 1 = 1 := trivial"
/-!
Inserts whitespace before comment, but not a hard newline.
There's a hard newline after the comment.
-/
/--
info: ‹∀ x : , -- commented out:
x = x
-/
#guard_msgs in #parse_term "∀ x : ,-- commented out:\n\
x = x"
/-!
When it's wide enough, gets a discretionary newline after.
-/
/--
info: ‹∀ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, /- commented out: -/
x = x
-/
#guard_msgs in
#parse_term "∀ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,/- commented out: -/x = x"
/-!
When it's wide enough, gets a discretionary newline before.
-/
/--
info: ‹∀ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
/- commented out: -/ x = x
-/
#guard_msgs in
#parse_term "∀ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,/- commented out: -/x = x"
/-!
Hard newlines before and after in terms.
-/
/--
info: ‹∀ x : ,
-- commented out:
x = x
-/
#guard_msgs in #parse_term "∀ x : , \n\
-- commented out:\n\
x = x"
/-!
No newlines before or after, but discretionary whitespace.
-/
/-- info: ‹∀ x : , /- commented out: -/ x = x -/
#guard_msgs in #parse_term "∀ x : , \
/- commented out: -/\
x = x"
/-!
Inline comments
-/
/-- info: [1, 2, 3 /- last comment -/ ] -/
#guard_msgs in #parse_term "[\n1,2,3 /- last comment -/]"
/--
info: [1, 2,
3 -- last comment
]
-/
#guard_msgs in #parse_term "[\n1,2,3 -- last comment\n]"
/-- info: (f /- c -/ x) -/
#guard_msgs in #parse_term "(f /- c -/ x)"
/-!
Comments after a term.
-/
/-- info: (x /-comment-/ ) -/
#guard_msgs in #parse_term "(x /-comment-/)"
/--
info: (x --comment
)
-/
#guard_msgs in #parse_term "(x --comment\n)"
/-!
Comments before a term. The `!` mode applies `updateLeading`, and the newline causes the comment
to be applied to the succeeding term.
-/
/--
info: (
/-comment-/ 1)
-/
#guard_msgs in #parse_term ! "(\n/-comment-/ 1)"
/--
info: (
-- comment
1)
-/
#guard_msgs in #parse_term ! "(\n-- comment\n 1)"