Commit 25c11858 authored by Rustam Vishniakov's avatar Rustam Vishniakov Committed by intellij-monorepo-bot
Browse files

IDEA-318457 Code Style resets after restart issue lists in IDEA 2023.1

* Revert the previous NPE fix
* Split init and read from data holder operations
* Add more debug logging
* Drop parent scheme name when loading, it's never used in code style schemes

(cherry picked from commit e98d4552534b885e3d75799e54257563b84f9a49)

IJ-CR-109148

(cherry picked from commit 01c1f1d9ddcea15f9ff72100ab8a9e824be72bf2)

GitOrigin-RevId: e5061ddcfebd9a77d631e51d04f78b720abb37d6
parent 945b27df
Showing with 45 additions and 37 deletions
+45 -37
// Copyright 2000-2018 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.
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.psi.impl.source.codeStyle;
import com.intellij.configurationStore.SchemeDataHolder;
......@@ -19,31 +19,31 @@ import org.jetbrains.annotations.Nullable;
public class CodeStyleSchemeImpl extends ExternalizableSchemeAdapter implements CodeStyleScheme, SerializableScheme {
private static final Logger LOG = Logger.getInstance(CodeStyleSchemeImpl.class);
private SchemeDataHolder<? super CodeStyleSchemeImpl> myDataHolder;
private String myParentSchemeName;
private volatile SchemeDataHolder<? super CodeStyleSchemeImpl> myDataHolder;
private final boolean myIsDefault;
private volatile CodeStyleSettings myCodeStyleSettings;
private volatile @NotNull CodeStyleSettings myCodeStyleSettings;
private long myLastModificationCount;
private final Object lock = new Object();
CodeStyleSchemeImpl(@NotNull String name, String parentSchemeName, @NotNull SchemeDataHolder<? super CodeStyleSchemeImpl> dataHolder) {
CodeStyleSchemeImpl(@NotNull String name, @NotNull SchemeDataHolder<? super CodeStyleSchemeImpl> dataHolder) {
setName(name);
myDataHolder = dataHolder;
myIsDefault = DEFAULT_SCHEME_NAME.equals(name);
myParentSchemeName = parentSchemeName;
myCodeStyleSettings = init(null);
}
public CodeStyleSchemeImpl(@NotNull String name, boolean isDefault, @Nullable CodeStyleScheme parentScheme) {
setName(name);
myIsDefault = isDefault;
init(parentScheme, null);
myCodeStyleSettings = init(parentScheme);
}
@NotNull
private CodeStyleSettings init(@Nullable CodeStyleScheme parentScheme, @Nullable Element root) {
private static CodeStyleSettings init(@Nullable CodeStyleScheme parentScheme) {
final CodeStyleSettings settings;
if (parentScheme == null) {
settings = CodeStyleSettingsManager.getInstance().createSettings();
LOG.debug("Initialized using empty settings");
}
else {
CodeStyleSettings parentSettings = parentScheme.getCodeStyleSettings();
......@@ -52,52 +52,54 @@ public class CodeStyleSchemeImpl extends ExternalizableSchemeAdapter implements
parentSettings = parentSettings.getParentSettings();
}
settings.setParentSettings(parentSettings);
LOG.debug("Initialized using parent scheme '" + parentScheme.getName() + "'");
}
if (root != null) {
try {
settings.readExternal(root);
}
catch (InvalidDataException e) {
LOG.error(e);
}
}
myCodeStyleSettings = settings;
return settings;
}
private static void readFromDataHolder(@NotNull CodeStyleSettings settings,
@NotNull SchemeDataHolder<? super CodeStyleSchemeImpl> dataHolder) {
Element root = dataHolder.read();
try {
settings.readExternal(root);
}
catch (InvalidDataException e) {
LOG.error(e);
}
}
@Override
@NotNull
public CodeStyleSettings getCodeStyleSettings() {
CodeStyleSettings settings = myCodeStyleSettings;
if (settings != null) {
return settings;
SchemeDataHolder<? super CodeStyleSchemeImpl> dataHolder = myDataHolder;
if (dataHolder == null) {
return myCodeStyleSettings;
}
synchronized (lock) {
SchemeDataHolder<? super CodeStyleSchemeImpl> dataHolder = myDataHolder;
Element element= null;
if (dataHolder != null) {
element = dataHolder.read();
// nullize only after element is successfully read, otherwise our state will be undefined - both myDataHolder and myCodeStyleSettings are null
myDataHolder = null;
}
settings = init(myParentSchemeName == null ? null : CodeStyleSchemesImpl.getSchemeManager().findSchemeByName(myParentSchemeName), element);
if (dataHolder != null) {
dataHolder.updateDigest(this);
dataHolder = myDataHolder;
if (dataHolder == null) {
return myCodeStyleSettings;
}
myParentSchemeName = null;
logDebug("Reinit settings from dataHolder");
CodeStyleSettings settings = init(null);
readFromDataHolder(settings, dataHolder);
// nullize only after element is successfully read, otherwise our state will be undefined - both myDataHolder and myCodeStyleSettings are null
myDataHolder = null;
dataHolder.updateDigest(this);
myCodeStyleSettings = settings;
return settings;
}
return settings;
}
public void setCodeStyleSettings(@NotNull CodeStyleSettings codeStyleSettings) {
myCodeStyleSettings = codeStyleSettings;
synchronized (lock) {
myParentSchemeName = null;
myDataHolder = null;
}
logDebug("Replaced code style settings");
}
@Override
......@@ -110,10 +112,10 @@ public class CodeStyleSchemeImpl extends ExternalizableSchemeAdapter implements
public SchemeState getSchemeState() {
synchronized (lock) {
if (myDataHolder == null) {
CodeStyleSettings settings = myCodeStyleSettings;
long currModificationCount = settings == null ? 0L : settings.getModificationTracker().getModificationCount();
final long currModificationCount = myCodeStyleSettings.getModificationTracker().getModificationCount();
if (myLastModificationCount != currModificationCount) {
myLastModificationCount = currModificationCount;
logDebug("Possibly changed");
return SchemeState.POSSIBLY_CHANGED;
}
}
......@@ -133,9 +135,11 @@ public class CodeStyleSchemeImpl extends ExternalizableSchemeAdapter implements
Element newElement = new Element(CODE_STYLE_TAG_NAME);
newElement.setAttribute(CODE_STYLE_NAME_ATTR, getName());
myCodeStyleSettings.writeExternal(newElement);
logDebug("Saved from CodeStyleSettings");
return newElement;
}
else {
logDebug("Saved from dataHolder");
return dataHolder.read();
}
}
......@@ -147,4 +151,8 @@ public class CodeStyleSchemeImpl extends ExternalizableSchemeAdapter implements
}
return super.getDisplayName();
}
private void logDebug(@NotNull String message) {
LOG.debug("Scheme '"+ getName() + "': "+ message);
}
}
......@@ -33,7 +33,7 @@ public abstract class CodeStyleSchemesImpl extends CodeStyleSchemes {
@NotNull String name,
@NotNull Function<? super String, String> attributeProvider,
boolean isBundled) {
return new CodeStyleSchemeImpl(attributeProvider.apply("name"), attributeProvider.apply("parent"), dataHolder);
return new CodeStyleSchemeImpl(attributeProvider.apply("name"), dataHolder);
}
}, null, null, SettingsCategory.CODE);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment