I understand that VSC does that, for example storing the caret and scroll positions, or selection if any, and so on, retrieving it when you reopen the file.
I believe it does that by storing data outside of the files, otherwise it would alter their data, or it would need to manipulate metadata according to the local system (Windows, macOS, Linux, etc.), which is not obvious, if even possible.
On Windows, in C:Users<user name>AppDataRoamingCode, there are a number of databases which might fit this purpose, you can do a similar thing.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…