Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RazorSyntaxMode.cs « Razor « AspNet « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1a07191a9e085c354749209462810b6832532826 (plain)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//
// RazorSyntaxMode.cs
//
// Author:
//		Piotr Dowgiallo <sparekd@gmail.com>
//
// Copyright (c) 2012 Piotr Dowgiallo
//
// 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.Linq;
using Mono.TextEditor.Highlighting;
using System.Xml;
using Mono.TextEditor;
using System.IO;
using System.Web.Razor.Text;
using System.Web.Razor.Tokenizer;
using System.Web.Razor.Tokenizer.Symbols;
using System.Web.Razor.Parser;
using System.Web.Mvc.Razor;
using MonoDevelop.AspNet.Razor.Parser;
using MonoDevelop.Ide;
using RazorSpan = System.Web.Razor.Parser.SyntaxTree.Span;
using MonoDevelop.Ide.Gui;

namespace MonoDevelop.AspNet.Razor
{
	public class RazorSyntaxMode : SyntaxMode, IDisposable
	{
		public RazorSyntaxMode (Document doc)
		{
			this.guiDocument = doc;
			guiDocument.DocumentParsed += HandleDocumentParsed; 
			ResourceStreamProvider provider = new ResourceStreamProvider (typeof (ResourceStreamProvider).Assembly, "RazorSyntaxMode.xml");
			using (var reader = provider.Open ()) {
				SyntaxMode baseMode = SyntaxMode.Read (reader);
				this.rules = new List<Rule> (baseMode.Rules);
				this.keywords = new List<Keywords> (baseMode.Keywords);
				this.spans = baseMode.Spans;
				this.matches = baseMode.Matches;
				this.prevMarker = baseMode.PrevMarker;
				this.SemanticRules = new List<SemanticRule> (baseMode.SemanticRules);
				this.keywordTable = baseMode.keywordTable;
				this.keywordTableIgnoreCase = baseMode.keywordTableIgnoreCase;
			}
		}

		#region IDisposable implementation

		public void Dispose ()
		{
			guiDocument.DocumentParsed -= HandleDocumentParsed;
		}

		#endregion
		enum State
		{
			None,
			InTag
		}

		IList<RazorSpan> currentSpans;
		State currentState;
		IList<Chunk> chunks;
		Document guiDocument;

		public override IEnumerable<Chunk> GetChunks (ColorScheme style, DocumentLine line, int offset, int length)
		{
			// Multiline comment
			if (line.StartSpan.Count != 0 && line.StartSpan.Peek ().Begin.Pattern == "@*")
				return base.GetChunks (style, line, offset, length);

			var tokenizer = new CSharpTokenizer (new SeekableTextReader (doc.GetTextAt (offset, length)));
			chunks = new List<Chunk> ();
			CSharpSymbol symbol;
			CSharpSymbol prevSymbol = null;
			int off = offset;
			currentState = State.None;

			while ((symbol = tokenizer.NextSymbol ()) != null) {
				// Apostrophes in text
				bool inApostrophes = false;
				if (symbol.Type == CSharpSymbolType.CharacterLiteral && prevSymbol != null && Char.IsLetterOrDigit (prevSymbol.Content.Last ())) {
					if (symbol.Content.Last () == '\'')
						inApostrophes = true;
					else {
						chunks.Add (new Chunk (off, 1, "Plain Text"));
						off++;
						tokenizer = new CSharpTokenizer (new SeekableTextReader (symbol.Content.Substring (1)));
						symbol = tokenizer.NextSymbol ();
						prevSymbol = null;
					}
				}

				string chunkStyle = inApostrophes ? "Plain Text" : GetStyleForChunk (symbol, prevSymbol, off);
				chunks.Add (new Chunk (off, symbol.Content.Length, chunkStyle));
				prevSymbol = symbol;
				off += symbol.Content.Length;
			}

			return chunks;
		}

		string GetStyleForChunk (CSharpSymbol symbol, CSharpSymbol prevSymbol, int off)
		{
			if (prevSymbol != null) {
				// Razor directives, statements and expressions
				if (prevSymbol.Type == CSharpSymbolType.Transition)
					return GetStyleForRazorFragment (symbol);
				// End of Razor comments
				if (prevSymbol.Type == CSharpSymbolType.Star && symbol.Type == CSharpSymbolType.Transition) {
					chunks.Last ().Style = "Xml Comment";
					return "Xml Comment";
				}
				// Email addresses
				if (symbol.Type == CSharpSymbolType.Transition && Char.IsLetterOrDigit (prevSymbol.Content.Last ()))
					return "Plain Text";
				// Html tags
				char c = symbol.Content.First ();
				if ((!symbol.Keyword.HasValue && prevSymbol.Type == CSharpSymbolType.LessThan && (Char.IsLetterOrDigit (c) || c == '/'))
					|| (prevSymbol.Type == CSharpSymbolType.Slash && currentState == State.InTag)) {
					currentState = State.InTag;
					chunks.Last ().Style = "Xml Name";
					return "Xml Name";
				}
				if (symbol.Type == CSharpSymbolType.GreaterThan && currentState == State.InTag) {
					currentState = State.None;
					if (prevSymbol.Type == CSharpSymbolType.Slash)
						chunks.Last ().Style = "Xml Name";
					return "Xml Name";
				}
			}
			if (symbol.Type == CSharpSymbolType.RightBrace || symbol.Type == CSharpSymbolType.RightParenthesis)
				return GetStyleForEndBracket (symbol, off);
			// Text in html tags
			if ((symbol.Keyword.HasValue || symbol.Type == CSharpSymbolType.IntegerLiteral || symbol.Type == CSharpSymbolType.RealLiteral)
				&& IsInHtmlContext (symbol, off))
				return "Plain Text";

			return GetStyleForCSharpSymbol (symbol);
		}

		string GetStyleForEndBracket (CSharpSymbol symbol, int off)
		{
			int matchingOff = doc.GetMatchingBracketOffset (off);
			if (matchingOff == -1 || doc.GetCharAt (matchingOff - 1) != '@')
				return "Plain Text";
			else
				return "Html Server-Side Script";
		}

		string GetStyleForRazorFragment (CSharpSymbol symbol)
		{
			if (symbol.Type == CSharpSymbolType.LeftParenthesis || symbol.Type == CSharpSymbolType.LeftBrace
				|| RazorSymbols.IsDirective (symbol.Content))
				return "Html Server-Side Script";
			if (symbol.Type == CSharpSymbolType.Star)
				return "Xml Comment";
			return GetStyleForCSharpSymbol (symbol);
		}

		void HandleDocumentParsed (object sender, EventArgs e)
		{
			var parsedDoc = (sender as Document).ParsedDocument as RazorCSharpParsedDocument;
			if (parsedDoc != null)
				currentSpans = parsedDoc.PageInfo.Spans.ToList ();
		}

		bool IsInHtmlContext (CSharpSymbol symbol, int off)
		{
			if (currentSpans == null)
				CreateSpans ();
			var owner = currentSpans.LastOrDefault (s => s.Start.AbsoluteIndex <= off);
			if (owner != null && owner.Kind == System.Web.Razor.Parser.SyntaxTree.SpanKind.Markup)
				return true;
			return false;
		}

		// Creates spans when the parsed document isn't available yet.
		void CreateSpans ()
		{
			using (SeekableTextReader source = new SeekableTextReader (doc.Text)) {
				var markupParser = new HtmlMarkupParser ();
				var codeParser = new MvcCSharpRazorCodeParser ();
				var context = new ParserContext (source, codeParser, markupParser, markupParser) { DesignTimeMode = true };
				codeParser.Context = context;
				markupParser.Context = context;

				context.ActiveParser.ParseDocument ();
				var results = context.CompleteParse ();
				currentSpans = results.Document.Flatten ().ToList ();
			}
		}

		string GetStyleForCSharpSymbol (CSharpSymbol symbol)
		{
			if (symbol.Content == "var" || symbol.Content == "dynamic")
				return "Keyword(Type)";

			string style = "Plain Text";
			switch (symbol.Type) {
				case CSharpSymbolType.CharacterLiteral:
				case CSharpSymbolType.StringLiteral:
					style = "String";
					break;
				case CSharpSymbolType.Comment:
					style = "Xml Comment";
					break;
				case CSharpSymbolType.IntegerLiteral:
				case CSharpSymbolType.RealLiteral:
					style = "Number";
					break;
				case CSharpSymbolType.Keyword:
					style = GetStyleForKeyword (symbol.Keyword);
					break;
				case CSharpSymbolType.RazorComment:
				case CSharpSymbolType.RazorCommentStar:
				case CSharpSymbolType.RazorCommentTransition:
					style = "Xml Comment";
					break;
				case CSharpSymbolType.Transition:
					style = "Html Server-Side Script";
					break;
				default:
					style = "Plain Text";
					break;
			}

			return style;
		}

		string GetStyleForKeyword (CSharpKeyword? keyword)
		{
			switch (keyword.Value) {
				case CSharpKeyword.Abstract:
				case CSharpKeyword.Const:
				case CSharpKeyword.Event:
				case CSharpKeyword.Extern:
				case CSharpKeyword.Internal:
				case CSharpKeyword.Override:
				case CSharpKeyword.Private:
				case CSharpKeyword.Protected:
				case CSharpKeyword.Public:
				case CSharpKeyword.Readonly:
				case CSharpKeyword.Sealed:
				case CSharpKeyword.Static:
				case CSharpKeyword.Virtual:
				case CSharpKeyword.Volatile:
					return "Keyword(Modifiers)";
				case CSharpKeyword.As:
				case CSharpKeyword.Is:
				case CSharpKeyword.New:
				case CSharpKeyword.Sizeof:
				case CSharpKeyword.Stackalloc:
				case CSharpKeyword.Typeof:
					return "Keyword(Operator)";
				case CSharpKeyword.Base:
				case CSharpKeyword.This:
					return "Keyword(Access)";
				case CSharpKeyword.Bool:
				case CSharpKeyword.Byte:
				case CSharpKeyword.Char:
				case CSharpKeyword.Decimal:
				case CSharpKeyword.Double:
				case CSharpKeyword.Enum:
				case CSharpKeyword.Float:
				case CSharpKeyword.Int:
				case CSharpKeyword.Long:
				case CSharpKeyword.Object:
				case CSharpKeyword.Sbyte:
				case CSharpKeyword.Short:
				case CSharpKeyword.String:
				case CSharpKeyword.Struct:
				case CSharpKeyword.Uint:
				case CSharpKeyword.Ulong:
				case CSharpKeyword.Ushort:
					return "Keyword(Type)";
				case CSharpKeyword.Break:
				case CSharpKeyword.Continue:
				case CSharpKeyword.Goto:
				case CSharpKeyword.Return:
					return "Keyword(Jump)";
				case CSharpKeyword.Case:
				case CSharpKeyword.Else:
				case CSharpKeyword.Default:
				case CSharpKeyword.If:
				case CSharpKeyword.Switch:
					return "Keyword(Selection)";
				case CSharpKeyword.Catch:
				case CSharpKeyword.Finally:
				case CSharpKeyword.Throw:
				case CSharpKeyword.Try:
					return "Keyword(Exception)";
				case CSharpKeyword.Checked:
				case CSharpKeyword.Fixed:
				case CSharpKeyword.Lock:
				case CSharpKeyword.Unchecked:
				case CSharpKeyword.Unsafe:
					return "Keyword(Other)";
				case CSharpKeyword.Class:
				case CSharpKeyword.Delegate:
				case CSharpKeyword.Interface:
					return "Keyword(Declaration)";
				case CSharpKeyword.Do:
				case CSharpKeyword.For:
				case CSharpKeyword.Foreach:
				case CSharpKeyword.In:
				case CSharpKeyword.While:
					return "Keyword(Iteration)";
				case CSharpKeyword.Explicit:
				case CSharpKeyword.Implicit:
				case CSharpKeyword.Operator:
					return "Keyword(Operator Declaration)";
				case CSharpKeyword.False:
				case CSharpKeyword.Null:
				case CSharpKeyword.True:
					return "Keyword(Constants)";
				case CSharpKeyword.Namespace:
				case CSharpKeyword.Using:
					return "Keyword(Namespace)";
				case CSharpKeyword.Out:
				case CSharpKeyword.Params:
				case CSharpKeyword.Ref:
					return "Keyword(Parameter)";
				case CSharpKeyword.Void:
					return "Keyword(Void)";
				default:
					return "Keyword(Other)";
			}
		}
	}
}