PageRenderTime 13ms CodeModel.GetById 8ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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