-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcFilePersist.pas
369 lines (342 loc) · 13.3 KB
/
cFilePersist.pas
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
unit cFilePersist;
{-----------------------------------------------------------------------------
Unit Name: cFilePersist
Author: Kiriakos Vlahos
Date: 09-Mar-2006
Purpose: Support class for editor file persistence
History:
-----------------------------------------------------------------------------}
interface
Uses
Classes, SysUtils, Contnrs, JvAppStorage, uEditAppIntfs, dlgSynEditOptions,
Controls;
Type
TBookMarkInfo = class(TPersistent)
private
fLine, fChar, fBookmarkNumber : integer;
published
property Line : integer read fLine write fLine;
property Char : integer read fChar write fChar;
property BookmarkNumber : integer read fBookmarkNumber write fBookmarkNumber;
end;
TFilePersistInfo = class (TInterfacedPersistent, IJvAppStorageHandler)
// For storage/loading of a file's persistent info
private
TabControlIndex : integer;
Line, Char, TopLine : integer;
BreakPoints : TObjectList;
BookMarks : TObjectList;
FileName : string;
Highlighter : string;
EditorOptions : TSynEditorOptionsContainer;
SecondEditorVisible : Boolean;
SecondEditorAlign : TAlign;
SecondEditorSize : integer;
EditorOptions2 : TSynEditorOptionsContainer;
protected
// IJvAppStorageHandler implementation
procedure ReadFromAppStorage(AppStorage: TJvCustomAppStorage; const BasePath: string); virtual;
procedure WriteToAppStorage(AppStorage: TJvCustomAppStorage; const BasePath: string); virtual;
function CreateListItem(Sender: TJvCustomAppStorage; const Path: string;
Index: Integer): TPersistent;
public
constructor Create;
constructor CreateFromEditor(Editor : IEditor);
destructor Destroy; override;
end;
TPersistFileInfo = class
// Stores/loads open editor file information through the class methods
// WriteToAppStorage and ReadFromAppStorage
private
fFileInfoList : TObjectList;
function CreateListItem(Sender: TJvCustomAppStorage; const Path: string;
Index: Integer): TPersistent;
public
constructor Create;
destructor Destroy; override;
procedure GetFileInfo;
class procedure WriteToAppStorage(AppStorage : TJvCustomAppStorage; Path : String);
class procedure ReadFromAppStorage(AppStorage : TJvCustomAppStorage; Path : String);
end;
TTabsPersistInfo = class (TInterfacedPersistent, IJvAppStorageHandler)
procedure ReadFromAppStorage(AppStorage: TJvCustomAppStorage; const BasePath: string); virtual;
procedure WriteToAppStorage(AppStorage: TJvCustomAppStorage; const BasePath: string); virtual;
end;
Var
TabsPersistsInfo : TTabsPersistInfo; // Singleton
implementation
uses
cPyBaseDebugger, frmPyIDEMain, SynEditTypes, dmCommands, uHighlighterProcs,
SynEdit, SpTBXTabs, TB2Item, Math;
{ TFilePersistInfo }
constructor TFilePersistInfo.Create;
begin
BreakPoints := TObjectList.Create(True);
BookMarks := TObjectList.Create(True);
EditorOptions := TSynEditorOptionsContainer.Create(nil);
EditorOptions2 := TSynEditorOptionsContainer.Create(nil);
SecondEditorAlign := alRight;
end;
procedure TFilePersistInfo.WriteToAppStorage(AppStorage: TJvCustomAppStorage;
const BasePath: string);
var
IgnoreProperties : TStringList;
begin
AppStorage.WriteString(BasePath+'\FileName', FileName);
AppStorage.WriteInteger(BasePath+'\TabControlIndex', TabControlIndex);
AppStorage.WriteInteger(BasePath+'\Line', Line);
AppStorage.WriteInteger(BasePath+'\Char', Char);
AppStorage.WriteInteger(BasePath+'\TopLine', TopLine);
AppStorage.WriteString(BasePath+'\Highlighter', Highlighter);
AppStorage.WriteObjectList(BasePath+'\BreakPoints', BreakPoints, 'BreakPoint');
AppStorage.WriteObjectList(BasePath+'\BookMarks', BookMarks, 'BookMarks');
AppStorage.WriteBoolean(BasePath+'\SecondEditorVisible', SecondEditorVisible);
IgnoreProperties := TStringList.Create;
try
IgnoreProperties.Add('Keystrokes');
AppStorage.WritePersistent(BasePath+'\Editor Options', EditorOptions,
True, IgnoreProperties);
if SecondEditorVisible then begin
AppStorage.WriteEnumeration(BasePath+'\Second Editor Align', TypeInfo(TAlign), SecondEditorAlign);
AppStorage.WriteInteger(BasePath+'Second Editor Size', SecondEditorSize);
AppStorage.WritePersistent(BasePath+'\Second Editor Options', EditorOptions2,
True, IgnoreProperties);
end;
finally
IgnoreProperties.Free;
end;
end;
procedure TFilePersistInfo.ReadFromAppStorage(AppStorage: TJvCustomAppStorage;
const BasePath: string);
begin
FileName := AppStorage.ReadString(BasePath+'\FileName');
TabControlIndex := AppStorage.ReadInteger(BasePath+'\TabControlIndex', 1);
Line := AppStorage.ReadInteger(BasePath+'\Line');
Char := AppStorage.ReadInteger(BasePath+'\Char');
TopLine := AppStorage.ReadInteger(BasePath+'\TopLine');
Highlighter := AppStorage.ReadString(BasePath+'\Highlighter', Highlighter);
AppStorage.ReadObjectList(BasePath+'\BreakPoints', BreakPoints, CreateListItem, True, 'BreakPoint');
AppStorage.ReadObjectList(BasePath+'\BookMarks', BookMarks, CreateListItem, True, 'BookMarks');
EditorOptions.Assign(CommandsDataModule.EditorOptions);
AppStorage.ReadPersistent(BasePath+'\Editor Options', EditorOptions, True, True);
SecondEditorVisible := AppStorage.ReadBoolean(BasePath+'\SecondEditorVisible', False);
if SecondEditorVisible then begin
AppStorage.ReadEnumeration(BasePath+'\Second Editor Align', TypeInfo(TAlign),
SecondEditorAlign, SecondEditorAlign);
SecondEditorSize := AppStorage.ReadInteger(BasePath+'Second Editor Size');
EditorOptions2.Assign(CommandsDataModule.EditorOptions);
AppStorage.ReadPersistent(BasePath+'\Second Editor Options', EditorOptions2, True, True);
end;
end;
destructor TFilePersistInfo.Destroy;
begin
BreakPoints.Free;
BookMarks.Free;
EditorOptions.Free;
EditorOptions2.Free;
inherited;
end;
function TFilePersistInfo.CreateListItem(Sender: TJvCustomAppStorage;
const Path: string; Index: Integer): TPersistent;
begin
if Pos('BreakPoint', Path) > 0 then
Result := TBreakPoint.Create
else if Pos('BookMark', Path) > 0 then
Result := TBookMarkInfo.Create
else
Result := nil;
end;
constructor TFilePersistInfo.CreateFromEditor(Editor: IEditor);
Var
i : integer;
BookMark : TBookMarkInfo;
BreakPoint : TBreakPoint;
begin
Create;
FileName := Editor.FileName;
TabControlIndex := Editor.TabControlIndex;
Char := Editor.SynEdit.CaretX;
Line := Editor.SynEdit.CaretY;
TopLine := Editor.Synedit.TopLine;
if Assigned(Editor.SynEdit.Highlighter) then
Highlighter := Editor.SynEdit.Highlighter.FriendlyLanguageName;
if Assigned(Editor.SynEdit.Marks) then
for i := 0 to Editor.SynEdit.Marks.Count - 1 do
if Editor.SynEdit.Marks[i].IsBookmark then with Editor.SynEdit.Marks[i] do
begin
BookMark := TBookMarkInfo.Create;
BookMark.fChar := Char;
BookMark.fLine := Line;
BookMark.fBookmarkNumber := BookmarkNumber;
BookMarks.Add(BookMark);
end;
for i := 0 to Editor.BreakPoints.Count - 1 do begin
BreakPoint := TBreakPoint.Create;
with TBreakPoint(Editor.BreakPoints[i]) do begin
BreakPoint.LineNo := LineNo;
BreakPoint.Disabled := Disabled;
BreakPoint.Condition := Condition;
BreakPoints.Add(BreakPoint);
end;
end;
EditorOptions.Assign(Editor.SynEdit);
SecondEditorVisible := Editor.SynEdit2.Visible;
if SecondEditorVisible then begin
SecondEditorAlign := Editor.SynEdit2.Align;
SecondEditorSize := IfThen(SecondEditorAlign = alRight,
Editor.SynEdit2.Width, Editor.SynEdit2.Height);
EditorOptions2.Assign(Editor.SynEdit2);
end;
end;
{ TPersistFileInfo }
constructor TPersistFileInfo.Create;
begin
fFileInfoList := TObjectList.Create(True);
end;
class procedure TPersistFileInfo.ReadFromAppStorage(
AppStorage: TJvCustomAppStorage; Path : String);
Var
PersistFileInfo : TPersistFileInfo;
FilePersistInfo : TFilePersistInfo;
Editor : IEditor;
i, j : integer;
FName : string;
begin
PersistFileInfo := TPersistFileInfo.Create;
try
AppStorage.ReadObjectList(Path, PersistFileInfo.fFileInfoList,
PersistFileInfo.CreateListItem, True, 'File');
for i := 0 to PersistFileInfo.fFileInfoList.Count - 1 do begin
FilePersistInfo := TFilePersistInfo(PersistFileInfo.fFileInfoList[i]);
if FileExists(FilePersistInfo.FileName) then
Editor := PyIDEMainForm.DoOpenFile(FilePersistInfo.FileName, '',
FilePersistInfo.TabControlIndex);
if Assigned(Editor) then begin
Editor.SynEdit.TopLine := FilePersistInfo.TopLine;
Editor.SynEdit.CaretXY := BufferCoord(FilePersistInfo.Char, FilePersistInfo.Line);
for j := 0 to FilePersistInfo.BreakPoints.Count - 1 do
with TBreakPoint(FilePersistInfo.BreakPoints[j]) do
PyControl.SetBreakPoint(FilePersistInfo.FileName,
LineNo, Disabled, Condition);
for j := 0 to FilePersistInfo.BookMarks.Count - 1 do
with TBookMarkInfo(FilePersistInfo.BookMarks[j]) do
Editor.SynEdit.SetBookMark(BookMarkNumber, Char, Line);
if FilePersistInfo.Highlighter <> '' then begin
Editor.SynEdit.Highlighter := GetHighlighterFromLanguageName(
FilePersistInfo.Highlighter, CommandsDataModule.Highlighters);
Editor.SynEdit2.Highlighter := Editor.SynEdit.Highlighter;
end;
Editor.SynEdit.Assign(FilePersistInfo.EditorOptions);
if FilePersistInfo.SecondEditorVisible then begin
Editor.SynEdit2.Assign(FilePersistInfo.EditorOptions2);
if FilePersistInfo.SecondEditorAlign = alRight then begin
Editor.SplitEditorVertrically;
Editor.SynEdit2.Width := FilePersistInfo.SecondEditorSize;
end else begin
Editor.SplitEditorHorizontally;
Editor.SynEdit2.Height := FilePersistInfo.SecondEditorSize;
end;
end;
end;
end;
finally
PersistFileInfo.Free;
end;
FName := AppStorage.ReadString(Path+'\ActiveEditor', FName);
if FName <> '' then begin
Editor := GI_EditorFactory.GetEditorByName(FName);
if Assigned(Editor) then
Editor.Activate;
end;
end;
function TPersistFileInfo.CreateListItem(Sender: TJvCustomAppStorage;
const Path: string; Index: Integer): TPersistent;
begin
Result := TFilePersistInfo.Create;
end;
destructor TPersistFileInfo.Destroy;
begin
fFileInfoList.Free;
inherited;
end;
class procedure TPersistFileInfo.WriteToAppStorage(
AppStorage: TJvCustomAppStorage; Path : String);
Var
PersistFileInfo : TPersistFileInfo;
ActiveEditor : IEditor;
FName : string;
begin
PersistFileInfo := TPersistFileInfo.Create;
try
PersistFileInfo.GetFileInfo;
AppStorage.WriteObjectList(Path, PersistFileInfo.fFileInfoList, 'File');
finally
PersistFileInfo.Free;
end;
ActiveEditor := PyIDEMainForm.GetActiveEditor;
if Assigned(ActiveEditor) then
FName := ActiveEditor.FileName
else
FName := '';
AppStorage.WriteString(Path+'\ActiveEditor', FName);
end;
procedure TPersistFileInfo.GetFileInfo;
procedure ProcessTabControl(TabControl : TSpTBXCustomTabControl);
var
I: Integer;
IV: TTBItemViewer;
Editor : IEditor;
FilePersistInfo : TFilePersistInfo;
begin
// Note that the Pages property may have a different order than the
// physical order of the tabs
for I := 0 to TabControl.View.ViewerCount - 1 do begin
IV := TabControl.View.Viewers[I];
if IV.Item is TSpTBXTabItem then begin
Editor := PyIDEMainForm.EditorFromTab(TSpTBXTabItem(IV.Item));
if Assigned(Editor) and (Editor.FileName <> '') then begin
FilePersistInfo := TFilePersistInfo.CreateFromEditor(Editor);
fFileInfoList.Add(FilePersistInfo)
end;
end;
end;
end;
begin
ProcessTabControl(PyIDEMainForm.TabControl1);
ProcessTabControl(PyIDEMainForm.TabControl2);
end;
{ TTabsPersistInfo }
procedure TTabsPersistInfo.ReadFromAppStorage(AppStorage: TJvCustomAppStorage;
const BasePath: string);
Var
IsVisible : Boolean;
Size : integer;
Alignment : TAlign;
begin
with PyIDEMainForm do begin
IsVisible := AppStorage.ReadBoolean(BasePath+'\Visible', False);
if IsVisible then begin
Alignment := alRight;
AppStorage.ReadEnumeration(BasePath+'\Align', TypeInfo(TAlign),
Alignment, Alignment);
Size := AppStorage.ReadInteger(BasePath+'\Size', -1);
SplitWorkspace(True, Alignment, Size);
end else
SplitWorkspace(False);
end;
end;
procedure TTabsPersistInfo.WriteToAppStorage(AppStorage: TJvCustomAppStorage;
const BasePath: string);
begin
AppStorage.WriteBoolean(BasePath+'\Visible', PyIDEMainForm.TabControl2.Visible);
with PyIDEMainForm do if TabControl2.Visible then begin
AppStorage.WriteEnumeration(BasePath+'\Align', TypeInfo(TAlign), TabControl2.Align);
AppStorage.WriteInteger(BasePath+'\Size',
IfThen(TabControl2.Align = alRight, TabControl2.Width, TabControl2.Height));
end;
end;
initialization
TabsPersistsInfo := TTabsPersistInfo.Create;
finalization
FreeAndNil(TabsPersistsInfo);
end.