Jedi code format: improved fake unit generator. Related to issue #40538

This commit is contained in:
DomingoGP 2023-10-08 16:54:55 +02:00
parent 8a98df0369
commit 7cdecac783
2 changed files with 35 additions and 8 deletions

View File

@ -434,6 +434,7 @@ var
sourceCodeLowerCase: string;
lcStartIndex, lcEndIndex: integer;
hasInterface, hasImplementation: boolean;
liInterfacePos,liImplementationPos:integer;
procedure AddFakeUnit;
begin
@ -441,17 +442,39 @@ var
end;
procedure AddFakeInterface;
var
liUsesPos:integer;
begin
sourceCode := sourceCode + 'interface{:*_*:}' + #10;
sourceCode := sourceCode + 'type' + #10; // if there is only a class selected this is required
sourceCode := sourceCode + 'faketjcfifc=' + END_MARK_INTERFACE + #10;
liUsesPos:=PosEx('uses',sourceCodeLowerCase,1);
if (liUsesPos>0) and (liImplementationPos>0) and (liUsesPos<liImplementationPos)
and (length(sourceCodeLowerCase)>=liUsesPos+4) and CharIsWhiteSpace(sourceCodeLowerCase[liUsesPos+4]) then
begin
sourceCode := sourceCode + '// ' + END_MARK_INTERFACE + #10;
end
else
begin
sourceCode := sourceCode + 'type' + #10; // if there is only a class selected this is required
sourceCode := sourceCode + 'faketjcfifc=' + END_MARK_INTERFACE + #10;
end;
end;
procedure AddFakeImplementation;
var
liUsesPos:integer;
begin
sourceCode := sourceCode + 'implementation{:*_*:}' + #10;
sourceCode := sourceCode + 'type' + #10;
sourceCode := sourceCode + 'faketjcfimpl=' + END_MARK_IMPLEMENTATION + #10;
liUsesPos:=PosEx('uses',sourceCodeLowerCase,1);
if ((not hasInterface) and (not hasImplementation)) and (liUsesPos>0)
and (length(sourceCodeLowerCase)>=liUsesPos+4) and CharIsWhiteSpace(sourceCodeLowerCase[liUsesPos+4]) then
begin
sourceCode := sourceCode + '// ' + END_MARK_IMPLEMENTATION + #10;
end
else
begin
sourceCode := sourceCode + 'type' + #10;
sourceCode := sourceCode + 'faketjcfimpl=' + END_MARK_IMPLEMENTATION + #10;
end;
end;
procedure AddFakeEnd;
@ -462,8 +485,8 @@ var
begin
//WRAPPING the inputCode in a fake unit
sourceCodeLowerCase := LowerCase(fsInputCode);
hasInterface := HasStringAtLineStart(sourceCodeLowerCase, 'interface');
hasImplementation := HasStringAtLineStart(sourceCodeLowerCase, 'implementation');
hasInterface := HasStringAtLineStart(sourceCodeLowerCase, 'interface', liInterfacePos);
hasImplementation := HasStringAtLineStart(sourceCodeLowerCase, 'implementation', liImplementationPos);
sourceCode := '';
AddFakeUnit;
if hasInterface = False then

View File

@ -154,7 +154,7 @@ procedure FindLineOffsets(const aStr: string; aLineStart, aLineEnd: integer;
out aLineStartOffset: integer; out aLineEndOffset:integer);
function SkipLeftSpaces(const aStr: string; aPos: integer): integer;
function SkipToNextLine(const aStr: string; aPos: integer): integer;
function HasStringAtLineStart(const aSourceCode: string; const aStr: string): boolean;
function HasStringAtLineStart(const aSourceCode: string; const aStr: string;var aStringPos:integer): boolean;
function StrTrimLastEndOfLine(const aStr:string):string;
// string starts with LF, CR, ignores prior spaces
function StrStartsWithLineEnd(const aStr:string):boolean;
@ -660,11 +660,12 @@ begin
Result := aPos;
end;
function HasStringAtLineStart(const aSourceCode: string; const aStr: string): boolean;
function HasStringAtLineStart(const aSourceCode: string; const aStr: string;var aStringPos:integer): boolean;
var
index, stringStart: integer;
begin
index := 1;
aStringPos := -1;
while (index > 0) and (index < length(aSourceCode)) do
begin
stringStart := PosEx(aStr, aSourceCode, index);
@ -672,7 +673,10 @@ begin
begin
index := SkipLeftSpaces(aSourceCode, stringStart);
if (index > 0) and ((index = 1) or (aSourceCode[index] in [#10, #13])) then
begin
aStringPos:=stringStart;
exit(True);
end;
index := stringStart + length(aStr);
end
else