import { window , Range , commands } from 'vscode' ; import { showErrorMessage } from '../index' ; export const deleteAllLog = context => { const deleteAllLogStatements = commands . registerCommand ( 'consoleLog.deleteAllLogStatements' , ( ) => { const editor = window . activeTextEditor ; if ( ! editor ) { showErrorMessage ( ) ; return ; } const document = editor . document ; const documentText = editor . document . getText ( ) ; const logStatements = getAllLogStatements ( document , documentText ) ; deleteFoundLogStatements ( logStatements ) ; } ) ; context . subscriptions . push ( deleteAllLogStatements ) ; } ; const getAllLogStatements = ( document , documentText ) => { const logStatements = [ ] ; const logRegex = /console.(log|debug|info|warn|error|assert|dir|dirxml|trace|group|groupEnd|time|timeEnd|profile|profileEnd|count)\(([\s\S]*?)\);?/g ; let match ; while ( ( match = logRegex . exec ( documentText ) ) ) { const matchRange = new Range ( document . positionAt ( match . index ) , document . positionAt ( match . index + match [ 0 ] . length ) ) ; if ( ! matchRange . isEmpty ) logStatements . push ( matchRange ) ; } return logStatements ; } ; const deleteFoundLogStatements = logs => { const editor = window . activeTextEditor ; const { document } = editor ; editor . edit ( edit => { logs . forEach ( range => { deleteFoundLogLines ( range , edit , document ) ; } ) ; deleteSuccessShowMessage ( logs ) ; } ) ; } ; const deleteFoundLogLines = ( range , edit , document ) => { for ( let index = range . start . line ; index <= range . end . line ; index ++ ) { edit . delete ( document . lineAt ( index ) . rangeIncludingLineBreak ) ; } } ; const deleteSuccessShowMessage = logs => { const message = logs . length ? `${ logs . length } console.logs deleted` : `${ logs . length } console.log deleted` ; window . showInformationMessage ( message ) ; } ;