-
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathDelphiAIDev.Utils.Context.pas
More file actions
75 lines (61 loc) · 1.57 KB
/
DelphiAIDev.Utils.Context.pas
File metadata and controls
75 lines (61 loc) · 1.57 KB
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
unit DelphiAIDev.Utils.Context;
interface
uses
System.SysUtils,
System.Classes,
ToolsAPI;
type
TUtilsContext = class
public
class function GetUnitInterfaceCode: string;
end;
implementation
class function TUtilsContext.GetUnitInterfaceCode: string;
const
ChunkSize = 1024;
var
LModule: IOTAModule;
LEditor: IOTASourceEditor;
LReader: IOTAEditReader;
LBuffer: array[0..ChunkSize - 1] of AnsiChar;
LPartStr: AnsiString;
LTotalText: AnsiString;
LPos: Integer;
LBytesRead: Integer;
LImplPos: Integer;
i: Integer;
begin
Result := '';
if not Assigned(BorlandIDEServices) then Exit;
LModule := (BorlandIDEServices as IOTAModuleServices).CurrentModule;
if not Assigned(LModule) then Exit;
LEditor := nil;
for i := 0 to LModule.GetModuleFileCount - 1 do
begin
if Supports(LModule.GetModuleFileEditor(i), IOTASourceEditor, LEditor) then
Break;
end;
if not Assigned(LEditor) then Exit;
LReader := LEditor.CreateReader;
if not Assigned(LReader) then Exit;
try
LPos := 0;
LTotalText := '';
repeat
LBytesRead := LReader.GetText(LPos, PAnsiChar(@LBuffer), ChunkSize);
if LBytesRead > 0 then
begin
SetString(LPartStr, PAnsiChar(@LBuffer), LBytesRead);
LTotalText := LTotalText + LPartStr;
Inc(LPos, LBytesRead);
end;
until LBytesRead < ChunkSize;
Result := String(LTotalText);
LImplPos := Pos('implementation', LowerCase(Result));
if LImplPos > 0 then
Result := Copy(Result, 1, LImplPos - 1);
finally
LReader := nil;
end;
end;
end.