"use strict" ; import * as vscode from "vscode" ; import TranslatorX from './TranslatorX' ; import { trim } from './utils' ; import SpeakPanel from './SpeakPanel' ; export function activate ( context ) { let { window , languages , commands , } = vscode ; let gReplaceableArr = [ ] ; let gReplaceableIndex = 0 ; let translatorX = new TranslatorX ( ) ; let enable = vscode . commands . registerCommand ( "extension.enable" , ( ) => { translatorX . setTranslatorXState ( true ) ; } ) ; let test = vscode . commands . registerCommand ( "extension.test" , ( args ) => { SpeakPanel . getInstance ( context . extensionPath ) . play ( decodeURIComponent ( args . speakUrl ) ) ; } ) ; let disable = vscode . commands . registerCommand ( "extension.disable" , ( ) => { translatorX . setTranslatorXState ( false ) ; } ) ; let replaceWithTranslationResults = vscode . commands . registerTextEditorCommand ( 'extension.replaceWithTranslationResults' , ( textEditor , edit ) => { let editor = window . activeTextEditor ; if ( ! editor ) return ; const selection = editor && editor . selection ; edit . replace ( selection , gReplaceableArr [ gReplaceableIndex % gReplaceableArr . length ] ) ; gReplaceableIndex ++ ; } ) ; let provider = { async provideHover ( document , position , token ) { let editor = window . activeTextEditor ; if ( ! editor ) return ; const selection = editor && editor . selection ; let range = document . getWordRangeAtPosition ( position ) ; let string = range ? document . getText ( range ) : "" ; if ( selection && ! selection . isEmpty ) { let text = document . getText ( selection ) ; if ( selection . contains ( range ) || ~ string . indexOf ( text ) ) string = text ; } string = trim ( string ) ; if ( ! string ) return ; string = string . replace ( /\n/g , "" ) ; const { replaceableArr , translateResult } = await translatorX . fetch ( { word : string } ) ; gReplaceableArr = replaceableArr ; gReplaceableIndex = 0 ; if ( translateResult ) { let hover = new vscode . Hover ( translateResult ) ; return hover ; } } } ; languages . registerHoverProvider ( { scheme : 'file' , language : '*' } , provider ) ; languages . registerHoverProvider ( { scheme : 'untitled' } , provider ) ; context . subscriptions . push ( enable ) ; context . subscriptions . push ( disable ) ; context . subscriptions . push ( test ) ; context . subscriptions . push ( replaceWithTranslationResults ) ; } export function deactivate ( ) { }