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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
unit NoReturnAfter;
{ AFS 11 Jan 2003
Some tokens should not have a return after them for fomatting
}
{(*}
(*------------------------------------------------------------------------------
Delphi Code formatter source code
The Original Code is NoReturnAfter, 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
uses SourceToken, SwitchableVisitor;
type
TNoReturnAfter = class(TSwitchableVisitor)
private
fcLastSolidToken: TSourceToken;
fbDoneWork: boolean;
function NoDeclarationBefore: boolean;
function CommentBefore: boolean;
function NoSemiColonBefore: boolean;
function NeedsNoReturn(const pt: TSourceToken): boolean;
protected
function EnabledVisitSourceToken(const pcNode: TObject): Boolean; override;
public
constructor Create; override;
function IsIncludedInSettings: boolean; override;
property DoneWork: boolean Read fbDoneWork;
end;
implementation
uses Tokens, ParseTreeNodeType, TokenUtils,
SetReturns, JcfSettings, FormatFlags, SettingsTypes;
constructor TNoReturnAfter.Create;
begin
inherited;
fcLastSolidToken := nil;
fbDoneWork := False;
FormatFlags := FormatFlags + [eRemoveReturn];
end;
function TNoReturnAfter.NeedsNoReturn(const pt: TSourceToken): boolean;
const
NoReturnWords: TTokenTypeSet = [ttProcedure, ttFunction,
ttConstructor, ttDestructor, ttProperty, ttGoto, ttGeneric];
var
lcSetReturns: TSetReturns;
lcNext, lcNext2: TSourceToken;
begin
Result := False;
if pt = nil then
exit;
if pt.HasParentNode(nAsm) then
exit;
lcSetReturns := FormattingSettings.Returns;
Assert(lcSetReturns <> nil);
if FormattingSettings.Returns.RemoveBadReturns then
begin
if pt.TokenType in NoReturnWords then
begin
Result := True;
exit;
end;
{ class helper declaration }
if IsCLassHelperWords(pt) then
begin
Result := True;
exit;
end;
{ only place a return after a colon is legit is at a label
in a proc body }
if pt.TokenType = ttColon then
begin
if ( not InStatements(pt)) and (RoundBracketLevel(pt) = 0) then
begin
Result := True;
exit;
end;
end;
{ var x absolute y; just after absolute is a bad place to break }
if (pt.TokenType = ttAbsolute) and pt.HasParentNode(nVarDecl) then
begin
Result := True;
exit;
end;
{ Default property values:
No return after 'default' in property def on non-array property
because it's always followed by a number, e.g.
property FloonCount: integer default 12;
as opposed to default (array) properties,
eg property Items[piIndex: integer]; default; }
if (pt.TokenType = ttDefault) and pt.HasParentNode(nPropertySpecifier) then
begin
{ use the persence of semicolon to distinguish
Default property values from default (array) properties }
if not SemiColonNext(pt) then
begin
Result := True;
exit;
end;
end;
{ array property params - no returns }
if (SquareBracketLevel(pt) > 0) and pt.HasParentNode(nPropertyParameterList) then
begin
{ use the persence of semicolon to distinguish
Default property values from default (array) properties }
if not SemiColonNext(pt) then
begin
Result := True;
exit;
end;
end;
{ in procedure params - no return after 'var' or 'const' }
if pt.HasParentnode(nFormalParams) and (RoundBracketLevel(pt) > 0) then
begin
if pt.TokenType in [ttVar, ttConst] then
begin
Result := True;
exit;
end;
end;
{ in procedure body - no return directly after 'if' }
if InStatements(pt) then
begin
if pt.TokenType = ttIf then
begin
Result := True;
exit;
end;
end;
if (pt.CommentStyle = eCompilerDirective) and (CompilerDirectiveLineBreak(pt, False) = eNever) then
begin
Result := True;
exit;
end;
end;
{ remove returns based on options }
{ the options don't apply after comments }
if (pt.TokenType = ttComment) then
begin
Result := False;
exit;
end;
{ or just before them }
lcNext := pt.NextTokenWithExclusions([ttWhiteSpace, ttReturn]);
if lcNext = nil then
exit;
if (lcNext.TokenType = ttComment) or CommentBefore then
begin
Result := False;
exit;
end;
if lcSetReturns.RemoveExpressionReturns and pt.HasParentNode(nExpression) then
begin
{ can have a block that ends in expression without a semicolon, eg Don't remove return here:
begin
a := a + 2
end; }
if lcNext.HasParentNode(nExpression) then
begin
Result := True;
exit;
end;
end;
if lcSetReturns.RemoveVarReturns and (pt.TokenType <> ttSemiColon) and
( not (pt.TokenType in Declarations)) and pt.HasParentNode(nVarDecl) then
begin
if NoDeclarationBefore and NoSemicolonBefore and lcNext.HasParentNode(nVarDecl) then
begin
Result := True;
exit;
end;
end;
if lcSetReturns.RemoveProcedureDefReturns and pt.HasParentNode(nFormalParams) then
begin
Result := True;
exit;
end;
if (pt.TokenType = ttOpenSquareBracket) then
begin
// start of guid in interface
if pt.HasParentNode(nInterfaceTypeGuid, 1) then
begin
Result := True;
exit;
end;
// start of attribute
if pt.HasParentNode(nAttribute, 1) then
begin
Result := True;
exit;
end;
end;
// "foo in Foo.pas, " has return only after the comma
if InFilesUses(pt) then
begin
if (pt.TokenType in [ttComma, ttWord, ttQuotedLiteralString]) or
((pt.TokenType = ttComment) and (pt.CommentStyle in CURLY_COMMENTS)) then
begin
Result := True;
exit;
end;
end;
if pt.HasParentNode(nUses) then
begin
// no blank line in uses
lcNext := pt.NextTokenWithExclusions([ttWhitespace]);
if (lcNext <> nil) and (lcNext.TokenType = ttReturn) then
begin
lcNext2 := lcNext.NextTokenWithExclusions([ttWhitespace]);
if (lcNext2 <> nil) and (lcNext2.TokenType = ttReturn) then
begin
Result := True;
exit;
end;
end;
end;
{ "strict private" - no return in the middle }
if (pt.TokenType = ttStrict) and (pt.HasParentNode(nClassVisibility, 1)) then
begin
Result := True;
end;
end;
function TNoReturnAfter.NoDeclarationBefore: boolean;
begin
Result := (fcLastSolidToken = nil) or
( not (fcLastSolidToken.TokenType in Declarations));
end;
function TNoReturnAfter.NoSemiColonBefore: boolean;
begin
Result := (fcLastSolidToken = nil) or
( not (fcLastSolidToken.TokenType = ttSemiColon));
end;
function TNoReturnAfter.CommentBefore: boolean;
begin
Result := (fcLastSolidToken <> nil) and (fcLastSolidToken.TokenType = ttComment)
end;
function TNoReturnAfter.EnabledVisitSourceToken(const pcNode: TObject): Boolean;
var
lcSourceToken: TSourceToken;
begin
Result := False;
lcSourceToken := TSourceToken(pcNode);
if (lcSourceToken.TokenType = ttReturn) and
(fcLastSolidToken <> nil) and NeedsNoReturn(fcLastSolidToken) then
begin
// must repeat this until all done
BlankToken(lcSourceToken);
fbDoneWork := True;
end
else
begin
{ store for next time }
if not (lcSourceToken.TokenType in [ttWhiteSpace, ttReturn]) then
fcLastSolidToken := lcSourceToken;
end;
end;
function TNoReturnAfter.IsIncludedInSettings: boolean;
begin
Result := FormattingSettings.Returns.RemoveBadReturns;
end;
end.
|