PageRenderTime 99ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/UiException/CodeFormatters/GeneralCodeFormatter.cs

#
C# | 168 lines | 71 code | 23 blank | 74 comment | 5 complexity | 20291a975c81a079956bb6069b4130ec MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // This is free software licensed under the NUnit license. You may
  3. // obtain a copy of the license at http://nunit.org
  4. // ****************************************************************
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. using NUnit.UiException.CodeFormatters;
  9. namespace NUnit.UiException.CodeFormatters
  10. {
  11. /// <summary>
  12. /// GeneralCodeFormatter is the front class responsible for making a basic
  13. /// syntax coloring of a string of text for a set of specific languages.
  14. /// The class encapsulates a set of algorithms where each of them addresses
  15. /// a specific language formatting. The use of one or another algorithm at
  16. /// analysing time is made through an language registering mechanism.
  17. /// For instance C# files are covered by CSharpCodeFormatter which has
  18. /// been assigned to "cs" language.
  19. /// If a query is made to GeneralCodeFormatter while there is no formatter
  20. /// that fit the given file language a default formatting is applyied
  21. /// through the use of the formatter registered into the property
  22. /// DefaultFormatter.
  23. /// </summary>
  24. public class GeneralCodeFormatter :
  25. IFormatterCatalog
  26. {
  27. /// <summary>
  28. /// The set of formatting algorithms.
  29. /// </summary>
  30. private CodeFormatterCollection _formatters;
  31. /// <summary>
  32. /// The default formatter to be used as last resort.
  33. /// </summary>
  34. private ICodeFormatter _default;
  35. /// <summary>
  36. /// Build and initialises GeneralCodeFormatter.
  37. /// </summary>
  38. public GeneralCodeFormatter()
  39. {
  40. _formatters = new CodeFormatterCollection();
  41. _formatters.Register(new CSharpCodeFormatter(), "cs");
  42. _default = new PlainTextCodeFormatter();
  43. return;
  44. }
  45. /// <summary>
  46. /// Gets or sets the formatter to be used as last resort when
  47. /// no formatter fit the given source language.
  48. /// The value cannot be null.
  49. /// </summary>
  50. public ICodeFormatter DefaultFormatter
  51. {
  52. get { return (_default); }
  53. set
  54. {
  55. UiExceptionHelper.CheckNotNull(value, "value");
  56. _default = value;
  57. }
  58. }
  59. /// <summary>
  60. /// Gives access to the underlying formatter collection.
  61. /// </summary>
  62. public CodeFormatterCollection Formatters
  63. {
  64. get { return (_formatters); }
  65. }
  66. /// <summary>
  67. /// Gets the best formatter that fits the given language. If there
  68. /// is no such formatter, a default one is returned.
  69. /// </summary>
  70. /// <param name="language">
  71. /// The language name. Ex: "C#", "Java. This parameter cannot be null.
  72. /// </param>
  73. /// <returns>
  74. /// A non-null ICodeFormatter that best fits the request.
  75. /// </returns>
  76. public ICodeFormatter GetFormatterFromLanguage(string languageName)
  77. {
  78. UiExceptionHelper.CheckNotNull(languageName, "language");
  79. if (_formatters.HasLanguage(languageName))
  80. return (_formatters[languageName]);
  81. return (DefaultFormatter);
  82. }
  83. /// <summary>
  84. /// Gets the formatter assigned to the given extension. If there
  85. /// is no such assignment, the default formatter is returned.
  86. /// </summary>
  87. /// <param name="extension">
  88. /// A file extension. Ex: "cs", "txt". This parameter cannot be null.
  89. /// </param>
  90. /// <returns>A non-null ICodeFormatter.</returns>
  91. public ICodeFormatter GetFormatterFromExtension(string extension)
  92. {
  93. UiExceptionHelper.CheckNotNull(extension, "extension");
  94. if (_formatters.HasExtension(extension))
  95. return (_formatters.GetFromExtension(extension));
  96. return (DefaultFormatter);
  97. }
  98. /// <summary>
  99. /// A convenient method to make the formatting of a piece of code when
  100. /// only the file extension is known.
  101. /// </summary>
  102. /// <param name="code">The piece of code to be formatted. This parameter
  103. /// cannot be null.</param>
  104. /// <param name="extension">The file extension associated to this piece of code.
  105. /// Ex: "cs", "cpp". This is used to pick the formatter assigned to. If no such
  106. /// formatter exists, the default one is picked up.</param>
  107. /// <returns>The FormattedCode for this piece of code.</returns>
  108. public FormattedCode FormatFromExtension(string code, string extension)
  109. {
  110. UiExceptionHelper.CheckNotNull(code, "code");
  111. UiExceptionHelper.CheckNotNull(extension, "extension");
  112. if (_formatters.HasExtension(extension))
  113. return (_formatters.GetFromExtension(extension).Format(code));
  114. return (DefaultFormatter.Format(code));
  115. }
  116. #region IFormatterCatalog Membres
  117. /// <summary>
  118. /// Pick the best available formatter to format the given piece of code.
  119. /// </summary>
  120. /// <param name="code">The code to be formatted. This parameter cannot be null.</param>
  121. /// <param name="language">
  122. /// The language into which code has been written. Ex: "C#", "Java".
  123. /// If no such formatter is available, a default formatting is applied.
  124. /// This parameter cannot be null.
  125. /// </param>
  126. /// <returns>
  127. /// The formatting for this piece of code.
  128. /// </returns>
  129. public FormattedCode Format(string code, string language)
  130. {
  131. UiExceptionHelper.CheckNotNull(code, "code");
  132. UiExceptionHelper.CheckNotNull(language, "language");
  133. if (_formatters.HasLanguage(language))
  134. return (_formatters[language].Format(code));
  135. return (DefaultFormatter.Format(code));
  136. }
  137. public string LanguageFromExtension(string extension)
  138. {
  139. if (_formatters.HasExtension(extension))
  140. return (_formatters.GetFromExtension(extension).Language);
  141. return (_default.Language);
  142. }
  143. #endregion
  144. }
  145. }