Skip to content

Commit

Permalink
Simplify management of directive application contexts. Optimize acces…
Browse files Browse the repository at this point in the history
…s to format configurations.
  • Loading branch information
objecthub committed May 29, 2023
1 parent cb62370 commit 241f553
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
59 changes: 36 additions & 23 deletions Sources/CLFormat/CLControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public struct CLControl: CustomStringConvertible {
tabsize: tabsize,
linewidth: linewidth,
args: args),
in: .root(self.config)).string
in: Context(config: self.config)).string
}

/// Main format function. Creates an `Argument` object from the given parameters
Expand All @@ -95,7 +95,7 @@ public struct CLControl: CustomStringConvertible {
tabsize: tabsize,
linewidth: linewidth,
args: arguments),
in: .root(self.config)).string
in: Context(config: self.config)).string
}

/// Driver of the formatting logic. Can be called directly for low-level applications.
Expand All @@ -107,7 +107,7 @@ public struct CLControl: CustomStringConvertible {
case .text(let str):
res += str
case .directive(let dir):
switch try dir.specifier.apply(context: .frame(res, context),
switch try dir.specifier.apply(context: context.drop(res),
parameters: dir.parameters.process(arguments: args),
modifiers: dir.modifiers,
arguments: args) {
Expand Down Expand Up @@ -136,29 +136,42 @@ public struct CLControl: CustomStringConvertible {
}

///
/// A `Context` value represents a formatting context (i.e. a linked list of
/// context values. The root refers to the format configuration (to make
/// it accessible from the formatting logic).
///
public enum Context {
case root(CLFormatConfig)
indirect case frame(String, Context)
/// A `Context` value represents a formatting context, i.e. a linked list of
/// `History` values and a format configuration.
///
public struct Context {

public var current: String {
switch self {
case .root(_):
return ""
case .frame(let str, let context):
return context.current + str
public enum History {
case root
indirect case frame(String, History)

public var string: String {
switch self {
case .root:
return ""
case .frame(let str, let history):
return history.string + str
}
}
}

public var config: CLFormatConfig {
switch self {
case .root(let config):
return config
case .frame(_, let context):
return context.config
}
let config: CLFormatConfig
let output: History

init(config: CLFormatConfig, output: History = .root) {
self.config = config
self.output = output
}

public func reconfig(_ config: CLFormatConfig) -> Context {
return Context(config: config, output: self.output)
}

public func drop(_ str: String) -> Context {
return Context(config: self.config, output: .frame(str, self.output))
}

public var current: String {
return self.output.string
}
}
6 changes: 3 additions & 3 deletions Sources/CLFormat/StandardDirectiveSpecifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ public enum StandardDirectiveSpecifier: DirectiveSpecifier {
if let obj = arg, let arr = arguments.coerceToArray(obj, capAt: subitercap) {
let formatted =
try control.format(with: arguments.new(args: arr, numArgumentsLeft: iterargs.left),
in: .frame(res, context))
in: context.drop(res))
res += formatted.string
if case .break = formatted {
break
Expand All @@ -644,7 +644,7 @@ public enum StandardDirectiveSpecifier: DirectiveSpecifier {
nas.append(arg)
let formatted =
try control.format(with: arguments.new(args: nas, numArgumentsLeft: iterargs.left),
in: .frame(res, context))
in: context.drop(res))
res += formatted.string
if case .break = formatted {
break
Expand All @@ -658,7 +658,7 @@ public enum StandardDirectiveSpecifier: DirectiveSpecifier {
while (iterargs.left > 0 || (iterargs.left == 0 && force)) && itercap > 0 {
force = false
itercap -= 1
res += try control.format(with: iterargs, in: .frame(res, context)).string
res += try control.format(with: iterargs, in: context.drop(res)).string
}
_ = iterargs.setFirstArg(to: firstArg)
}
Expand Down

0 comments on commit 241f553

Please sign in to comment.