-
Notifications
You must be signed in to change notification settings - Fork 18
/
SpecflowSettingsFilesCache.cs
169 lines (146 loc) · 6.09 KB
/
SpecflowSettingsFilesCache.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#nullable enable
using System;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Xml.XPath;
using JetBrains.Application.Threading;
using JetBrains.Collections;
using JetBrains.Diagnostics;
using JetBrains.DocumentModel;
using JetBrains.Lifetimes;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.Caches;
using JetBrains.ReSharper.Resources.Shell;
using Newtonsoft.Json;
using ReSharperPlugin.SpecflowRiderPlugin.Psi;
namespace ReSharperPlugin.SpecflowRiderPlugin.Caching.SpecflowJsonSettings
{
[PsiComponent]
public class SpecflowSettingsFilesCache : SimpleICache<SpecflowSettings>
{
private readonly SpecflowSettingsProvider _settingsProvider;
public SpecflowSettingsFilesCache(Lifetime lifetime,
IShellLocks shellLocks,
IPersistentIndexManager persistentIndexManager,
SpecflowSettingsProvider settingsProvider,
SpecflowSettingsMarshaller marshaller)
: base(lifetime, shellLocks, persistentIndexManager, marshaller)
{
_settingsProvider = settingsProvider;
}
public override string Version => "2";
protected override bool IsApplicable(IPsiSourceFile sf)
{
return GetConfigSource(sf) != ConfigSource.None;
}
public override void MergeLoaded(object data)
{
base.MergeLoaded(data);
foreach (var (key, value) in Map)
UpdateInProvider(key, value);
}
public override object? Build(IPsiSourceFile sourceFile, bool isStartup)
{
return GetConfigSource(sourceFile) switch
{
ConfigSource.Json => LoadSpecflowJson(sourceFile),
ConfigSource.AppConfig => LoadFromAppConfig(sourceFile),
_ => null,
};
}
public override void Drop(IPsiSourceFile sourceFile)
{
UpdateInProvider(sourceFile, null);
base.Drop(sourceFile);
}
public override void Merge(IPsiSourceFile sourceFile, object? builtPart)
{
var newSettings = (SpecflowSettings?)builtPart;
UpdateInProvider(sourceFile, newSettings);
base.Merge(sourceFile, builtPart);
}
private ConfigSource GetConfigSource(IPsiSourceFile file)
{
if (file.Name == "specflow.json")
return ConfigSource.Json;
if (file.Name.Equals("app.config", StringComparison.OrdinalIgnoreCase))
return ConfigSource.AppConfig;
return ConfigSource.None;
}
private static SpecflowSettings? LoadFromAppConfig(IPsiSourceFile sourceFile)
{
try
{
var xml = sourceFile.Document.GetText();
using var sr = new StringReader(xml);
using var xmlReader = XmlReader.Create(sr);
var document = XDocument.Load(xmlReader);
var element = document.XPathSelectElement("//configuration/specFlow");
if (element == null)
return null;
var xmlSerializer = new XmlSerializer(typeof(SpecflowSettings));
using var reader = element.CreateReader();
var specflowSettings = xmlSerializer.Deserialize(reader);
return (SpecflowSettings?)specflowSettings;
}
catch (Exception)
{
//Possible invalid json
}
return null;
}
private static SpecflowSettings? LoadSpecflowJson(IPsiSourceFile sourceFile)
{
var specflowSettingsText = sourceFile.Document.GetText();
try
{
return JsonConvert.DeserializeObject<SpecflowSettings>(specflowSettingsText);
}
catch (Exception)
{
//Possible invalid json
}
return null;
}
private void UpdateInProvider(IPsiSourceFile file, SpecflowSettings? newSettings)
{
var specflowJsonProjectOwner = file.GetProject();
if (specflowJsonProjectOwner == null)
return;
var oldSettings = _settingsProvider.GetSettings(specflowJsonProjectOwner);
if (!_settingsProvider.TryUpdate(specflowJsonProjectOwner, GetConfigSource(file), newSettings))
return;
if (oldSettings.Language.Feature == newSettings?.Language.Feature)
return;
var featureFilesInProject = specflowJsonProjectOwner.GetAllProjectFiles(o => o.Name.EndsWith(".feature"));
foreach (var featureFile in featureFilesInProject)
{
var featurePsiFiles = featureFile.ToSourceFiles();
foreach (var featurePsiFile in featurePsiFiles)
{
using (WriteLockCookie.Create())
{
var psiServices = featurePsiFile.GetPsiServices();
var cachedPsiFile = psiServices.Files.PsiFilesCache.TryGetCachedPsiFile(featurePsiFile, GherkinLanguage.Instance.NotNull());
if (cachedPsiFile != null)
{
cachedPsiFile.OnDocumentChanged(
new DocumentChange(
featurePsiFile.Document,
0,
featurePsiFile.Document.GetTextLength(),
featurePsiFile.Document.GetText(),
featurePsiFile.Document.LastModificationStamp,
TextModificationSide.NotSpecified));
psiServices.Files.MarkAsDirty(featurePsiFile);
psiServices.Caches.MarkAsDirty(featurePsiFile);
}
}
}
}
}
}
}