@@ -66,28 +66,17 @@ extension NSDocumentController {
6666 }
6767
6868 // MARK: - Private shared implementation
69- // ─────────────────────────────────────────────────────────────────────
7069 private enum SaveMode { case file, folder }
7170
72- /// Internal helper that contains 100 % of the common logic.
73- @MainActor
74- private func _createDocument(
75- mode: SaveMode ,
76- configuration: DocumentSaveDialogConfiguration ,
77- onDialogPresented: @escaping ( ) -> Void ,
78- onCompletion: @escaping ( ) -> Void ,
79- onCancel: @escaping ( ) -> Void
80- ) {
81-
82- // 1 ────────────────────────────────────────────────────────────────
83- // Configure the NSSavePanel
71+ /// Configure a save panel for ``_createDocument``.
72+ private func configureSavePanel( mode: SaveMode , configuration: DocumentSaveDialogConfiguration ) -> NSSavePanel {
8473 let panel = NSSavePanel ( )
85- panel. prompt = configuration. prompt
86- panel. title = configuration. title
87- panel. nameFieldLabel = configuration. nameFieldLabel
74+ panel. prompt = configuration. prompt
75+ panel. title = configuration. title
76+ panel. nameFieldLabel = configuration. nameFieldLabel
8877 panel. canCreateDirectories = true
89- panel. directoryURL = configuration. directoryURL
90- panel. level = . modalPanel
78+ panel. directoryURL = configuration. directoryURL
79+ panel. level = . modalPanel
9180 panel. treatsFilePackagesAsDirectories = true
9281
9382 switch mode {
@@ -96,52 +85,65 @@ extension NSDocumentController {
9685 panel. allowedContentTypes = configuration. allowedContentTypes
9786 case . folder:
9887 panel. nameFieldStringValue =
99- URL ( fileURLWithPath: configuration. defaultFileName)
100- . deletingPathExtension ( )
101- . lastPathComponent
88+ URL ( fileURLWithPath: configuration. defaultFileName)
89+ . deletingPathExtension ( )
90+ . lastPathComponent
10291 panel. allowedContentTypes = [ ] // treat as plain folder
10392 }
10493
94+ return panel
95+ }
96+
97+ /// Internal helper that contains 100 % of the common logic.
98+ @MainActor
99+ private func _createDocument(
100+ mode: SaveMode ,
101+ configuration: DocumentSaveDialogConfiguration ,
102+ onDialogPresented: @escaping ( ) -> Void ,
103+ onCompletion: @escaping ( ) -> Void ,
104+ onCancel: @escaping ( ) -> Void
105+ ) {
106+ // 1 ────────────────────────────────────────────────────────────────
107+ // Configure the NSSavePanel
108+ let panel = configureSavePanel ( mode: mode, configuration: configuration)
109+
105110 DispatchQueue . main. async { onDialogPresented ( ) }
106111
107112 guard panel. runModal ( ) == . OK,
108- let baseURL = panel . url // e.g. …/ProjectName
109- else {
113+ // e.g. …/ProjectName
114+ let baseURL = panel . url else {
110115 onCancel ( )
111116 return
112117 }
113118
114- // 2 ────────────────────────────────────────────────────────────────
115- // For a *folder* document, create the workspace directory up front.
116- if mode == . folder {
117- do {
119+ do {
120+ // 2 ────────────────────────────────────────────────────────────────
121+ // For a * folder* document, create the workspace directory up front.
122+ if mode == . folder {
118123 try FileManager . default. createDirectory (
119124 at: baseURL,
120125 withIntermediateDirectories: true ,
121126 attributes: nil
122127 )
123- } catch {
124- NSAlert ( error: error) . runModal ( )
125- onCancel ( )
126- return
127128 }
128- }
129129
130- // 3 ────────────────────────────────────────────────────────────────
131- // Derive the final URL of the actual NSDocument header file.
132- // …/ProjectName/ProjectName.circuitproj (folder mode)
133- // …/SomeFile.circuitproj (file mode)
134- let ext = configuration
135- . defaultFileType
136- . preferredFilenameExtension ?? " file "
130+ // 3 ────────────────────────────────────────────────────────────────
131+ // Derive the final URL of the actual NSDocument header file.
132+ // …/ProjectName/ProjectName.circuitproj (folder mode)
133+ // …/SomeFile.circuitproj (file mode)
134+ let ext = configuration
135+ . defaultFileType
136+ . preferredFilenameExtension ?? " file "
137137
138- let finalURL = ( mode == . folder)
139- ? baseURL. appendingPathComponent ( " \( baseURL. lastPathComponent) . \( ext) " )
140- : baseURL
138+ let finalURL = if mode == . folder {
139+ baseURL. appendingPathComponent ( " \( baseURL. lastPathComponent) . \( ext) " )
140+ } else {
141+ baseURL
142+ }
143+
144+ // 4 ────────────────────────────────────────────────────────────────
145+ // Create, write and open the document.
141146
142- // 4 ────────────────────────────────────────────────────────────────
143- // Create, write and open the document.
144- do {
145147 let document = try makeUntitledDocument (
146148 ofType: configuration. defaultFileType. identifier
147149 )
@@ -154,14 +156,10 @@ extension NSDocumentController {
154156 originalContentsURL: nil
155157 )
156158
157- openDocument (
158- at: finalURL,
159- onCompletion: onCompletion,
160- onError: { error in
161- NSAlert ( error: error) . runModal ( )
162- onCancel ( )
163- }
164- )
159+ openDocument ( at: finalURL, onCompletion: onCompletion, onError: { error in
160+ NSAlert ( error: error) . runModal ( )
161+ onCancel ( )
162+ } )
165163 } catch {
166164 NSAlert ( error: error) . runModal ( )
167165 onCancel ( )
0 commit comments