forked from codecadwallader/codemaid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeModelCache.cs
More file actions
98 lines (83 loc) · 3.21 KB
/
CodeModelCache.cs
File metadata and controls
98 lines (83 loc) · 3.21 KB
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
using EnvDTE;
using SteveCadwallader.CodeMaid.Helpers;
using SteveCadwallader.CodeMaid.Properties;
using System.Collections.Generic;
namespace SteveCadwallader.CodeMaid.Model
{
/// <summary>
/// A class for encapsulating a cache of code models.
/// </summary>
internal class CodeModelCache
{
#region Fields
private readonly Dictionary<string, CodeModel> _cache;
#endregion Fields
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="CodeModelCache" /> class.
/// </summary>
internal CodeModelCache()
{
_cache = new Dictionary<string, CodeModel>();
}
#endregion Constructors
#region Internal Methods
/// <summary>
/// Gets a code model for the specified document. If the code model is not present in the
/// cache, a new code model will be generated and added to the cache.
/// </summary>
/// <param name="document">The document.</param>
/// <returns>A code model representing the document.</returns>
internal CodeModel GetCodeModel(Document document)
{
CodeModel codeModel;
OutputWindowHelper.DiagnosticWriteLine($"CodeModelCache.GetCodeModel for '{document.FullName}'");
lock (_cache)
{
if (!_cache.TryGetValue(document.FullName, out codeModel))
{
codeModel = new CodeModel(document) { IsStale = true };
if (Settings.Default.General_CacheFiles)
{
_cache.Add(document.FullName, codeModel);
OutputWindowHelper.DiagnosticWriteLine(" --added to cache (stale).");
}
}
else
{
OutputWindowHelper.DiagnosticWriteLine(codeModel.IsStale
? " --retrieved from cache (stale)."
: " --retrieved from cache (not stale).");
}
}
return codeModel;
}
/// <summary>
/// Removes the code model associated with the specified document if it exists.
/// </summary>
/// <param name="document">The document.</param>
internal void RemoveCodeModel(Document document)
{
lock (_cache)
{
if (_cache.Remove(document.FullName))
{
OutputWindowHelper.DiagnosticWriteLine($"CodeModelCache.RemoveCodeModel from cache for '{document.FullName}'");
}
}
}
/// <summary>
/// Marks the code model associated with the specified document as stale if it exists.
/// </summary>
/// <param name="document">The document.</param>
internal void StaleCodeModel(Document document)
{
if (_cache.TryGetValue(document.FullName, out CodeModel codeModel))
{
codeModel.IsStale = true;
OutputWindowHelper.DiagnosticWriteLine($"CodeModelCache.StaleCodeModel in cache for '{document.FullName}'");
}
}
#endregion Internal Methods
}
}