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
|
{(*}
(*------------------------------------------------------------------------------
Delphi Code formatter source code
The Original Code is AlignStatements.pas, released April 2000.
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
------------------------------------------------------------------------------*)
{*)}
unit AlignBase;
{ AFS 6 Feb 2K
base class Generalisation of AlignAssign and co.
This is the base class for all aligners
}
{$I JcfGlobal.inc}
interface
uses SwitchableVisitor, SourceToken, SourceTokenList;
type
TAlignBase = class(TSwitchableVisitor)
private
fcTokens: TSourceTokenList;
fcResumeToken: TSourceToken;
procedure AlignTheBlock(const pcToken: TSourceToken);
function StillSuspended(const pc: TSourceToken): boolean;
procedure IndentAll(const piIndent: integer);
procedure AddToken(const pcToken: TSOurceToken; const pbAligned: boolean);
protected
{ API for descendant classes }
function TokenIsAligned(const pt: TSourceToken): boolean; virtual; abstract;
function TokenEndsStatement(const pt: TSourceToken): boolean; virtual; abstract;
function IsTokenInContext(const {%H-}pt: TSourceToken): boolean; virtual;
function TokenEndsAlignment(const {%H-}pt: TSourceToken): boolean; virtual;
{ override this to let the child class see the tokens as they come
this is used by the align vars to detect the first non-white space token after the : }
procedure OnTokenRead(const {%H-}pt: TSourceToken); virtual;
procedure ResetState; virtual;
function EnabledVisitSourceToken(const pcNode: TObject): boolean; override;
public
constructor Create; override;
destructor Destroy; override;
end;
implementation
uses
{ delphi }SysUtils, Math,
{ jcl }
{ jcf } JcfSettings, TokenUtils, ParseTreeNodeType;
{ TAlignBase }
constructor TAlignBase.Create;
begin
inherited;
fcResumeToken := nil;
fcTokens := TSourceTokenList.Create;
end;
destructor TAlignBase.Destroy;
begin
FreeAndNil(fcTokens);
inherited;
end;
function TAlignBase.EnabledVisitSourceToken(const pcNode: TObject): Boolean;
var
lcToken: TSourceToken;
begin
Result := False;
lcToken := TSourceToken(pcNode);
if StillSuspended(lcToken) then
exit;
if not IsTokenInContext(lcToken) then
exit;
if TokenIsAligned(lcToken) then
AlignTheBlock(lcToken);
end;
procedure TAlignBase.AlignTheBlock(const pcToken: TSourceToken);
var
liCurrent, liLastKnownAlignedStatement: integer;
lcCurrent: TSourceToken;
lbDone, lbFirst, bThisStatementIsAligned, lbThisTokenIsAligned: boolean;
liMaxIndent, liMinIndent: integer;
liThisIndent: integer;
liSettingsMin, liSettingsMax, liSettingsMaxVariance: integer;
liUnalignedCount, liMaxUnaligned: integer;
liAlignedCount: integer;
begin
ResetState;
Assert(TokenIsAligned(pcToken));
lcCurrent := pcToken;
OnTokenRead(lcCurrent);
AddToken(lcCurrent, True);
liLastKnownAlignedStatement := 0;
liMaxIndent := lcCurrent.XPosition;
liMinIndent := liMaxIndent;
{ locate end of first statement
BufferTokens(0) is the first :=
there must be a semicolon soon after }
liCurrent := 0;
repeat
Inc(liCurrent);
lcCurrent := lcCurrent.NextToken;
if lcCurrent <> nil then
begin
OnTokenRead(pcToken);
AddToken(lcCurrent, False);
end;
until (lcCurrent = nil) or TokenEndsStatement(lcCurrent);
{ end the first statement on EOF?! - abort! }
if (lcCurrent = nil) then
exit;
with FormattingSettings do
begin
liSettingsMin := Align.MinColumn;
liSettingsMax := Align.MaxColumn;
if pcToken.HasParentNode(nInterfaceSection) then
liSettingsMaxVariance := Align.MaxVarianceInterface
else
liSettingsMaxVariance := Align.MaxVariance;
liMaxUnaligned := Align.MaxUnalignedStatements;
end;
{ locate block end - include all consecutive aligned statements }
lbDone := False;
lbFirst := True;
liUnalignedCount := 0;
liAlignedCount := 1;
bThisStatementIsAligned := True; // first statement just read will be aligned
{ now look for consecutive similar statements to align }
while not lbDone do
begin
{ EOF?! - abort! }
if (lcCurrent = nil) then
begin
lbDone := True;
end
else
begin
lbThisTokenIsAligned := TokenIsAligned(lcCurrent);
if not lbFirst then
AddToken(lcCurrent, lbThisTokenIsAligned); // first one has been added above
lbFirst := False;
{ an aligned statement has the aligned token in it -
e.g. an assign statement has a ':=' in it :) }
if lbThisTokenIsAligned then
begin
bThisStatementIsAligned := True;
liThisIndent := lcCurrent.XPosition;
if liThisIndent >= liSettingsMin then
liMinIndent := Min(liThisIndent, liMinIndent);
{ store the higest indent in liMaxIndent
unless it is out of bounds, ie < liSettingsMin or > liSettingsMax }
liThisIndent := Max(liThisIndent, liSettingsMin);
if (liThisIndent > liMaxIndent) and (liThisIndent < liSettingsMax) and
(liThisIndent <= liMinIndent + liSettingsMaxVariance) then
liMaxIndent := liThisIndent;
{ may need to knock down the min if the first one is an outlier }
if (liThisIndent + liSettingsMaxVariance) < liMaxIndent then
liMaxIndent := liThisIndent;
end;
if TokenEndsStatement(lcCurrent) then
begin
{ ending a statement - was it an aligned one?
If not, maybe we should have stopped with the last statement }
if bThisStatementIsAligned then
begin
liLastKnownAlignedStatement := liCurrent;
liUnalignedCount := 0;
bThisStatementIsAligned := False;
Inc(liAlignedCount);
end
else
begin
{ look for consecutive unaligned statements to end the aligned block
depending on the config, this could be just 1 unalaigned statement
or it could be more
}
Inc(liUnalignedCount);
if liUnalignedCount > liMaxUnaligned then
lbDone := True;
end;
end;
if TokenEndsAlignment(lcCurrent) then
lbDone := True;
Inc(liCurrent);
lcCurrent := lcCurrent.NextToken;
OnTokenRead(lcCurrent);
end; { not EOF }
end; { while loop }
{ set iResume equal to the last token aligned }
fcResumeToken := fcTokens.SourceTokens[fcTokens.Count - 1];
{ now we know how far to go and how far to indent, do it? }
if (liLastKnownAlignedStatement > 0) and (liAlignedCount > 1) then
IndentAll(liMaxIndent);
ResetState;
end;
procedure TAlignBase.IndentAll(const piIndent: integer);
var
liLoop: integer;
lcCurrent, lcNew: TSourceToken;
begin
for liLoop := fcTokens.Count - 1 downto 0 do
begin
lcCurrent := fcTokens.SourceTokens[liLoop];
if (lcCurrent.UserTag > 0) and (lcCurrent.XPosition < piIndent) then
begin
{ indent to the specified level - make a new space token }
lcNew := InsertSpacesBefore(lcCurrent, piIndent - lcCurrent.XPosition);
fcTokens.Insert(liLoop, lcNew);
end;
end;
end;
procedure TAlignBase.OnTokenRead(const pt: TSourceToken);
begin
// here for override
end;
procedure TAlignBase.ResetState;
begin
fcTokens.Clear;
end;
function TAlignBase.IsTokenInContext(const pt: TSourceToken): boolean;
begin
// here for override
Result := True;
end;
function TAlignBase.StillSuspended(const pc: TSourceToken): boolean;
begin
if (fcResumeToken = nil) then
begin
// we are not suspended, so go.
Result := False;
end
else
begin
// have we reached the end of suspension?
Result := (fcResumeToken <> pc);
if not Result then
fcResumeToken := nil;
end;
end;
procedure TAlignBase.AddToken(const pcToken: TSourceToken; const pbAligned: boolean);
begin
Assert(pcToken <> nil);
if pbAligned then
pcToken.UserTag := 1
else
pcToken.UserTag := 0;
fcTokens.Add(pcToken);
end;
function TAlignBase.TokenEndsAlignment(const pt: TSourceToken): boolean;
begin
Result := False;
end;
end.
|