Skip to content

Commit a079352

Browse files
committed
Pass nil as the default indentationWidth
1 parent a050bdc commit a079352

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ open class BasicFormat: SyntaxRewriter {
4949
private var previousToken: TokenSyntax? = nil
5050

5151
public init(
52-
indentationWidth: Trivia = .spaces(4),
52+
indentationWidth: Trivia? = nil,
5353
initialIndentation: Trivia = [],
5454
viewMode: SyntaxTreeViewMode = .sourceAccurate
5555
) {
56-
self.indentationWidth = indentationWidth
56+
// Default to 4 spaces if no indentation was passed.
57+
// In the future, we could consider inferring the indentation width from the
58+
// source file to format in case it is already partially formatted.
59+
self.indentationWidth = indentationWidth ?? .spaces(4)
5760
self.indentationStack = [(indentation: initialIndentation, isUserDefined: false)]
5861
super.init(viewMode: viewMode)
5962
}

Sources/SwiftSyntaxMacroExpansion/MacroExpansion.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public func expandFreestandingMacro(
9292
macroRole: MacroRole,
9393
node: FreestandingMacroExpansionSyntax,
9494
in context: some MacroExpansionContext,
95-
indentationWidth: Trivia = .spaces(4)
95+
indentationWidth: Trivia? = nil
9696
) -> String? {
9797
do {
9898
let expandedSyntax: Syntax
@@ -192,7 +192,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
192192
extendedType: TypeSyntax?,
193193
conformanceList: InheritedTypeListSyntax?,
194194
in context: Context,
195-
indentationWidth: Trivia = .spaces(4)
195+
indentationWidth: Trivia? = nil
196196
) -> [String]? {
197197
do {
198198
switch (definition, macroRole) {
@@ -320,7 +320,7 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
320320
extendedType: TypeSyntax?,
321321
conformanceList: InheritedTypeListSyntax?,
322322
in context: Context,
323-
indentationWidth: Trivia = .spaces(4)
323+
indentationWidth: Trivia? = nil
324324
) -> String? {
325325
let expandedSources = expandAttachedMacroWithoutCollapsing(
326326
definition: definition,
@@ -341,7 +341,7 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
341341
fileprivate extension SyntaxProtocol {
342342
/// Perform a format if required and then trim any leading/trailing
343343
/// whitespace.
344-
func formattedExpansion(_ mode: FormatMode, indentationWidth: Trivia) -> String {
344+
func formattedExpansion(_ mode: FormatMode, indentationWidth: Trivia?) -> String {
345345
let formatted: Syntax
346346
switch mode {
347347
case .auto:
@@ -396,7 +396,7 @@ public func collapse<Node: SyntaxProtocol>(
396396
expansions: [String],
397397
for role: MacroRole,
398398
attachedTo declarationNode: Node,
399-
indentationWidth: Trivia = .spaces(4)
399+
indentationWidth: Trivia? = nil
400400
) -> String {
401401
if expansions.isEmpty {
402402
return ""
@@ -421,7 +421,10 @@ public func collapse<Node: SyntaxProtocol>(
421421
onDeclarationWithoutAccessor = false
422422
}
423423
if onDeclarationWithoutAccessor {
424-
expansions = expansions.map({ $0.indented(by: indentationWidth) })
424+
// Default to 4 spaces if no indentation was passed.
425+
// In the future, we could consider inferring the indentation width from
426+
// the expansions to collapse.
427+
expansions = expansions.map({ $0.indented(by: indentationWidth ?? .spaces(4)) })
425428
expansions[0] = "{\n" + expansions[0]
426429
expansions[expansions.count - 1] += "\n}"
427430
}

Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extension SyntaxProtocol {
2323
public func expand(
2424
macros: [String: Macro.Type],
2525
in context: some MacroExpansionContext,
26-
indentationWidth: Trivia = .spaces(4)
26+
indentationWidth: Trivia? = nil
2727
) -> Syntax {
2828
// Build the macro system.
2929
var system = MacroSystem()
@@ -463,11 +463,14 @@ private class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
463463
init(
464464
macroSystem: MacroSystem,
465465
context: Context,
466-
indentationWidth: Trivia
466+
indentationWidth: Trivia?
467467
) {
468468
self.macroSystem = macroSystem
469469
self.context = context
470-
self.indentationWidth = indentationWidth
470+
// Default to 4 spaces if no indentation was passed.
471+
// In the future, we could consider inferring the indentation width from the
472+
// source file in which we expand the macros.
473+
self.indentationWidth = indentationWidth ?? .spaces(4)
471474
super.init(viewMode: .sourceAccurate)
472475
}
473476

0 commit comments

Comments
 (0)