import type { TextDocument , Range , Position } from 'vscode-languageserver' ; export function isValidLocationForEmmetAbbreviation ( document , abbreviationRange ) { const startAngle = '<' ; const endAngle = '>' ; const escape = '\\' ; const question = '?' ; let start = { line : 0 , character : 0 } ; let textToBackTrack = document . getText ( { start : { line : start . line , character : start . character } , end : { line : abbreviationRange . start . line , character : abbreviationRange . start . character } } ) ; if ( textToBackTrack . length > 500 ) { textToBackTrack = textToBackTrack . substr ( textToBackTrack . length - 500 ) ; } if ( ! textToBackTrack . trim ( ) ) { return true ; } let valid = true ; let foundSpace = false ; let i = textToBackTrack . length - 1 ; if ( textToBackTrack [ i ] === startAngle ) { return false ; } while ( i >= 0 ) { const char = textToBackTrack [ i ] ; i -- ; if ( ! foundSpace && /\s/ . test ( char ) ) { foundSpace = true ; continue ; } if ( char === question && textToBackTrack [ i ] === startAngle ) { i -- ; continue ; } if ( /\s/ . test ( char ) && textToBackTrack [ i ] === startAngle ) { i -- ; continue ; } if ( char !== startAngle && char !== endAngle ) { continue ; } if ( i >= 0 && textToBackTrack [ i ] === escape ) { i -- ; continue ; } if ( char === endAngle ) { if ( i >= 0 && textToBackTrack [ i ] === '=' ) { continue ; } else { break ; } } if ( char === startAngle ) { valid = ! foundSpace ; break ; } } return valid ; }