/components/jcf2/Settings/SetReturns.pas

http://github.com/graemeg/lazarus · Pascal · 323 lines · 218 code · 68 blank · 37 comment · 0 complexity · fb91bf066ba6e789124c80244b46bce7 MD5 · raw file

  1. unit SetReturns;
  2. {(*}
  3. (*------------------------------------------------------------------------------
  4. Delphi Code formatter source code
  5. The Original Code is SetReturns.pas, released April 2000.
  6. The Initial Developer of the Original Code is Anthony Steele.
  7. Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
  8. All Rights Reserved.
  9. Contributor(s): Anthony Steele.
  10. The contents of this file are subject to the Mozilla Public License Version 1.1
  11. (the "License"). you may not use this file except in compliance with the License.
  12. You may obtain a copy of the License at http://www.mozilla.org/NPL/
  13. Software distributed under the License is distributed on an "AS IS" basis,
  14. WITHOUT WARRANTY OF ANY KIND, either express or implied.
  15. See the License for the specific language governing rights and limitations
  16. under the License.
  17. Alternatively, the contents of this file may be used under the terms of
  18. the GNU General Public License Version 2 or later (the "GPL")
  19. See http://www.gnu.org/licenses/gpl.html
  20. ------------------------------------------------------------------------------*)
  21. {*)}
  22. { mostly spacing and line breaking +options }
  23. {$I JcfGlobal.inc}
  24. interface
  25. uses JcfSetBase, SettingsTypes, SettingsStream;
  26. type
  27. { rebreak lines has three modes:
  28. - off, don't rebreak lines
  29. - only if you can find a good spot to do it
  30. yes (if there is any spot to do it)
  31. }
  32. TWhenToRebreakLines = (rbOff, rbOnlyIfGood, rbUsually);
  33. TSetReturns = class(TSetBase)
  34. private
  35. { line breaking }
  36. feRebreakLines: TWhenToRebreakLines;
  37. fiMaxLineLength: integer;
  38. { return removal and adding }
  39. fbRemoveBadReturns: boolean;
  40. fbAddGoodReturns: boolean;
  41. fbUsesClauseOnePerLine: boolean;
  42. fbBreakAfterUses: boolean;
  43. fbRemoveExpressionReturns: boolean;
  44. fbRemoveVarReturns: boolean;
  45. fbRemovePropertyReturns: boolean;
  46. fbRemoveProcedureDefReturns: boolean;
  47. fbRemoveBlockBlankLines: boolean;
  48. fbRemoveVarBlankLines: boolean;
  49. fbRemoveProcHeaderBlankLines: boolean;
  50. fiNumReturnsAfterFinalEnd: integer;
  51. { returns on blocks }
  52. feBlockStyle, feBlockBeginStyle: TTriOptionStyle;
  53. feLabelStyle, feLabelBeginStyle: TTriOptionStyle;
  54. feCaseLabelStyle, feCaseBeginStyle: TTriOptionStyle;
  55. feCaseElseStyle, feCaseElseBeginStyle: TTriOptionStyle;
  56. feEndElseStyle: TTriOptionStyle;
  57. feElseIfStyle: TTriOptionStyle;
  58. feElseBeginStyle: TTriOptionStyle;
  59. { returns on compiler directives }
  60. feBeforeCompilerDirectUses: TTriOptionStyle;
  61. feBeforeCompilerDirectStatements: TTriOptionStyle;
  62. feBeforeCompilerDirectGeneral: TTriOptionStyle;
  63. feAfterCompilerDirectUses: TTriOptionStyle;
  64. feAfterCompilerDirectStatements: TTriOptionStyle;
  65. feAfterCompilerDirectGeneral: TTriOptionStyle;
  66. feReturnChars: TReturnChars;
  67. fbRemoveConsecutiveBlankLines: boolean;
  68. fiMaxConsecutiveBlankLines: integer;
  69. fiMaxBlankLinesInSection: integer;
  70. fiLinesBeforeProcedure: integer;
  71. protected
  72. public
  73. constructor Create;
  74. procedure WriteToStream(const pcOut: TSettingsOutput); override;
  75. procedure ReadFromStream(const pcStream: TSettingsInput); override;
  76. property RebreakLines: TWhenToRebreakLines Read feRebreakLines Write feRebreakLines;
  77. property MaxLineLength: integer Read fiMaxLineLength Write fiMaxLineLength;
  78. property NumReturnsAfterFinalEnd: integer
  79. Read fiNumReturnsAfterFinalEnd Write fiNumReturnsAfterFinalEnd;
  80. property RemoveBadReturns: boolean Read fbRemoveBadReturns Write fbRemoveBadReturns;
  81. property AddGoodReturns: boolean Read fbAddGoodReturns Write fbAddGoodReturns;
  82. property UsesClauseOnePerLine: boolean Read fbUsesClauseOnePerLine
  83. Write fbUsesClauseOnePerLine;
  84. property BreakAfterUses: boolean read fbBreakAfterUses write fbBreakAfterUses;
  85. property RemoveExpressionReturns: boolean
  86. Read fbRemoveExpressionReturns Write fbRemoveExpressionReturns;
  87. property RemoveVarReturns: boolean Read fbRemoveVarReturns Write fbRemoveVarReturns;
  88. property RemovePropertyReturns: boolean
  89. Read fbRemovePropertyReturns Write fbRemovePropertyReturns;
  90. property RemoveProcedureDefReturns: boolean
  91. Read fbRemoveProcedureDefReturns Write fbRemoveProcedureDefReturns;
  92. property RemoveBlockBlankLines: boolean
  93. Read fbRemoveBlockBlankLines Write fbRemoveBlockBlankLines;
  94. property RemoveVarBlankLines: boolean Read fbRemoveVarBlankLines
  95. Write fbRemoveVarBlankLines;
  96. property RemoveProcHeaderBlankLines: boolean
  97. Read fbRemoveProcHeaderBlankLines Write fbRemoveProcHeaderBlankLines;
  98. property BlockStyle: TTriOptionStyle Read feBlockStyle Write feBlockStyle;
  99. property BlockBeginStyle: TTriOptionStyle Read feBlockBeginStyle Write feBlockBeginStyle;
  100. property LabelStyle: TTriOptionStyle Read feLabelStyle Write feLabelStyle;
  101. property LabelBeginStyle: TTriOptionStyle Read feLabelBeginStyle Write feLabelBeginStyle;
  102. property CaseLabelStyle: TTriOptionStyle Read feCaseLabelStyle Write feCaseLabelStyle;
  103. property CaseBeginStyle: TTriOptionStyle Read feCaseBeginStyle Write feCaseBeginStyle;
  104. property CaseElseStyle: TTriOptionStyle Read feCaseElseStyle Write feCaseElseStyle;
  105. property CaseElseBeginStyle: TTriOptionStyle Read feCaseElseBeginStyle Write feCaseElseBeginStyle;
  106. property EndElseStyle: TTriOptionStyle Read feEndElseStyle Write feEndElseStyle;
  107. property ElseIfStyle: TTriOptionStyle Read feElseIfStyle Write feElseIfStyle;
  108. property ElseBeginStyle: TTriOptionStyle Read feElseBeginStyle Write feElseBeginStyle;
  109. property BeforeCompilerDirectUses: TTriOptionStyle read feBeforeCompilerDirectUses write feBeforeCompilerDirectUses;
  110. property BeforeCompilerDirectStatements: TTriOptionStyle read feBeforeCompilerDirectStatements write feBeforeCompilerDirectStatements;
  111. property BeforeCompilerDirectGeneral: TTriOptionStyle read feBeforeCompilerDirectGeneral write feBeforeCompilerDirectGeneral;
  112. property AfterCompilerDirectUses: TTriOptionStyle read feAfterCompilerDirectUses write feAfterCompilerDirectUses;
  113. property AfterCompilerDirectStatements: TTriOptionStyle read feAfterCompilerDirectStatements write feAfterCompilerDirectStatements;
  114. property AfterCompilerDirectGeneral: TTriOptionStyle read feAfterCompilerDirectGeneral write feAfterCompilerDirectGeneral;
  115. property ReturnChars: TReturnChars Read feReturnChars Write feReturnChars;
  116. property RemoveConsecutiveBlankLines: boolean
  117. Read fbRemoveConsecutiveBlankLines Write fbRemoveConsecutiveBlankLines;
  118. property MaxConsecutiveBlankLines: integer
  119. Read fiMaxConsecutiveBlankLines Write fiMaxConsecutiveBlankLines;
  120. property MaxBlankLinesInSection: integer
  121. Read fiMaxBlankLinesInSection Write fiMaxBlankLinesInSection;
  122. property LinesBeforeProcedure: integer read fiLinesBeforeProcedure write fiLinesBeforeProcedure;
  123. end;
  124. implementation
  125. const
  126. REG_WHEN_REBREAK_LINES = 'WhenRebreakLines';
  127. REG_MAX_LINE_LENGTH = 'MaxLineLength';
  128. REG_NUM_RETURNS_AFTER_FINAL_END = 'NumReturnsAfterFinalEnd';
  129. //REG_ALIGN_ASSIGN = 'AlignAssign';
  130. REG_REMOVE_BAD_RETURNS = 'RemoveBadReturns';
  131. REG_ADD_GOOD_RETURNS = 'AddGoodReturns';
  132. REG_USES_ONE_PER_LINE = 'UsesOnePerLine';
  133. REG_BREAK_AFTER_USES = 'BreakAfterUses';
  134. REG_REMOVE_EXPRESSION_RETURNS = 'RemoveExpressionReturns';
  135. REG_REMOVE_VAR_RETURNS = 'RemoveVarReturns';
  136. REG_REMOVE_PROC_HEADER_BLANK_LINES = 'RemoveProcHeaderBlankLines';
  137. REG_REMOVE_PROPERTY_RETURNS = 'NoReturnsInProperty';
  138. REG_REMOVE_PROCEDURE_DEF_RETURNS = 'RemoveProcedureDefReturns';
  139. REG_REMOVE_BLOCK_BLANK_LINES = 'RemoveReturns';
  140. REG_REMOVE_VAR_BLANK_LINES = 'RemoveVarBlankLines';
  141. { block line breaking styles }
  142. REG_BLOCK_STYLE = 'Block';
  143. REG_BLOCK_BEGIN_STYLE = 'BlockBegin';
  144. REG_LABEL_STYLE = 'Label';
  145. REG_LABEL_BEGIN_STYLE = 'LabelBegin';
  146. REG_CASE_LABEL_STYLE = 'CaseLabel';
  147. REG_CASE_BEGIN_STYLE = 'CaseBegin';
  148. REG_CASE_ELSE_STYLE = 'CaseElse';
  149. REG_CASE_ELSE_BEGIN_STYLE = 'CaseElseBegin';
  150. REG_END_ELSE_STYLE = 'EndElse';
  151. REG_ELSE_IF_STYLE = 'ElseIf';
  152. REG_ELSE_BEGIN_STYLE = 'ElseBegin';
  153. REG_BEFORE_COMPILER_DIRECT_USES_STYLE = 'BeforeCompilerDirectUses';
  154. REG_BEFORE_COMPILER_DIRECT_STATEMENTS_STYLE = 'BeforeCompilerDirectStatements';
  155. REG_BEFORE_COMPILER_DIRECT_GENERAL_STYLE = 'BeforeCompilerDirectGeneral';
  156. REG_AFTER_COMPILER_DIRECT_USES_STYLE = 'AfterCompilerDirectUses';
  157. REG_AFTER_COMPILER_DIRECT_STATEMENTS_STYLE = 'AfterCompilerDirectStatements';
  158. REG_AFTER_COMPILER_DIRECT_GENERAL_STYLE = 'AfterCompilerDirectGeneral';
  159. REG_RETURN_CHARS = 'ReturnChars';
  160. REG_REMOVE_CONSECUTIVE_BLANK_LINES = 'RemoveConsecutiveBlankLines';
  161. REG_MAX_CONSECUTIVE_BLANK_LINES = 'MaxConsecutiveBlankLines';
  162. REG_MAX_BLANK_LINES_IN_SECTION = 'MaxBlankLinesInSection';
  163. REG_LINES_BEFORE_PROCEDURE = 'LinesBeforeProcedure';
  164. constructor TSetReturns.Create;
  165. begin
  166. inherited;
  167. SetSection('Returns');
  168. end;
  169. procedure TSetReturns.ReadFromStream(const pcStream: TSettingsInput);
  170. begin
  171. Assert(pcStream <> nil);
  172. feRebreakLines := TWhenToRebreakLines(pcStream.Read(REG_WHEN_REBREAK_LINES,
  173. Ord(rbOnlyIfGood)));
  174. fiMaxLineLength := pcStream.Read(REG_MAX_LINE_LENGTH, 90);
  175. fiNumReturnsAfterFinalEnd := pcStream.Read(REG_NUM_RETURNS_AFTER_FINAL_END, 1);
  176. fbRemoveBadReturns := pcStream.Read(REG_REMOVE_BAD_RETURNS, True);
  177. fbAddGoodReturns := pcStream.Read(REG_ADD_GOOD_RETURNS, True);
  178. fbUsesClauseOnePerLine := pcStream.Read(REG_USES_ONE_PER_LINE, False);
  179. fbBreakAfterUses := pcStream.Read(REG_BREAK_AFTER_USES, False);
  180. fbRemoveExpressionReturns := pcStream.Read(REG_REMOVE_EXPRESSION_RETURNS, False);
  181. fbRemoveVarReturns := pcStream.Read(REG_REMOVE_VAR_RETURNS, True);
  182. fbRemovePropertyReturns := pcStream.Read(REG_REMOVE_PROPERTY_RETURNS, True);
  183. fbRemoveProcedureDefReturns := pcStream.Read(REG_REMOVE_PROCEDURE_DEF_RETURNS, False);
  184. fbRemoveBlockBlankLines := pcStream.Read(REG_REMOVE_BLOCK_BLANK_LINES, True);
  185. fbRemoveVarBlankLines := pcStream.Read(REG_REMOVE_VAR_BLANK_LINES, False);
  186. fbRemoveProcHeaderBlankLines :=
  187. pcStream.Read(REG_REMOVE_PROC_HEADER_BLANK_LINES, True);
  188. feBlockStyle := TTriOptionStyle(pcStream.Read(REG_BLOCK_STYLE, Ord(eLeave)));
  189. feBlockBeginStyle := TTriOptionStyle(pcStream.Read(REG_BLOCK_BEGIN_STYLE, Ord(eLeave)));
  190. feLabelStyle := TTriOptionStyle(pcStream.Read(REG_LABEL_STYLE, Ord(eLeave)));
  191. feLabelBeginStyle := TTriOptionStyle(pcStream.Read(REG_LABEL_BEGIN_STYLE, Ord(eLeave)));
  192. feCaseLabelStyle := TTriOptionStyle(pcStream.Read(REG_CASE_LABEL_STYLE, Ord(eLeave)));
  193. feCaseBeginStyle := TTriOptionStyle(pcStream.Read(REG_CASE_BEGIN_STYLE, Ord(eLeave)));
  194. feCaseElseStyle := TTriOptionStyle(pcStream.Read(REG_CASE_ELSE_STYLE, Ord(eLeave)));
  195. feCaseElseBeginStyle := TTriOptionStyle(pcStream.Read(REG_CASE_ELSE_BEGIN_STYLE, Ord(eLeave)));
  196. feEndElseStyle := TTriOptionStyle(pcStream.Read(REG_END_ELSE_STYLE, Ord(eLeave)));
  197. feElseIfStyle := TTriOptionStyle(pcStream.Read(REG_ELSE_IF_STYLE, Ord(eNever)));
  198. feElseBeginStyle := TTriOptionStyle(pcStream.Read(REG_ELSE_BEGIN_STYLE, Ord(eNever)));
  199. feBeforeCompilerDirectUses := TTriOptionStyle(pcStream.Read(REG_BEFORE_COMPILER_DIRECT_USES_STYLE, Ord(eLeave)));
  200. feBeforeCompilerDirectStatements := TTriOptionStyle(pcStream.Read(REG_BEFORE_COMPILER_DIRECT_STATEMENTS_STYLE, Ord(eAlways)));
  201. feBeforeCompilerDirectGeneral := TTriOptionStyle(pcStream.Read(REG_BEFORE_COMPILER_DIRECT_GENERAL_STYLE, Ord(eLeave)));
  202. feAfterCompilerDirectUses := TTriOptionStyle(pcStream.Read(REG_AFTER_COMPILER_DIRECT_USES_STYLE, Ord(eLeave)));
  203. feAfterCompilerDirectStatements := TTriOptionStyle(pcStream.Read(REG_AFTER_COMPILER_DIRECT_STATEMENTS_STYLE, Ord(eAlways)));
  204. feAfterCompilerDirectGeneral := TTriOptionStyle(pcStream.Read(REG_AFTER_COMPILER_DIRECT_GENERAL_STYLE, Ord(eLeave)));
  205. feReturnChars := TReturnChars(pcStream.Read(REG_RETURN_CHARS, Ord(rcLeaveAsIs)));
  206. fbRemoveConsecutiveBlankLines := pcStream.Read(REG_REMOVE_CONSECUTIVE_BLANK_LINES, True);
  207. fiMaxConsecutiveBlankLines := pcStream.Read(REG_MAX_CONSECUTIVE_BLANK_LINES, 4);
  208. fiMaxBlankLinesInSection := pcStream.Read(REG_MAX_BLANK_LINES_IN_SECTION, 1);
  209. fiLinesBeforeProcedure := pcStream.Read(REG_LINES_BEFORE_PROCEDURE, 1);
  210. end;
  211. procedure TSetReturns.WriteToStream(const pcOut: TSettingsOutput);
  212. begin
  213. Assert(pcOut <> nil);
  214. pcOut.Write(REG_WHEN_REBREAK_LINES, Ord(feRebreakLines));
  215. pcOut.Write(REG_MAX_LINE_LENGTH, fiMaxLineLength);
  216. pcOut.Write(REG_NUM_RETURNS_AFTER_FINAL_END, fiNumReturnsAfterFinalEnd);
  217. pcOut.Write(REG_REMOVE_BAD_RETURNS, fbRemoveBadReturns);
  218. pcOut.Write(REG_ADD_GOOD_RETURNS, fbAddGoodReturns);
  219. pcOut.Write(REG_USES_ONE_PER_LINE, UsesClauseOnePerLine);
  220. pcOut.Write(REG_BREAK_AFTER_USES, BreakAfterUses);
  221. pcOut.Write(REG_REMOVE_EXPRESSION_RETURNS, fbRemoveExpressionReturns);
  222. pcOut.Write(REG_REMOVE_VAR_RETURNS, fbRemoveVarReturns);
  223. pcOut.Write(REG_REMOVE_PROPERTY_RETURNS, fbRemovePropertyReturns);
  224. pcOut.Write(REG_REMOVE_PROCEDURE_DEF_RETURNS, fbRemoveProcedureDefReturns);
  225. pcOut.Write(REG_REMOVE_BLOCK_BLANK_LINES, fbRemoveBlockBlankLines);
  226. pcOut.Write(REG_REMOVE_VAR_BLANK_LINES, fbRemoveVarBlankLines);
  227. pcOut.Write(REG_REMOVE_PROC_HEADER_BLANK_LINES, fbRemoveProcHeaderBlankLines);
  228. pcOut.Write(REG_BLOCK_STYLE, Ord(feBlockStyle));
  229. pcOut.Write(REG_BLOCK_BEGIN_STYLE, Ord(feBlockBeginStyle));
  230. pcOut.Write(REG_LABEL_STYLE, Ord(feLabelStyle));
  231. pcOut.Write(REG_LABEL_BEGIN_STYLE, Ord(feLabelBeginStyle));
  232. pcOut.Write(REG_CASE_LABEL_STYLE, Ord(feCaseLabelStyle));
  233. pcOut.Write(REG_CASE_BEGIN_STYLE, Ord(feCaseBeginStyle));
  234. pcOut.Write(REG_CASE_ELSE_STYLE, Ord(feCaseElseStyle));
  235. pcOut.Write(REG_CASE_ELSE_BEGIN_STYLE, Ord(feCaseElseBeginStyle));
  236. pcOut.Write(REG_END_ELSE_STYLE, Ord(feEndElseStyle));
  237. pcOut.Write(REG_ELSE_IF_STYLE, Ord(feElseIfStyle));
  238. pcOut.Write(REG_ELSE_BEGIN_STYLE, Ord(feElseBeginStyle));
  239. pcOut.Write(REG_BEFORE_COMPILER_DIRECT_USES_STYLE, Ord(feBeforeCompilerDirectUses));
  240. pcOut.Write(REG_BEFORE_COMPILER_DIRECT_STATEMENTS_STYLE, Ord(feBeforeCompilerDirectStatements));
  241. pcOut.Write(REG_BEFORE_COMPILER_DIRECT_GENERAL_STYLE, Ord(feBeforeCompilerDirectGeneral));
  242. pcOut.Write(REG_AFTER_COMPILER_DIRECT_USES_STYLE, Ord(feAfterCompilerDirectUses));
  243. pcOut.Write(REG_AFTER_COMPILER_DIRECT_STATEMENTS_STYLE, Ord(feAfterCompilerDirectStatements));
  244. pcOut.Write(REG_AFTER_COMPILER_DIRECT_GENERAL_STYLE, Ord(feAfterCompilerDirectGeneral));
  245. pcOut.Write(REG_RETURN_CHARS, Ord(feReturnChars));
  246. pcOut.Write(REG_REMOVE_CONSECUTIVE_BLANK_LINES, fbRemoveConsecutiveBlankLines);
  247. pcOut.Write(REG_MAX_CONSECUTIVE_BLANK_LINES, fiMaxConsecutiveBlankLines);
  248. pcOut.Write(REG_MAX_BLANK_LINES_IN_SECTION, fiMaxBlankLinesInSection);
  249. pcOut.Write(REG_LINES_BEFORE_PROCEDURE, fiLinesBeforeProcedure);
  250. end;
  251. end.