import * as vscode from 'vscode' ; import { Extension } from '../extension' ; import { Citation } from './completer/citation' ; import { Crossref } from './completer/crossref' ; export class Completer implements vscode . CompletionItemProvider { extension : Extension ; citation : Citation ; crossref : Crossref ; constructor ( extension ) { this . extension = extension ; this . citation = new Citation ( extension ) ; this . crossref = new Crossref ( extension ) ; } provideCompletionItems ( document , position , _token , context ) { const invoker = document . lineAt ( position . line ) . text [ position . character - 1 ] ; if ( invoker !== '@' ) { return ; } const line = document . lineAt ( position . line ) . text . substr ( 0 , position . character ) . trim ( ) . split ( " " ) ; const trigger = line [ line . length - 1 ] ; const suggestions = this . completion ( trigger ) . concat ( this . completionCrossref ( trigger , document ) ) ; this . extension . log ( `Showing ${ suggestions . length } suggestions` ) ; if ( suggestions . length > 0 ) { const configuration = vscode . workspace . getConfiguration ( 'PandocCiter' ) ; if ( ( configuration . get ( 'ViewType' ) as string ) === 'browser' ) { setTimeout ( ( ) => this . citation . browser ( ) , 10 ) ; return ; } return suggestions ; } return ; } completion ( line ) { let reg = /(?:^|[ ;\[-])\@([^\]\s]*)/ ; let provider = this . citation ; const result = line . match ( reg ) ; let suggestions = [ ] ; if ( result ) { suggestions = provider . provide ( ) ; } return suggestions ; } completionCrossref ( line , doc ) { return this . crossref . provide ( { document : doc } ) ; } }