Search code examples
javaintellij-ideaintellij-pluginpersistent-storage

Persistent storage in IntelliJ plugin


I am developing an IntelliJ plugin, and I would like to store some settings information as a string. How am I able to store it? I found that PersistentStateComponent<T> is for this purpose, but I didn't find any working and simple use of this class.

Could you please provide some info , how am I able to create a class with only one String argument and methods like:

     Void Save(String value)
    {
       //save SettingsAsString
    {

     String Load()
    {
       //Do something
       return savedSettingsAsString
    }

Thank You!


Solution

  • Here is a simple example:

    Implement the applicationService extension point

    <applicationService serviceImplementation="com.sylvanaar.idea.Lua.options.LuaApplicationSettings"/>

    Then the settings implementation is pretty much boilerplate, you just create fields on the settings class for all the settings you want to store.

    public class LuaApplicationSettings implements PersistentStateComponent<LuaApplicationSettings> {
        public boolean INCLUDE_ALL_FIELDS_IN_COMPLETIONS = false;
        public boolean SHOW_TAIL_CALLS_IN_GUTTER = true;
        public boolean ENABLE_TYPE_INFERENCE = true;
    
        @Override
        public LuaApplicationSettings getState() {
            return this;
        }
    
        @Override
        public void loadState(LuaApplicationSettings state) {
             XmlSerializerUtil.copyBean(state, this);
        }
    
        public static LuaApplicationSettings getInstance() {
            return ServiceManager.getService(LuaApplicationSettings.class);
        }
    }