AddXmlDocTransform.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.IO;
  20. using ICSharpCode.NRefactory.CSharp;
  21. using Mono.Cecil;
  22. namespace ICSharpCode.ILSpy.XmlDoc
  23. {
  24. /// <summary>
  25. /// Adds XML documentation for member definitions.
  26. /// </summary>
  27. static class AddXmlDocTransform
  28. {
  29. public static void Run(AstNode node)
  30. {
  31. if (node is EntityDeclaration) {
  32. MemberReference mr = node.Annotation<MemberReference>();
  33. if (mr != null && mr.Module != null) {
  34. var xmldoc = XmlDocLoader.LoadDocumentation(mr.Module);
  35. if (xmldoc != null) {
  36. string doc = xmldoc.GetDocumentation(XmlDocKeyProvider.GetKey(mr));
  37. if (doc != null) {
  38. InsertXmlDocumentation(node, new StringReader(doc));
  39. }
  40. }
  41. }
  42. if (!(node is TypeDeclaration))
  43. return; // don't recurse into attributed nodes, except for type definitions
  44. }
  45. foreach (AstNode child in node.Children)
  46. Run(child);
  47. }
  48. static void InsertXmlDocumentation(AstNode node, StringReader r)
  49. {
  50. // Find the first non-empty line:
  51. string firstLine;
  52. do {
  53. firstLine = r.ReadLine();
  54. if (firstLine == null)
  55. return;
  56. } while (string.IsNullOrWhiteSpace(firstLine));
  57. string indentation = firstLine.Substring(0, firstLine.Length - firstLine.TrimStart().Length);
  58. string line = firstLine;
  59. int skippedWhitespaceLines = 0;
  60. // Copy all lines from input to output, except for empty lines at the end.
  61. while (line != null) {
  62. if (string.IsNullOrWhiteSpace(line)) {
  63. skippedWhitespaceLines++;
  64. } else {
  65. while (skippedWhitespaceLines > 0) {
  66. node.Parent.InsertChildBefore(node, new Comment(string.Empty, CommentType.Documentation), Roles.Comment);
  67. skippedWhitespaceLines--;
  68. }
  69. if (line.StartsWith(indentation, StringComparison.Ordinal))
  70. line = line.Substring(indentation.Length);
  71. node.Parent.InsertChildBefore(node, new Comment(" " + line, CommentType.Documentation), Roles.Comment);
  72. }
  73. line = r.ReadLine();
  74. }
  75. }
  76. }
  77. }