Skip to content

Commit

Permalink
Fix path construction (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
olsh authored Nov 5, 2022
1 parent 17cf0d2 commit 37b0c5e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions .nuke/build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"AppVeyor",
"AzurePipelines",
"Bamboo",
"Bitbucket",
"Bitrise",
"GitHubActions",
"GitLab",
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<SdkVersion>2022.2.0</SdkVersion>
<SdkVersion>2022.2.3</SdkVersion>
</PropertyGroup>
<!-- https://jetbrains.slack.com/archives/CBZ36NH7C/p1628090127002200 -->
<PropertyGroup>
Expand Down
45 changes: 45 additions & 0 deletions src/Resharper.ConfigurationSense/Extensions/JPropertyExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<string>();
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<JToken>)current).IndexOf(previous);

positions.Add(index.ToString());
}
break;
}

previous = current;
}

positions.Reverse();

return string.Join(":", positions);
}
}
}
10 changes: 2 additions & 8 deletions src/Resharper.ConfigurationSense/Extensions/ProjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -62,7 +62,7 @@ public static IEnumerable<KeyValueSetting> 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))
Expand Down Expand Up @@ -147,12 +147,6 @@ public static IEnumerable<KeyValueSetting> 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<IXmlTag> GetSettingTags(string settingsTagName, IProjectFile configFile)
{
var xmlFile = configFile.GetPrimaryPsiFile() as XmlFile;
Expand Down

0 comments on commit 37b0c5e

Please sign in to comment.