My plugin includes a PersistentStateComponent that is stored in the IDE’s config/options directory. I’ve been refactoring it quite a bit – specifically breaking it up into more modular sub-classes – and have written logic to migrate field values from their previous location into the new location. I’ve retained the original fields and marked them as @Deprecated, and the migration logic resides in the PersistentStateComponent#loadState() method, modifying the loaded instance before returning it.
I can see everything get modified properly, and my plugin works against the migrated settings, and when I close the IDE, I can see the PersistentStateComponent#getState() method being called with the post-migration state. However, the actual file in config/options is unmodified.
However, if I open the applicationConfigurable that manages that PersistentStateComponent and make any change to it, everything gets persisted properly. Looking at the caller of that UI’s apply() method, it looks like it’s just calling SaveAndSyncHandler.getInstance().scheduleSave(new SaveAndSyncHandler.SaveTask(null, true)), but if I do that after my own migration logic, it’s still not persisted.
Obviously there must be some step I’m missing here to tell the IDE that I’ve changed one of its persistent state components and I’d like those changes written to disk. Does anyone know what that might be?
Hello!
For migration, there is a ConverterProvider mechanism. It’s underdocumented, but it might fit your scenario.
The actual writing of the state to the disk has many moving parts and might not always be immediate.
But you can force it via File | Save All.
Additionally,
- non-roamable components tend to be persisted only once per 5 minutes
- if the modification tracker on the persistent component is not changed, the actual persistence does not happen.
Thanks, @robert.novotny. I apologize for what is likely a dumb question, but where is the modification tracker for the persistent state component? I’ve never added one explicitly as it’s not part if the PersistentStateComponent interface. If it’s just a matter of changing a modification count, that’s obviously simple.
It’s definitely not a dumb question, I am just updating docs on that.
If I remember correctly, you are in Java mode, plainly implementing a PersistentStateComponent.
- Let your component implement
PersistentStateComponentWithModificationTracker.
- Add a
SimpleModificationTracker as an instance variable to the component.
- override
public long getStateModificationCount() from the PSCWMT interface in the component.
- create a custom logic that increments the modification tracker count on actual change via
incModificationCount() on the tracker.
- if there is no change, just return the
getModificationCount() from the tracker.
See how font handling does that in Java that uses a very simple versioning to actually track that some modification took place.
Thanks for the details. Unfortunately I’m still not seeing the changes being persisted. Let me explain what I did, but it’s actually quite simple:
- Made my
PersistentStateComponent implement PersistentStateComponentWithModificationTracker instead and extend SimpleModificationTracker.
- Made
getStateModificationCount() return getModificationCount() from SimpleModificationTracker.
- Updated the logic that reorganizes the contents of that component to call
incModificationCount() followed by SaveAndSyncHandler.getInstance().scheduleSave(new SaveAndSyncHandler.SaveTask(null, true)).
After launching the IDE, I can see the component state being changed properly, and I can see the post-change steps described above happening followed by a call to getStateModificationCount() with the updated modification count value, but upon closing the IDE process, I don’t see the changes persisted.
I even tried implementing getStateModificationCount() to return System.currentTimeMillis(), and I don’t see the changes persisted. While entirely sub-optimal, that should guarantee that the changes are written to disk when the IDE process is closed, no?
I figured it out. By applying the changes in loadState(), the IDE was never seeing anything as being different because it was always comparing the post-change state. That’s why making a token other change would cause things to persist. I just moved the change logic out of loadState() and into the settings object’s getInstance() method (checking for whether changes are needed exactly once per-application lifetime) and everything persists properly now, even without needing a modification tracker. Thanks for the help!