Plugin Project Settings, SettingsState is always null
Answered
My plugin has the need for Application and Project level settings. I have the App settings working based on this example: https://github.com/JetBrains/intellij-sdk-docs/tree/main/code_samples/settings
However, that is only for Application Settings. Searching through these forums I have found these posts:
- https://intellij-support.jetbrains.com/hc/en-us/community/posts/206759615-Problem-with-custom-Plugin-Settings
- https://intellij-support.jetbrains.com/hc/en-us/community/posts/206125139-Persisting-State-of-Components
Both of these indicate that I need a constructor in the SettingsState class for the Project, which is also called out here: https://plugins.jetbrains.com/docs/intellij/settings-guide.html#the-configurable-interface
Are there any code examples you could point me to which show the correct way to implement a PersistentStateComponent for Project level settings?
Please sign in to leave a comment.
OK, I got it working with the following, after reading the docs more carefully:
In my `ProjectSettingsConfigurable` file, I created a constructor which accepts a Program object and assigns it to a local variable:
public class ProjectSettingsConfigurable implements Configurable {
protected Project project;
protected ProjectSettingsConfigurable(Project _project) {
project = _project;
}
Then, in the same file, in the `isModified`, `apply`, etc methods, I get settings like so:
Then in my ProjectSettingsState class, I use that object coming in like so:
public class ProjectSettingsState implements PersistentStateComponent<ProjectSettingsState> {
public static ProjectSettingsState getInstance(Project project) {
return ServiceManager.getService(project, ProjectSettingsState.class);
}
Its working great now!