import { ApplicationCacheConfigAndExecutorImpl , ApplicationImpl , Group , GroupName , Platform , ProjectItemImpl , SettingItem , ShellExecutor , SwitchSettingItem , } from '../../Types' ; import { readFile } from 'fs/promises' ; import { isNil , unique } from 'licia' ; import { parse } from 'path' ; import { existsOrNot , generateStringByOS , systemUser } from '../../Utils' ; import { Context } from '../../Context' ; import { i18n , sentenceKey } from '../../i18n' ; import { generatePinyinIndex } from '../../utils/index-generator/PinyinIndex' ; const SUBLIME = 'sublime' ; export class SublimeProjectItemImpl extends ProjectItemImpl { } export class SublimeApplicationImpl extends ApplicationCacheConfigAndExecutorImpl < SublimeProjectItemImpl > { openInNew : boolean = false ; constructor ( ) { super ( SUBLIME , 'Sublime Text' , 'icon/sublime.png' , SUBLIME , [ Platform . win32 , Platform . darwin , Platform . linux ] , Group [ GroupName . editor ] , ( ) => `${ i18n . t ( sentenceKey . configFileAt ) } ${ this . defaultConfigPath ( ) }, ${ i18n . t ( sentenceKey . executorFileAt ) } ${ this . defaultExecutorPath ( ) } (注意 Sublime Text 单独提供了命令行程序, 不是程序本体)` , undefined , 'Session.sublime_session' ) ; } override defaultConfigPath ( ) { return generateStringByOS ( { win32 : `C:\\Users\\${ systemUser ( ) }\\AppData\\Roaming\\Sublime Text 3\\Local\\Session.sublime_session` , darwin : `/Users/${ systemUser ( ) }/Library/Application Support/Sublime Text/Local/Session.sublime_session` , linux : `/home/${ systemUser ( ) }/.config/sublime-text/Local/Session.sublime_session` , } ) ; } override defaultExecutorPath ( ) { return generateStringByOS ( { win32 : `C:\\Program Files\\Sublime Text\\subl.exe` , darwin : `/Users/${ systemUser ( ) }/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl` , linux : `(不同发行版安装路径差异较大, 自行使用 which 命令找到 subl 命令所在路径作为可执行文件路径)` , } ) ; } parsePath ( source ) { return utools . isWindows ( ) ? source . replace ( /^\/(\w+)(?=\/)/g , '$1:' ) : source ; } async generateCacheProjectItems ( context ) { let items = [ ] ; let buffer = await readFile ( this . config ) ; if ( ! isNil ( buffer ) ) { let args = this . openInNew ? '-n' : '' ; let content = buffer . toString ( ) ; let session = JSON . parse ( content ) ; let folderSet = new Set < string > ( ) ; let fileSet = new Set < string > ( ) ; let workspaceSet = new Set < string > ( ) ; let folderHistory = session ?. folder_history ; if ( ! isNil ( folderHistory ) ) { for ( let folder of folderHistory ) { folderSet . add ( folder ) ; } } let workspaceHistory = session ?. workspaces ?. recent_workspaces ; if ( ! isNil ( workspaceHistory ) ) { for ( let workspace of workspaceHistory ) { workspaceSet . add ( workspace ) ; } } [ ... folderSet , ... fileSet , ... workspaceSet , ] . forEach ( path => { let parser = parse ( path ) ; let readPath = this . parsePath ( path ) ; let { exists , description , icon } = existsOrNot ( readPath , { description : readPath , icon : context . enableGetFileIcon ? utools . getFileIcon ( readPath ) : this . icon , } ) ; items . push ( { id : '' , title : `${ parser . name }${ parser . ext }` , description : description , icon : icon , searchKey : unique ( [ ... generatePinyinIndex ( context , path ) , path ] ) , exists : exists , command : new ShellExecutor ( `"${ this . executor }" ${ args } "${ this . parsePath ( path ) }"` ) , } ) ; } ) ; } return items ; } openInNewId ( nativeId ) { return `${ nativeId }/${ this . id }-open-in-new` ; } override update ( nativeId ) { super . update ( nativeId ) ; this . openInNew = utools . dbStorage . getItem ( this . openInNewId ( nativeId ) ) ?? false ; } override generateSettingItems ( context , nativeId ) { let superSettings = super . generateSettingItems ( context , nativeId ) ; superSettings . splice ( 1 , 0 , new SwitchSettingItem ( this . openInNewId ( nativeId ) , i18n . t ( sentenceKey . openInNew ) , this . openInNew , i18n . t ( sentenceKey . openInNewDesc ) ) ) ; return superSettings ; } } export const applications = [ new SublimeApplicationImpl ( ) , ] ;