Inconsistent File Layout When #if Directive Used
I'm using R# 2018.3.4 on Visual Studio 15.9.11 with mostly default settings, although I do have the "StyleCop by JetBrains 2018.3.0" and "Unity Support 2018.3.0.1103" extensions installed.
Given the following class, when I perform a Silent Code Cleanup (CTRL+E, F) that includes the "Apply file layout" action using the StyleCop pattern, the nested class is moved to the top of the CheckpointSystem class. When the same command is then executed while the #if and #endif lines are commented out, the nested class is moved to the bottom. It should be noted that Visual Studio recognizes that the code between the compile directives is active ("UNITY_EDITOR" is defined).
namespace Company.Project
{
using System;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;
public partial class CheckpointSystem
{
#if UNITY_EDITOR
private static readonly Dictionary<string, string> SavedValues = new Dictionary<string, string>();
public static void LoadCheck(string key, string loaded)
{
Debug.LogWarning($"#CP LOAD:\t{key}{loaded}");
if (SavedValues.TryGetValue(key, out var saved) == false)
{
Debug.LogError($"#CP LOAD: Bad key: {key}");
return;
}
if (saved != loaded)
{
Debug.LogWarning($"#CP LOAD: Failed!\n\tLOAD:\t{key}{loaded}\n\tSAVE:\t{key}{saved}");
}
}
public static void SaveCheck(string key, string saved)
{
Debug.LogWarning($"#CP SAVE:\t{key}{saved}");
SavedValues[key] = saved;
}
#endif
public class Editor
{
public static event Action OnRepaintInspector;
public static event Action<Object> OnSelectCheckpointSystemObject;
public static void RepaintInspector()
{
OnRepaintInspector?.Invoke();
}
public static void SelectCheckpointSystemObject()
{
OnSelectCheckpointSystemObject?.Invoke(instance.gameObject);
}
}
}
}
The above class becomes:
namespace Company.Project
{
using System;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;
public partial class CheckpointSystem
{
public class Editor
{
public static event Action OnRepaintInspector;
public static event Action<Object> OnSelectCheckpointSystemObject;
public static void RepaintInspector()
{
OnRepaintInspector?.Invoke();
}
public static void SelectCheckpointSystemObject()
{
OnSelectCheckpointSystemObject?.Invoke(instance.gameObject);
}
}
#if UNITY_EDITOR
private static readonly Dictionary<string, string> SavedValues = new Dictionary<string, string>();
public static void LoadCheck(string key, string loaded)
{
Debug.LogWarning($"#CP LOAD:\t{key}{loaded}");
if (SavedValues.TryGetValue(key, out var saved) == false)
{
Debug.LogError($"#CP LOAD: Bad key: {key}");
return;
}
if (saved != loaded)
{
Debug.LogWarning($"#CP LOAD: Failed!\n\tLOAD:\t{key}{loaded}\n\tSAVE:\t{key}{saved}");
}
}
public static void SaveCheck(string key, string saved)
{
Debug.LogWarning($"#CP SAVE:\t{key}{saved}");
SavedValues[key] = saved;
}
#endif
}
}
The default StyleCop pattern is being used for the file layout:

Is this a bug or is there something I can configure to avoid this behavior during code cleanup?
When I add additional code to the class outside of the compile directive block, the code is generally laid out correctly, but the compile directive block remains at the bottom:
namespace Company.Product
{
using System;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;
public partial class CheckpointSystem
{
private static readonly Dictionary<string, string> OutsideDirective = new Dictionary<string, string>();
public void PublicMethod()
{
}
private void PrivateMethod()
{
}
public class Editor
{
public static event Action OnRepaintInspector;
public static event Action<Object> OnSelectCheckpointSystemObject;
public static void RepaintInspector()
{
OnRepaintInspector?.Invoke();
}
public static void SelectCheckpointSystemObject()
{
OnSelectCheckpointSystemObject?.Invoke(instance.gameObject);
}
}
#if UNITY_EDITOR
private static readonly Dictionary<string, string> SavedValues = new Dictionary<string, string>();
public static void LoadCheck(string key, string loaded)
{
Debug.LogWarning($"#CP LOAD:\t{key}{loaded}");
if (SavedValues.TryGetValue(key, out var saved) == false)
{
Debug.LogError($"#CP LOAD: Bad key: {key}");
return;
}
if (saved != loaded)
{
Debug.LogWarning($"#CP LOAD: Failed!\n\tLOAD:\t{key}{loaded}\n\tSAVE:\t{key}{saved}");
}
}
public static void SaveCheck(string key, string saved)
{
Debug.LogWarning($"#CP SAVE:\t{key}{saved}");
SavedValues[key] = saved;
}
#endif
}
}
Perhaps this layout is correct, given the current settings, but I'm curious if this can be changed.
Please sign in to leave a comment.
Hello Michael!
Thank you for the feedback.
Seems to be relevant to the following issue - https://youtrack.jetbrains.com/issue/RSRP-470812.
You are welcome to comment or vote for it.
Thank you.
Thanks, Angelina!