'use strict' ; import * as vscode from 'vscode' ; var path = require ( 'path' ) ; var _ = require ( 'lodash' ) ; var fetch = require ( 'node-fetch' ) ; var parseString = require ( 'xml2js' ) . parseString ; var disposableProvider ; var isProviderEnabled = false ; /**
 *  Activate Function
 */ export function activate ( context ) { /**
     *  googleCompleteMe Command Function
     */ let disposableCommand = vscode . commands . registerTextEditorCommand ( 'extension.googleCompleteMe' , ( textEditor , edit ) => { if ( isProviderEnabled ) { isProviderEnabled = false ; vscode . window . setStatusBarMessage ( 'Google Complete Me is disabled' ) ; vscode . Disposable . from ( disposableProvider ) . dispose ( ) ; } else { isProviderEnabled = true ; vscode . window . setStatusBarMessage ( 'Google Complete Me is enabled' ) ; var completeProvider = new CompleteProvider ( ) ; disposableProvider = vscode . languages . registerCompletionItemProvider ( '*' , completeProvider , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'v' , 'w' , 'x' , 'y' , 'z' ) ; let document = textEditor . document ; let filepath = document . fileName ; let firstSelectEnd = textEditor . _selections [ 0 ] . end ; let uri = document . uri ; let position = new vscode . Position ( firstSelectEnd . line , firstSelectEnd . character ) ; vscode . commands . executeCommand ( 'vscode.executeCompletionItemProvider' , uri , position ) . then ( ( result ) => { } ) ; } } ) ; context . subscriptions . push ( disposableCommand ) ; } /**
 *  CompleteProvider
 */ class CompleteProvider implements vscode . CompletionItemProvider { provideCompletionItems ( document , position , token ) { const lineAt = document . lineAt ( position ) ; const lineText = document . getText ( lineAt . range ) ; let userQuery = getUserKeyIn ( lineText , position ) ; if ( userQuery !== '' ) { return new Promise ( ( resolve , reject ) => { fetch ( 'http://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=' + getUserKeyIn ( lineText , position ) ) . then ( function ( res ) { return res . text ( ) ; } ) . then ( function ( body ) { parseString ( body , function ( err , result ) { if ( err ) { vscode . window . showInformationMessage ( 'Google Autocomplete API is not available for this query' ) ; reject ( err ) ; } else { if ( typeof _ . get ( result , 'toplevel.CompleteSuggestion' ) === 'undefined' ) { reject ( ) ; } else { var items = result . toplevel . CompleteSuggestion . map ( ( item ) => { return new vscode . CompletionItem ( item . suggestion [ 0 ] [ '$' ] . data ) ; } ) ; resolve ( items ) ; } } } ) ; } ) ; } ) ; } } } function getUserKeyIn ( lineText , position ) { let temp = lineText . substr ( 0 , position . character ) . trim ( ) . split ( ' ' ) ; return temp [ temp . length - 1 ] ; } export function deactivate ( ) { }