Skip to content

add indentationStyle to MarkupFormatter.Options #204

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 25 additions & 4 deletions Sources/Markdown/Walker/Walkers/MarkupFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,21 @@ public struct MarkupFormatter: MarkupWalker {
/// Precede Doxygen commands with an at-sign (`@`).
case at = "@"
}

public enum IndentationStyle: String, CaseIterable {
case spaces = "spaces" // use spaces for indentation
case tabs = "tabs" // Use tab characters for indentation

/// Generates an indentation string based on the current indentation style.
func indentation() -> String {
switch self {
case .spaces:
return String(" ")
case .tabs:
return String("\t")
}
}
}

// MARK: Option Properties

Expand All @@ -253,6 +268,7 @@ public struct MarkupFormatter: MarkupWalker {
var preferredLineLimit: PreferredLineLimit?
var customLinePrefix: String
var doxygenCommandPrefix: DoxygenCommandPrefix
var indentationStyle: IndentationStyle

/**
Create a set of formatting options to use when printing an element.
Expand All @@ -269,6 +285,8 @@ public struct MarkupFormatter: MarkupWalker {
- preferredHeadingStyle: The preferred heading style.
- lineLimit: The preferred maximum line length and method for splitting ``Text`` elements in an attempt to maintain that line length.
- customLinePrefix: An addition prefix to print at the start of each line, useful for adding documentation comment markers.
- indentationStyle: Whether to use spaces or tabs when indenting.

*/
public init(unorderedListMarker: UnorderedListMarker = .dash,
orderedListNumerals: OrderedListNumerals = .allSame(1),
Expand All @@ -281,7 +299,8 @@ public struct MarkupFormatter: MarkupWalker {
preferredHeadingStyle: PreferredHeadingStyle = .atx,
preferredLineLimit: PreferredLineLimit? = nil,
customLinePrefix: String = "",
doxygenCommandPrefix: DoxygenCommandPrefix = .backslash) {
doxygenCommandPrefix: DoxygenCommandPrefix = .backslash,
indentationStyle: IndentationStyle = .spaces) {
self.unorderedListMarker = unorderedListMarker
self.orderedListNumerals = orderedListNumerals
self.useCodeFence = useCodeFence
Expand All @@ -296,6 +315,7 @@ public struct MarkupFormatter: MarkupWalker {
self.thematicBreakLength = max(3, thematicBreakLength)
self.customLinePrefix = customLinePrefix
self.doxygenCommandPrefix = doxygenCommandPrefix
self.indentationStyle = indentationStyle
}

/// The default set of formatting options.
Expand Down Expand Up @@ -431,12 +451,12 @@ public struct MarkupFormatter: MarkupWalker {
prefix += "> "
} else if element is UnorderedList {
if unorderedListCount > 0 {
prefix += " "
prefix += formattingOptions.indentationStyle.indentation()
}
unorderedListCount += 1
} else if element is OrderedList {
if orderedListCount > 0 {
prefix += " "
prefix += formattingOptions.indentationStyle.indentation()
Comment on lines +454 to +459
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These indentation markers use different numbers of spaces between ordered and unordered lists to align trailing text with the previous line. While it may still parse correctly, this is a regression in behavior.

}
orderedListCount += 1
} else if !(element is ListItem),
Expand All @@ -461,11 +481,12 @@ public struct MarkupFormatter: MarkupWalker {
// Unordered list markers are of fixed length.
prefix += " "
} else if let numeralPrefix = numeralPrefix(for: parentListItem) {
// I don't know exactly what will happen when this produces an indent of 4 or more spaces.
prefix += String(repeating: " ", count: numeralPrefix.count)
}
}
if element.parent is BlockDirective {
prefix += " "
prefix += formattingOptions.indentationStyle.indentation()
}
}
return prefix
Expand Down
7 changes: 6 additions & 1 deletion Tools/markdown-tool/Commands/FormatCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ extension MarkupFormatter.Options.ThematicBreakCharacter: ExpressibleByArgument
extension MarkupFormatter.Options.EmphasisMarker: ExpressibleByArgument {}
extension MarkupFormatter.Options.PreferredHeadingStyle: ExpressibleByArgument {}
extension MarkupFormatter.Options.PreferredLineLimit.SplittingElement: ExpressibleByArgument {}
extension MarkupFormatter.Options.IndentionStyle: ExpressibleByArgument {}

extension MarkdownCommand {
/**
Expand Down Expand Up @@ -108,6 +109,9 @@ extension MarkdownCommand {
@Option(help: "The kind of element to use to split text elements while enforcing --preferred-maximum-line-length; choices: \(MarkupFormatter.Options.PreferredLineLimit.SplittingElement.allCases.map { $0.rawValue }.joined(separator: ", "))")
var lineSplittingElement: MarkupFormatter.Options.PreferredLineLimit.SplittingElement = .softBreak

@Option(help: "The indentation style to use when emitting indented blocks; choices:\(MarkupFormatter.Options.IndentationStyle.allCases.map { $0.rawValue }.joined(separator: ", "))")
var indentationStyle: MarkupFormatter.Options.IndentationStyle = .spaces

@Option(help: "Prepend this prefix to every line")
var customLinePrefix: String = ""

Expand Down Expand Up @@ -232,7 +236,8 @@ extension MarkdownCommand {
condenseAutolinks: condenseAutolinks,
preferredHeadingStyle: preferredHeadingStyle,
preferredLineLimit: preferredLineLimit,
customLinePrefix: customLinePrefix)
customLinePrefix: customLinePrefix,
indentationStyle: indentationStyle)
let formatted = document.format(options: formatOptions)
print(formatted)
if checkStructuralEquivalence {
Expand Down