import * as vscode from 'vscode' ; import { FlagDiagnostic } from './extension' ; export class ActionProvider implements vscode . CodeActionProvider { provideCodeActions ( document , range , context , token ) { if ( context . only !== undefined && context . only !== vscode . CodeActionKind . QuickFix ) { return ; } return context . diagnostics . map ( ( diag ) => { if ( ! ( diag instanceof FlagDiagnostic ) ) { return ; } if ( diag . flag === '*' ) { return ; } const action = new vscode . CodeAction ( 'Flag as okay' , vscode . CodeActionKind . QuickFix ) ; const workspaceEdit = new vscode . WorkspaceEdit ( ) ; const flagPosition = this . findFlagPosition ( document , range , diag . flag ) ; if ( flagPosition === undefined ) { return ; } if ( document . lineAt ( flagPosition . start ) . firstNonWhitespaceCharacterIndex === flagPosition . start . character ) { workspaceEdit . delete ( document . uri , flagPosition . with ( flagPosition . start , flagPosition . end . with ( flagPosition . end . line , flagPosition . end . character + 1 ) ) ) ; } else { workspaceEdit . replace ( document . uri , flagPosition , '*' ) ; } action . edit = workspaceEdit ; return action ; } ) . filter ( ( e ) => e !== undefined ) as vscode . CodeAction [ ] ; } findFlagPosition ( document , range , flag ) { let line = document . lineAt ( range . start . line ) ; let i = 0 ; while ( line . isEmptyOrWhitespace ) { line = document . lineAt ( range . start . line + i ) ; i ++ ; } const flagCharacterIndex = line . text . search ( new RegExp ( '\\s\\' + flag + '\\s' ) ) + 1 ; if ( flagCharacterIndex === 0 ) { return ; } let result ; if ( line . firstNonWhitespaceCharacterIndex === flagCharacterIndex ) { result = line . range . with ( new vscode . Position ( line . lineNumber , flagCharacterIndex ) , new vscode . Position ( line . lineNumber , flagCharacterIndex + 1 ) ) ; } else { result = line . range . with ( new vscode . Position ( line . lineNumber , flagCharacterIndex ) , new vscode . Position ( line . lineNumber , flagCharacterIndex + 1 ) ) ; } return result ; } }