forked from groehner/JavaEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUStructure.pas
More file actions
195 lines (169 loc) · 5.03 KB
/
UStructure.pas
File metadata and controls
195 lines (169 loc) · 5.03 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
unit UStructure;
interface
uses Classes, Types;
const EndStructureWidth = 10;
type
(*
This could be optimized by making the structure a record instead of a
full fledged class. I opted for the class because I was thinking I may
add some additional features later on that would be more maintainable
with the structure as a class element. Some optimization of class versus
record was achieved by pooling the structure classes instead of creating
new objects each time.
TextRect is used to hold the start and end text positions where the structure
line should be drawn. GraphicRect is where the position should be drawn in
graphical coordinates. TextRect is only updated if the a change is made to the text
and is updated in Build Structure. GraphicRect is updated on each paint operation
since we always need correct graphical coordinates to determine how lines
should be invalidated. For example, when InvalidateRect is called the invalidation
rectangle may need to be increased so the entire structure line can be repainted.
*)
TStructureEx = class
public
FDepth: Integer;
FTopBottom: Integer;
FTopBottomRight: Integer;
FBottomTop: Integer;
FGraphicRect: TRect;
FTextRect: TRect;
FFoldedTextRect: TRect;
FSingleStatement: Boolean;
FHidden: Boolean;
function ToString: string; override;
function Clone: TStructureEx;
property GraphicRect: TRect read FGraphicRect write FGraphicRect;
property TextRect: TRect read FTextRect write FTextRect;
property FoldedTextRect: TRect read FFoldedTextRect write FFoldedTextRect;
property Hidden: Boolean read FHidden write FHidden;
property SingleStatement: Boolean read FSingleStatement write FSingleStatement;
procedure DecTopBottom;
procedure DecBottom;
end;
TStructures = class
private
FList: TList;
FPooled: TList;
FMaxDepth: Integer;
function GetCount: Integer;
function GetStructure(Index: Integer): TStructureEx;
public
constructor Create;
destructor Destroy; override;
function NewStructure: TStructureEx;
procedure Add(Value: TStructureEx);
procedure Delete(Index: Integer);
procedure Clear;
function ToString: string; override;
function Clone: TStructures;
property Count: Integer read GetCount;
property Structure[Index: Integer]: TStructureEx read GetStructure; default;
property MaxDepth: Integer read FMaxDepth write FMaxDepth;
end;
implementation
uses SysUtils;
function TStructureEx.ToString: string;
begin
var Str:= 'L:' + IntToStr(FTextRect.Left) + ' T:' + IntToStr(FTextRect.Top) +
' R:' + IntToStr(FTextRect.Right) + ' B:' + IntToStr(FTextRect.Bottom) +
' De:' + IntToStr(FDepth);
Result:= Str;
end;
function TStructureEx.Clone: TStructureEx;
begin
Result:= TStructureEx.Create;
Result.FDepth:= FDepth;
Result.FTopBottom:= FTopBottom;
Result.FTopBottomRight:= FTopBottomRight;
Result.FBottomTop:= FBottomTop;
Result.FGraphicRect:= FGraphicRect;
Result.FTextRect:= FTextRect;
Result.FSingleStatement:= FSingleStatement;
Result.FoldedTextRect:= FFoldedTextRect;
Result.Hidden:= FHidden;
end;
procedure TStructureEx.DecTopBottom;
begin
Dec(FFoldedTextRect.Top);
Dec(FFoldedTextRect.Bottom);
end;
procedure TStructureEx.DecBottom;
begin
Dec(FFoldedTextRect.Bottom);
end;
{ TStructures }
constructor TStructures.Create;
begin
FList := TList.Create;
FPooled:= TList.Create;
end;
destructor TStructures.Destroy;
var struct: TStructureEx;
begin
Clear;
while FList.Count > 0 do begin
struct:= TStructureEx(FList[0]);
FreeAndNil(struct);
FList.Delete(0);
end;
FreeAndNil(FList);
while FPooled.Count > 0 do begin
struct:= TStructureEx(FPooled[0]);
FreeAndNil(struct);
FPooled.Delete(0);
end;
FreeAndNil(FPooled);
end;
procedure TStructures.Delete(Index: Integer);
begin
FPooled.Add(Structure[Index]);
FList.Delete(Index);
end;
procedure TStructures.Add(Value: TStructureEx);
begin
FList.Add(Value);
end;
function TStructures.GetCount: Integer;
begin
Result:= FList.Count
end;
function TStructures.GetStructure(Index: Integer): TStructureEx;
begin
if (0 <= Index) and (Index < FList.Count)
then Result:= TStructureEx(FList[Index])
else Result:= nil;
end;
procedure TStructures.Clear;
begin
while FList.Count > 0 do
Delete(0);
end;
function TStructures.NewStructure: TStructureEx;
begin
if FPooled.Count > 0 then begin
Result:= TStructureEx(FPooled[FPooled.Count-1]);
FPooled.Delete(FPooled.Count-1);
end
else
Result:= TStructureEx.Create;
end;
function TStructures.ToString: string;
begin
var Str:= '';
for var Int := 0 to FList.Count-1 do begin
var aStructure:= GetStructure(Int);
if Assigned(aStructure)
then Str:= Str + aStructure.ToString + #13#10
else Str:= Str + '<structure index error>' + #13#10
end;
Result:= Str;
end;
function TStructures.Clone: TStructures;
begin
Result:= TStructures.Create;
for var Int:= 0 to Count - 1 do begin
var aStructure:= getStructure(Int);
if Assigned(aStructure) then
Result.Add(aStructure.Clone);
end;
end;
end.