IDEA-203877 Help to write PersistentStateComponent correctly

GitOrigin-RevId: 4ab7d0e6711c8576ef54769b9c5a337d6c63d4ee
This commit is contained in:
Vladimir Krivosheev
2019-07-26 18:03:15 +03:00
committed by intellij-monorepo-bot
parent cc294e1eea
commit 439b653189
6 changed files with 76 additions and 95 deletions
@@ -2,9 +2,8 @@
package com.intellij.openapi.project
import com.intellij.openapi.components.BaseState
import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.SimplePersistentStateComponent
import com.intellij.openapi.components.State
import com.intellij.openapi.util.ModificationTracker
import com.intellij.util.xmlb.annotations.Property
@Property(style = Property.Style.ATTRIBUTE)
@@ -13,19 +12,7 @@ class ExternalStorageConfiguration : BaseState() {
}
@State(name = "ExternalStorageConfigurationManager")
internal class ExternalStorageConfigurationManagerImpl : PersistentStateComponent<ExternalStorageConfiguration>, ModificationTracker, ExternalStorageConfigurationManager {
private var state = ExternalStorageConfiguration()
override fun getModificationCount(): Long = state.modificationCount
override fun getState(): ExternalStorageConfiguration {
return state
}
override fun loadState(state: ExternalStorageConfiguration) {
this.state = state
}
internal class ExternalStorageConfigurationManagerImpl : SimplePersistentStateComponent<ExternalStorageConfiguration>(ExternalStorageConfiguration()), ExternalStorageConfigurationManager {
override fun isEnabled(): Boolean = state.enabled
/**
@@ -3,24 +3,13 @@ package com.intellij.configurationStore
import com.intellij.openapi.components.*
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.ModificationTracker
import com.intellij.util.xmlb.annotations.Attribute
@State(name = "ProjectId", storages = [(Storage(StoragePathMacros.WORKSPACE_FILE))])
internal class ProjectIdManager : PersistentStateComponent<ProjectIdState>, ModificationTracker {
internal class ProjectIdManager : SimplePersistentStateComponent<ProjectIdState>(ProjectIdState()) {
companion object {
fun getInstance(project: Project) = project.service<ProjectIdManager>()
}
private var state = ProjectIdState()
override fun getState() = state
override fun loadState(state: ProjectIdState) {
this.state = state
}
override fun getModificationCount() = state.modificationCount
}
internal class ProjectIdState : BaseState() {
@@ -1,7 +1,7 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.components
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.util.ModificationTracker
import com.intellij.serialization.PropertyAccessor
import com.intellij.util.xmlb.Accessor
@@ -14,9 +14,9 @@ import java.util.concurrent.atomic.AtomicLongFieldUpdater
import kotlin.collections.ArrayList
import kotlin.collections.LinkedHashMap
private val LOG = Logger.getInstance(BaseState::class.java)
private val LOG = logger<BaseState>()
private val factory: StatePropertyFactory = ServiceLoader.load<StatePropertyFactory>(StatePropertyFactory::class.java).first()
private val factory: StatePropertyFactory = ServiceLoader.load(StatePropertyFactory::class.java).first()
abstract class BaseState : SerializationFilter, ModificationTracker {
companion object {
@@ -37,6 +37,7 @@ abstract class BaseState : SerializationFilter, ModificationTracker {
return p
}
@Suppress("RemoveExplicitTypeArguments")
protected fun <T : BaseState> property(): StoredPropertyBase<T?> = addProperty(factory.stateObject<T?>(null))
/**
@@ -0,0 +1,15 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.components
abstract class SimplePersistentStateComponent<T : BaseState>(initialState: T) : PersistentStateComponentWithModificationTracker<T> {
@Volatile
private var state: T = initialState
final override fun getState() = state
final override fun getStateModificationCount() = state.modificationCount
override fun loadState(state: T) {
this.state = state
}
}
@@ -22,7 +22,6 @@ import java.io.Reader
import java.nio.file.Path
import java.nio.file.Paths
// we cannot use the same approach as we generate JSON scheme because we should load option classes only in a lazy manner
// that's why we don't use snakeyaml TypeDescription approach to load
internal class ConfigurationFileManager(project: Project) {
@@ -7,7 +7,6 @@ import com.intellij.dvcs.branch.DvcsCompareSettings;
import com.intellij.dvcs.branch.DvcsSyncSettings;
import com.intellij.openapi.components.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.ModificationTracker;
import com.intellij.util.ArrayUtilRt;
import com.intellij.util.xmlb.annotations.Attribute;
import com.intellij.util.xmlb.annotations.Tag;
@@ -26,11 +25,10 @@ import static git4idea.config.GitIncomingCheckStrategy.Never;
* Git VCS settings
*/
@State(name = "Git.Settings", storages = @Storage(StoragePathMacros.WORKSPACE_FILE))
public class GitVcsSettings implements PersistentStateComponent<GitVcsOptions>, DvcsSyncSettings, DvcsCompareSettings, ModificationTracker {
public class GitVcsSettings extends SimplePersistentStateComponent<GitVcsOptions> implements DvcsSyncSettings, DvcsCompareSettings {
private static final int PREVIOUS_COMMIT_AUTHORS_LIMIT = 16; // Limit for previous commit authors
private final GitVcsApplicationSettings myAppSettings;
private GitVcsOptions myState = new GitVcsOptions();
/**
* The way the local changes are saved before update if user has selected auto-stash
@@ -41,6 +39,8 @@ public class GitVcsSettings implements PersistentStateComponent<GitVcsOptions>,
}
public GitVcsSettings(GitVcsApplicationSettings appSettings) {
super(new GitVcsOptions());
myAppSettings = appSettings;
}
@@ -54,20 +54,20 @@ public class GitVcsSettings implements PersistentStateComponent<GitVcsOptions>,
@NotNull
public UpdateMethod getUpdateMethod() {
return myState.getUpdateMethod();
return getState().getUpdateMethod();
}
public void setUpdateMethod(UpdateMethod updateType) {
myState.setUpdateMethod(updateType);
getState().setUpdateMethod(updateType);
}
@NotNull
public UpdateChangesPolicy updateChangesPolicy() {
return myState.getUpdateChangesPolicy();
return getState().getUpdateChangesPolicy();
}
public void setUpdateChangesPolicy(UpdateChangesPolicy value) {
myState.setUpdateChangesPolicy(value);
getState().setUpdateChangesPolicy(value);
}
/**
@@ -76,7 +76,7 @@ public class GitVcsSettings implements PersistentStateComponent<GitVcsOptions>,
* @param author an author to save
*/
public void saveCommitAuthor(String author) {
List<String> previousCommitAuthors = myState.getPreviousCommitAuthors();
List<String> previousCommitAuthors = getState().getPreviousCommitAuthors();
previousCommitAuthors.remove(author);
while (previousCommitAuthors.size() >= PREVIOUS_COMMIT_AUTHORS_LIMIT) {
previousCommitAuthors.remove(previousCommitAuthors.size() - 1);
@@ -85,208 +85,198 @@ public class GitVcsSettings implements PersistentStateComponent<GitVcsOptions>,
}
public String[] getCommitAuthors() {
return ArrayUtilRt.toStringArray(myState.getPreviousCommitAuthors());
}
@Override
public long getModificationCount() {
return myState.getModificationCount();
}
@Override
public GitVcsOptions getState() {
return myState;
return ArrayUtilRt.toStringArray(getState().getPreviousCommitAuthors());
}
@Override
public void loadState(@NotNull GitVcsOptions state) {
myState = state;
super.loadState(state);
migrateUpdateIncomingBranchInfo(state);
}
private void migrateUpdateIncomingBranchInfo(@NotNull GitVcsOptions state) {
private static void migrateUpdateIncomingBranchInfo(@NotNull GitVcsOptions state) {
if (!state.isUpdateBranchesInfo()) {
myState.setIncomingCheckStrategy(Never);
state.setIncomingCheckStrategy(Never);
//set default value
myState.setUpdateBranchesInfo(true);
state.setUpdateBranchesInfo(true);
}
}
@Nullable
public String getPathToGit() {
return myState.getPathToGit();
return getState().getPathToGit();
}
public void setPathToGit(@Nullable String value) {
myState.setPathToGit(value);
getState().setPathToGit(value);
}
public boolean autoUpdateIfPushRejected() {
return myState.isPushAutoUpdate();
return getState().isPushAutoUpdate();
}
public void setAutoUpdateIfPushRejected(boolean value) {
myState.setPushAutoUpdate(value);
getState().setPushAutoUpdate(value);
}
public boolean shouldUpdateAllRootsIfPushRejected() {
return myState.isPushUpdateAllRoots();
return getState().isPushUpdateAllRoots();
}
public void setUpdateAllRootsIfPushRejected(boolean value) {
myState.setPushUpdateAllRoots(value);
getState().setPushUpdateAllRoots(value);
}
@Override
@NotNull
public Value getSyncSetting() {
return myState.getRootSync();
return getState().getRootSync();
}
@Override
public void setSyncSetting(@NotNull Value value) {
myState.setRootSync(value);
getState().setRootSync(value);
}
@Nullable
public String getRecentRootPath() {
return myState.getRecentGitRootPath();
return getState().getRecentGitRootPath();
}
public void setRecentRoot(@NotNull String value) {
myState.setRecentGitRootPath(value);
getState().setRecentGitRootPath(value);
}
@NotNull
public Map<String, String> getRecentBranchesByRepository() {
return myState.getRecentBranchByRepository();
return getState().getRecentBranchByRepository();
}
public void setRecentBranchOfRepository(@NotNull String repositoryPath, @NotNull String branch) {
myState.getRecentBranchByRepository().put(repositoryPath, branch);
getState().getRecentBranchByRepository().put(repositoryPath, branch);
}
@Nullable
public String getRecentCommonBranch() {
return myState.getRecentCommonBranch();
return getState().getRecentCommonBranch();
}
public void setRecentCommonBranch(@NotNull String value) {
myState.setRecentCommonBranch(value);
getState().setRecentCommonBranch(value);
}
public void setAutoCommitOnRevert(boolean value) {
myState.setAutoCommitOnRevert(value);
getState().setAutoCommitOnRevert(value);
}
public boolean isAutoCommitOnRevert() {
return myState.isAutoCommitOnRevert();
return getState().isAutoCommitOnRevert();
}
public boolean warnAboutCrlf() {
return myState.getWarnAboutCrlf();
return getState().getWarnAboutCrlf();
}
public void setWarnAboutCrlf(boolean value) {
myState.setWarnAboutCrlf(value);
getState().setWarnAboutCrlf(value);
}
public boolean warnAboutDetachedHead() {
return myState.isWarnAboutDetachedHead();
return getState().isWarnAboutDetachedHead();
}
public void setWarnAboutDetachedHead(boolean value) {
myState.setWarnAboutDetachedHead(value);
getState().setWarnAboutDetachedHead(value);
}
@Nullable
public GitResetMode getResetMode() {
return myState.getResetMode();
return getState().getResetMode();
}
public void setResetMode(@NotNull GitResetMode mode) {
myState.setResetMode(mode);
getState().setResetMode(mode);
}
@Nullable
public GitPushTagMode getPushTagMode() {
return myState.getPushTags();
return getState().getPushTags();
}
public void setPushTagMode(@Nullable GitPushTagMode value) {
myState.setPushTags(value);
getState().setPushTags(value);
}
public boolean shouldSignOffCommit() {
return myState.isSignOffCommit();
return getState().isSignOffCommit();
}
public void setSignOffCommit(boolean value) {
myState.setSignOffCommit(value);
getState().setSignOffCommit(value);
}
@NotNull
public GitIncomingCheckStrategy getIncomingCheckStrategy() {
return myState.getIncomingCheckStrategy();
return getState().getIncomingCheckStrategy();
}
public void setIncomingCheckStrategy(@NotNull GitIncomingCheckStrategy strategy) {
myState.setIncomingCheckStrategy(strategy);
getState().setIncomingCheckStrategy(strategy);
}
public boolean shouldPreviewPushOnCommitAndPush() {
return myState.isPreviewPushOnCommitAndPush();
return getState().isPreviewPushOnCommitAndPush();
}
public void setPreviewPushOnCommitAndPush(boolean value) {
myState.setPreviewPushOnCommitAndPush(value);
getState().setPreviewPushOnCommitAndPush(value);
}
public boolean isPreviewPushProtectedOnly() {
return myState.isPreviewPushProtectedOnly();
return getState().isPreviewPushProtectedOnly();
}
public void setPreviewPushProtectedOnly(boolean value) {
myState.setPreviewPushProtectedOnly(value);
getState().setPreviewPushProtectedOnly(value);
}
public boolean isCommitRenamesSeparately() {
return myState.isCommitRenamesSeparately();
return getState().isCommitRenamesSeparately();
}
public void setCommitRenamesSeparately(boolean value) {
myState.setCommitRenamesSeparately(value);
getState().setCommitRenamesSeparately(value);
}
@NotNull
public DvcsBranchSettings getFavoriteBranchSettings() {
return myState.getFavoriteBranchSettings();
return getState().getFavoriteBranchSettings();
}
public boolean shouldSetUserNameGlobally() {
return myState.isSetUserNameGlobally();
return getState().isSetUserNameGlobally();
}
public void setUserNameGlobally(boolean value) {
myState.setSetUserNameGlobally(value);
getState().setSetUserNameGlobally(value);
}
@Override
public boolean shouldSwapSidesInCompareBranches() {
return myState.isSwapSidesInCompareBranches();
return getState().isSwapSidesInCompareBranches();
}
@Override
public void setSwapSidesInCompareBranches(boolean value) {
myState.setSwapSidesInCompareBranches(value);
getState().setSwapSidesInCompareBranches(value);
}
public boolean shouldAddSuffixToCherryPicksOfPublishedCommits() {
return myState.isAddSuffixToCherryPicksOfPublishedCommits();
return getState().isAddSuffixToCherryPicksOfPublishedCommits();
}
public void setAddSuffixToCherryPicks(boolean value) {
myState.setAddSuffixToCherryPicksOfPublishedCommits(value);
getState().setAddSuffixToCherryPicksOfPublishedCommits(value);
}
@Tag("push-target-info")