Skip to content

Commit 4b77024

Browse files
committed
Warn when saving snippet information looses data
When saving snippet information that contains characters not supported in the selected output encoding a warning message now appears to inform that the saved or previewed text differs from the original. This usually happens when saving Markdown or plain text in the default ANSI encoding. Fixes #165
1 parent 0c0403e commit 4b77024

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

Src/USaveInfoMgr.pas

+33-11
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ TSaveInfoMgr = class(TNoPublicConstructObject)
3535
fSaveDlg: TSaveSourceDlg;
3636
fSourceFileInfo: TSourceFileInfo;
3737

38+
/// <summary>Displays a warning message about data loss if
39+
/// <c>ExpectedStr</c> doesn't match <c>EncodedStr</c>.</summary>
40+
class procedure WarnIfDataLoss(const ExpectedStr, EncodedStr: string);
41+
3842
/// <summary>Returns encoded data containing a RTF representation of
3943
/// information about the snippet represented by the given view.</summary>
4044
class function GenerateRichText(View: IView; const AUseHiliting: Boolean):
@@ -140,6 +144,7 @@ implementation
140144
Hiliter.UGlobals,
141145
UIOUtils,
142146
UMarkdownSnippetDoc,
147+
UMessageBox,
143148
UOpenDialogHelper,
144149
UPreferences,
145150
URTFSnippetDoc,
@@ -231,18 +236,20 @@ function TSaveInfoMgr.GenerateHTML(const AUseHiliting: Boolean;
231236
function TSaveInfoMgr.GenerateMarkdown: TEncodedData;
232237
var
233238
Doc: TMarkdownSnippetDoc;
234-
GeneratedData: TEncodedData;
239+
ExpectedMarkown: string;
235240
begin
236241
Assert(Supports(fView, ISnippetView),
237-
ClassName + '.GeneratePlainText: View is not a snippet view');
242+
ClassName + '.GenerateMarkdown: View is not a snippet view');
238243
Doc := TMarkdownSnippetDoc.Create(
239244
(fView as ISnippetView).Snippet.Kind <> skFreeform
240245
);
241246
try
242-
GeneratedData := Doc.Generate((fView as ISnippetView).Snippet);
243-
Result := TEncodedData.Create(
244-
GeneratedData.ToString, fSaveDlg.SelectedEncoding
245-
);
247+
// Generate Markdown using default UTF-16 encoding
248+
ExpectedMarkown := Doc.Generate((fView as ISnippetView).Snippet).ToString;
249+
// Convert Markdown to encoding to that selected in save dialogue box
250+
Result := TEncodedData.Create(ExpectedMarkown, fSaveDlg.SelectedEncoding);
251+
// Check for data loss in required encoding
252+
WarnIfDataLoss(ExpectedMarkown, Result.ToString);
246253
finally
247254
Doc.Free;
248255
end;
@@ -266,19 +273,23 @@ function TSaveInfoMgr.GenerateOutput(const FileType: TSourceFileType):
266273

267274
function TSaveInfoMgr.GeneratePlainText: TEncodedData;
268275
var
269-
Doc: TTextSnippetDoc; // object that generates RTF document
270-
HiliteAttrs: IHiliteAttrs; // syntax highlighter formatting attributes
271-
GeneratedData: TEncodedData;
276+
Doc: TTextSnippetDoc; // object that generates plain text document
277+
HiliteAttrs: IHiliteAttrs; // syntax highlighter formatting attributes
278+
ExpectedText: string; // expected plain text
272279
begin
273280
Assert(Supports(fView, ISnippetView),
274281
ClassName + '.GeneratePlainText: View is not a snippet view');
275282
HiliteAttrs := THiliteAttrsFactory.CreateNulAttrs;
276283
Doc := TTextSnippetDoc.Create;
277284
try
278-
GeneratedData := Doc.Generate((fView as ISnippetView).Snippet);
285+
// Generate text using default UTF-16 encoding
286+
ExpectedText := Doc.Generate((fView as ISnippetView).Snippet).ToString;
287+
// Convert encoding to that selected in save dialogue box
279288
Result := TEncodedData.Create(
280-
GeneratedData.ToString, fSaveDlg.SelectedEncoding
289+
ExpectedText, fSaveDlg.SelectedEncoding
281290
);
291+
// Check for data loss in required encoding
292+
WarnIfDataLoss(ExpectedText, Result.ToString);
282293
finally
283294
Doc.Free;
284295
end;
@@ -448,4 +459,15 @@ function TSaveInfoMgr.SelectedFileType: TSourceFileType;
448459
Result := fSourceFileInfo.FileTypeFromFilterIdx(fSaveDlg.FilterIndex);
449460
end;
450461

462+
class procedure TSaveInfoMgr.WarnIfDataLoss(const ExpectedStr,
463+
EncodedStr: string);
464+
resourcestring
465+
sEncodingError = 'The selected snippet contains characters that can''t be '
466+
+ 'represented in the chosen file encoding.' + sLineBreak + sLineBreak
467+
+ 'Please compare the output to the snippet displayed in the Details pane.';
468+
begin
469+
if ExpectedStr <> EncodedStr then
470+
TMessageBox.Warning(nil, sEncodingError);
471+
end;
472+
451473
end.

0 commit comments

Comments
 (0)