Skip to content

Commit

Permalink
Fix argument propagation to support all attribute metadata. Support g…
Browse files Browse the repository at this point in the history
…eneric metadata at config-level (via an environment).
objecthub committed May 20, 2023
1 parent 6868a02 commit 7cb2766
Showing 3 changed files with 29 additions and 18 deletions.
13 changes: 11 additions & 2 deletions Sources/CLFormat/Arguments.swift
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ open class Arguments: CustomStringConvertible {

/// If this instance isn't referring to all arguments (at formatting time,
/// nested arguments might be created, e.g. to implement the ~?/indirection
/// directive.
/// directive.)
internal let numArgumentsLeft: Int?

/// The first argument to consume.
@@ -77,6 +77,15 @@ open class Arguments: CustomStringConvertible {
return self.args.count - self.index
}

/// Returns new arguments inheriting all settings from the current `Arguments` object.
public func new(args: [Any?], numArgumentsLeft: Int? = nil) -> Self {
return Self(locale: self.locale,
tabsize: self.tabsize,
linewidth: self.linewidth,
args: args,
numArgumentsLeft: numArgumentsLeft)
}

/// Returns the current first argument and sets a new first argument.
public func setFirstArg(to f: Int? = nil) -> Int {
let res = self.firstArg
@@ -188,7 +197,7 @@ open class Arguments: CustomStringConvertible {
public func nextAsArguments(maxArgs: Int = Int.max) throws -> Self {
if let arg = try self.next() {
if let arr = self.coerceToArray(arg, capAt: maxArgs) {
return Self(locale: self.locale, tabsize: self.tabsize, args: arr)
return self.new(args: arr)
} else {
throw CLFormatError.expectedSequenceArgument(self.index - 1, arg)
}
12 changes: 10 additions & 2 deletions Sources/CLFormat/CLFormatConfig.swift
Original file line number Diff line number Diff line change
@@ -29,15 +29,17 @@ import Foundation
/// directives have more complex parsing logic.
///
public struct CLFormatConfig {
public var makeArguments: (Locale?, Int, Int, [Any?], Int?) -> Arguments
public var nilRepresentation: String
private var makeArguments: (Locale?, Int, Int, [Any?], Int?) -> Arguments
private var directiveParsers: [Character : DirectiveParser]
public var nilRepresentation: String
public var environment: [String : AnyClass]

public init(makeArguments: @escaping (Locale?, Int, Int, [Any?], Int?) -> Arguments =
Arguments.init,
nilRepresentation: String = "nil") {
self.makeArguments = makeArguments
self.nilRepresentation = nilRepresentation
self.environment = [:]
self.directiveParsers = [:]
}

@@ -50,6 +52,12 @@ public struct CLFormatConfig {
return self.makeArguments(locale, tabsize, linewidth, args, numArgumentsLeft)
}

/// Determines what `makeArguments` is eventually invoking.
public mutating func setArgumentFactory(makeArguments:
@escaping (Locale?, Int, Int, [Any?], Int?) -> Arguments) {
self.makeArguments = makeArguments
}

/// Add a new directive parser to this format configuration for the given
/// array of characters.
public mutating func parse(_ chars: [Character], with parser: @escaping DirectiveParser) {
22 changes: 8 additions & 14 deletions Sources/CLFormat/StandardDirectiveSpecifier.swift
Original file line number Diff line number Diff line change
@@ -632,25 +632,19 @@ public enum StandardDirectiveSpecifier: DirectiveSpecifier {
let arg = try iterargs.next()
let subitercap = try parameters.number(1) ?? Int.max
if let obj = arg, let arr = arguments.coerceToArray(obj, capAt: subitercap) {
let formatted = try control.format(with:
context.config.makeArguments(
locale: arguments.locale,
tabsize: arguments.tabsize,
args: arr,
numArgumentsLeft: iterargs.left), in: .frame(res, context))
let formatted =
try control.format(with: arguments.new(args: arr, numArgumentsLeft: iterargs.left),
in: .frame(res, context))
res += formatted.string
if case .break = formatted {
break
}
} else if subitercap > 0 {
var newargs = [Any?]()
newargs.append(arg)
let formatted = try control.format(with:
context.config.makeArguments(
locale: arguments.locale,
tabsize: arguments.tabsize,
args: newargs,
numArgumentsLeft: iterargs.left), in: .frame(res, context))
var nas = [Any?]()
nas.append(arg)
let formatted =
try control.format(with: arguments.new(args: nas, numArgumentsLeft: iterargs.left),
in: .frame(res, context))
res += formatted.string
if case .break = formatted {
break

0 comments on commit 7cb2766

Please sign in to comment.