import { ICaseStyleConverter } from "../Casing/CaseStyleConverter" ; import { NameSplitter } from "../Casing/NameSplitter" ; import { CommandResult } from "../Commands/CommandResult" ; import { Import } from "../Languages/Imports/Import" ; import { ImportRelativity } from "../Languages/Imports/ImportRelativity" ; import { Language } from "../Languages/Language" ; import { LineResults } from "../LineResults" ; /**
 * Renders imports to output line results.
 */ export class ImportsPrinter { /**
     * Converts directory path components to a case.
     */ private directoryCaseStyleConverter : ICaseStyleConverter ; /**
     * Converts file names to a case.
     */ private fileCaseStyleConverter : ICaseStyleConverter ; /**
     * Language to output line results in.
     */ private language : Language ; /**
     * Splits name strings into words.
     */ private nameSplitter : NameSplitter ; param directoryCaseStyleConverter param fileCaseStyleConverter param language param nameSplitter public constructor ( language , directoryCaseStyleConverter , fileCaseStyleConverter , nameSplitter ) { this . directoryCaseStyleConverter = directoryCaseStyleConverter ; this . fileCaseStyleConverter = fileCaseStyleConverter ; this . language = language ; this . nameSplitter = nameSplitter ; } param addedImport returns public render ( addedImport ) { let lines ; if ( this . language . syntax . imports . explicitLines ) { lines = this . renderMultipleLines ( addedImport ) ; } else { lines = [ this . renderCombinedLine ( addedImport ) ] ; } return new LineResults ( lines ) ; } param addedImport returns private renderCombinedLine ( addedImport ) { return this . renderLine ( addedImport , addedImport . items . join ( ", " ) ) ; } param relativity returns private renderImportLeft ( relativity ) { if ( relativity === ImportRelativity . Absolute ) { return this . language . syntax . imports . leftAbsolute ; } return this . language . syntax . imports . leftLocal ; } param addedImport param item returns private renderLine ( addedImport , item ) { let line = this . renderImportLeft ( addedImport . relativity ) ; if ( this . language . syntax . imports . itemsBeforePackage ) { if ( this . language . syntax . imports . explicitItems ) { line += item ; line += this . language . syntax . imports . middle ; } line += this . renderPackagePath ( addedImport ) ; } else { line += this . renderPackagePath ( addedImport ) ; if ( this . language . syntax . imports . explicitItems ) { line += this . language . syntax . imports . middle ; line += item ; } } line += this . language . syntax . imports . right ; return new CommandResult ( line , 0 ) ; } param addedImport returns private renderMultipleLines ( addedImport ) { const results = [ ] ; for ( const item of addedImport . items ) { results . push ( this . renderLine ( addedImport , item ) ) ; } return results ; } param addedImport returns private renderPackagePath ( addedImport ) { let packagePath = addedImport . packagePath ; if ( addedImport . relativity !== ImportRelativity . Absolute || this . language . syntax . imports . explicitAbsoluteFileName ) { packagePath = this . individualizePackagePaths ( packagePath ) ; } let line = this . directoryCaseStyleConverter . convert ( packagePath ) ; if ( addedImport . relativity !== ImportRelativity . Absolute && this . language . syntax . imports . useLocalRelativePaths && line [ 0 ] !== "." ) { line = "./" + line ; } return line ; } param packagePath returns private individualizePackagePaths ( packagePath ) { const paths = [ ] ; for ( const component of packagePath ) { paths . push ( this . fileCaseStyleConverter . convert ( this . nameSplitter . split ( component ) ) ) ; } return paths ; } }