Repository navigation
Expand file tree
/
Copy pathCodeCompletionDataUsageCache.cs
More file actions
218 lines (203 loc) · 7.08 KB
/
Copy pathCodeCompletionDataUsageCache.cs
File metadata and controls
218 lines (203 loc) · 7.08 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.IO;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Project;
namespace ICSharpCode.SharpDevelop.Editor.CodeCompletion
{
/// <summary>
/// Tracks the names of the top-used CodeCompletionData items and gives them higher
/// priority in the completion dropdown.
/// </summary>
public static class CodeCompletionDataUsageCache
{
struct UsageStruct {
public int Uses;
public int ShowCount;
public UsageStruct(int Uses, int ShowCount) {
this.Uses = Uses;
this.ShowCount = ShowCount;
}
}
static Dictionary<string, UsageStruct> dict;
// File format for stored CodeCompletionDataUsageCache
// long magic = 0x6567617355444343 (identifies file type = 'CCDUsage')
// short version = 1 (file version)
// int itemCount
// {
// string itemName
// int itemUses
// int itemShowCount
// }
const long magic = 0x6567617355444343;
const short version = 1;
/// <summary>Minimum number how often an item must be used to be saved to
/// the file. Items with less uses than this count also get a priority penalty.
/// (Because the first use would otherwise always be 100% priority)</summary>
const int MinUsesForSave = 2;
public static string CacheFilename {
get {
if (string.IsNullOrEmpty(PropertyService.ConfigDirectory))
return null;
else
return Path.Combine(PropertyService.ConfigDirectory, "CodeCompletionUsageCache.dat");
}
}
static void LoadCache()
{
dict = new Dictionary<string, UsageStruct>();
SD.ProjectService.SolutionClosed += delegate { SaveCache(); };
string cacheFileName = CodeCompletionDataUsageCache.CacheFilename;
if (string.IsNullOrEmpty(cacheFileName) || !File.Exists(cacheFileName))
return;
using (FileStream fs = new FileStream(cacheFileName, FileMode.Open, FileAccess.Read)) {
using (BinaryReader reader = new BinaryReader(fs)) {
if (reader.ReadInt64() != magic) {
LoggingService.Warn("CodeCompletionDataUsageCache: wrong file magic");
return;
}
if (reader.ReadInt16() != version) {
LoggingService.Warn("CodeCompletionDataUsageCache: unknown file version");
return;
}
int itemCount = reader.ReadInt32();
for (int i = 0; i < itemCount; i++) {
string key = reader.ReadString();
int uses = reader.ReadInt32();
int showCount = reader.ReadInt32();
if (showCount > 1000) {
// reduce count because the usage in the next time
// should have more influence on the past
showCount /= 3;
uses /= 3;
}
dict.Add(key, new UsageStruct(uses, showCount));
}
}
}
LoggingService.Info("Loaded CodeCompletionDataUsageCache (" + dict.Count + " items)");
}
public static void SaveCache()
{
string cacheFileName = CodeCompletionDataUsageCache.CacheFilename;
if (dict == null || string.IsNullOrEmpty(cacheFileName)) {
return;
}
try {
int count;
using (FileStream fs = new FileStream(cacheFileName, FileMode.Create, FileAccess.Write)) {
using (BinaryWriter writer = new BinaryWriter(fs)) {
count = SaveCache(writer);
}
}
LoggingService.Info("Saved CodeCompletionDataUsageCache (" + count + " of " + dict.Count + " items)");
} catch (IOException ex) {
// e.g. cannot open file for write because two SD instances are shutting down simultanously
LoggingService.Warn("Error saving CC cache", ex);
}
}
static int SaveCache(BinaryWriter writer)
{
writer.Write(magic);
writer.Write(version);
int maxSaveItems = CodeCompletionOptions.DataUsageCacheItemCount;
if (dict.Count < maxSaveItems) {
writer.Write(dict.Count);
foreach (KeyValuePair<string, UsageStruct> entry in dict) {
writer.Write(entry.Key);
writer.Write(entry.Value.Uses);
writer.Write(entry.Value.ShowCount);
}
return dict.Count;
} else {
List<KeyValuePair<string, UsageStruct>> saveItems = new List<KeyValuePair<string, UsageStruct>>();
foreach (KeyValuePair<string, UsageStruct> entry in dict) {
if (entry.Value.Uses > MinUsesForSave) {
saveItems.Add(entry);
}
}
if (saveItems.Count > maxSaveItems) {
saveItems.Sort(new SaveItemsComparer());
}
int count = Math.Min(maxSaveItems, saveItems.Count);
writer.Write(count);
for (int i = 0; i < count; i++) {
KeyValuePair<string, UsageStruct> entry = saveItems[i];
writer.Write(entry.Key);
writer.Write(entry.Value.Uses);
writer.Write(entry.Value.ShowCount);
}
return count;
}
}
class SaveItemsComparer : IComparer<KeyValuePair<string, UsageStruct>>
{
public int Compare(KeyValuePair<string, UsageStruct> x, KeyValuePair<string, UsageStruct> y)
{
double a = ((double)x.Value.Uses / x.Value.ShowCount);
return -a.CompareTo((double)y.Value.Uses / y.Value.ShowCount);
}
}
public static void ResetCache()
{
dict = new Dictionary<string, UsageStruct>();
try {
if (File.Exists(CacheFilename)) {
File.Delete(CacheFilename);
}
} catch (Exception ex) {
LoggingService.Warn("CodeCompletionDataUsageCache.ResetCache(): " + ex.Message);
}
}
public static double GetPriority(string dotnetName, bool incrementShowCount)
{
if (!CodeCompletionOptions.DataUsageCacheEnabled)
return 0;
if (dict == null) {
LoadCache();
}
UsageStruct usage;
if (!dict.TryGetValue(dotnetName, out usage))
return 0;
double priority = (double)usage.Uses / usage.ShowCount;
if (usage.Uses < MinUsesForSave)
priority *= 0.2;
if (incrementShowCount) {
usage.ShowCount += 1;
dict[dotnetName] = usage;
}
return priority;
}
public static void IncrementUsage(string dotnetName)
{
if (!CodeCompletionOptions.DataUsageCacheEnabled)
return;
if (dict == null) {
LoadCache();
}
UsageStruct usage;
if (!dict.TryGetValue(dotnetName, out usage)) {
usage = new UsageStruct(0, 2);
}
usage.Uses += 1;
dict[dotnetName] = usage;
}
}
}