mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-11 09:16:16 +02:00
codetools: added function to move DEFINEs a level up
git-svn-id: trunk@11790 -
This commit is contained in:
parent
9708b1003e
commit
ece5c99e5b
@ -714,7 +714,7 @@ end;
|
|||||||
|
|
||||||
procedure TCodeTree.AddNodeInFrontOf(NextBrotherNode, ANode: TCodeTreeNode);
|
procedure TCodeTree.AddNodeInFrontOf(NextBrotherNode, ANode: TCodeTreeNode);
|
||||||
begin
|
begin
|
||||||
ANode.Parent:=ANode.Parent;
|
ANode.Parent:=NextBrotherNode.Parent;
|
||||||
ANode.NextBrother:=NextBrotherNode;
|
ANode.NextBrother:=NextBrotherNode;
|
||||||
ANode.PriorBrother:=NextBrotherNode.PriorBrother;
|
ANode.PriorBrother:=NextBrotherNode.PriorBrother;
|
||||||
NextBrotherNode.PriorBrother:=ANode;
|
NextBrotherNode.PriorBrother:=ANode;
|
||||||
|
@ -232,7 +232,7 @@ function ComparePCharWithH2PasFuncName(Name, H2PasFunc: Pointer): integer;
|
|||||||
|
|
||||||
function CDNodeDescAsString(Desc: TCompilerDirectiveNodeDesc): string;
|
function CDNodeDescAsString(Desc: TCompilerDirectiveNodeDesc): string;
|
||||||
|
|
||||||
procedure AdjustPositionAfterInsert(var p: integer;
|
procedure AdjustPositionAfterInsert(var p: integer; IsStart: boolean;
|
||||||
FromPos, ToPos, DiffPos: integer);
|
FromPos, ToPos, DiffPos: integer);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
@ -289,14 +289,23 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
procedure AdjustPositionAfterInsert(var p: integer;
|
procedure AdjustPositionAfterInsert(var p: integer;
|
||||||
FromPos, ToPos, DiffPos: integer);
|
IsStart: boolean; FromPos, ToPos, DiffPos: integer);
|
||||||
begin
|
begin
|
||||||
if (ToPos>FromPos) then begin
|
if (ToPos>FromPos) then begin
|
||||||
// replace
|
// replace
|
||||||
if p>FromPos then inc(p,DiffPos);
|
if p>FromPos then begin
|
||||||
|
if p>ToPos then
|
||||||
|
inc(p,DiffPos)
|
||||||
|
else
|
||||||
|
p:=FromPos;
|
||||||
|
end;
|
||||||
end else begin
|
end else begin
|
||||||
// insert
|
// insert
|
||||||
if p>=FromPos then inc(p,DiffPos);
|
if IsStart then begin
|
||||||
|
if p>=FromPos then inc(p,DiffPos);
|
||||||
|
end else begin
|
||||||
|
if p>FromPos then inc(p,DiffPos);
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -692,6 +701,11 @@ var
|
|||||||
NameStart: integer;
|
NameStart: integer;
|
||||||
LastDefineNode: TCodeTreeNode;
|
LastDefineNode: TCodeTreeNode;
|
||||||
LastIFNode: TCodeTreeNode;
|
LastIFNode: TCodeTreeNode;
|
||||||
|
NextSubNode: TCodeTreeNode;
|
||||||
|
EndNode: TCodeTreeNode;
|
||||||
|
InsertPos: LongInt;
|
||||||
|
NewSrc: String;
|
||||||
|
LastChildDefineNode: TCodeTreeNode;
|
||||||
begin
|
begin
|
||||||
Node:=Tree.Root;
|
Node:=Tree.Root;
|
||||||
while Node<>nil do begin
|
while Node<>nil do begin
|
||||||
@ -701,8 +715,10 @@ begin
|
|||||||
// an IF with a single test
|
// an IF with a single test
|
||||||
LastIFNode:=nil;
|
LastIFNode:=nil;
|
||||||
LastDefineNode:=nil;
|
LastDefineNode:=nil;
|
||||||
|
LastChildDefineNode:=nil;
|
||||||
SubNode:=Node.FirstChild;
|
SubNode:=Node.FirstChild;
|
||||||
while SubNode<>nil do begin
|
while SubNode<>nil do begin
|
||||||
|
NextSubNode:=SubNode.Next;
|
||||||
case SubNode.Desc of
|
case SubNode.Desc of
|
||||||
|
|
||||||
cdnIf, cdnElseIf:
|
cdnIf, cdnElseIf:
|
||||||
@ -729,14 +745,49 @@ begin
|
|||||||
or IFDEF then DEFINE
|
or IFDEF then DEFINE
|
||||||
-> remove define
|
-> remove define
|
||||||
}
|
}
|
||||||
end else begin
|
NextSubNode:=SubNode.NextSkipChilds;
|
||||||
|
DisableDefineNode(SubNode,Changed);
|
||||||
|
SubNode:=nil;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
if SubNode<>nil then begin
|
||||||
|
LastDefineNode:=SubNode;
|
||||||
|
LastIFNode:=nil;
|
||||||
|
if SubNode.Parent=Node then begin
|
||||||
|
// this define is valid for end of the IF block
|
||||||
|
LastChildDefineNode:=SubNode;
|
||||||
|
end else if (LastChildDefineNode<>nil)
|
||||||
|
and (LastChildDefineNode.SubDesc<>SubNode.SubDesc) then begin
|
||||||
|
// this sub define can cancel the higher level define
|
||||||
|
LastChildDefineNode:=nil;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
LastDefineNode:=SubNode;
|
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
SubNode:=SubNode.Next;
|
SubNode:=NextSubNode;
|
||||||
|
end;
|
||||||
|
|
||||||
|
if (LastDefineNode<>nil) and (LastIFNode=nil) then begin
|
||||||
|
(* this is
|
||||||
|
{$IFNDEF Name}
|
||||||
|
...
|
||||||
|
{$DEFINE Name}
|
||||||
|
... Name not used ...
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
|
or IFDEF and UNDEF
|
||||||
|
-> move define behind IF block
|
||||||
|
*)
|
||||||
|
EndNode:=Node;
|
||||||
|
while (EndNode<>nil) and (EndNode.Desc<>cdnEnd) do
|
||||||
|
EndNode:=EndNode.NextBrother;
|
||||||
|
if EndNode<>nil then begin
|
||||||
|
InsertPos:=FindLineEndOrCodeAfterPosition(Src,EndNode.EndPos,SrcLen,
|
||||||
|
NestedComments);
|
||||||
|
NewSrc:=LineEnding+GetDirective(LastDefineNode);
|
||||||
|
InsertDefine(InsertPos,NewSrc,LastDefineNode.SubDesc);
|
||||||
|
DisableDefineNode(LastDefineNode,Changed);
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
Node:=NextNode;
|
Node:=NextNode;
|
||||||
@ -966,11 +1017,13 @@ begin
|
|||||||
ParentNode:=FindNodeAtPos(Position);
|
ParentNode:=FindNodeAtPos(Position);
|
||||||
if ParentNode=nil then
|
if ParentNode=nil then
|
||||||
ParentNode:=Tree.Root;
|
ParentNode:=Tree.Root;
|
||||||
|
while (ParentNode<>Tree.Root) and (ParentNode.EndPos=Position) do
|
||||||
|
ParentNode:=ParentNode.Parent;
|
||||||
Result:=NodeMemManager.NewNode;
|
Result:=NodeMemManager.NewNode;
|
||||||
Result.Desc:=cdnDefine;
|
Result.Desc:=cdnDefine;
|
||||||
Result.SubDesc:=SubDesc;
|
Result.SubDesc:=SubDesc;
|
||||||
Result.StartPos:=Position;
|
Result.StartPos:=FindNextCompilerDirective(Src,Position,NestedComments);
|
||||||
Result.EndPos:=Result.StartPos+length(NewSrc);
|
Result.EndPos:=FindCommentEnd(Src,Result.StartPos,NestedComments);
|
||||||
NextBrotherNode:=ParentNode.FirstChild;
|
NextBrotherNode:=ParentNode.FirstChild;
|
||||||
while (NextBrotherNode<>nil) and (NextBrotherNode.StartPos<=Position) do
|
while (NextBrotherNode<>nil) and (NextBrotherNode.StartPos<=Position) do
|
||||||
NextBrotherNode:=NextBrotherNode.NextBrother;
|
NextBrotherNode:=NextBrotherNode.NextBrother;
|
||||||
@ -978,6 +1031,8 @@ begin
|
|||||||
Tree.AddNodeInFrontOf(NextBrotherNode,Result);
|
Tree.AddNodeInFrontOf(NextBrotherNode,Result);
|
||||||
end else begin
|
end else begin
|
||||||
Tree.AddNodeAsLastChild(ParentNode,Result);
|
Tree.AddNodeAsLastChild(ParentNode,Result);
|
||||||
|
if ParentNode.EndPos<Result.EndPos then
|
||||||
|
ParentNode.EndPos:=Result.EndPos;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -1791,8 +1846,8 @@ begin
|
|||||||
if DiffPos<>0 then begin
|
if DiffPos<>0 then begin
|
||||||
Node:=Tree.Root;
|
Node:=Tree.Root;
|
||||||
while Node<>nil do begin
|
while Node<>nil do begin
|
||||||
AdjustPositionAfterInsert(Node.StartPos,FromPos,ToPos,DiffPos);
|
AdjustPositionAfterInsert(Node.StartPos,true,FromPos,ToPos,DiffPos);
|
||||||
AdjustPositionAfterInsert(Node.EndPos,FromPos,ToPos,DiffPos);
|
AdjustPositionAfterInsert(Node.EndPos,false,FromPos,ToPos,DiffPos);
|
||||||
Node:=Node.Next;
|
Node:=Node.Next;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
@ -1838,10 +1893,10 @@ end;
|
|||||||
procedure TH2PasFunction.AdjustPositionsAfterInsert(FromPos, ToPos,
|
procedure TH2PasFunction.AdjustPositionsAfterInsert(FromPos, ToPos,
|
||||||
DiffPos: integer);
|
DiffPos: integer);
|
||||||
begin
|
begin
|
||||||
AdjustPositionAfterInsert(HeaderStart,FromPos,ToPos,DiffPos);
|
AdjustPositionAfterInsert(HeaderStart,true,FromPos,ToPos,DiffPos);
|
||||||
AdjustPositionAfterInsert(HeaderEnd,FromPos,ToPos,DiffPos);
|
AdjustPositionAfterInsert(HeaderEnd,false,FromPos,ToPos,DiffPos);
|
||||||
AdjustPositionAfterInsert(BeginStart,FromPos,ToPos,DiffPos);
|
AdjustPositionAfterInsert(BeginStart,true,FromPos,ToPos,DiffPos);
|
||||||
AdjustPositionAfterInsert(BeginEnd,FromPos,ToPos,DiffPos);
|
AdjustPositionAfterInsert(BeginEnd,false,FromPos,ToPos,DiffPos);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
@ -108,9 +108,10 @@ begin
|
|||||||
inc(Pass);
|
inc(Pass);
|
||||||
Changed:=false;
|
Changed:=false;
|
||||||
Tree.ReduceCompilerDirectives(Undefines,Defines,Changed);
|
Tree.ReduceCompilerDirectives(Undefines,Defines,Changed);
|
||||||
|
if not Changed then break;
|
||||||
writeln('-----------------------------------');
|
writeln('-----------------------------------');
|
||||||
writeln('after reduce number ',Pass,':');
|
writeln('after reduce number ',Pass,':');
|
||||||
Tree.WriteDebugReport;
|
Tree.WriteDebugReport;
|
||||||
until not Changed;
|
until false;
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user