File: BlockStyles.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (259 lines) | stat: -rw-r--r-- 7,510 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
unit BlockStyles;

{(*}
(*------------------------------------------------------------------------------
 Delphi Code formatter source code 

The Original Code is BlockStyles, released May 2003.
The Initial Developer of the Original Code is Anthony Steele. 
Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
All Rights Reserved. 
Contributor(s): Anthony Steele. 

The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"). you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.mozilla.org/NPL/

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied.
See the License for the specific language governing rights and limitations 
under the License.

Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 or later (the "GPL") 
See http://www.gnu.org/licenses/gpl.html
------------------------------------------------------------------------------*)
{*)}

{$I JcfGlobal.inc}

interface

{ AFS 22 April 2003
  This unit handles the different styles of line breaking & spacing after the constructs
  if <expression> then
    statement;

  if <expression> then
  begin
     ..statements
  end;

  The styles are
  - never break line (subject to approval by the linebreaker.
    If the resulting line is too long, just after the then
    is a very good place to break and may be chosen. )
  - Leave as is
  - Always break line. This is the official style
    http://www.borland.com/techvoyage/articles/DelphiStyle/StyleGuide.html

  This style also applies to
    for <expression> do
  and
    while <expression> do
  and
    else
      <statement>

 to do:
 apply to

 case exp of
  value: statement;
  value: begin statements end;

 end;
}

uses SwitchableVisitor, ParseTreeNodeType;

type
  TBlockStyles = class(TSwitchableVisitor)
  private

  protected
    function EnabledVisitSourceToken(const pcNode: TObject): Boolean; override;
  public
    constructor Create; override;

    function IsIncludedInSettings: boolean; override;
  end;

implementation

uses
  { local }
  JcfStringUtils,
  Tokens, SourceToken, TokenUtils, JcfSettings,
  FormatFlags, SettingsTypes;

const
  BreakWords: TTokenTypeSet = [ttThen, ttDo, ttElse, ttEnd];

function GetStyle(const pt: TSourceToken): TTriOptionStyle;
var
  lcNextToken: TSourceToken;
begin
  Result := eLeave;

  lcNextToken := pt.NextSolidToken;
  if lcNextToken = nil then
    exit;

  { only do anything to an end if it is followed by an else }
  if pt.TokenType = ttEnd then
  begin
    if lcNextToken.TokenType = ttElse then
      Result := FormattingSettings.Returns.EndElseStyle;
  end
  else if pt.TokenType = ttColon then
  begin
    if IsCaseColon(pt) then
    begin
      if lcNextToken.TokenType = ttBegin then
        Result := FormattingSettings.Returns.CaseBeginStyle
      else
        Result := FormattingSettings.Returns.CaseLabelStyle;
    end
    else if IsLabelColon(pt) then
    begin
      { otherwise, is there a begin next? }
      if lcNextToken.TokenType = ttBegin then
        Result := FormattingSettings.Returns.LabelBeginStyle
      else
        Result := FormattingSettings.Returns.LabelStyle;
    end;
  end
  else if (pt.TokenType = ttElse) then
  begin

    if pt.HasParentNode(nElseCase, 1) then
    begin
      if lcNextToken.TokenType = ttBegin then
        Result := FormattingSettings.Returns.CaseElseBeginStyle
      else
        Result := FormattingSettings.Returns.CaseElseStyle;
    end
    else if (lcNextToken.TokenType = ttIf) then
    begin
      { though else normally starts a block,
       according to standards, there is never a return in "else if" (!!! check!)
       But we have a config setting for Marcus F

        **NB** rare exception: this does not apply when the if is not related to the else
        ie
         case (foo) of
           1:
            DoSomething1;
           2:
            SoSomething2;
           else
             // this is the else case, not part of an if.
             // All statements from the 'else' to the 'end' form a block
             if (SomeCond) then // though the 'if' is directly after the else, this is not an else-if
               DoSomethingElse;
             if (SomeOtherCond) then
               DoSomeOtherThing;
         end;

         end;

       }
      Result := FormattingSettings.Returns.ElseIfStyle;
    end
    else if (lcNextToken.TokenType = ttBegin) then
    begin
      Result := FormattingSettings.Returns.ElseBeginStyle;
    end;
  end
  else
  begin
    { otherwise, is there a begin next? }
    if lcNextToken.TokenType = ttBegin then
      Result := FormattingSettings.Returns.BlockBeginStyle
    else
      Result := FormattingSettings.Returns.BlockStyle;
  end;
end;


constructor TBlockStyles.Create;
begin
  inherited;
  FormatFlags := FormatFlags + [eBlockStyle, eAddReturn, eRemoveReturn];
end;

function TBlockStyles.EnabledVisitSourceToken(const pcNode: TObject): Boolean;
var
  leStyle: TTriOptionStyle;
  lcSourceToken, lcNextReturn, lcNextComment, lcNextSpace: TSourceToken;
begin
  Result := False;
  lcSourceToken := TSourceToken(pcNode);

  if (lcSourceToken.TokenType in BreakWords) or IsLabelColon(lcSourceToken) or
    IsCaseColon(lcSourceToken) then
  begin
    leStyle := GetStyle(lcSourceToken);

    case leStyle of
      eLeave: ; // do nothing, leave as is
      eAlways:
      begin
        lcNextReturn := lcSourceToken.NextTokenWithExclusions([ttWhiteSpace, ttComment]);
        if (lcNextReturn <> nil) and (lcNextReturn.TokenType <> ttReturn) then
        begin
          { if there's a white space at heand, turn it into a return. Else make a return }
          lcNextSpace := lcSourceToken.NextToken;
          if lcNextSpace.TokenType = ttWhiteSpace then
          begin
            lcNextSpace.TokenType := ttReturn;
            lcNextSpace.SourceCode := NativeLineBreak;
          end
          else
            InsertTokenAfter(lcSourceToken, NewReturn);
        end;
      end;
      eNever:
      begin
        lcNextReturn := lcSourceToken.NextTokenWithExclusions([ttWhiteSpace, ttComment]);
        if (lcNextReturn <> nil) and (lcNextReturn.TokenType = ttReturn) then
        begin
          { if there is a '//' comment before the return, don't remove the return :) }
          lcNextComment := lcSourceToken.NextTokenWithExclusions([ttWhiteSpace]);
          if lcNextComment.CommentStyle <> eDoubleSlash then
          begin
            // turn to space
            lcNextReturn.TokenType := ttWhiteSpace;
            // need some space here - don't leave nothing between tokens
            if NextToWhiteSpace(lcNextReturn) then
            begin
              // null space as it's next to space already
              lcNextReturn.SourceCode := '';

              // make the folling space into a single space
              lcNextSpace := lcNextReturn.NextToken;
              if (lcNextSpace <> nil) and (lcNextSpace.TokenType = ttWhiteSpace) then
              begin
                lcNextSpace.SourceCode := ' ';
              end;

            end
            else
              // keep a single space
              lcNextReturn.SourceCode := ' ';
          end;
        end;
      end;
      else
        Assert(False);
    end;
  end;
end;

function TBlockStyles.IsIncludedInSettings: boolean;
begin
  Result := True;
end;

end.