import * as vscode from 'vscode' ; import * as path from 'path' ; import { AutoCorrect } from './RuboCop' ; export class RubyDocumentFormattingEditProvider implements vscode . DocumentFormattingEditProvider { private autoCorrect : AutoCorrect ; public register ( ctx , documentSelector ) { if ( vscode . workspace . getConfiguration ( "ruby" ) . get ( "format" ) !== "rubocop" ) { return ; } this . autoCorrect = new AutoCorrect ( ) ; this . autoCorrect . test ( ) . then ( ( ) => ctx . subscriptions . push ( vscode . languages . registerDocumentFormattingEditProvider ( documentSelector , this ) ) ) ; } public provideDocumentFormattingEdits ( document , options , token ) { const root = document . fileName ? path . dirname ( document . fileName ) : vscode . workspace . rootPath ; const input = document . getText ( ) ; return this . autoCorrect . correct ( input , root ) . then ( result => { return [ new vscode . TextEdit ( document . validateRange ( new vscode . Range ( 0 , 0 , Infinity , Infinity ) ) , result ) ] ; } , err => { return [ ] ; } ) ; } }