CodeWriter.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace etoy
  5. {
  6. /// <summary>
  7. /// Static and instance helpers for code generation
  8. /// </summary>
  9. class CodeWriter : IDisposable
  10. {
  11. #region Settings
  12. public static string DefaultIndentPrefix = " ";
  13. public static string DefaultNewLine = "\r\n";
  14. public string IndentPrefix;
  15. public string NewLine;
  16. #endregion
  17. #region Constructors
  18. readonly TextWriter w;
  19. MemoryStream ms = new MemoryStream();
  20. /// <summary>
  21. /// Writes to memory, get the code using the "Code" property
  22. /// </summary>
  23. public CodeWriter()
  24. {
  25. this.IndentPrefix = DefaultIndentPrefix;
  26. this.NewLine = DefaultNewLine;
  27. ms = new MemoryStream();
  28. w = new StreamWriter(ms, Encoding.UTF8);
  29. //w.NewLine = NewLine; // does not appear to work
  30. }
  31. /// <summary>
  32. /// Writes code directly to file
  33. /// </summary>
  34. public CodeWriter(string csPath)
  35. {
  36. this.IndentPrefix = DefaultIndentPrefix;
  37. this.NewLine = DefaultNewLine;
  38. w = new StreamWriter(csPath, false, Encoding.UTF8);
  39. }
  40. /// <summary>
  41. /// Return the generated code as a string.
  42. /// </summary>
  43. public string Code
  44. {
  45. get
  46. {
  47. w.Flush();
  48. return Encoding.UTF8.GetString(ms.ToArray());
  49. }
  50. }
  51. public virtual void Flush()
  52. {
  53. w.Flush();
  54. }
  55. public virtual void Dispose()
  56. {
  57. //Console.WriteLine("CodeWriter Dispose.");
  58. w.Close();
  59. }
  60. #endregion
  61. #region Indentation
  62. /// <summary>
  63. /// Level of indentation
  64. /// </summary>
  65. public int IndentLevel { get; private set; }
  66. /// <summary>
  67. /// Accumulated prefixes over indentations
  68. /// </summary>
  69. string prefix = "";
  70. ///<summary> 增加缩进 </summary>
  71. public void Indent()
  72. {
  73. IndentLevel += 1;
  74. prefix += IndentPrefix;
  75. }
  76. ///<summary> 减少缩进 </summary>
  77. public void Dedent()
  78. {
  79. IndentLevel -= 1;
  80. if (IndentLevel < 0)
  81. throw new InvalidOperationException("Indent error");
  82. prefix = prefix.Substring(0, prefix.Length - IndentPrefix.Length);
  83. }
  84. #endregion
  85. public void Attribute(string attributeConstructor)
  86. {
  87. WriteLine("[" + attributeConstructor + "]");
  88. }
  89. /// <summary>
  90. /// Write leading bracket and indent
  91. /// </summary>
  92. /// <param name="str">String.</param>
  93. public void Bracket()
  94. {
  95. WriteLine("{");
  96. Indent();
  97. }
  98. /// <summary>
  99. /// Write leading bracket and indent
  100. /// </summary>
  101. /// <param name="str">Line before bracket</param>
  102. public void Bracket(string str)
  103. {
  104. WriteLine(str);
  105. WriteLine("{");
  106. Indent();
  107. }
  108. public void Using(string str)
  109. {
  110. WriteLine("using (" + str + ")");
  111. WriteLine("{");
  112. Indent();
  113. }
  114. public void IfBracket(string str)
  115. {
  116. WriteLine("if (" + str + ")");
  117. WriteLine("{");
  118. Indent();
  119. }
  120. /// <summary>
  121. /// Close a previous Bracket and start an "else if"
  122. /// </summary>
  123. public void ElseIfBracket(string str)
  124. {
  125. WriteLine("}");
  126. Dedent();
  127. WriteLine("else if (" + str + ")");
  128. WriteLine("{");
  129. Indent();
  130. }
  131. /// <summary>
  132. /// Close a previous IfBracket and start an else
  133. /// </summary>
  134. public void ElseBracket()
  135. {
  136. Dedent();
  137. WriteLine("}");
  138. WriteLine("else");
  139. WriteLine("{");
  140. Indent();
  141. }
  142. public void WhileBracket(string str)
  143. {
  144. WriteLine("while (" + str + ")");
  145. WriteLine("{");
  146. Indent();
  147. }
  148. public void Switch(string str)
  149. {
  150. Bracket("switch (" + str + ")");
  151. Indent();
  152. }
  153. public void SwitchEnd()
  154. {
  155. Dedent();
  156. EndBracket();
  157. }
  158. public void Case(string str)
  159. {
  160. Dedent();
  161. WriteLine("case " + str + ":");
  162. Indent();
  163. }
  164. public void Case(int id)
  165. {
  166. Dedent();
  167. WriteLine("case " + id + ":");
  168. Indent();
  169. }
  170. public void CaseDefault()
  171. {
  172. Dedent();
  173. WriteLine("default:");
  174. Indent();
  175. }
  176. public void ForeachBracket(string str)
  177. {
  178. WriteLine("foreach (" + str + ")");
  179. WriteLine("{");
  180. Indent();
  181. }
  182. public void EndBracket()
  183. {
  184. Dedent();
  185. WriteLine("}");
  186. }
  187. public void EndBracketSpace()
  188. {
  189. Dedent();
  190. WriteLine("}");
  191. WriteLine();
  192. }
  193. /// <summary>
  194. /// Writes a singe line indented.
  195. /// </summary>
  196. public void WriteIndent(string str)
  197. {
  198. WriteLine(IndentPrefix + str);
  199. }
  200. public void Write(string line, bool needPrefix = false)
  201. {
  202. if (needPrefix)
  203. w.Write(prefix);
  204. w.Write(line);
  205. }
  206. public void OnlyWriteLine(string line, bool needPrefix = false)
  207. {
  208. if (needPrefix)
  209. w.Write(prefix);
  210. w.WriteLine(line);
  211. }
  212. public void WriteLine(string line)
  213. {
  214. foreach (string l in SplitTrimEnd(line))
  215. {
  216. string pl = (prefix + l).TrimEnd(' ', '\t');
  217. w.Write(pl + NewLine);
  218. }
  219. }
  220. public void WritePragma(string line)
  221. {
  222. w.Write("#pragma " + line + NewLine);
  223. }
  224. public void WriteLine()
  225. {
  226. WriteLine("");
  227. }
  228. #region Region
  229. public void BeginRegion(string regionName)
  230. {
  231. WriteLine($"#region {regionName}");
  232. }
  233. public void EndRegion()
  234. {
  235. WriteLine("#endregion");
  236. }
  237. #endregion
  238. #region Comments
  239. public void Comment(string code)
  240. {
  241. if (code == null)
  242. return;
  243. const string commentPrefix = "// ";
  244. prefix += commentPrefix;
  245. foreach (string line in SplitTrimEnd(code))
  246. WriteLine(line);
  247. prefix = prefix.Substring(0, prefix.Length - commentPrefix.Length);
  248. }
  249. /// <summary>
  250. /// 增加注释
  251. /// </summary>
  252. /// <param name="summary"></param>
  253. public void Summary(string summary)
  254. {
  255. if (summary == null || summary.Trim() == "")
  256. return;
  257. string[] lines = SplitTrimEnd(summary);
  258. if (lines.Length == 1)
  259. {
  260. WriteLine("/// <summary> " + lines[0] + " </summary>");
  261. return;
  262. }
  263. prefix += "/// ";
  264. WriteLine("<summary>");
  265. foreach (string line in lines)
  266. WriteLine("<para>" + line + "</para>");
  267. WriteLine("</summary>");
  268. prefix = prefix.Substring(0, prefix.Length - 4);
  269. }
  270. /// <summary>
  271. ///
  272. /// </summary>
  273. /// <param name="name"></param>
  274. /// <param name="description"></param>
  275. public void SummaryParam(string name, string description)
  276. {
  277. if (name == null || description == null)
  278. return;
  279. if (string.IsNullOrEmpty(name) || IsNullOrWhiteSpace(description))
  280. return;
  281. string[] lines = SplitTrimEnd(description);
  282. if (lines.Length == 1)
  283. {
  284. WriteLine("/// <param name=\"" + name + "\">" + lines[0] + "</summary>");
  285. return;
  286. }
  287. prefix += "/// ";
  288. WriteLine("<param name=\"" + name + "\">");
  289. foreach (string line in lines)
  290. WriteLine("<para>" + line + "</para>");
  291. WriteLine("</param>");
  292. prefix = prefix.Substring(0, prefix.Length - 4);
  293. }
  294. #endregion
  295. /// <summary>
  296. /// Split string into an array of lines and trim whitespace at the end
  297. /// </summary>
  298. static string[] SplitTrimEnd(string text)
  299. {
  300. var lines = text.Replace("\r\n", "\n").Replace('\r', '\n').Split('\n');
  301. for (int n = 0; n < lines.Length; n++)
  302. lines[n] = lines[n].TrimEnd(' ', '\t');
  303. return lines;
  304. }
  305. private bool IsNullOrWhiteSpace(string value)
  306. {
  307. if (value != null)
  308. {
  309. for (int i = 0; i < value.Length; i++)
  310. {
  311. if (!char.IsWhiteSpace(value[i]))
  312. {
  313. return false;
  314. }
  315. }
  316. }
  317. return true;
  318. }
  319. public virtual void Clear()
  320. {
  321. ms.SetLength(0);
  322. }
  323. }
  324. }