mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-09-23 20:19:44 +02:00
Converter: Treat main and implementation uses sections separately. Patch from Stephano, issue #0017578.
git-svn-id: trunk@28099 -
This commit is contained in:
parent
b10f4d426b
commit
e1a6a2e960
@ -46,6 +46,8 @@ uses
|
|||||||
|
|
||||||
type
|
type
|
||||||
|
|
||||||
|
TUsesSection=(usMain, usImplementation);
|
||||||
|
|
||||||
{ TConvDelphiCodeTool }
|
{ TConvDelphiCodeTool }
|
||||||
|
|
||||||
TConvDelphiCodeTool = class
|
TConvDelphiCodeTool = class
|
||||||
@ -74,6 +76,8 @@ type
|
|||||||
|
|
||||||
function AddDelphiAndLCLSections: boolean;
|
function AddDelphiAndLCLSections: boolean;
|
||||||
function AddModeDelphiDirective: boolean;
|
function AddModeDelphiDirective: boolean;
|
||||||
|
procedure ConvAddDelphiAndLCLUnitsToUsesSection(AUsesSection: TUsesSection;
|
||||||
|
DelphiOnlyUnits, LCLOnlyUnits: TStringList);
|
||||||
function RenameResourceDirectives: boolean;
|
function RenameResourceDirectives: boolean;
|
||||||
function CommentOutUnits: boolean;
|
function CommentOutUnits: boolean;
|
||||||
function ReplaceFuncsInSource: boolean;
|
function ReplaceFuncsInSource: boolean;
|
||||||
@ -229,6 +233,81 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure TConvDelphiCodeTool.ConvAddDelphiAndLCLUnitsToUsesSection(
|
||||||
|
AUsesSection: TUsesSection; DelphiOnlyUnits, LCLOnlyUnits: TStringList);
|
||||||
|
var
|
||||||
|
AUsesNode: TCodeTreeNode;
|
||||||
|
i: Integer;
|
||||||
|
InsPos: Integer;
|
||||||
|
nl: string;
|
||||||
|
s: string;
|
||||||
|
DelphiOnlyUnitsStr, LclOnlyUnitsStr: string;
|
||||||
|
begin
|
||||||
|
if (LclOnlyUnits.Count=0) and (DelphiOnlyUnits.Count=0) then
|
||||||
|
exit;
|
||||||
|
DelphiOnlyUnitsStr:='';
|
||||||
|
for i:=0 to DelphiOnlyUnits.Count-1 do begin
|
||||||
|
if i<DelphiOnlyUnits.Count-1 then
|
||||||
|
DelphiOnlyUnitsStr:=DelphiOnlyUnitsStr+DelphiOnlyUnits[i]+', '
|
||||||
|
else
|
||||||
|
DelphiOnlyUnitsStr:=DelphiOnlyUnitsStr+DelphiOnlyUnits[i];
|
||||||
|
end;
|
||||||
|
LclOnlyUnitsStr:='';
|
||||||
|
for i:=0 to LclOnlyUnits.Count-1 do begin
|
||||||
|
if i<LclOnlyUnits.Count-1 then
|
||||||
|
LclOnlyUnitsStr:=LclOnlyUnitsStr+DelphiOnlyUnits[i]+', '
|
||||||
|
else
|
||||||
|
LclOnlyUnitsStr:=LclOnlyUnitsStr+DelphiOnlyUnits[i];
|
||||||
|
end;
|
||||||
|
fSrcCache.MainScanner:=fCodeTool.Scanner;
|
||||||
|
fCodeTool.BuildTree(AUsesSection=usMain);
|
||||||
|
case AUsesSection Of
|
||||||
|
usMain: AUsesNode:=fCodeTool.FindMainUsesSection;
|
||||||
|
usImplementation: AUsesNode:=fCodeTool.FindImplementationUsesSection;
|
||||||
|
end;
|
||||||
|
nl:=fSrcCache.BeautifyCodeOptions.LineEnd;
|
||||||
|
if AUsesNode<>nil then begin
|
||||||
|
//uses section exists
|
||||||
|
s:='{$IFNDEF FPC}'+nl;
|
||||||
|
if DelphiOnlyUnits.Count>=1 then
|
||||||
|
s:=s+' '+DelphiOnlyUnitsStr+','+nl;
|
||||||
|
s:=s+'{$ELSE}'+nl;
|
||||||
|
if LclOnlyUnits.Count>=1 then
|
||||||
|
s:=s+' '+LclOnlyUnitsStr+','+nl;
|
||||||
|
s:=s+'{$ENDIF}'+nl;
|
||||||
|
s:=s+' ';
|
||||||
|
//TODO: check for special units
|
||||||
|
fCodeTool.MoveCursorToUsesStart(AUsesNode);
|
||||||
|
InsPos:=fCodeTool.CurPos.StartPos;
|
||||||
|
end
|
||||||
|
else begin
|
||||||
|
//uses section does not exist
|
||||||
|
s:=nl;
|
||||||
|
s:=s+'{$IFNDEF FPC}'+nl;
|
||||||
|
if DelphiOnlyUnits.Count>=1 then begin
|
||||||
|
s:=s+'uses'+nl;
|
||||||
|
s:=s+' '+DelphiOnlyUnitsStr+';'+nl;
|
||||||
|
end;
|
||||||
|
s:=s+'{$ELSE}'+nl;
|
||||||
|
if LclOnlyUnits.Count>=1 then begin
|
||||||
|
s:=s+'uses'+nl;
|
||||||
|
s:=s+' '+LclOnlyUnitsStr+';'+nl;
|
||||||
|
end;
|
||||||
|
s:=s+'{$ENDIF}';
|
||||||
|
case AUsesSection Of
|
||||||
|
usMain: AUsesNode:=fCodeTool.FindInterfaceNode;
|
||||||
|
usImplementation: AUsesNode:=fCodeTool.FindImplementationNode;
|
||||||
|
end;
|
||||||
|
// set insert position behind interface or implementation keyword
|
||||||
|
// TODO: what about program?
|
||||||
|
fCodeTool.MoveCursorToNodeStart(AUsesNode);
|
||||||
|
fCodeTool.ReadNextAtom;
|
||||||
|
InsPos:=fCodeTool.FindLineEndOrCodeAfterPosition(fCodeTool.CurPos.EndPos,false);
|
||||||
|
end;
|
||||||
|
// Now add the generated lines.
|
||||||
|
if not fSrcCache.Replace(gtNewLine,gtNone,InsPos,InsPos,s) then exit;
|
||||||
|
end;
|
||||||
|
|
||||||
function TConvDelphiCodeTool.AddDelphiAndLCLSections: boolean;
|
function TConvDelphiCodeTool.AddDelphiAndLCLSections: boolean;
|
||||||
// Add unit names into conditional blocks for Delphi and Lazarus targets. If the name
|
// Add unit names into conditional blocks for Delphi and Lazarus targets. If the name
|
||||||
// would otherwise be deleted or commented out, now it is added to Delphi block.
|
// would otherwise be deleted or commented out, now it is added to Delphi block.
|
||||||
@ -237,18 +316,23 @@ var
|
|||||||
LclOnlyUnits: TStringList; // LCL specific units.
|
LclOnlyUnits: TStringList; // LCL specific units.
|
||||||
MainUsesNode, ImplementationUsesNode: TCodeTreeNode;
|
MainUsesNode, ImplementationUsesNode: TCodeTreeNode;
|
||||||
|
|
||||||
procedure ConvUsesUnits(AUsesNode: TCodeTreeNode; AUsesUnits: TStringList);
|
procedure ConvUsesUnits(AUsesSection: TUsesSection; AUsesUnits: TStringList);
|
||||||
var
|
var
|
||||||
i, ind: Integer;
|
i, ind: Integer;
|
||||||
InsPos: Integer;
|
|
||||||
nl: string;
|
|
||||||
s: string;
|
s: string;
|
||||||
RenameList: TStringList;
|
RenameList: TStringList;
|
||||||
|
AUsesNode: TCodeTreeNode;
|
||||||
begin
|
begin
|
||||||
DelphiOnlyUnits.Clear;
|
DelphiOnlyUnits.Clear;
|
||||||
LCLOnlyUnits.Clear;
|
LCLOnlyUnits.Clear;
|
||||||
fCodeTool.MoveCursorToUsesStart(AUsesNode);
|
fSrcCache.MainScanner:=fCodeTool.Scanner;
|
||||||
InsPos:=fCodeTool.CurPos.StartPos;
|
fCodeTool.BuildTree(AUsesSection=usMain);
|
||||||
|
case AUsesSection Of
|
||||||
|
usMain: AUsesNode:=fCodeTool.FindMainUsesSection;
|
||||||
|
usImplementation: AUsesNode:=fCodeTool.FindImplementationUsesSection;
|
||||||
|
end;
|
||||||
|
if AUsesNode=nil then
|
||||||
|
exit;
|
||||||
// Don't remove the unit names but add to Delphi block instead.
|
// Don't remove the unit names but add to Delphi block instead.
|
||||||
for i:=0 to fUnitsToRemove.Count-1 do begin
|
for i:=0 to fUnitsToRemove.Count-1 do begin
|
||||||
s:=fUnitsToRemove[i];
|
s:=fUnitsToRemove[i];
|
||||||
@ -280,21 +364,8 @@ var
|
|||||||
finally
|
finally
|
||||||
RenameList.Free;
|
RenameList.Free;
|
||||||
end;
|
end;
|
||||||
{ TODO : handling of one used unit in one line is not functional yet
|
// Add LCL and Delphi sections for output.
|
||||||
ex: uses consts;}
|
ConvAddDelphiAndLCLUnitsToUsesSection(AUsesSection, DelphiOnlyUnits, LclOnlyUnits);
|
||||||
if (LclOnlyUnits.Count>0) or (DelphiOnlyUnits.Count>0) then begin
|
|
||||||
// Add LCL and Delphi sections for output.
|
|
||||||
nl:=fSrcCache.BeautifyCodeOptions.LineEnd;
|
|
||||||
s:='{$IFNDEF FPC}'+nl+' ';
|
|
||||||
for i:=0 to DelphiOnlyUnits.Count-1 do
|
|
||||||
s:=s+DelphiOnlyUnits[i]+', ';
|
|
||||||
s:=s+nl+'{$ELSE}'+nl+' ';
|
|
||||||
for i:=0 to LclOnlyUnits.Count-1 do
|
|
||||||
s:=s+LclOnlyUnits[i]+', ';
|
|
||||||
s:=s+nl+'{$ENDIF}';
|
|
||||||
// Now add the generated lines.
|
|
||||||
if not fSrcCache.Replace(gtEmptyLine,gtNewLine,InsPos,InsPos,s) then exit;
|
|
||||||
end;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
@ -302,16 +373,10 @@ begin
|
|||||||
DelphiOnlyUnits:=TStringList.Create;
|
DelphiOnlyUnits:=TStringList.Create;
|
||||||
LclOnlyUnits:=TStringList.Create;
|
LclOnlyUnits:=TStringList.Create;
|
||||||
try
|
try
|
||||||
fCodeTool.BuildTree(false);
|
|
||||||
fSrcCache.MainScanner:=fCodeTool.Scanner;
|
|
||||||
// Main uses section
|
// Main uses section
|
||||||
MainUsesNode:=fCodeTool.FindMainUsesSection;
|
ConvUsesUnits(usMain, fExistingUsesMain);
|
||||||
if MainUsesNode<>nil then
|
|
||||||
ConvUsesUnits(MainUsesNode, fExistingUsesMain);
|
|
||||||
// Implementation uses section
|
// Implementation uses section
|
||||||
ImplementationUsesNode:=fCodeTool.FindImplementationUsesSection;
|
ConvUsesUnits(usImplementation, fExistingUsesImplementation);
|
||||||
if ImplementationUsesNode<>nil then
|
|
||||||
ConvUsesUnits(ImplementationUsesNode, fExistingUsesImplementation);
|
|
||||||
Result:=true;
|
Result:=true;
|
||||||
finally
|
finally
|
||||||
LclOnlyUnits.Free;
|
LclOnlyUnits.Free;
|
||||||
|
Loading…
Reference in New Issue
Block a user