[code-style] provide new overload for working with local code style settings

GitOrigin-RevId: 77a5dc01aac435b43c7c1f2c7b76eb2cd067e9c8
This commit is contained in:
Vojtech Balik
2025-02-04 13:14:27 +01:00
committed by intellij-monorepo-bot
parent 8ad81962ea
commit 85e1dfd642
3 changed files with 48 additions and 8 deletions

View File

@@ -1,5 +1,6 @@
f:com.intellij.application.options.CodeStyle
- sf:LOG:com.intellij.openapi.diagnostic.Logger
- s:computeWithLocalSettings(com.intellij.openapi.project.Project,com.intellij.psi.codeStyle.CodeStyleSettings,java.util.function.Function):java.lang.Object
- s:createTestSettings():com.intellij.psi.codeStyle.CodeStyleSettings
- s:createTestSettings(com.intellij.psi.codeStyle.CodeStyleSettings):com.intellij.psi.codeStyle.CodeStyleSettings
- s:doWithTemporarySettings(com.intellij.openapi.project.Project,com.intellij.psi.codeStyle.CodeStyleSettings,java.lang.Runnable):V
@@ -623,6 +624,7 @@ c:com.intellij.psi.codeStyle.CodeStyleSettingsManager
- USE_PER_PROJECT_SETTINGS:Z
- <init>():V
- f:cloneSettings(com.intellij.psi.codeStyle.CodeStyleSettings):com.intellij.psi.codeStyle.CodeStyleSettings
- computeWithLocalSettings(com.intellij.psi.codeStyle.CodeStyleSettings,java.util.function.Function):java.lang.Object
- createSettings():com.intellij.psi.codeStyle.CodeStyleSettings
- f:createTemporarySettings():com.intellij.psi.codeStyle.CodeStyleSettings
- s:createTestSettings(com.intellij.psi.codeStyle.CodeStyleSettings):com.intellij.psi.codeStyle.CodeStyleSettings

View File

@@ -28,6 +28,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import java.util.function.Consumer;
import java.util.function.Function;
import static java.lang.Math.max;
@@ -325,7 +326,13 @@ public final class CodeStyle {
* Invoke a runnable using the specified settings.
* <p>
* Inside the <code>runnable</code>, <code>localSettings</code> override code style settings for all files associated with
* <code>project</code>. This effect is limited to current thread.
* <code>project</code>. This effect is limited to the current thread.
* <p>
* <code>localSettings</code> <b>must not</b> be an instance representing actual settings
* (e.g. the output of {@link CodeStyle#getSettings}).
* It is preferable to use {@link CodeStyle#runWithLocalSettings(Project, CodeStyleSettings, Consumer)}
* or {@link CodeStyle#computeWithLocalSettings(Project, CodeStyleSettings, Function)} instead,
* which provides a copy of provided base settings out-of-the-box.
*
* @param project The current project.
* @param localSettings The local settings.
@@ -341,7 +348,7 @@ public final class CodeStyle {
* Invoke the specified consumer with a copy of the given <code>baseSettings</code>.
* <p>
* Inside <code>localSettingsConsumer</code>, this copy will override code style settings for all files associated with <code>project</code>.
* This effect is limited to current thread. It is safe to make any changes to the copy of settings passed to the consumer, these changes
* This effect is limited to the current thread. It is safe to make any changes to the copy of settings passed to the consumer, these changes
* will not affect any currently set code style.
*
* @param project The current project.
@@ -354,6 +361,25 @@ public final class CodeStyle {
CodeStyleSettingsManager.getInstance(project).runWithLocalSettings(baseSettings, localSettingsConsumer);
}
/**
* Invoke the specified function with a copy of the given <code>baseSettings</code>.
* <p>
* Inside <code>localSettingsFunction</code>,
* this copy will override code style settings for all files associated with <code>project</code>.
* This effect is limited to the current thread.
* It is safe to make any changes to the copy of settings passed to the function, these changes
* will not affect any currently set code style.
*
* @param project The current project.
* @param baseSettings The base settings to be cloned and used in the function.
* @param localSettingsFunction The function to execute with the base settings copy.
*/
public static <T> T computeWithLocalSettings(@NotNull Project project,
@NotNull CodeStyleSettings baseSettings,
@NotNull Function<? super @NotNull CodeStyleSettings, T> localSettingsFunction) {
return CodeStyleSettingsManager.getInstance(project).computeWithLocalSettings(baseSettings, localSettingsFunction);
}
/**
* Execute the specified runnable with the given temporary code style settings and restore the old settings even if the runnable fails
* with an exception.

View File

@@ -30,6 +30,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
public class CodeStyleSettingsManager implements PersistentStateComponentWithModificationTracker<Element> {
private static final Logger LOG = Logger.getInstance(CodeStyleSettingsManager.class);
@@ -101,15 +102,26 @@ public class CodeStyleSettingsManager implements PersistentStateComponentWithMod
*/
public void runWithLocalSettings(@NotNull CodeStyleSettings baseSettings,
@NotNull Consumer<? super @NotNull CodeStyleSettings> localSettingsConsumer) {
CodeStyleSettings tempSettingsBefore = myLocalSettings.get();
computeWithLocalSettings(baseSettings, localSettings -> {
localSettingsConsumer.accept(localSettings);
return null;
});
}
/**
* @see CodeStyle#computeWithLocalSettings(Project, CodeStyleSettings, Function)
*/
public <T> T computeWithLocalSettings(@NotNull CodeStyleSettings baseSettings,
@NotNull Function<? super @NotNull CodeStyleSettings, T> localSettingsFunction) {
CodeStyleSettings localSettingsBefore = myLocalSettings.get();
try {
CodeStyleSettings tempSettings = new CodeStyleSettings(true, false);
tempSettings.copyFrom(baseSettings);
myLocalSettings.set(tempSettings);
localSettingsConsumer.accept(tempSettings);
CodeStyleSettings localSettings = new CodeStyleSettings(true, false);
localSettings.copyFrom(baseSettings);
myLocalSettings.set(localSettings);
return localSettingsFunction.apply(localSettings);
}
finally {
myLocalSettings.set(tempSettingsBefore);
myLocalSettings.set(localSettingsBefore);
}
}