import * as vscode from 'vscode' ; import { FuncMap } from './helm.funcmap' ; import { Resources } from './helm.resources' ; import { isPositionInKey } from "./yaml-support/yaml-util" ; export class HelmTemplateHoverProvider implements vscode . HoverProvider { private funcmap : vscode . CompletionItem [ ] ; private valmap : vscode . CompletionItem [ ] ; private resmap : vscode . CompletionItem [ ] ; public constructor ( ) { const fm = new FuncMap ( ) ; const rs = new Resources ( ) ; this . funcmap = fm . all ( ) ; this . valmap = fm . helmVals ( ) ; this . resmap = rs . all ( ) ; } public provideHover ( doc , pos , _token ) { const wordRange = doc . getWordRangeAtPosition ( pos ) ; const word = wordRange ? doc . getText ( wordRange ) : "" ; if ( word === "" ) { return Promise . resolve ( null ) ; } if ( this . inActionVal ( doc , pos , word ) ) { const found = this . findVal ( word ) ; if ( found ) { return new vscode . Hover ( found , wordRange ) ; } } if ( this . inAction ( doc , pos , word ) ) { const found = this . findFunc ( word ) ; if ( found ) { return new vscode . Hover ( found , wordRange ) ; } } if ( this . notInAction ( doc , pos , word ) ) { try { if ( ! isPositionInKey ( doc , pos ) ) { return undefined ; } } catch ( ex ) { } const found = this . findResourceDef ( word ) ; if ( found ) { return new vscode . Hover ( found , wordRange ) ; } } return Promise . resolve ( null ) ; } private inAction ( doc , pos , word ) { const lineText = doc . lineAt ( pos . line ) . text ; const r = new RegExp ( `{{[^}]*[\\s\\(|]?(${ word })\\s[^{]*}}` ) ; return r . test ( lineText ) ; } private notInAction ( doc , pos , word ) { const lineText = doc . lineAt ( pos . line ) . text ; const r = new RegExp ( `(^|})[^{]*(${ word })` ) ; return r . test ( lineText ) ; } private findFunc ( word ) { for ( const item of this . funcmap ) { if ( item . label === word ) { return [ { language : "helm" , value : `{{ ${ item . detail } }}` } , `${ item . documentation }` ] ; } } return [ ] ; } private inActionVal ( doc , pos , word ) { const lineText = doc . lineAt ( pos . line ) . text ; const r = new RegExp ( `{{[^}]*\\.(${ word })[\\.\\s]?[^{]*}}` ) ; return r . test ( lineText ) ; } private findVal ( word ) { for ( const item of this . valmap ) { if ( item . label === word ) { return [ { language : "helm" , value : `{{ ${ item . detail } }}` } , `${ item . documentation }` ] ; } } return [ ] ; } private findResourceDef ( word ) { for ( const item of this . resmap ) { if ( item . label === word ) { return [ { language : "helm" , value : `${ item . detail }` } , `${ item . documentation }` ] ; } } return [ ] ; } }