1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { ArkMetadataKind, CommentsMetadata } from '../../core/model/ArkMetadata'; 17import { ArkNamespace } from '../../core/model/ArkNamespace'; 18 19import { BasePrinter, Dump } from '../base/BasePrinter'; 20import { ArkIRClassPrinter } from './ArkIRClassPrinter'; 21import { ExportPrinter } from '../base/ExportPrinter'; 22 23/** 24 * @category save 25 */ 26export class ArkIRNamespacePrinter extends BasePrinter { 27 ns: ArkNamespace; 28 29 public constructor(ns: ArkNamespace, indent: string = '') { 30 super(indent); 31 this.ns = ns; 32 } 33 34 public getLine(): number { 35 return this.ns.getLine(); 36 } 37 38 public dump(): string { 39 const commentsMetadata = this.ns.getMetadata(ArkMetadataKind.LEADING_COMMENTS); 40 if (commentsMetadata instanceof CommentsMetadata) { 41 const comments = commentsMetadata.getComments(); 42 comments.forEach(comment => { 43 this.printer.writeIndent().writeLine(comment.content); 44 }); 45 } 46 this.printer.writeIndent().writeSpace(this.modifiersToString(this.ns.getModifiers())).writeLine(`namespace ${this.ns.getName()} {`); 47 this.printer.incIndent(); 48 49 let items: Dump[] = []; 50 // print class 51 for (let cls of this.ns.getClasses()) { 52 items.push(new ArkIRClassPrinter(cls, this.printer.getIndent())); 53 } 54 // print namespace 55 for (let childNs of this.ns.getNamespaces()) { 56 items.push(new ArkIRNamespacePrinter(childNs, this.printer.getIndent())); 57 } 58 // print exportInfos 59 for (let exportInfo of this.ns.getExportInfos()) { 60 items.push(new ExportPrinter(exportInfo, this.printer.getIndent())); 61 } 62 63 items.sort((a, b) => a.getLine() - b.getLine()); 64 items.forEach((v): void => { 65 this.printer.write(v.dump()); 66 }); 67 this.printer.decIndent(); 68 this.printer.writeIndent().writeLine('}'); 69 return this.printer.toString(); 70 } 71} 72