import { TextDocument } from 'vscode' ; import { KeyStyle , ParserOptions , KeyInDocument , Config } from '~/core' ; import { File } from '~/utils' ; export abstract class Parser { abstract readonly id : string ; private supportedExtsRegex : RegExp ; readonly readonly : boolean = false ; constructor ( public readonly languageIds , public readonly supportedExts , public options = { get indent ( ) { return Config . indent ; } , get tab ( ) { return Config . tabStyle ; } , } ) { this . supportedExtsRegex = new RegExp ( `.?(${ this . supportedExts })$` ) ; } supports ( ext ) { return ! ! ext . toLowerCase ( ) . match ( this . supportedExtsRegex ) ; } async load ( filepath ) { const raw = await File . read ( filepath ) ; if ( ! raw ) return { } ; return await this . parse ( raw ) ; } async save ( filepath , object , sort ) { const text = await this . dump ( object , sort ) ; await File . write ( filepath , text ) ; } abstract parse ( text ) ; abstract dump ( object , sort ) ; parseAST ( text ) { return [ ] ; } navigateToKey ( text , keypath , keystyle ) { return this . parseAST ( text ) . find ( k => k . key === keypath ) ; } annotationSupported = false ; annotationLanguageIds : string [ ] = [ ] ; annotationGetKeys ( document ) { return this . parseAST ( document . getText ( ) ) ; } }