IDEA 插件:PersistentStateComponent 不持久 xml
IDEA Plugin: PersistentStateComponent not persisting xml
我无法让我的插件保持其状态。
文件 configProvider.xml 永远不会被创建,@State
注释也没有任何效果(显然)。
这是plugin.xml
中的相关部分
<extensions defaultExtensionNs="com.intellij">
<applicationService serviceImplementation="my.plugins.idea.vcs.ConfigProvider" serviceInterface="my.plugins.idea.vcs.ConfigProvider"/>
</extensions>
这是 Class 提供应该持久化的对象:
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
}
}
我错过了什么?
谢谢,
克里斯托弗
这对使 State
class public 和静态有帮助吗?
public static class State {
//...
}
现在这是一个工作示例:
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
页面上有重要说明 Persisting State of Components :
Note that instances of extensions cannot persist their state by implementing PersistentStateComponent
因此,您的第一次尝试没有成功,因为组件已作为 <extensions>
的一部分征募。
第二个版本有效,因为该组件现在已在 <application-components>
中注册
可能值得删除 extension
部分以使答案更准确。
我无法让我的插件保持其状态。
文件 configProvider.xml 永远不会被创建,@State
注释也没有任何效果(显然)。
这是plugin.xml
中的相关部分<extensions defaultExtensionNs="com.intellij">
<applicationService serviceImplementation="my.plugins.idea.vcs.ConfigProvider" serviceInterface="my.plugins.idea.vcs.ConfigProvider"/>
</extensions>
这是 Class 提供应该持久化的对象:
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
}
}
我错过了什么?
谢谢, 克里斯托弗
这对使 State
class public 和静态有帮助吗?
public static class State {
//...
}
现在这是一个工作示例:
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
页面上有重要说明 Persisting State of Components :
Note that instances of extensions cannot persist their state by implementing
PersistentStateComponent
因此,您的第一次尝试没有成功,因为组件已作为 <extensions>
的一部分征募。
第二个版本有效,因为该组件现在已在 <application-components>
中注册
可能值得删除 extension
部分以使答案更准确。