Menu

[r4761]: / branches / parsnip / Src / USaveUnitMgr.pas  Maximize  Restore  History

Download this file

231 lines (200 with data), 6.8 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
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
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2006-2014, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Defines a class that manages generation, previewing and saving of a pascal
* unit.
}
unit USaveUnitMgr;
interface
uses
// Project
CS.Database.Types,
CS.SourceCode.Pascal.SourceGen,
UIStringList,
USourceFileInfo,
USaveSourceMgr;
type
/// <summary>
/// Manages generation, previewing and saving of a Pascal unit to disk.
/// </summary>
/// <remarks>
/// Generated file can be a valid Pascal unit, a plain text file, an HTML
/// file or a RTF file. The last two file types can optionally be syntax
/// highlighted.
/// </remarks>
TSaveUnitMgr = class(TSaveSourceMgr)
strict private
var
/// <summary>Used to generate source code unit.</summary>
fSourceGen: TPascalSourceGen;
/// <summary>Name of generated unit.</summary>
/// <remarks>If empty string a default name is used.</remarks>
fUnitName: string;
/// <summary>Gets name of unit to be used in generated code.</summary>
function UnitName: string;
/// <summary>Creates a string list containing comments to be written to
/// head of unit.</summary>
function CreateHeaderComments: IStringList;
strict protected
/// <summary>Object constuctor. Sets up object to save a unit containing
/// all snippets in given list.</summary>
constructor InternalCreate(Snips: ISnippetIDList);
/// <summary>Gets description of given source code file type.</summary>
function GetFileTypeDesc(const FileType: TSourceOutputFileType): string;
override;
/// <summary>Gets default file name to display in dialogue box.</summary>
function GetDefaultFileName: string; override;
/// <summary>Gets dialogue box title.</summary>
function GetDlgTitle: string; override;
/// <summary>Get dialogue box's help keyword.</summary>
function GetDlgHelpKeyword: string; override;
/// <summary>Gets title to be used for source document.</summary>
function GetDocTitle: string; override;
/// <summary>Generates raw, un-highlighted, source code.</summary>
/// <param name="CommentStyle">TPascalCommentStyle [in] Style of commenting
/// to be used in source code.</param>
/// <param name="TruncateComments">Boolean [in] Indicates whether multi
/// paragraph comments are to be truncated to first paragraph.</param>
/// <returns>String containing generated source code.</returns>
function GenerateSource(const CommentStyle: TPascalCommentStyle;
const TruncateComments: Boolean): string;
override;
/// <summary>Checks if a file name is valid for the kind of file being
/// saved.</summary>
/// <param name="FileName">string [in] Name of file to check.</param>
/// <param name="NameOK">Boolean [out] Set True if file name OK, False if
/// not.</param>
/// <param name="ErrorMessage">string [out] Error message describing
/// problem with invalid file name. Undefined if NameOK is True.</param>
procedure CheckFileName(const FileName: string; out NameOK: Boolean;
out ErrorMessage: string); override;
public
/// <summary>Object destructor. Tears down object.</summary>
destructor Destroy; override;
/// <summary>Creates and outputs a Pascal unit file containing specified
/// snippets with name and format speficied by user.</summary>
/// <param name="Snips">ISnippetIDList [in] IDs of snippets to include in
/// unit.</param>
class procedure Execute(Snips: ISnippetIDList);
end;
implementation
uses
// Delphi
SysUtils,
// Project
CS.Utils.Dates,
UAppInfo,
Web.UInfo;
resourcestring
// Dialogue box title
sSaveDlgTitle = 'Save Unit';
// File filter strings
sHTMLDesc = 'HTML file';
sRTFDesc = 'Rich text file';
sPascalDesc = 'Pascal unit';
sTextDesc = 'Plain text file';
// Default file / unit name
sDefUnitName = 'Snippets';
// Error message
sErrorMsg = 'Filename is not valid for a Pascal unit';
// Unit header comments
sGenerated = 'Generated on : %0:s.';
sGenerator = 'Generated by : %0:s %1:s.';
sHeaderDescription = 'This unit was generated automatically.';
// Output document title
sDocTitle = 'Pascal unit generated by %s';
{ TSaveUnitMgr }
procedure TSaveUnitMgr.CheckFileName(const FileName: string;
out NameOK: Boolean; out ErrorMessage: string);
begin
NameOK := TPascalSourceGen.IsFileNameValidUnitName(FileName);
if NameOK then
fUnitName := TPascalSourceGen.UnitNameFromFileName(FileName)
else
begin
fUnitName := '';
ErrorMessage := sErrorMsg
end;
end;
function TSaveUnitMgr.CreateHeaderComments: IStringList;
begin
Result := TIStringList.Create;
Result.Add(sHeaderDescription);
Result.Add('');
Result.Add(Format(sGenerated, [TUTCDateTime.Now.ToRFC1123String]));
Result.Add(
Format(
sGenerator, [TAppInfo.FullProgramName, TAppInfo.ProgramReleaseInfo]
)
);
end;
destructor TSaveUnitMgr.Destroy;
begin
fSourceGen.Free;
inherited;
end;
class procedure TSaveUnitMgr.Execute(Snips: ISnippetIDList);
begin
with InternalCreate(Snips) do
try
DoExecute;
finally
Free;
end;
end;
function TSaveUnitMgr.GenerateSource(const CommentStyle: TPascalCommentStyle;
const TruncateComments: Boolean): string;
begin
Result := fSourceGen.UnitAsString(
UnitName, CommentStyle, TruncateComments, CreateHeaderComments
);
end;
function TSaveUnitMgr.GetDefaultFileName: string;
begin
Result := sDefUnitName;
end;
function TSaveUnitMgr.GetDlgHelpKeyword: string;
begin
Result := 'SaveUnitDlg';
end;
function TSaveUnitMgr.GetDlgTitle: string;
begin
Result := sSaveDlgTitle;
end;
function TSaveUnitMgr.GetDocTitle: string;
begin
Result := Format(sDocTitle, [TAppInfo.ProgramName]);
end;
function TSaveUnitMgr.GetFileTypeDesc(const FileType: TSourceOutputFileType):
string;
const
Descriptions: array[TSourceOutputFileType] of string = (
sTextDesc, sPascalDesc, sHTMLDesc, sRTFDesc
);
begin
Result := Descriptions[FileType];
end;
constructor TSaveUnitMgr.InternalCreate(Snips: ISnippetIDList);
begin
Assert(Assigned(Snips), ClassName + '.InternalCreate: Snips is nil');
inherited InternalCreate;
// Create source generator and initialize it with required snippets
fSourceGen := TPascalSourceGen.Create;
fSourceGen.IncludeSnippets(Snips);
end;
function TSaveUnitMgr.UnitName: string;
begin
// If we have valid unit name based on file, use it, otherwise use default
if fUnitName <> '' then
Result := fUnitName
else
Result := sDefUnitName;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.