From 37b0c5e91d060c4048a1b32aed7fa1572a10f386 Mon Sep 17 00:00:00 2001 From: Oleg Shevchenko Date: Sat, 5 Nov 2022 17:30:30 +0300 Subject: [PATCH] Fix path construction (#43) --- .nuke/build.schema.json | 1 + Directory.Build.props | 2 +- .../Extensions/JPropertyExtensions.cs | 45 +++++++++++++++++++ .../Extensions/ProjectExtensions.cs | 10 +---- 4 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 src/Resharper.ConfigurationSense/Extensions/JPropertyExtensions.cs diff --git a/.nuke/build.schema.json b/.nuke/build.schema.json index d164a96..4ffc1ed 100644 --- a/.nuke/build.schema.json +++ b/.nuke/build.schema.json @@ -24,6 +24,7 @@ "AppVeyor", "AzurePipelines", "Bamboo", + "Bitbucket", "Bitrise", "GitHubActions", "GitLab", diff --git a/Directory.Build.props b/Directory.Build.props index a7dbdec..0c01950 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - 2022.2.0 + 2022.2.3 diff --git a/src/Resharper.ConfigurationSense/Extensions/JPropertyExtensions.cs b/src/Resharper.ConfigurationSense/Extensions/JPropertyExtensions.cs new file mode 100644 index 0000000..5b98503 --- /dev/null +++ b/src/Resharper.ConfigurationSense/Extensions/JPropertyExtensions.cs @@ -0,0 +1,45 @@ +using System.Collections.Generic; + +using Newtonsoft.Json.Linq; + +namespace Resharper.ConfigurationSense.Extensions +{ + public static class JPropertyExtensions + { + public static string GetSettingsPath(this JProperty property) + { + if (property.Parent == null) + { + return string.Empty; + } + + var positions = new List(); + JToken previous = null; + for (JToken current = property; current != null; current = current.Parent) + { + switch (current.Type) + { + case JTokenType.Property: + var prop = (JProperty)current; + positions.Add(prop.Name); + break; + case JTokenType.Array: + case JTokenType.Constructor: + if (previous != null) + { + int index = ((IList)current).IndexOf(previous); + + positions.Add(index.ToString()); + } + break; + } + + previous = current; + } + + positions.Reverse(); + + return string.Join(":", positions); + } + } +} diff --git a/src/Resharper.ConfigurationSense/Extensions/ProjectExtensions.cs b/src/Resharper.ConfigurationSense/Extensions/ProjectExtensions.cs index be040b8..d3b0a5e 100644 --- a/src/Resharper.ConfigurationSense/Extensions/ProjectExtensions.cs +++ b/src/Resharper.ConfigurationSense/Extensions/ProjectExtensions.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -62,7 +62,7 @@ public static IEnumerable GetJsonProjectSettings( if ((property.Value.Type != JTokenType.Object && property.Value.Type != JTokenType.Array && settingType == JsonSettingType.Value) || settingType == JsonSettingType.All) { - formattedPath = FormatJsonPath(property); + formattedPath = property.GetSettingsPath(); } if (string.IsNullOrEmpty(formattedPath)) @@ -147,12 +147,6 @@ public static IEnumerable GetXmlProjectSettings( return result.Select(x => new KeyValueSetting(x.Key, string.Join(", ", x.Value))); } - private static string FormatJsonPath(JToken property) - { - var formattedPath = property.Path.Replace(".", ":"); - return formattedPath; - } - private static IEnumerable GetSettingTags(string settingsTagName, IProjectFile configFile) { var xmlFile = configFile.GetPrimaryPsiFile() as XmlFile;