import themeX = require ( '../themeX' ) ; var theme ; const pListHead = `<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n<plist version="1.0">\n<dict>` ; export = ( currentTheme ) : string => { theme = currentTheme ; return generateTheme ( ) ; } ; function generateTheme ( ) { let themeXML = new Array < string > ( ) ; themeXML . push ( addPListKeyInline ( 'name' , theme . theme . project . themes [ theme . index ] . name ) ) ; themeXML . push ( generateTmThemeSettingsArray ( ) ) ; themeXML . push ( generateTmThemeSettings ( ) ) ; return `${ pListHead }${ themeX . indent ( themeXML . join ( '' ) ) }\n</dict>\n</plist>` ; } function generateTmThemeSettingsArray ( ) { let settings = new Array < string > ( ) ; settings . push ( generateMainColorSchemeSettings ( ) ) ; let commentRule = { color : themeX . parseColor ( theme , theme . theme . project . themes [ theme . index ] . settings . comment ) , scope : 'comment' , name : 'Comment' } ; settings . push ( generateRuleXML ( commentRule ) ) ; theme . theme . project . rules . forEach ( rule => { settings . push ( generateRuleXML ( rule ) ) ; } ) ; return addPListSettingsArray ( settings ) ; } function generateMainColorSchemeSettings ( ) { const settings = theme . theme . project . themes [ theme . index ] . settings ; const result = new Array < string > ( ) ; function addSettingColor ( name , color ) { result . push ( addPListKeyInline ( name , themeX . parseColor ( theme , color ) ) ) ; } addSettingColor ( 'background' , settings . background ) ; addSettingColor ( 'foreground' , settings . foreground ) ; addSettingColor ( 'caret' , settings . caret ) ; addSettingColor ( 'invisibles' , settings . invisibles ) ; addSettingColor ( 'lineHighlight' , settings . lineHighlight ) ; addSettingColor ( 'selection' , settings . selection ) ; return `\n<!-- Main Color Settings -->${ addPListKeyBlock ( 'settings' , result . join ( '' ) ) }` ; } function generateRuleXML ( rule ) { let result = [ addPListKeyInline ( 'name' , rule . name ) ] ; if ( rule . scope !== null && rule . scope !== undefined ) result . push ( addPListKeyInline ( 'scope' , rule . scope ) ) ; else if ( rule . scopes !== null && rule . scopes !== undefined ) result . push ( addPListKeyInline ( 'scope' , rule . scopes . join ( ', ' ) ) ) ; else { themeX . report ( `bad scope definition: "${ rule . name }".` ) ; return '' ; } let ruleForegroundColor = "#FF0000" ; try { ruleForegroundColor = themeX . parseColor ( theme , rule . color ) ; } catch { throw new Error ( "Problem with color " + rule . color + " at rule: " + rule . name ) ; } const settings = [ addPListKeyInline ( 'foreground' , ruleForegroundColor ) ] ; let styleSettings = '' ; [ 'bold' , 'italic' , 'underline' ] . forEach ( style => { if ( rule [ style ] === true ) { styleSettings = `${ styleSettings } ${ style }` ; } } ) ; if ( styleSettings !== '' ) { settings . push ( addPListKeyInline ( 'fontStyle' , styleSettings ) ) ; } result . push ( addPListKeyBlock ( 'settings' , settings . join ( '' ) ) ) ; return result . join ( '' ) ; } function generateTmThemeSettings ( ) { let settingsXML = '\n<!-- Theme Settings -->' ; settingsXML += addPListKeyInline ( 'uuid' , theme . theme . project . themes [ theme . index ] . uuid ) ; settingsXML += addPListKeyInline ( 'semanticClass' , `theme.${ theme . theme . project . themes [ theme . index ] . baseColor }.${ theme . theme . project . themes [ theme . index ] . uuid }` ) ; settingsXML += addPListKeyInline ( 'author' , theme . theme . project . author ) ; settingsXML += addPListKeyInline ( 'colorSpaceName' , 'sRGB' ) ; return settingsXML ; } function addPListKeyInline ( key , text ) { return `\n<key>${ key }</key>\n<string>${ text }</string>` ; } function addPListKeyBlock ( key , text ) { return `\n<key>${ key }</key>\n<dict>${ themeX . indent ( text ) }\n</dict>` ; } function addPListSettingsArray ( array ) { let result = array . map ( dict => { return `\n<dict>${ themeX . indent ( dict ) }\n</dict>` ; } ) ; return `\n<key>settings</key>\n<array>${ themeX . indent ( result . join ( '' ) ) }\n</array>` ; }