Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown: handle LineEndings.windows correctly #4768

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions scalafmt-core/shared/src/main/scala/org/scalafmt/Scalafmt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,16 @@ object Scalafmt {
file: String,
range: Set[Range],
): Try[String] =
if (FileOps.isMarkdown(file)) MarkdownParser
.transformMdoc(code)(doFormatOne(_, style, file, range))
else doFormatOne(code, style, file, range)
if (FileOps.isMarkdown(file)) {
val mdocStyle = style.withLineEndings(LineEndings.preserve)
val res = MarkdownParser
.transformMdoc(code)(doFormatOne(_, mdocStyle, file, range))
style.lineEndings match {
case Some(LineEndings.unix) => res.map(_.replaceAll("\r*\n", "\n"))
case Some(LineEndings.windows) => res.map(_.replaceAll("\r*\n", "\r\n"))
case _ => res
}
} else doFormatOne(code, style, file, range)

private[scalafmt] def toInput(code: String, file: String): Input = {
val fileInput = Input.VirtualFile(file, code).withTokenizerOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ private[scalafmt] object MarkdownParser {
val parts = MarkdownPart.parse(code, settings)
parts.foreach {
case p: CodeFence if p.getMdocMode.isDefined =>
fmt(p.body.value) match {
case Success(b) => hadFencedParts = true; p.newBody = Some(b.trim)
val old = p.body.value
fmt(old) match {
case Success(b) =>
hadFencedParts = true
p.newBody = Some(if (old.endsWith("\n")) b else b.trim)
case failure => return failure // RETURNING!
}
case _ =>
Expand Down