I have this sample piece of code:
func something(_ a: Int) -> String {
if a == 10 {
return "hello"
}
return "bye"
}
I want to add a tab Trivia to every line inside a function.
Here is what I've tried:
extension SyntaxProtocol {
func appendingTabToLeadingTrivia() -> Self {
var trivia = leadingTrivia.appending(.tab)
var copy = self
copy.leadingTrivia = trivia
return copy
}
}
extension CodeBlockItemListSyntax {
func appendingTabToLeadingTriviaForAllChildren() -> Self {
var newCodeBlockItemSyntaxes: [Element] = []
// Iterate throught every CodeBlockItemList and add a `tab` Trivia
self.forEach { block in
var copy = block
copy = copy.appendingTabToLeadingTrivia()
newCodeBlockItemSyntaxes.append(copy)
}
return .init(newCodeBlockItemSyntaxes)
}
}
func modify(originalSyntax: CodeBlockItemListSyntax) {
var copy = originalSyntax.appendingTabToLeadingTriviaForAllChildren()
debugPrint(copy.description)
}
Expected
func something(_ a: Int) -> String {
if a == 10 {
return "hello"
}
return "bye"
}
Actual Result
func something(_ a: Int) -> String {
if a == 10 {
return "hello"
}
return "bye"
}
Questions:
Seems like my approach only adds a tab to the first line of a CodeBlockItemList.
How can I achieve the expected result?