From e40a6a4e5a330a86191c238b28237e969bdb680c Mon Sep 17 00:00:00 2001 From: Karan Date: Wed, 7 Jan 2026 02:52:47 +0530 Subject: [PATCH] Refactor initialIndentation in BasicFormat Signed-off-by: Karan --- .../ConvertJSONToCodableStruct.swift | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/Sources/SwiftLanguageService/CodeActions/ConvertJSONToCodableStruct.swift b/Sources/SwiftLanguageService/CodeActions/ConvertJSONToCodableStruct.swift index 783a539e..99d3fa0b 100644 --- a/Sources/SwiftLanguageService/CodeActions/ConvertJSONToCodableStruct.swift +++ b/Sources/SwiftLanguageService/CodeActions/ConvertJSONToCodableStruct.swift @@ -101,13 +101,6 @@ package struct ConvertJSONToCodableStruct: EditRefactoringProvider { // Render the top-level object as a struct. // Infer indentation from the entire file to match the source file's style. let indentation = BasicFormat.inferIndentation(of: Syntax(syntax.root)) - let format = BasicFormat(indentationWidth: indentation) - let decls = topLevelObject.asDeclSyntax(name: "JSONValue") - .formatted(using: format) - - // Apply base indentation to the generated struct. - // The first line inherits the existing indentation from the source file. - // Subsequent lines need to be explicitly indented. let jsonNode: Syntax switch preflight { case .closure(let closure): jsonNode = Syntax(closure) @@ -115,13 +108,11 @@ package struct ConvertJSONToCodableStruct: EditRefactoringProvider { case .stringLiteral(let literal, _): jsonNode = Syntax(literal) } let baseIndentation = getBaseIndentation(from: jsonNode) - let indentedDecls = decls.description.split(separator: "\n", omittingEmptySubsequences: false) - .enumerated() - .map { (index, line) in - if index == 0 { return String(line) } - return baseIndentation.description + line - } - .joined(separator: "\n") + + let format = BasicFormat(indentationWidth: indentation, initialIndentation: baseIndentation) + let indentedDecls = topLevelObject.asDeclSyntax(name: "JSONValue") + .formatted(using: format) + .description // Render the change into a set of source edits. switch preflight {