-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGcodeClassifier.cs
More file actions
202 lines (175 loc) · 9.66 KB
/
GcodeClassifier.cs
File metadata and controls
202 lines (175 loc) · 9.66 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
namespace GcodeLanguage
{
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Tagging;
using Microsoft.VisualStudio.Utilities;
/// <summary>
/// Classifier that classifies all text as an instance of the "GcodeClassifier" classification type.
/// </summary>
// internal class GcodeClassifier : IClassifier
// {
// /// <summary>
// /// Classification type.
// /// </summary>
// private readonly IClassificationType classificationType;
// /// <summary>
// /// Initializes a new instance of the <see cref="GcodeClassifier"/> class.
// /// </summary>
// /// <param name="registry">Classification registry.</param>
// internal GcodeClassifier(IClassificationTypeRegistryService registry)
// {
// this.classificationType = registry.GetClassificationType("GcodeClassifier");
// }
// #region IClassifier
//#pragma warning disable 67
// /// <summary>
// /// An event that occurs when the classification of a span of text has changed.
// /// </summary>
// /// <remarks>
// /// This event gets raised if a non-text change would affect the classification in some way,
// /// for example typing /* would cause the classification to change in C# without directly
// /// affecting the span.
// /// </remarks>
// public event EventHandler<ClassificationChangedEventArgs> ClassificationChanged;
//#pragma warning restore 67
// /// <summary>
// /// Gets all the <see cref="ClassificationSpan"/> objects that intersect with the given range of text.
// /// </summary>
// /// <remarks>
// /// This method scans the given SnapshotSpan for potential matches for this classification.
// /// In this instance, it classifies everything and returns each span as a new ClassificationSpan.
// /// </remarks>
// /// <param name="span">The span currently being classified.</param>
// /// <returns>A list of ClassificationSpans that represent spans identified to be of this classification.</returns>
// public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
// {
// var result = new List<ClassificationSpan>()
// {
// new ClassificationSpan(new SnapshotSpan(span.Snapshot, new Span(span.Start, span.Length)), this.classificationType)
// };
// return result;
// }
// #endregion
// }
internal sealed class GcodeClassifier : ITagger<ClassificationTag>
{
ITextBuffer _buffer;
private readonly ITagAggregator<GcodeTokenTag> _aggregator;
private readonly IDictionary<GcodeTokenTypes, IClassificationType> _GcodeTypes;
public event EventHandler<SnapshotSpanEventArgs> TagsChanged;
/// <summary>
/// Construct the classifier and define search tokens
/// </summary>
internal GcodeClassifier(ITextBuffer buffer,
ITagAggregator<GcodeTokenTag> GcodeTagAggregator,
IClassificationTypeRegistryService typeService)
{
_buffer = buffer;
_aggregator = GcodeTagAggregator;
_aggregator.TagsChanged += OnAggregatorTagsChanged;
_GcodeTypes = new Dictionary<GcodeTokenTypes, IClassificationType>();
_GcodeTypes[GcodeTokenTypes.Gcode_Undefined] = typeService.GetClassificationType("Gcode_Undefined");
_GcodeTypes[GcodeTokenTypes.Gcode_A] = typeService.GetClassificationType("Gcode_A");
_GcodeTypes[GcodeTokenTypes.Gcode_B] = typeService.GetClassificationType("Gcode_B");
_GcodeTypes[GcodeTokenTypes.Gcode_C] = typeService.GetClassificationType("Gcode_C");
_GcodeTypes[GcodeTokenTypes.Gcode_D] = typeService.GetClassificationType("Gcode_D");
_GcodeTypes[GcodeTokenTypes.Gcode_E] = typeService.GetClassificationType("Gcode_E");
_GcodeTypes[GcodeTokenTypes.Gcode_F] = typeService.GetClassificationType("Gcode_F");
_GcodeTypes[GcodeTokenTypes.Gcode_G] = typeService.GetClassificationType("Gcode_G");
_GcodeTypes[GcodeTokenTypes.Gcode_H] = typeService.GetClassificationType("Gcode_H");
_GcodeTypes[GcodeTokenTypes.Gcode_I] = typeService.GetClassificationType("Gcode_I");
_GcodeTypes[GcodeTokenTypes.Gcode_J] = typeService.GetClassificationType("Gcode_J");
_GcodeTypes[GcodeTokenTypes.Gcode_K] = typeService.GetClassificationType("Gcode_K");
_GcodeTypes[GcodeTokenTypes.Gcode_L] = typeService.GetClassificationType("Gcode_L");
_GcodeTypes[GcodeTokenTypes.Gcode_M] = typeService.GetClassificationType("Gcode_M");
_GcodeTypes[GcodeTokenTypes.Gcode_N] = typeService.GetClassificationType("Gcode_N");
_GcodeTypes[GcodeTokenTypes.Gcode_O] = typeService.GetClassificationType("Gcode_O");
_GcodeTypes[GcodeTokenTypes.Gcode_P] = typeService.GetClassificationType("Gcode_P");
_GcodeTypes[GcodeTokenTypes.Gcode_Q] = typeService.GetClassificationType("Gcode_Q");
_GcodeTypes[GcodeTokenTypes.Gcode_R] = typeService.GetClassificationType("Gcode_R");
_GcodeTypes[GcodeTokenTypes.Gcode_S] = typeService.GetClassificationType("Gcode_S");
_GcodeTypes[GcodeTokenTypes.Gcode_T] = typeService.GetClassificationType("Gcode_T");
_GcodeTypes[GcodeTokenTypes.Gcode_U] = typeService.GetClassificationType("Gcode_U");
_GcodeTypes[GcodeTokenTypes.Gcode_V] = typeService.GetClassificationType("Gcode_V");
_GcodeTypes[GcodeTokenTypes.Gcode_W] = typeService.GetClassificationType("Gcode_W");
_GcodeTypes[GcodeTokenTypes.Gcode_X] = typeService.GetClassificationType("Gcode_X");
_GcodeTypes[GcodeTokenTypes.Gcode_Y] = typeService.GetClassificationType("Gcode_Y");
_GcodeTypes[GcodeTokenTypes.Gcode_Z] = typeService.GetClassificationType("Gcode_Z");
_GcodeTypes[GcodeTokenTypes.Gcode_minus] = typeService.GetClassificationType("Gcode_minus");
_GcodeTypes[GcodeTokenTypes.Gcode_0] = typeService.GetClassificationType("Gcode_0");
_GcodeTypes[GcodeTokenTypes.Gcode_1] = typeService.GetClassificationType("Gcode_1");
_GcodeTypes[GcodeTokenTypes.Gcode_2] = typeService.GetClassificationType("Gcode_2");
_GcodeTypes[GcodeTokenTypes.Gcode_3] = typeService.GetClassificationType("Gcode_3");
_GcodeTypes[GcodeTokenTypes.Gcode_4] = typeService.GetClassificationType("Gcode_4");
_GcodeTypes[GcodeTokenTypes.Gcode_5] = typeService.GetClassificationType("Gcode_5");
_GcodeTypes[GcodeTokenTypes.Gcode_6] = typeService.GetClassificationType("Gcode_6");
_GcodeTypes[GcodeTokenTypes.Gcode_7] = typeService.GetClassificationType("Gcode_7");
_GcodeTypes[GcodeTokenTypes.Gcode_8] = typeService.GetClassificationType("Gcode_8");
_GcodeTypes[GcodeTokenTypes.Gcode_9] = typeService.GetClassificationType("Gcode_9");
_GcodeTypes[GcodeTokenTypes.Gcode_Comment] = typeService.GetClassificationType("Gcode_Comment");
_GcodeTypes[GcodeTokenTypes.Gcode_ocode] = typeService.GetClassificationType("Gcode_ocode");
// if typeService.GetClassificationType returns Null, check GcodeClassifierClassificationDefinition
// o-codes
// Operators (in order of precedence from highest to lowest)
// **
// * / MOD
// + -
// EQ NE GT GE LT LE
// AND OR XOR
// Functions
// ATAN
// ABS
// ACOS
// ASIN
// COS
// EXP
// FIX
// FUP
// ROUND
// LN
// SIN
// SQRT
// TAN
// EXISTS
// keywords
// if else endif sub endsub call
}
private void OnAggregatorTagsChanged(object sender, TagsChangedEventArgs e) {
if (TagsChanged == null) {
return;
}
NormalizedSnapshotSpanCollection spans = e.Span.GetSpans(_buffer.CurrentSnapshot);
if (spans.Count == 0) {
return;
}
TagsChanged.Invoke(this, new SnapshotSpanEventArgs(spans[0]));
}
/// <summary>
/// Search the given span for any instances of classified tags
/// </summary>
public IEnumerable<ITagSpan<ClassificationTag>> GetTags(NormalizedSnapshotSpanCollection spans) {
if (spans.Count == 0) {
yield break;
}
foreach (var tagSpan in _aggregator.GetTags(spans)) {
var tagSpans = tagSpan.Span.GetSpans(spans[0].Snapshot);
if (tagSpans.Count == 0) {
continue;
}
if (_GcodeTypes.TryGetValue(tagSpan.Tag.type, out IClassificationType classificationType) &&
classificationType != null) {
yield return new TagSpan<ClassificationTag>(
tagSpans[0],
new ClassificationTag(classificationType)
);
}
}
}
}
}