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
|
unit SingleSpaceBefore;
{(*}
(*------------------------------------------------------------------------------
Delphi Code formatter source code
The Original Code is SingleSpaceBefore, 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 7 Dec 1999
single space before certain tokens (e.g. ':='
This process and SingleSpaceAfter must be carefull with directives:
words like "read" and "write" must be single-spaced in property defs
but in normal code these are valid procedure names, and
converting "Result := myObject.Read;" to
"Result := myObject. read ;" compiles, but looks all wrong
}
uses SwitchableVisitor;
type
TSingleSpaceBefore = class(TSwitchableVisitor)
private
protected
function EnabledVisitSourceToken(const pcNode: TObject): boolean; override;
public
constructor Create; override;
function IsIncludedInSettings: boolean; override;
end;
implementation
uses
{ local }
JcfStringUtils,
SourceToken, Tokens, ParseTreeNodeType, JcfSettings,
FormatFlags, TokenUtils, SettingsTypes;
const
// space before all operators
SingleSpaceBeforeWords: TTokenTypeSet = [ttEquals, ttThen, ttOf, ttDo,
ttTo, ttDownTo];
NoSpaceAfterTokens: TTokenTypeSet = [ttOpenBracket, ttOpenSquareBracket];
function NeedsSpaceBefore(const pt: TSourceToken): boolean;
var
lcPrev: TSourceToken;
begin
Result := False;
if pt = nil then
exit;
if pt.HasParentNode(nGeneric, 1) then
exit;
if pt.TokenType = ttCloseBracket then
begin
if FormattingSettings.Spaces.SpaceBeforeCloseBrackets then
begin
Result := true;
exit;
end;
end;
if (pt.TokenType = ttOpenBracket) then
begin
if FormattingSettings.Spaces.SpaceBeforeOpenBracketsInFunctionDeclaration then
begin
if pt.HasParentNode(nFormalParams, 1) then
begin
Result := true;
exit;
end;
end;
if FormattingSettings.Spaces.SpaceBeforeOpenBracketsInFunctionCall then
begin
if pt.HasParentNode(nActualParams, 1) then
begin
Result := true;
exit;
end;
end;
end
else if (pt.TokenType = ttOpenSquareBracket) then
begin
if FormattingSettings.Spaces.SpaceBeforeOpenSquareBracketsInExpression then
begin
if pt.HasParentNode(nExpression) then
begin
Result := true;
exit;
end;
end;
end;
if pt.HasParentNode(nLiteralString) then
begin
Result := False;
exit;
end;
if IsClassHelperWords(pt) then
begin
Result := True;
exit;
end;
{ not in Asm block }
if pt.HasParentNode(nAsm) then
exit;
if (pt.TokenType in AssignmentDirectives) then
begin
Result := True;
exit;
end;
if IsHintDirective(pt) then
begin
Result := True;
exit;
end;
if (pt.TokenType in AllDirectives) and (pt.HasParentNode(DirectiveNodes)) then
begin
Result := True;
exit;
end;
if (pt.TokenType in SingleSpaceBeforeWords) then
begin
Result := True;
exit;
end;
if FormattingSettings.Spaces.SpaceForOperator = eAlways then
begin
if (pt.TokenType in SingleSpaceOperators) then
begin
Result := True;
end;
{ 'a := --3;' and 'lc := ptr^;'
are the only exceptions to the rule of a space before an operator }
if (pt.TokenType in Operators) then
begin
if (pt.TokenType = ttHat) or
(IsUnaryOperator(pt) and IsUnaryOperator(pt.PriorSolidToken)) then
Result := False
else
Result := True;
exit;
end;
end;
{ 'in' in the uses clause }
if ((pt.TokenType = ttIn) and (pt.HasParentNode(nUses))) then
begin
Result := True;
exit;
end;
{ comment just after uses clause, unless it's a compiler directive }
if (pt.TokenType = ttComment) and (pt.CommentStyle <> eCompilerDirective) then
begin
lcPrev := pt.PriorSolidToken;
if (lcPrev <> nil) and (lcPrev.TokenType = ttUses) then
begin
Result := True;
exit;
end;
end;
{ 'absolute' as a var directive }
if (pt.TokenType = ttAbsolute) and pt.HasParentNode(nAbsoluteVar) then
begin
Result := True;
exit;
end;
{ any token that starts a literal string }
if StartsLiteralString(pt) then
begin
Result := True;
exit;
end;
if (pt.TokenType = ttDefault) and pt.HasParentNode(nPropertySpecifier) then
begin
Result := True;
exit;
end;
{ signle space before read, write etc in property }
if pt.HasParentNode(nProperty) then
begin
if (pt.TokenType in [ttProperty, ttRead, ttWrite, ttDefault,
ttStored, ttNoDefault, ttImplements]) then
begin
Result := True;
exit;
end;
end;
{ program uses clauses has a form link comment }
if InFilesUses(pt) then
begin
if ((pt.TokenType = ttComment) and (pt.CommentStyle in CURLY_COMMENTS)) and
pt.IsOnRightOf(nUses, ttUses) then
begin
Result := True;
exit;
end;
end;
end;
constructor TSingleSpaceBefore.Create;
begin
inherited;
FormatFlags := FormatFlags + [eAddSpace, eRemoveSpace, eRemoveReturn];
end;
function TSingleSpaceBefore.EnabledVisitSourceToken(const pcNode: TObject): boolean;
var
lcSourceToken, lcNext, lcNew: TSourceToken;
begin
Result := False;
lcSourceToken := TSourceToken(pcNode);
lcNext := lcSourceToken.NextToken;
if lcNext = nil then
exit;
// suspend this rule after open brackets
// e.g. if there's a space before a '-' minus sign
// this applies to "x - 1" but not "(-1 + x)"
if lcSourceToken.TokenType in NoSpaceAfterTokens then
begin
exit;
end;
if NeedsSpaceBefore(lcNext) then
begin
if (lcSourceToken.TokenType = ttWhiteSpace) then
begin
{ one space }
lcSourceToken.SourceCode := NativeSpace;
{ empty any preceeding whitespace }
repeat
lcSourceToken := lcSourceToken.PriorToken;
if lcSourceToken.TokenType = ttWhiteSpace then
lcSourceToken.SourceCode := '';
until lcSourceToken.TokenType <> ttWhiteSpace;
end
else
begin
lcNew := TSourceToken.Create;
lcNew.TokenType := ttWhiteSpace;
lcNew.SourceCode := NativeSpace;
InsertTokenAfter(lcSourceToken, lcNew);
end;
end;
end;
function TSingleSpaceBefore.IsIncludedInSettings: boolean;
begin
Result := FormattingSettings.Spaces.FixSpacing;
end;
end.
|