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

AmbienceService.cs « MonoDevelop.Ide.TypeSystem « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 794144d93106b14a4f66c23c5ce483413a30c1e7 (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//
// AmbienceService.cs
//
// Author:
//   Mike Krüger <mkrueger@novell.com>
//
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
//
// 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 Mono.Addins;
using System.Xml;
using System.Text;
using ICSharpCode.NRefactory.TypeSystem;
using MonoDevelop.Ide;
using MonoDevelop.Projects;
using MonoDevelop.Core;
using MonoDevelop.Ide.Extensions;
using ICSharpCode.NRefactory.Documentation;

namespace MonoDevelop.Ide.TypeSystem
{
	public static class AmbienceService
	{
		static Dictionary <string, Ambience> ambiences = new Dictionary <string, Ambience> ();
		
		static AmbienceService ()
		{
			// may not have been initialized in testing environment.
			if (AddinManager.IsInitialized) {
				AddinManager.AddExtensionNodeHandler ("/MonoDevelop/TypeSystem/Ambiences", delegate(object sender, ExtensionNodeEventArgs args) {
					var ambience = args.ExtensionNode as MimeTypeExtensionNode;
					switch (args.Change) {
					case ExtensionChange.Add:
						ambiences[ambience.MimeType] = (Ambience) ambience.CreateInstance ();
						break;
					case ExtensionChange.Remove:
						ambiences.Remove (ambience.MimeType);
						break;
					}
				});
			}
		}
		
		public static Ambience GetAmbience (IMember member)
		{
			return GetAmbienceForFile (member.DeclaringTypeDefinition.Region.FileName) ?? new NetAmbience ();
		}
		
		public static Ambience GetAmbienceForFile (string fileName)
		{
			if (string.IsNullOrEmpty (fileName))
				return DefaultAmbience;
			return GetAmbience (DesktopService.GetMimeTypeForUri (fileName));
		}
		
		public static Ambience GetAmbience (string mimeType)
		{
			Ambience result;
			if (!string.IsNullOrEmpty (mimeType) && ambiences.TryGetValue (mimeType, out result))
				return result;
			return defaultAmbience;
		}
		
		static Ambience defaultAmbience = new NetAmbience ();
		
		public static Ambience DefaultAmbience { get { return defaultAmbience; } }
		#region Documentation
		
		public class DocumentationFormatOptions 
		{
			public static readonly DocumentationFormatOptions Empty = new DocumentationFormatOptions ();
			public string HighlightParameter {
				get;
				set;
			}
			
			public int MaxLineLength {
				get;
				set;
			}
			
			public bool BigHeadings {
				get;
				set;
			}
			
			public bool BoldHeadings {
				get;
				set;
			}
			
			public bool SmallText {
				get;
				set;
			}
			
			public Ambience Ambience {
				get;
				set;
			}
			
			
			public DocumentationFormatOptions ()
			{
				BoldHeadings = true;
			}
			
			public string FormatHeading (string heading)
			{
				string result = heading;
				if (BigHeadings)
					result = "<big>" + result + "</big>";
				if (BoldHeadings)
					result = "<b>" + result + "</b>";
				return result;
			}
			
			public string FormatBody (string body)
			{
				var str = body.Trim ();
				if (string.IsNullOrEmpty (str))
					return "";
				return SmallText ? "<small>" + str + Environment.NewLine + "</small>" : str + Environment.NewLine;
			}
		}

		public static string GetSummaryMarkup (IEntity member)
		{
			if (member == null || member.Documentation == null)
				return null;
			string documentation = member.Documentation.Xml.Text;
			if (!string.IsNullOrEmpty (documentation)) {
				int idx1 = documentation.IndexOf ("<summary>", StringComparison.Ordinal);
				int idx2 = documentation.LastIndexOf ("</summary>", StringComparison.Ordinal);
				string result;
				if (idx1 >= 0 && idx2 > idx1) {
					try {
						var xmlText = documentation.Substring (idx1, idx2 - idx1 + "</summary>".Length);
						return ParseBody (member,
							new XmlTextReader (xmlText, XmlNodeType.Element, null),
							"summary", 
							DocumentationFormatOptions.Empty
						);
					} catch (Exception e) {
						LoggingService.LogWarning ("Malformed documentation xml detected:" + documentation, e);
						// may happen on malformed xml.
						var len = idx2 - idx1 - "</summary>".Length;
						result = len > 0 ? documentation.Substring (idx1 + "<summary>".Length, len) : documentation;
					}
				} else if (idx1 >= 0) {
					result = documentation.Substring (idx1 + "<summary>".Length);
				} else if (idx2 >= 0) {
					result = documentation.Substring (0, idx2 - 1);
				} else {
					result = documentation;
				}
				return GetDocumentationMarkup (member, CleanEmpty (result));
			}
			
			return GetDocumentationMarkup (member, CleanEmpty (documentation));
		}
		
		static string CleanEmpty (string doc)
		{
			return IsEmptyDocumentation (doc)? null : doc;
		}

		static bool IsEmptyDocumentation (string documentation)
		{
			return string.IsNullOrWhiteSpace (documentation) || documentation.StartsWith ("To be added") || documentation == "we have not entered docs yet";
		}
		
		public static string GetDocumentation (IEntity member)
		{
			if (member == null)
				return null;
			if (member.Documentation != null)
				return CleanEmpty (member.Documentation);
			return null;
		}
		
		static string GetCref (ITypeResolveContext ctx, string cref)
		{
			if (cref == null)
				return "";

			if (cref.Length < 2)
				return cref;
			try {
				var entity = new DocumentationComment ("", ctx).ResolveCref (cref.Replace("<", "{").Replace(">", "}"));
	
				if (entity != null) {
					var ambience = new ICSharpCode.NRefactory.CSharp.CSharpAmbience ();
					ambience.ConversionFlags = ConversionFlags.ShowParameterList | ConversionFlags.ShowParameterNames | ConversionFlags.ShowTypeParameterList;
					return ambience.ConvertSymbol (entity);
				}
			} catch (Exception e) {
				LoggingService.LogWarning ("Invalid cref:" + cref, e);
			}

			if (cref.Substring (1, 1) == ":")
				return cref.Substring (2, cref.Length - 2);
			
			return cref;
		}
		
		static bool IsSpecialChar (int charValue)
		{
			return 
				0x01 <= charValue && charValue <= 0x08 ||
				0x0B <= charValue && charValue <= 0x0C ||
				0x0E <= charValue && charValue <= 0x1F ||
				0x7F <= charValue && charValue <= 0x84 ||
				0x86 <= charValue && charValue <= 0x9F;
		}
		
		public static string BreakLines (string text, int maxLineLength)
		{
			if (maxLineLength <= 0)
				return text;
			StringBuilder result = new StringBuilder ();
			int lineLength = 0;
			bool inTag = false;
			bool inAmp = false;
			foreach (char ch in text) {
				switch (ch) {
				case '<':
					inTag = true;
					break;
				case '>':
					inTag = false;
					break;
				case '&':
					inAmp = true;
					break;
				case ';':
					inAmp = false;
					break;
				case '\n':
					lineLength = 0;
					break;
				case '\r':
					lineLength = 0;
					break;
				}
				
				result.Append (ch);
				if (!inTag && !inAmp)
					lineLength++;
				if (!Char.IsLetterOrDigit (ch) && lineLength > maxLineLength) {
					result.AppendLine ();
					lineLength = 0;
				}
			}
			return result.ToString ();
		}
		
		public static string EscapeText (string text)
		{
			if (text == null)
				return null;
			StringBuilder result = new StringBuilder ();
			foreach (char ch in text) {
				switch (ch) {
				case '<':
					result.Append ("&lt;");
					break;
				case '>':
					result.Append ("&gt;");
					break;
				case '&':
					result.Append ("&amp;");
					break;
				case '\'':
					result.Append ("&apos;");
					break;
				case '"':
					result.Append ("&quot;");
					break;
				default:
					int charValue = (int)ch;
					if (IsSpecialChar (charValue)) {
						result.AppendFormat ("&#x{0:X};", charValue);
					} else {
						result.Append (ch);
					}
					break;
				}
			}
			return result.ToString ();
		}
		
		public static string UnescapeText (string text)
		{
			var sb = new StringBuilder ();
			for (int i = 0; i < text.Length; i++) {
				char ch = text[i];
				if (ch == '&') {
					int end = text.IndexOf (';', i);
					if (end == -1)
						break;
					string entity = text.Substring (i + 1, end - i - 1);
					switch (entity) {
					case "lt":
						sb.Append ('<');
						break;
					case "gt":
						sb.Append ('>');
						break;
					case "amp":
						sb.Append ('&');
						break;
					case "apos":
						sb.Append ('\'');
						break;
					case "quot":
						sb.Append ('"');
						break;
					}
					i = end;
				} else {
					sb.Append (ch);
				}
			}
			return sb.ToString ();	
		}
		
		
		public static string GetDocumentationMarkup (IEntity member, string doc)
		{
			return GetDocumentationMarkup (member, doc, DocumentationFormatOptions.Empty);
		}
		
		static string ParseBody (IEntity member, XmlTextReader xml, string endTagName, DocumentationFormatOptions options)
		{
			StringBuilder result = new StringBuilder (); 
			bool wasWhiteSpace = true;
			bool appendSpace = false;
			ITypeResolveContext ctx = member.Compilation.TypeResolveContext;
			while (xml.Read ()) {
				switch (xml.NodeType) {
				case XmlNodeType.EndElement:
					if (xml.Name == endTagName) 
						goto end;
					break;
				case XmlNodeType.Element:
					switch (xml.Name.ToLower ()) {
					case "para":
						result.AppendLine (ParseBody (member, xml, xml.Name, options));
						wasWhiteSpace = true;
						break;
					case "see":
						if (!wasWhiteSpace) {
							result.Append (' ');
							wasWhiteSpace = true;
						}
						result.Append ("<i>");
						string name = (GetCref (ctx, xml ["cref"]) + xml ["langword"]).Trim ();
						if (options.Ambience != null)
							name = options.Ambience.GetIntrinsicTypeName (name);
						result.Append (EscapeText (name));
						result.Append ("</i>");
						wasWhiteSpace = false;
						appendSpace = true;
						break;
					case "paramref":
						if (!wasWhiteSpace) {
							result.Append (' ');
							wasWhiteSpace = true;
						}
						result.Append ("<i>");
						result.Append (EscapeText (xml ["name"].Trim ()));
						result.Append ("</i>");
						appendSpace = true;
						wasWhiteSpace = false;
						break;
					}
					break;
				case XmlNodeType.Text:
					if (IsEmptyDocumentation (xml.Value))
						break;
					foreach (char ch in xml.Value) {
						if (!Char.IsWhiteSpace (ch) && appendSpace) {
							result.Append (' ');
							appendSpace = false;
						}
						if (Char.IsWhiteSpace (ch) || ch == '\n' || ch == '\r') {
							if (!wasWhiteSpace) {
								result.Append (' ');
								wasWhiteSpace = true;
							}
							continue;
						}
						wasWhiteSpace = false;
						result.Append (EscapeText (ch.ToString ()));
					}
					break;
				}
			}
		end:
			return result.ToString ().Trim ();
		}
		
		public static string GetDocumentationMarkup (IEntity member, string doc, DocumentationFormatOptions options)
		{
			if (string.IsNullOrEmpty (doc))
				return null;
			System.IO.StringReader reader = new System.IO.StringReader ("<docroot>" + doc + "</docroot>");
			XmlTextReader xml = new XmlTextReader (reader);
			StringBuilder ret = new StringBuilder (70);
			StringBuilder parameterBuilder = new StringBuilder ();
			StringBuilder exceptions = new StringBuilder ();
			exceptions.AppendLine (options.FormatHeading (GettextCatalog.GetString ("Exceptions:")));
			//		ret.Append ("<small>");
			int paramCount = 0, exceptionCount = 0, summaryEnd = -1;
			try {
				xml.Read ();
				do {
					if (xml.NodeType == XmlNodeType.Element) {
						switch (xml.Name.ToLower ()) {
						case "para":
							ret.Append (options.FormatBody (ParseBody (member, xml, xml.Name, options)));
							if (summaryEnd < 0)
								summaryEnd = ret.Length;
							break;
						case "summary":
							var summary = options.FormatBody (ParseBody (member, xml, xml.Name, options));
							if (!IsEmptyDocumentation (summary)) {
								//							ret.AppendLine (GetHeading ("Summary:", options));
								ret.Append (summary);
								if (summaryEnd < 0)
									summaryEnd = ret.Length;
							}
							break;
						case "remarks":
							if (string.IsNullOrEmpty (options.HighlightParameter)) {
								ret.AppendLine (options.FormatHeading (GettextCatalog.GetString ("Remarks:")));
								ret.Append (options.FormatBody (ParseBody (member, xml, xml.Name, options)));
								if (summaryEnd < 0)
									summaryEnd = ret.Length;
							} else {
								options.FormatBody (ParseBody (member, xml, xml.Name, options));
							}
							break;
						// skip <example>-nodes
						case "example":
							xml.Skip ();
							xml.Skip ();
							break;
						case "exception":
							exceptionCount++;
							if (options.SmallText)
								exceptions.Append ("<small>");
							exceptions.Append ("<b>");
							exceptions.Append (EscapeText (GetCref (member.Compilation.TypeResolveContext, xml ["cref"])));
							exceptions.Append (": ");
							exceptions.Append ("</b>");
							if (options.SmallText)
								exceptions.Append ("</small>");
							
							exceptions.AppendLine (options.FormatBody (ParseBody (member, xml, xml.Name, options)));
							break;
						case "returns":
							if (string.IsNullOrEmpty (options.HighlightParameter)) {
								ret.AppendLine (options.FormatHeading (GettextCatalog.GetString ("Returns:")));
								ret.Append (options.FormatBody (ParseBody (member, xml, xml.Name, options)));
							} else {
								options.FormatBody (ParseBody (member, xml, xml.Name, options));
							}
							break;
						case "param":
							string paramName = xml.GetAttribute ("name") != null ? xml ["name"].Trim () : "";
								
							var body = options.FormatBody (ParseBody (member, xml, xml.Name, options));
							if (!IsEmptyDocumentation (body)) {
								paramCount++;
								parameterBuilder.Append ("<i>");
								if (options.HighlightParameter == paramName)
									parameterBuilder.Append ("<b>");
								if (options.SmallText)
									parameterBuilder.Append ("<small>");
								parameterBuilder.Append (EscapeText (paramName));
								if (options.SmallText)
									parameterBuilder.Append ("</small>");
								if (options.HighlightParameter == paramName)
									parameterBuilder.Append ("</b>");
								parameterBuilder.Append (":</i> ");
								parameterBuilder.Append (body);
							} else {
								return null;
							}
							break;
						case "value":
							ret.AppendLine (options.FormatHeading (GettextCatalog.GetString ("Value:")));
							ret.AppendLine (options.FormatBody (ParseBody (member, xml, xml.Name, options)));
							break;
						case "seealso":
							if (string.IsNullOrEmpty (options.HighlightParameter)) {
								ret.Append (options.FormatHeading (GettextCatalog.GetString ("See also:")));
								ret.Append (" " + EscapeText (GetCref (member.Compilation.TypeResolveContext, xml ["cref"]) + xml ["langword"]));
							}
							break;
						}
					}
				} while (xml.Read ());
			
			} catch (Exception ex) {
				MonoDevelop.Core.LoggingService.LogError (ex.ToString ());
				return EscapeText (doc);
			}

			if (IsEmptyDocumentation (ret.ToString ()) && IsEmptyDocumentation (parameterBuilder.ToString ()))
				return EscapeText (doc);
			if (string.IsNullOrEmpty (options.HighlightParameter) && exceptionCount > 0)
				ret.Append (exceptions.ToString ());
			
			string result = ret.ToString ();
			if (summaryEnd < 0)
				summaryEnd = result.Length;
			if (paramCount > 0) {
				var paramSb = new StringBuilder ();
				if (result.Length > 0)
					paramSb.AppendLine ();/*
				paramSb.Append ("<small>");
				paramSb.AppendLine (options.FormatHeading (GettextCatalog.GetPluralString ("Parameter:", "Parameters:", paramCount)));
				paramSb.Append ("</small>");*/
				paramSb.Append (parameterBuilder.ToString ());
				result = result.Insert (summaryEnd, paramSb.ToString ());
			}
			result = result.Trim ();
			if (result.EndsWith (Environment.NewLine + "</small>"))
				result = result.Substring (0, result.Length - (Environment.NewLine + "</small>").Length) + "</small>";
			return result;
		}
		#endregion
	}
}