import { commands , window , ViewColumn , workspace , TextDocument } from 'vscode' ; import { EditorPanel } from '../webview/panel' ; import { LocaleTreeItem } from '../views' ; import { Commands } from './commands' ; import { CommandOptions } from './manipulations/common' ; import { ExtensionModule } from '~/modules' ; import i18n from '~/i18n' ; import { ActionSource , Global , Telemetry , TelemetryKey } from '~/core' ; import { promptKeys } from '~/utils' ; export default < ExtensionModule > function ( ctx ) { const supportedFileOpen = ( ) => { const doc = window . activeTextEditor ?. document ; if ( ! doc || ! Global . isLanguageIdSupported ( doc . languageId ) ) return false ; return true ; } ; const openEditor = async ( item ? ) => { let actionSource = ActionSource . None ; let key ; let locale ; let mode = 'standalone' ; let index ; if ( ! item ) { actionSource = ActionSource . CommandPattele ; if ( supportedFileOpen ( ) ) mode = 'currentFile' ; key = await promptKeys ( i18n . t ( 'prompt.choice_key_to_open' ) ) ; } else if ( item instanceof LocaleTreeItem ) { actionSource = ActionSource . TreeView ; key = item . node . keypath ; locale = item . node . type === 'record' ? item . node . locale : undefined ; } else if ( typeof item === 'string' ) { key = item ; } else if ( item . keypath ) { actionSource = ActionSource . Hover ; key = item . keypath ; locale = item . locale ; if ( item . keyIndex != null ) { mode = 'currentFile' ; index = item . keyIndex ; } } if ( ! key ) return ; if ( actionSource !== ActionSource . None ) Telemetry . track ( TelemetryKey . EditorOpen , { source : actionSource } ) ; const panel = EditorPanel . createOrShow ( ctx , mode === 'currentFile' ? ViewColumn . Two : undefined ) ; panel . mode = mode ; panel . openKey ( key , locale , index ) ; } ; function updateContext ( doc ? ) { if ( Global . enabled && doc && window . activeTextEditor ?. document === doc && EditorPanel . currentPanel ?. visible && supportedFileOpen ( ) ) EditorPanel . currentPanel . sendCurrentFileContext ( ) ; } return [ commands . registerCommand ( Commands . open_in_editor , openEditor ) , commands . registerCommand ( Commands . open_editor , openEditor ) , workspace . onDidSaveTextDocument ( updateContext ) , window . onDidChangeActiveTextEditor ( e => updateContext ( e ?. document ) ) , ] ; } ;