codetools: fixed commenting units with directives

git-svn-id: trunk@8993 -
This commit is contained in:
mattias 2006-03-22 20:45:37 +00:00
parent dac3215c52
commit 9f07b81e81
3 changed files with 52 additions and 27 deletions

View File

@ -39,6 +39,8 @@ var
Code: TCodeBuffer; Code: TCodeBuffer;
Filename: String; Filename: String;
MissingUnits: TStrings; MissingUnits: TStrings;
ReplaceUnits: TStringList;
MissingIncludeFilesCodeXYPos: TFPList;
begin begin
// setup the Options // setup the Options
Options:=TCodeToolsOptions.Create; Options:=TCodeToolsOptions.Create;
@ -72,8 +74,20 @@ begin
raise Exception.Create('unable to read '+Filename); raise Exception.Create('unable to read '+Filename);
// fix the filenames in the include directives // fix the filenames in the include directives
if not CodeToolBoss.FixIncludeFilenames(Code,true) then MissingIncludeFilesCodeXYPos:=nil;
if not CodeToolBoss.FixIncludeFilenames(Code,true,
MissingIncludeFilesCodeXYPos)
then
raise Exception.Create('unable to fix include filesnames in '+Filename+' '+CodeToolBoss.ErrorMessage); raise Exception.Create('unable to fix include filesnames in '+Filename+' '+CodeToolBoss.ErrorMessage);
CodeToolBoss.FreeListOfPCodeXYPosition(MissingIncludeFilesCodeXYPos);
// replace some unit names
ReplaceUnits:=TStringList.Create;
ReplaceUnits.Add('biglettersunit=SysUtils');
ReplaceUnits.Free;
writeln('==================================================================');
writeln(Code.Source);
// fix the unitnames in the uses section // fix the unitnames in the uses section
MissingUnits:=nil; MissingUnits:=nil;
@ -84,9 +98,12 @@ begin
writeln('MissingUnits=',MissingUnits.Text); writeln('MissingUnits=',MissingUnits.Text);
if not CodeToolBoss.CommentUnitsInUsesSections(Code,MissingUnits) then if not CodeToolBoss.CommentUnitsInUsesSections(Code,MissingUnits) then
raise Exception.Create('unable to comment units in uses section in '+Filename+' '+CodeToolBoss.ErrorMessage); raise Exception.Create('unable to comment units in uses section in '+Filename+' '+CodeToolBoss.ErrorMessage);
MissingUnits.Free;
end; end;
writeln('=================================================================='); writeln('==================================================================');
writeln(Code.Source); writeln(Code.Source);
Options.Free;
end. end.

View File

@ -10,7 +10,12 @@ uses
biglettersunit, // must be fixed to BigLettersUnit biglettersunit, // must be fixed to BigLettersUnit
biglettersunit in 'biglettersunit.pas',// -> BigLettersUnit.pas biglettersunit in 'biglettersunit.pas',// -> BigLettersUnit.pas
biglettersunit in '..\ScanExamples\biglettersunit.pas',// -> ../scanexamples/BigLettersUnit biglettersunit in '..\ScanExamples\biglettersunit.pas',// -> ../scanexamples/BigLettersUnit
NonExistingUnit1, NonExistingUnit2, SysUtils, NonExistingUnit3; NonExistingUnit1, NonExistingUnit2, SysUtils, NonExistingUnit3,
{$IFDEF FPC}
NonExistingUnit4
{$ELSE}
NonExistingUnit5
{$ENDIF};
{$I BROKENincfiles.inc}// must be fixed to brokenincfiles.inc {$I BROKENincfiles.inc}// must be fixed to brokenincfiles.inc
{$I ../ScanExamples/BROKENincfiles.inc}// must be fixed to ../scanexamples/brokenincfiles.inc {$I ../ScanExamples/BROKENincfiles.inc}// must be fixed to ../scanexamples/brokenincfiles.inc

View File

@ -1000,36 +1000,39 @@ function TStandardCodeTool.CommentUnitsInUsesSections(MissingUnits: TStrings;
procedure Comment(CommentStartPos, CommentEndPos: integer); procedure Comment(CommentStartPos, CommentEndPos: integer);
var var
i: LongInt; i: LongInt;
CurStartPos: LongInt;
CommentNeeded: Boolean;
CurEndPos: LongInt;
begin begin
if CommentStartPos>=CommentEndPos then if CommentStartPos>=CommentEndPos then
RaiseException('TStandardCodeTool Comment'); RaiseException('TStandardCodeTool Comment');
// try curly brackets {} // comment with curly brackets {}
i:=CommentStartPos; i:=CommentStartPos;
while (i<CommentEndPos) and (Src[i]<>'}') do inc(i); CurStartPos:=CommentStartPos;
if i=CommentEndPos then begin CurEndPos:=CurStartPos;
SourceChangeCache.Replace(gtNone,gtNone,CommentStartPos,CommentStartPos,'{'); CommentNeeded:=false;
SourceChangeCache.Replace(gtNone,gtNone,CommentEndPos,CommentEndPos,'}'); repeat
//debugln('Comment {} "',copy(Src,CommentStartPos,CommentEndPos-CommentStartPos)); if (Src[i]='{') or (i>=CommentEndPos) then begin
exit; // the area contains a comment -> comment in front
end; if CommentNeeded then begin
// try (* *) SourceChangeCache.Replace(gtNone,gtNone,CurStartPos,CurStartPos,'{');
i:=CommentStartPos; SourceChangeCache.Replace(gtNone,gtNone,CurEndPos,CurEndPos,'}');
while (i<CommentEndPos-1) and ((Src[i]<>'*') or (Src[i+1]<>')')) do inc(i); //DebugLn('Comment "',copy(Src,CurStartPos,i-CurStartPos),'"');
if i=CommentEndPos-1 then begin CommentNeeded:=false;
SourceChangeCache.Replace(gtNone,gtNone,CommentStartPos,CommentStartPos,'(*'); end;
SourceChangeCache.Replace(gtNone,gtNone,CommentEndPos,CommentEndPos,'*)'); if i>=CommentEndPos then break;
//debugln('Comment (**) "',copy(Src,CommentStartPos,CommentEndPos-CommentStartPos)); // skip comment
exit; i:=FindCommentEnd(Src,i,Scanner.NestedComments);
end; end else if not IsSpaceChar[Src[i]] then begin
// do it with // comments if not CommentNeeded then begin
SourceChangeCache.Replace(gtNone,gtNone,CommentStartPos,CommentStartPos,'//'); CurStartPos:=i;
SourceChangeCache.Replace(gtNone,gtNewLine,CommentEndPos,CommentEndPos,' '); CommentNeeded:=true;
//debugln('Comment // "',copy(Src,CommentStartPos,CommentEndPos-CommentStartPos)); end;
for i:=CommentStartPos+1 to CommentEndPos-1 do CurEndPos:=i+1;
if (Src[i-1] in [#10,#13]) and (not (Src[i] in [#10,#13])) then begin
SourceChangeCache.Replace(gtNone,gtNone,i,i,'//');
end; end;
inc(i);
until false;
end; end;
function CommentUnitsInUsesSection(UsesNode: TCodeTreeNode): boolean; function CommentUnitsInUsesSection(UsesNode: TCodeTreeNode): boolean;