@@ -167,11 +167,13 @@ static bool printModuleInterfaceDecl(Decl *D,
167167 };
168168 }
169169 }
170- if (!LeadingComment.empty () && Options.shouldPrint (D))
171- Printer << LeadingComment << " \n " ;
170+ if (!LeadingComment.empty () && Options.shouldPrint (D)) {
171+ Printer << LeadingComment;
172+ Printer.printNewline ();
173+ }
172174 if (D->print (Printer, Options)) {
173175 if (Options.BracketOptions .shouldCloseNominal (D))
174- Printer << " \n " ;
176+ Printer. printNewline () ;
175177 Options.BracketOptions = BracketOptions ();
176178 if (auto NTD = dyn_cast<NominalTypeDecl>(D)) {
177179 std::queue<NominalTypeDecl *> SubDecls{{NTD}};
@@ -194,11 +196,13 @@ static bool printModuleInterfaceDecl(Decl *D,
194196 }
195197 if (extensionHasClangNode (Ext))
196198 continue ; // will be printed in its source location, see above.
197- Printer << " \n " ;
198- if (!LeadingComment.empty ())
199- Printer << LeadingComment << " \n " ;
199+ Printer.printNewline ();
200+ if (!LeadingComment.empty ()) {
201+ Printer << LeadingComment;
202+ Printer.printNewline ();
203+ }
200204 Ext->print (Printer, Options);
201- Printer << " \n " ;
205+ Printer. printNewline () ;
202206 }
203207 for (auto Sub : Ext->getMembers ())
204208 if (auto N = dyn_cast<NominalTypeDecl>(Sub))
@@ -226,7 +230,7 @@ static bool printModuleInterfaceDecl(Decl *D,
226230 if (ET.IsSynthesized )
227231 Options.clearSynthesizedExtension ();
228232 if (Options.BracketOptions .shouldCloseExtension (ET.Ext ))
229- Printer << " \n " ;
233+ Printer. printNewline () ;
230234 }
231235 });
232236 }
@@ -250,9 +254,11 @@ static bool printModuleInterfaceDecl(Decl *D,
250254 ET.Ext , !Opened, Decls.back ().Ext == ET.Ext , true
251255 };
252256 if (Options.BracketOptions .shouldOpenExtension (ET.Ext )) {
253- Printer << " \n " ;
254- if (Options.shouldPrint (ET.Ext ) && !LeadingComment.empty ())
255- Printer << LeadingComment << " \n " ;
257+ Printer.printNewline ();
258+ if (Options.shouldPrint (ET.Ext ) && !LeadingComment.empty ()) {
259+ Printer << LeadingComment;
260+ Printer.printNewline ();
261+ }
256262 }
257263 if (ET.IsSynthesized ) {
258264 if (ET.EnablingExt )
@@ -265,7 +271,7 @@ static bool printModuleInterfaceDecl(Decl *D,
265271 if (ET.IsSynthesized )
266272 Options.clearSynthesizedExtension ();
267273 if (Options.BracketOptions .shouldCloseExtension (ET.Ext )) {
268- Printer << " \n " ;
274+ Printer. printNewline () ;
269275 }
270276 }
271277 });
@@ -411,18 +417,22 @@ static void printCrossImportOverlays(ModuleDecl *Declaring, ASTContext &Ctx,
411417 BystanderList += Bystanders[I].str ();
412418 }
413419
414- Printer << " \n // MARK: - " << BystanderList << " Additions\n\n " ;
420+ Printer.printNewline ();
421+ Printer << " // MARK: - " << BystanderList << " Additions" ;
422+ Printer.printNewline ();
423+ Printer.printNewline ();
415424 for (auto *Import : DeclLists.first )
416425 PrintDecl (Import);
417- Printer << " \n " ;
426+ if (!DeclLists.first .empty ())
427+ Printer.printNewline ();
418428
419429 std::string PerDeclComment = " // Available when " + BystanderList;
420430 PerDeclComment += Bystanders.size () == 1 ? " is" : " are" ;
421431 PerDeclComment += " imported with " + Declaring->getNameStr ().str ();
422432
423433 for (auto *D : DeclLists.second ) {
424- if (PrintDecl (D, PerDeclComment))
425- Printer << " \n " ;
434+ if (PrintDecl (D, PerDeclComment) && Options. EmptyLineBetweenDecls )
435+ Printer. printNewline () ;
426436 }
427437 }
428438}
@@ -668,7 +678,7 @@ void swift::ide::printModuleInterface(
668678 if (!TargetMod->isStdlibModule ()) {
669679 for (auto *D : ImportDecls)
670680 PrintDecl (D);
671- Printer << " \n " ;
681+ Printer. printNewline () ;
672682 }
673683
674684 {
@@ -686,14 +696,15 @@ void swift::ide::printModuleInterface(
686696
687697 for (auto CM : ClangModules) {
688698 for (auto DeclAndLoc : ClangDecls[CM.first ])
689- PrintDecl (DeclAndLoc.first );
699+ if (PrintDecl (DeclAndLoc.first ) && Options.EmptyLineBetweenDecls )
700+ Printer.printNewline ();
690701 }
691702 }
692703
693704 if (!(TraversalOptions & ModuleTraversal::SkipOverlay) || !TargetClangMod) {
694705 for (auto *D : SwiftDecls) {
695- if (PrintDecl (D))
696- Printer << " \n " ;
706+ if (PrintDecl (D) && Options. EmptyLineBetweenDecls )
707+ Printer. printNewline () ;
697708 }
698709
699710 // If we're printing the entire target module (not specific sub-groups),
@@ -704,6 +715,8 @@ void swift::ide::printModuleInterface(
704715 AdjustedOptions, PrintSynthesizedExtensions);
705716 }
706717 }
718+ // Flush pending newlines.
719+ Printer.forceNewlines ();
707720}
708721
709722static SourceLoc getDeclStartPosition (SourceFile &File) {
@@ -813,8 +826,9 @@ void swift::ide::printHeaderInterface(
813826 continue ;
814827 }
815828 if (D->print (Printer, AdjustedOptions))
816- Printer << " \n " ;
829+ Printer. printNewline () ;
817830 }
831+ Printer.forceNewlines ();
818832}
819833
820834void swift::ide::printSymbolicSwiftClangModuleInterface (
0 commit comments