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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
unit SortUses;
{(*}
(*------------------------------------------------------------------------------
Delphi Code formatter source code
The Original Code is SortUses.pas, released July 2004.
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
{ unit to sort the uses clause }
uses BaseVisitor, ParseTreeNode;
type
TSortUses = class(TBaseTreeNodeVisitor)
private
procedure SortUsesInSections(const pcUsesIdentList: TParseTreeNode);
protected
public
constructor Create; override;
procedure PostVisitParseTreeNode(const pcNode: TObject); override;
function IsIncludedInSettings: boolean; override;
end;
implementation
uses
{ delphi }
Contnrs, Classes,
{ local }
ParseTreeNodeType, SourceToken, Tokens, JcfSettings,
SetTransform, TokenUtils, SortUsesData;
function GetSortableToken(pcNode: TParseTreeNode): TSourceToken;
var
lcTest: TParseTreeNode;
begin
Result := nil;
if pcNode is TSourceToken then
begin
Result := TSourceToken(pcNode);
end
else
begin
lcTest := pcNode.GetImmediateChild(nUsesItem);
if lcTest <> nil then
pcNode := lcTest;
lcTest := pcNode.GetImmediateChild(nIdentifier);
if lcTest <> nil then
pcNode := lcTest;
lcTest := pcNode.FirstSolidLeaf;
if lcTest <> nil then
Result := TSourceToken(lcTest);
end;
end;
constructor TSortUses.Create;
begin
inherited;
HasPostVisit := True;
HasSourceTokenVisit := False;
end;
function TSortUses.IsIncludedInSettings: boolean;
begin
with FormattingSettings.Transform do
Result := SortInterfaceUses or SortImplementationUses;
end;
procedure TSortUses.PostVisitParseTreeNode(const pcNode: TObject);
var
lcNode: TParseTreeNode;
lcIdentList: TParseTreeNode;
begin
lcNode := TParseTreeNode(pcNode);
{ is this a uses clause? }
if lcNode.NodeType <> nUses then
exit;
if FormattingSettings.Transform.SortUsesNoComments and lcNode.HasChildNode([ttComment]) then
exit;
{ is it turned on in this section? }
if lcNode.HasParentNode(nInterfaceSection) then
begin
{ interface section }
if not FormattingSettings.Transform.SortInterfaceUses then
exit;
end
else if lcNode.HasParentNode(nImplementationSection) then
begin
{ implentation section }
if not FormattingSettings.Transform.SortImplementationUses then
exit;
end
else
begin
{ other - program, package or library section }
if not FormattingSettings.Transform.SortProgramUses then
exit;
end;
lcIdentList := lcNode.GetImmediateChild(nIdentList);
if lcIdentList <> nil then
begin
SortUsesInSections(lcIdentList);
end;
end;
procedure CleanEnd(const pcRootNode: TParseTreeNode);
var
lcLastLeaf, lcRemove: TSourceToken;
begin
lcLastLeaf := TSourceToken(pcRootNode.LastLeaf);
while (lcLastLeaf <> nil) and (lcLastLeaf.TokenType in [ttComma, ttWhiteSpace, ttReturn]) do
begin
lcRemove := lcLastLeaf;
lcLastLeaf := lcLastLeaf.PriorToken;
{ abort after a // comment }
if (lcRemove.TokenType = ttReturn) and
(lcLastLeaf.TokenType = ttComment) and (lcLastLeaf.CommentStyle = eDoubleSlash) then
break;
lcRemove.Parent.RemoveChild(lcRemove);
end;
end;
procedure RemoveAllChildrenOnwards(const pcParent, pcChild: TParseTreeNode);
var
lcChild, lcLast: TParseTreeNode;
begin
if pcChild = nil then
lcChild := pcParent.FirstLeaf
else
lcChild := pcChild;
while (lcChild <> nil) and lcChild.HasParentNode(pcParent) do
begin
lcLast := lcChild;
lcChild := lcChild.NextLeafNode;
lcLast.Parent.ExtractChild(lcLast);
end;
end;
function EndsWithComma(const pcUsesIdentList: TParseTreeNode): boolean;
var
lcToken: TSourceToken;
begin
lcToken := pcUsesIdentList.LastLeaf as TSourceToken;
if (lcToken <> nil) and (not lcToken.IsSolid) then
lcToken := lcToken.PriorSolidToken;
Result := (lcToken <> nil) and (lcToken.TokenType = ttComma);
end;
procedure TSortUses.SortUsesInSections(const pcUsesIdentList: TParseTreeNode);
var
lcToken: TSourceToken;
lcSections: TObjectList;
lcCurrentSection: TUsesSection;
leBreakTokens: TTokenTypeSet;
procedure CollectWhiteSpace;
begin
{ start with a breaking section for the starting white space and comments where they are }
while (lcToken <> nil) and (not lcToken.IsSolid) do
begin
lcCurrentSection.AddToken(lcToken);
lcToken := lcToken.NextToken;
end;
end;
procedure NewSection(const pbSorted: Boolean);
begin
lcCurrentSection := TUsesSection.Create;
lcCurrentSection.Sorted := pbSorted;
lcSections.Add(lcCurrentSection);
end;
function IsBreakToken(const pcToken: TSourceToken): Boolean;
begin
Result := (pcToken.TokenType in leBreakTokens) or
((pcToken.TokenType = ttComment) and (pcToken.CommentStyle = eCompilerDirective));
end;
var
lbStartFirstSection: boolean;
lbStartUsesItem: boolean;
liSectionLoop: integer;
lbEndsWithComma: boolean;
begin
leBreakTokens := [];
if FormattingSettings.Transform.BreakUsesSortOnReturn then
Include(leBreakTokens, ttReturn);
if FormattingSettings.Transform.BreakUsesSortOnComment then
Include(leBreakTokens, ttComment);
{ sections is a list of lists
each section is a list of parse tree nodes
Each section is sorted }
lcSections := TObjectList.Create;
lcSections.OwnsObjects := True;
lbStartFirstSection := True;
lbStartUsesItem := True;
lbEndsWithComma := False;
try
{ at least one section, but can be more }
NewSection(True);
{ each child node is in a section }
lcToken := TSourceToken(pcUsesIdentList.FirstLeaf);
if (lcToken <> nil) and (not lcToken.IsSolid) then
begin
lcCurrentSection.Sorted := False;
CollectWhiteSpace;
NewSection(True);
end;
while (lcToken <> nil) and lcToken.HasParentNode(nIdentList) and lcToken.HasParentNode(nUses) do
begin
{ end of section, start of the next }
if IsBreakToken(lcToken) and (not lbStartUsesItem) then
begin
{ the break token is in a non-sorted section }
NewSection(False);
lcCurrentSection.AddToken(lcToken);
lcToken := lcToken.NextToken;
CollectWhiteSpace;
NewSection(True);
continue;
end;
if IsIdentifier(lcToken, idAny) and (lcToken.HasParentNode(nUsesItem, 2)) then
begin
if not lbStartUsesItem then
begin
lcCurrentSection.AddUsesItem;
lbStartUsesItem := True;
end;
end
else
begin
{ a uses item contains exactly one identifier.
but in Delphi.Net it can be dotted, e.g.
"System.Runtime.Remoting"
}
lbStartUsesItem := False;
end;
lcCurrentSection.AddToken(lcToken);
{ don't end the first section until it's begun }
if lbStartFirstSection and (not (lcToken.TokenType in NotSolidTokens)) then
lbStartFirstSection := False;
lcToken := lcToken.NextToken;
if lcToken.TokenType = ttDot then
begin
// delphi.net dotted unit name
while (lcToken.TokenType = ttDot) or IsIdentifier(lcToken, idAny) do
begin
lcCurrentSection.AddToken(lcToken);
lcToken := lcToken.NextToken;
end;
end;
end;
{ remove the unsorted tokens }
RemoveAllChildrenOnwards(pcUsesIdentList, nil);
{ sort each section, and reattach it }
for liSectionLoop := 0 to lcSections.Count - 1 do
begin
lcCurrentSection := TUsesSection(lcSections.Items[liSectionLoop]);
if lcCurrentSection.Sorted then
begin
{ sort this section }
case FormattingSettings.Transform.UsesSortOrder of
eAlpha:
lcCurrentSection.Sort(AlphaNameSort);
eReverseAlpha:
lcCurrentSection.Sort(ReverseAlphaNameSort);
eShortToLong:
lcCurrentSection.Sort(LengthNameSort);
eLongToShort:
lcCurrentSection.Sort(ReverseLengthNameSort);
else
Assert(False);
end;
end;
{ repopulate the original parent with sorted items
if the last one ends with a comma, this one must not start with a comma }
lcCurrentSection.AttachTo(pcUsesIdentList, not lbEndsWithComma);
lbEndsWithComma := EndsWithComma(pcUsesIdentList);
end;
finally
lcSections.Free;
end;
{ no comma or space before the semicolon }
CleanEnd(pcUsesIdentList);
end;
end.
|