~ubuntu-branches/ubuntu/wily/monodevelop/wily

« back to all changes in this revision

Viewing changes to src/core/Mono.Texteditor/Mono.TextEditor.Highlighting/SyntaxModeService.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2012-05-27 18:08:20 UTC
  • mfrom: (19.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20120527180820-fydl21qnbnfr8w2t
Tags: 3.0.2+dfsg-3
* [fcecfe7] Fix monodevelop-core-addins.pc.in to point to actual 
  installed location of assemblies.
* [26e1a07] DebSrc 3.0 does not support Quilt's -p parameter, so 
  manually adjust the path in the patch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
using System.Threading;
34
34
using System.Xml;
35
35
using System.Xml.Schema;
 
36
using System.Linq;
36
37
 
37
38
namespace Mono.TextEditor.Highlighting
38
39
{
39
40
        public static class SyntaxModeService
40
41
        {
41
 
                static Dictionary<string, SyntaxMode> syntaxModes = new Dictionary<string, SyntaxMode> ();
42
 
                static Dictionary<string, ColorSheme>      styles      = new Dictionary<string, ColorSheme> ();
 
42
                static Dictionary<string, ISyntaxModeProvider> syntaxModes = new Dictionary<string, ISyntaxModeProvider> ();
 
43
                static Dictionary<string, ColorScheme> styles      = new Dictionary<string, ColorScheme> ();
43
44
                static Dictionary<string, IXmlProvider> syntaxModeLookup = new Dictionary<string, IXmlProvider> ();
44
45
                static Dictionary<string, IXmlProvider> styleLookup      = new Dictionary<string, IXmlProvider> ();
45
46
                static Dictionary<string, string> isLoadedFromFile = new Dictionary<string, string> ();
59
60
                        }
60
61
                }
61
62
                
62
 
                public static string GetFileNameForStyle (ColorSheme style)
 
63
                public static string GetFileNameForStyle (ColorScheme style)
63
64
                {
64
65
                        string result;
65
66
                        if (!isLoadedFromFile.TryGetValue (style.Name, out result))
67
68
                        return result;
68
69
                }
69
70
                
70
 
                public static void InstallSyntaxMode (string mimeType, SyntaxMode mode)
 
71
                public static void InstallSyntaxMode (string mimeType, ISyntaxModeProvider modeProvider)
71
72
                {
72
73
                        if (syntaxModeLookup.ContainsKey (mimeType))
73
74
                                syntaxModeLookup.Remove (mimeType);
74
 
                        syntaxModes[mimeType] = mode;
 
75
                        syntaxModes[mimeType] = modeProvider;
75
76
                }
76
77
                
77
 
                public static ColorSheme GetColorStyle (Gtk.Style widgetStyle, string name)
 
78
                public static ColorScheme GetColorStyle (Gtk.Style widgetStyle, string name)
78
79
                {
79
80
                        if (styles.ContainsKey (name))
80
81
                                return styles [name];
94
95
                        return null;
95
96
                }
96
97
                
97
 
                public static IXmlProvider GetProvider (ColorSheme style)
 
98
                public static IXmlProvider GetProvider (ColorScheme style)
98
99
                {
99
100
                        if (styleLookup.ContainsKey (style.Name)) 
100
101
                                return styleLookup[style.Name];
107
108
                                throw new System.ArgumentException ("Style " + name + " not found", "name");
108
109
                        XmlReader reader = styleLookup [name].Open ();
109
110
                        try {
110
 
                                styles [name] = ColorSheme.LoadFrom (reader);
 
111
                                styles [name] = ColorScheme.LoadFrom (reader);
111
112
                        } catch (Exception e) {
112
113
                                throw new IOException ("Error while loading style :" + name, e);
113
114
                        } finally {
121
122
                                throw new System.ArgumentException ("Syntax mode for mime:" + mimeType + " not found", "mimeType");
122
123
                        XmlReader reader = syntaxModeLookup [mimeType].Open ();
123
124
                        try {
124
 
                                SyntaxMode mode = SyntaxMode.Read (reader);
 
125
                                var mode = SyntaxMode.Read (reader);
125
126
                                foreach (string mime in mode.MimeType.Split (';')) {
126
 
                                        syntaxModes [mime] = mode;
 
127
                                        syntaxModes [mime] = new ProtoTypeSyntaxModeProvider (mode);
127
128
                                }
128
129
                        } catch (Exception e) {
129
130
                                throw new IOException ("Error while syntax mode for mime:" + mimeType, e);
132
133
                        }
133
134
                }
134
135
                
135
 
                public static SyntaxMode GetSyntaxMode (string mimeType)
136
 
                {
137
 
                        if (syntaxModes.ContainsKey (mimeType))
138
 
                                return syntaxModes[mimeType];
139
 
                        if (syntaxModeLookup.ContainsKey (mimeType)) {
 
136
                public static SyntaxMode GetSyntaxMode (TextDocument doc)
 
137
                {
 
138
                        return GetSyntaxMode (doc, doc.MimeType);
 
139
                }
 
140
                
 
141
                public static SyntaxMode GetSyntaxMode (TextDocument doc, string mimeType)
 
142
                {
 
143
                        SyntaxMode result = null;
 
144
                        if (syntaxModes.ContainsKey (mimeType)) {
 
145
                                result = syntaxModes [mimeType].Create (doc);
 
146
                        } else if (syntaxModeLookup.ContainsKey (mimeType)) {
140
147
                                LoadSyntaxMode (mimeType);
141
148
                                syntaxModeLookup.Remove (mimeType);
142
 
                                return GetSyntaxMode (mimeType);
143
 
                        }
144
 
                        return null;
 
149
                                result = GetSyntaxMode (doc, mimeType);
 
150
                        }
 
151
                        if (result != null) {
 
152
                                foreach (var rule in semanticRules.Where (r => r.Item1 == mimeType)) {
 
153
                                        result.AddSemanticRule (rule.Item2, rule.Item3);
 
154
                                }
 
155
                        }
 
156
                        return result;
145
157
                }
146
158
                
147
159
                public static bool ValidateAllSyntaxModes ()
148
160
                {
 
161
                        var doc = new TextDocument ();
149
162
                        foreach (string mime in new List<string> (syntaxModeLookup.Keys)) {
150
 
                                GetSyntaxMode (mime);
 
163
                                GetSyntaxMode (doc, mime);
151
164
                        }
152
165
                        syntaxModeLookup.Clear ();
153
166
                        foreach (string style in new List<string> (styleLookup.Keys)) {
155
168
                        }
156
169
                        styleLookup.Clear ();
157
170
                        bool result = true;
158
 
                        foreach (KeyValuePair<string, ColorSheme> style in styles) {
159
 
                                HashSet<SyntaxMode> checkedModes = new HashSet<SyntaxMode> ();
160
 
                                foreach (KeyValuePair<string, SyntaxMode> mode in syntaxModes) {
 
171
                        foreach (KeyValuePair<string, ColorScheme> style in styles) {
 
172
                                var checkedModes = new HashSet<ISyntaxModeProvider> ();
 
173
                                foreach (var mode in syntaxModes) {
161
174
                                        if (checkedModes.Contains (mode.Value))
162
175
                                                continue;
163
 
                                        if (!mode.Value.Validate (style.Value)) {
164
 
                                                System.Console.WriteLine(mode.Key + " failed to validate against:" + style.Key);
 
176
                                        if (!mode.Value.Create (doc).Validate (style.Value)) {
 
177
                                                System.Console.WriteLine (mode.Key + " failed to validate against:" + style.Key);
165
178
                                                result = false;
166
179
                                        }
167
180
                                        checkedModes.Add (mode.Value);
170
183
                        return result;
171
184
                }
172
185
                
173
 
                public static void Remove (ColorSheme style)
 
186
                public static void Remove (ColorScheme style)
174
187
                {
175
188
                        if (styles.ContainsKey (style.Name))
176
189
                                styles.Remove (style.Name);
188
201
                        }
189
202
                }
190
203
                
191
 
                public static void ScanSpans (Document doc, SyntaxMode mode, Rule rule, CloneableStack<Span> spanStack, int start, int end)
 
204
                public static void ScanSpans (TextDocument doc, SyntaxMode mode, Rule rule, CloneableStack<Span> spanStack, int start, int end)
192
205
                {
193
 
                        SyntaxMode.SpanParser parser = mode.CreateSpanParser (doc, mode, null, spanStack);
 
206
                        SyntaxMode.SpanParser parser = mode.CreateSpanParser (null, spanStack);
194
207
                        parser.ParseSpans (start, end - start);
195
208
                }
196
209
                
198
211
                
199
212
                class UpdateWorker
200
213
                {
201
 
                        Document doc;
 
214
                        TextDocument doc;
202
215
                        SyntaxMode mode;
203
216
                        int startOffset;
204
217
                        int endOffset;
208
221
                                private set;
209
222
                        }
210
223
                        
211
 
                        public Document Doc {
 
224
                        public TextDocument Doc {
212
225
                                get { return this.doc; }
213
226
                        }
214
227
                        
216
229
                                get;
217
230
                                set;
218
231
                        }
219
 
                        public UpdateWorker (Document doc, SyntaxMode mode, int startOffset, int endOffset)
 
232
                        public UpdateWorker (TextDocument doc, SyntaxMode mode, int startOffset, int endOffset)
220
233
                        {
221
234
                                this.doc = doc;
222
235
                                this.mode = mode;
227
240
                        }
228
241
                        
229
242
                        
230
 
                        bool EndsWithContinuation (Span span, LineSegment line)
 
243
                        bool EndsWithContinuation (Span span, DocumentLine line)
231
244
                        {
232
245
                                return !span.StopAtEol || span.StopAtEol && !string.IsNullOrEmpty (span.Continuation) &&
233
246
                                        line != null && doc.GetTextAt (line).Trim ().EndsWith (span.Continuation);
247
260
                                        if (span == null)
248
261
                                                return;
249
262
                                        var spanStack = span.Clone ();
250
 
                                        SyntaxMode.SpanParser parser = mode.CreateSpanParser(doc, mode, null, spanStack);
 
263
                                        SyntaxMode.SpanParser parser = mode.CreateSpanParser(null, spanStack);
251
264
                                        foreach (var line in doc.GetLinesStartingAt (startLine)) {
252
265
                                                if (line == null)
253
266
                                                        return;
261
274
                                                                break;
262
275
                                                }
263
276
                                                line.StartSpan = spanStack.Clone();
264
 
                                                parser.ParseSpans(line.Offset, line.Length);
 
277
                                                parser.ParseSpans(line.Offset, line.LengthIncludingDelimiter);
265
278
                                                while (spanStack.Count > 0 && !EndsWithContinuation(spanStack.Peek(), line))
266
279
                                                        parser.PopSpan();
267
280
                                        }
311
324
                        }
312
325
                }
313
326
                
314
 
                public static void StartUpdate (Document doc, SyntaxMode mode, int startOffset, int endOffset)
 
327
                public static void StartUpdate (TextDocument doc, SyntaxMode mode, int startOffset, int endOffset)
315
328
                {
316
329
                        lock (updateQueue) {
317
330
                                updateQueue.Enqueue (new UpdateWorker (doc, mode, startOffset, endOffset));
319
332
                        queueSignal.Set ();
320
333
                }
321
334
                
322
 
                public static void WaitUpdate (Document doc)
 
335
                public static void WaitUpdate (TextDocument doc)
323
336
                {
324
337
                        UpdateWorker[] arr;
325
338
                        lock (updateQueue) {
403
416
                                        }
404
417
                                } else if (file.EndsWith ("Style.xml")) {
405
418
                                        using (XmlTextReader reader =  new XmlTextReader (file)) {
406
 
                                                string styleName = Scan (reader, ColorSheme.NameAttribute);
 
419
                                                string styleName = Scan (reader, ColorScheme.NameAttribute);
407
420
                                                styleLookup [styleName] = new UrlXmlProvider (file);
408
421
                                                isLoadedFromFile [styleName] = file;
409
422
                                        }
427
440
                                } else if (resource.EndsWith ("Style.xml")) {
428
441
                                        using (Stream stream = assembly.GetManifestResourceStream (resource)) 
429
442
                                        using (XmlTextReader reader = new XmlTextReader (stream)) {
430
 
                                                string styleName = Scan (reader, ColorSheme.NameAttribute);
 
443
                                                string styleName = Scan (reader, ColorScheme.NameAttribute);
431
444
                                                styleLookup [styleName] = new ResourceXmlProvider (assembly, resource);
432
445
                                        }
433
446
                                }
454
467
                        }
455
468
                }
456
469
                
457
 
                public static void AddStyle (string fileName, ColorSheme style)
 
470
                public static void AddStyle (string fileName, ColorScheme style)
458
471
                {
459
472
                        isLoadedFromFile [style.Name] = fileName;
460
473
                        styles [style.Name] = style;
462
475
                public static void AddStyle (IXmlProvider provider)
463
476
                {
464
477
                        using (XmlReader reader = provider.Open ()) {
465
 
                                string styleName = Scan (reader, ColorSheme.NameAttribute);
 
478
                                string styleName = Scan (reader, ColorScheme.NameAttribute);
466
479
                                styleLookup [styleName] = provider;
467
480
                        }
468
481
                }
469
482
                public static void RemoveStyle (IXmlProvider provider)
470
483
                {
471
484
                        using (XmlReader reader = provider.Open ()) {
472
 
                                string styleName = Scan (reader, ColorSheme.NameAttribute);
 
485
                                string styleName = Scan (reader, ColorScheme.NameAttribute);
473
486
                                styleLookup.Remove (styleName);
474
487
                        }
475
488
                }
476
489
                
 
490
                static List<Tuple<string, string, SemanticRule>> semanticRules = new List<Tuple<string, string, SemanticRule>> ();
 
491
                
 
492
                public static void AddSemanticRule (string mime, string ruleName, SemanticRule rule)
 
493
                {
 
494
                        semanticRules.Add (Tuple.Create (mime, ruleName, rule));
 
495
                }
 
496
                
477
497
                static SyntaxModeService ()
478
498
                {
479
499
                        StartUpdateThread ();
480
 
                        LoadStylesAndModes (typeof (SyntaxModeService).Assembly);
481
 
                        SyntaxModeService.GetSyntaxMode ("text/x-csharp").AddSemanticRule ("Comment", new HighlightUrlSemanticRule ("comment"));
482
 
                        SyntaxModeService.GetSyntaxMode ("text/x-csharp").AddSemanticRule ("XmlDocumentation", new HighlightUrlSemanticRule ("comment"));
483
 
                        SyntaxModeService.GetSyntaxMode ("text/x-csharp").AddSemanticRule ("String", new HighlightUrlSemanticRule ("string"));
 
500
                        LoadStylesAndModes (typeof(SyntaxModeService).Assembly);
 
501
                        SyntaxModeService.AddSemanticRule ("text/x-csharp", "Comment", new HighlightUrlSemanticRule ("comment"));
 
502
                        SyntaxModeService.AddSemanticRule ("text/x-csharp", "XmlDocumentation", new HighlightUrlSemanticRule ("comment"));
 
503
                        SyntaxModeService.AddSemanticRule ("text/x-csharp", "String", new HighlightUrlSemanticRule ("string"));
484
504
                        
485
 
                        InstallSyntaxMode ("text/x-jay", new JaySyntaxMode ());
 
505
                        InstallSyntaxMode ("text/x-jay", new SyntaxModeProvider (doc => new JaySyntaxMode (doc)));
486
506
                }
487
507
        }
488
508
}