import * as vscode from "vscode" ; import { SwitchToControllerCommand } from "../../../vscommands/switchers/SwitchToControllerCommand" ; import { InsertType , MethodInserter } from "../util/MethodInserter" ; import { CustomDiagnostics } from "../../../registrators/DiagnosticsRegistrator" ; import { XMLParser } from "ui5plugin-parser" ; import { TextDocumentTransformer } from "ui5plugin-parser/dist/classes/utils/TextDocumentTransformer" ; import { UI5Plugin } from "../../../../UI5Plugin" ; import { TextDocumentAdapter } from "../../../adapters/vscode/TextDocumentAdapter" ; export class XMLCodeActionProvider { static async getCodeActions ( document , range ) { const providerResult = this . _getEventAutofillCodeActions ( document , range ) ; return providerResult ; } private static _getEventAutofillCodeActions ( document , range ) { const providerResult = [ ] ; const diagnostics = vscode . languages . getDiagnostics ( document . uri ) ; const diagnostic = diagnostics . filter ( diagnostic => diagnostic instanceof CustomDiagnostics ) . find ( diagnostic => { return diagnostic . range . contains ( range ) ; } ) ; const XMLFile = diagnostic ?. attribute && TextDocumentTransformer . toXMLFile ( new TextDocumentAdapter ( document ) ) ; if ( diagnostic ?. attribute && XMLFile ) { const currentPositionOffset = document ?. offsetAt ( range . end ) ; const attributeData = XMLParser . getAttributeNameAndValue ( diagnostic . attribute ) ; attributeData . attributeValue = XMLParser . getEventHandlerNameFromAttributeValue ( attributeData . attributeValue ) ; const tagText = XMLParser . getTagInPosition ( XMLFile , currentPositionOffset ) . text ; const tagPrefix = XMLParser . getTagPrefix ( tagText ) ; const classNameOfTheTag = XMLParser . getClassNameFromTag ( tagText ) ; const libraryPath = XMLParser . getLibraryPathFromTagPrefix ( XMLFile , tagPrefix , currentPositionOffset ) ; const classOfTheTag = [ libraryPath , classNameOfTheTag ] . join ( "." ) ; const events = UI5Plugin . getInstance ( ) . parser . classFactory . getClassEvents ( classOfTheTag ) ; const event = events . find ( event => event . name === attributeData . attributeName ) ; if ( event ) { const controllerName = SwitchToControllerCommand . getResponsibleClassForCurrentView ( ) ; if ( controllerName ) { const insertCodeAction = MethodInserter . createInsertMethodCodeAction ( controllerName , attributeData . attributeValue , "oEvent" , "" , InsertType . Method ) ; if ( insertCodeAction ) { insertCodeAction . diagnostics = [ diagnostic ] ; providerResult . push ( insertCodeAction ) ; } } } } return providerResult ; } }