import * as vscode from 'vscode' ; import { BazelDocConfiguration , BazelDocGroup , makeBazelDocGroupMap } from './configuration' ; /**
 * Provide a hover for Starlark files and the rule definitions therein.
 */ export class BazelDocGroupHover implements vscode . HoverProvider , vscode . Disposable { private disposables : vscode . Disposable [ ] = [ ] ; private groups : Map < string , BazelDocGroup > | undefined ; private cfg : BazelDocConfiguration | undefined ; constructor ( onDidConfigurationChange ) { onDidConfigurationChange ( this . handleConfiguration , this , this . disposables ) ; this . disposables . push ( vscode . languages . registerHoverProvider ( [ { language : 'bazel' } , { language : 'starlark' } ] , this ) ) ; } private handleConfiguration ( cfg ) { this . cfg = cfg ; this . groups = makeBazelDocGroupMap ( cfg . groups ) ; } public async provideHover ( document , position , _token ) { if ( ! ( this . cfg && this . groups ) ) { return ; } const range = document . getWordRangeAtPosition ( position ) ; if ( range === undefined ) { return ; } const word = document . getText ( range ) ; if ( ! word ) { return ; } const nextChar = document . getText ( new vscode . Range ( range . end , range . end . translate ( 0 , + 1 ) ) ) ; if ( nextChar !== '(' ) { return ; } const group = this . groups . get ( word ) ; if ( ! group ) { return ; } const hover = makeBazelDocGroupHover ( word , group , this . cfg . baseUrl ) ; hover . range = range ; return hover ; } public dispose ( ) { for ( const disposable of this . disposables ) { disposable . dispose ( ) ; } } } param item param group function makeBazelDocGroupHover ( item , group , baseUrl ) { return new vscode . Hover ( makeBazelDocGroupHoverMarkdown ( item , group , baseUrl ) ) ; } param item param group export function makeBazelDocGroupHoverMarkdown ( item , group , baseUrl ) { let lines = [ ] ; lines . push ( `**${ makeDocEntryLink ( item , group , baseUrl ) }** is a member of _${ group . name }_` ) ; lines . push ( '' ) ; lines . push ( group . items . map ( e => makeDocEntryLink ( e , group , baseUrl ) ) . join ( ', ' ) ) ; if ( group . also ) { lines . push ( '' ) ; for ( const raw of group . also ) { lines . push ( `@see ${ raw }` ) ; } } return new vscode . MarkdownString ( lines . join ( '\n' ) ) ; } param item param group function makeDocEntryLink ( item , group , baseUrl ) { return `[${ item }](${ baseUrl }/${ group . path }#${ item })` ; }