import * as vscode from 'vscode' ; import { buildifierFormat , getBuildifierFileType } from './execute' ; import { BuildifierSettings } from './settings' ; /**
 * Provides document formatting functionality for Bazel files by invoking
 * buildifier.
 */ export class BuildifierFormatter implements vscode . DocumentFormattingEditProvider { constructor ( private settings , disposables ) { disposables . push ( vscode . languages . registerDocumentFormattingEditProvider ( [ { language : 'bazel' } , { language : 'starlark' } , { pattern : '**/BUILD' } , { pattern : '**/BUILD.bazel' } , { pattern : '**/WORKSPACE' } , { pattern : '**/WORKSPACE.bazel' } , { pattern : '**/*.BUILD' } , { pattern : '**/*.bzl' } , { pattern : '**/*.sky' } , ] , this ) ) ; } public async provideDocumentFormattingEdits ( document , options , token ) { const cfg = await this . settings . get ( ) ; if ( ! cfg ) { return [ ] ; } if ( ! cfg . enabled ) { return [ ] ; } const fileContent = document . getText ( ) ; const type = getBuildifierFileType ( document . uri . fsPath ) ; try { const formattedContent = await buildifierFormat ( cfg , fileContent , type , cfg . fixOnFormat ) ; if ( formattedContent === fileContent ) { return [ ] ; } return [ new vscode . TextEdit ( new vscode . Range ( document . positionAt ( 0 ) , document . positionAt ( fileContent . length ) ) , formattedContent ) , ] ; } catch ( err ) { console . warn ( 'buildifier error:' , err ) ; } return [ ] ; } }