IDEA Plugin: PersistentStateComponent not persisting xml

吃可爱长大的小学妹 提交于 2020-01-04 05:51:28

问题


I can not get my plugin to persist its state. The file configProvider.xml never gets created, nor does the @State annotation has any effect (apparently).

This is the relevant part in the plugin.xml

<extensions defaultExtensionNs="com.intellij">
    <applicationService serviceImplementation="my.plugins.idea.vcs.ConfigProvider" serviceInterface="my.plugins.idea.vcs.ConfigProvider"/>
</extensions>

This is the Class providing the object that should get persisted:

import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import java.util.LinkedHashMap;

@State(
  name = "ConfigProvider",
  storages = {
@Storage(id = "main", file = "$APP_CONFIG$/configProvider.xml") 
  }
)
public class ConfigProvider  implements PersistentStateComponent<ConfigProvider.State> {

  private State state = new State();

  class State {
    public State() {}
    public LinkedHashMap<String, String> commitTypes = null;
    public Integer selectedDefaultCommitTypeIndex = null;
    public String jiraApiUrl;
    public String jiraAuthorization;
    public String jiraFilterId;
  } 

  public static ConfigProvider getInstance() {
return ServiceManager.getService(ConfigProvider.class);
  }

  @Override
  @org.jetbrains.annotations.Nullable
  public ConfigProvider.State getState() {
return state; // gets called regulary
  } 

  @Override
  public void loadState(ConfigProvider.State state) {
this.state = state; // not called at all
  }

}

What am I missing?

Thanks, Christopher


回答1:


Does this help, making the State class public and static?

public static class State {
    //...
}



回答2:


This is now a working example:

package com.foo

import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import org.jetbrains.annotations.NotNull;

@State(
  name = "ConfigProviderState",
  storages = {
    @Storage(id = "other", file = "$APP_CONFIG$/configProvider.xml")
  }
)
public class ConfigProvider implements ApplicationComponent, PersistentStateComponent<ConfigProvider.State> {

  @NotNull
  @Override
  public String getComponentName() {
    return getClass().getSimpleName();
  }
  @Override
  public void disposeComponent() {}
  @Override
  public void initComponent() {}

  public State state = new State();

  public static class State {
    public State() {}
    public String foo;
  }

  @Override
  @org.jetbrains.annotations.Nullable
  public ConfigProvider.State getState() {
    return state; //Saves all public variables to disk.
  }

  @Override
  public void loadState(ConfigProvider.State state) {
    this.state = state; //restores state from disk
  }


  public String getFoo() {
    if (this.state.foo == null) {
      this.state.foo = "https://jira.rz.is/rest/api/2/";
    }
    return this.state.foo;
  }

  public void setFoo(String foo) {
    this.state.foo = foo;
  }

}

plugin.xml

<extensions defaultExtensionNs="com.intellij">
    <!-- Add your extensions here -->
    <applicationService
      serviceImplementation="com.foo.ConfigProvider"
      serviceInterface="com.foo.ConfigProvider"
      overrides="true"
      />
  </extensions>

  <application-components>
    <component>
      <implementation-class>com.foo.ConfigProvider</implementation-class>
      <interface-class>com.foo.ConfigProvider</interface-class>
    </component>
  </application-components>

  <project-components>
    <!-- Add your project components here -->
  </project-components



回答3:


There is essential note on the page Persisting State of Components :

Note that instances of extensions cannot persist their state by implementing PersistentStateComponent

Therefore, your first attempt did not work out due to component was enlisted as part of the <extensions>. The second version works because the component is now registered inside <application-components> May it's worth to remove the extension section to make the answer more precise.



来源:https://stackoverflow.com/questions/32738848/idea-plugin-persistentstatecomponent-not-persisting-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!