import { CustomUIClass , ICustomClassUIMethod } from "ui5plugin-parser/dist/classes/UI5Classes/UI5Parser/UIClass/CustomUIClass" ; import { TextDocumentTransformer } from "ui5plugin-parser/dist/classes/utils/TextDocumentTransformer" ; import * as vscode from "vscode" ; import { TextDocumentAdapter } from "../../adapters/vscode/TextDocumentAdapter" ; export class ReusableMethods { static getPositionOfTheLastUIDefine ( document ) { let position ; const UIClass = TextDocumentTransformer . toCustomUIClass ( new TextDocumentAdapter ( document ) ) ; if ( UIClass ) { const mainFunction = UIClass . fileContent ?. body [ 0 ] ?. expression ; const definePaths = mainFunction ?. arguments [ 0 ] ?. elements ; let insertPosition = 0 ; if ( definePaths ?. length ) { const lastDefinePath = definePaths [ definePaths . length - 1 ] ; insertPosition = lastDefinePath . end ; } else { insertPosition = mainFunction ?. arguments [ 0 ] ?. start ; } position = document . positionAt ( insertPosition ) ; } return position ; } static getIfPositionIsInTheLastOrAfterLastMember ( UIClass , position ) { const currentMethod = UIClass . methods . find ( method => method . acornNode ?. start < position && method . acornNode ?. end > position ) ; const positionIsInMethod = ! ! currentMethod ; const positionIsAfterLastMethod = positionIsInMethod ? false : this . _getIfPositionIsAfterLastMember ( UIClass , position ) ; return positionIsInMethod || positionIsAfterLastMethod ; } private static _getIfPositionIsAfterLastMember ( UIClass , position ) { let isPositionAfterLastMethod = false ; const properties = UIClass . acornClassBody ?. properties || [ ] ; const lastProperty = properties [ properties . length - 1 ] ; if ( lastProperty ) { isPositionAfterLastMethod = lastProperty . end <= position && UIClass . acornClassBody . end > position ; } return isPositionAfterLastMethod ; } static getIfMethodIsLastOne ( UIClass , method ) { let currentMethodIsLastMethod = false ; const propertyValues = UIClass . acornClassBody ?. properties ?. map ( ( node ) => node . value ) ; if ( propertyValues ) { const methodsInClassBody = UIClass . methods . filter ( method => { return propertyValues . includes ( method . acornNode ) ; } ) ; currentMethodIsLastMethod = methodsInClassBody . indexOf ( method ) === methodsInClassBody . length - 1 ; } return currentMethodIsLastMethod ; } static getIfPositionIsInPropertyName ( UIClass , position ) { let bPositionIsInPropertyName = true ; const positionIsBetweenProperties = ! ! UIClass . acornClassBody . properties ?. find ( ( node , index ) => { let correctNode = false ; const nextNode = UIClass . acornClassBody . properties [ index + 1 ] ; if ( nextNode && node . end < position && nextNode . start > position ) { correctNode = true ; } return correctNode ; } ) ; const positionIsInPropertyKey = ! ! UIClass . acornClassBody . properties ?. find ( ( node ) => { return node . key ?. start <= position && node . key ?. end >= position ; } ) ; bPositionIsInPropertyName = positionIsBetweenProperties || positionIsInPropertyKey ; return bPositionIsInPropertyName ; } }