-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortcuts.cs
More file actions
129 lines (106 loc) · 4.06 KB
/
Shortcuts.cs
File metadata and controls
129 lines (106 loc) · 4.06 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace CodeNavigator
{
/// <summary>
/// A lsit of shortcuts for a language
/// </summary>
internal class Shortcuts
{
private String language;
/// <summary>
/// the language these shortcuts are used for
/// </summary>
internal String Language { get { return language; } }
internal String Path { get; set; }
/// <summary>
/// Shortcuts for the given language
/// </summary>
/// <param name="language"></param>
internal Shortcuts(String language)
{
this.language = language;
}
/// <summary>
/// Add a new shortcut
/// </summary>
/// <param name="shortcutKey">the key used for completion</param>
/// <param name="text">the replacement text</param>
internal void AddShortcut(String shortcutKey, String text)
{
Shortcut shortcut = new Shortcut(shortcutKey, text);
//for each substring starting at the first char add an entry
//to the list
for (int i = 0; i < shortcutKey.Length; i++)
{
String substr = shortcutKey.Substring(i, shortcutKey.Length - i);
//lookup entry
Shortcut dummy = null;
bool bEntryExists = _shortcutMap.TryGetValue(substr, out dummy);
//entry already there, check if it is a shortcut list
if (i==0 && bEntryExists && dummy != _shortcutListDummy)
{
_errors.Add(
"Shortcut '" + shortcutKey +
"' used multiple times.");
//cannot add an entry with the same key
return;
}
//not there -> add
if (i==0)
{
//remove the dummy
if (bEntryExists)
_shortcutMap.Remove(substr);
_shortcutMap.Add(substr, shortcut);
}
else if (!bEntryExists)
//new entry
_shortcutMap.Add(substr, _shortcutListDummy);
//otherwise, the dummy is already there
}
_shortcutList.Add(shortcut);
}
internal Shortcut Get(String key)
{
Shortcut shortcut = null;
_shortcutMap.TryGetValue(key, out shortcut);
if (shortcut == _shortcutListDummy)
shortcut = null;
return shortcut;
}
internal bool HasSubstring(String key)
{
return _shortcutMap.ContainsKey(key);
}
internal int Count()
{
return _shortcutList.Count;
}
//map from key to shortcut
//- also includes all possible endings of keys and their mapping
// to a list of keywords;
//- e.g., fori results in the follwoing entries
// - i -> List
// - ri -> List
// - ori -> List
// - fori -> Shortcut
//- currently we use a dummy entry instead the list because we do not need the list
private System.Collections.Generic.Dictionary<String, Shortcut> _shortcutMap
= new System.Collections.Generic.Dictionary<String, Shortcut>();
//list of shortcuts
private System.Collections.Generic.List<Shortcut> _shortcutList
= new System.Collections.Generic.List<Shortcut>();
public System.Collections.Generic.List<Shortcut> ShortcutList
{
get { return _shortcutList; }
set { _shortcutList = value; }
}
private Shortcut _shortcutListDummy = new Shortcut("<<this is a dummy entry>>","");
private System.Collections.Generic.List<String> _errors = new System.Collections.Generic.List<String>();
}
}