From 6c771a6218e3ef29df7ad640d50fe05b2a758f8c Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Fri, 19 Mar 2010 19:40:13 +0000 Subject: [PATCH] Fixed SD2-1652: "Cannot save with current encoding" not implemented in AvalonEdit.AddIn git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@5630 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- data/resources/StringResources.resx | 9 ++++++ .../Src/AvalonEditViewContent.cs | 32 +++++++++++++++++-- .../AvalonEdit.AddIn/Src/CodeEditor.cs | 18 +++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/data/resources/StringResources.resx b/data/resources/StringResources.resx index b7b520d457..84730ab371 100644 --- a/data/resources/StringResources.resx +++ b/data/resources/StringResources.resx @@ -890,6 +890,15 @@ SharpDevelop can collect this information and upload it automatically. Use markup extension completion + + The file cannot be saved with the current encoding ${encoding} without losing data. + + + Continue + + + Save as UTF-8 + Project is configured as dll and no execution command is specified. You can specifiy an execution command in the project options. diff --git a/src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/AvalonEditViewContent.cs b/src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/AvalonEditViewContent.cs index 3a22fbfa89..d33469e276 100644 --- a/src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/AvalonEditViewContent.cs +++ b/src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/AvalonEditViewContent.cs @@ -77,7 +77,35 @@ namespace ICSharpCode.AvalonEdit.AddIn { if (file != PrimaryFile) return; - codeEditor.Save(stream); + + if (codeEditor.CanSaveWithCurrentEncoding()) { + codeEditor.Save(stream); + } else { + int r = MessageService.ShowCustomDialog( + "${res:Dialog.Options.IDEOptions.TextEditor.General.FontGroupBox.FileEncodingGroupBox}", + StringParser.Parse("${res:AvalonEdit.FileEncoding.EncodingCausesDataLoss}", + new StringTagPair("encoding", codeEditor.Encoding.EncodingName)), + 0, -1, + "${res:AvalonEdit.FileEncoding.EncodingCausesDataLoss.UseUTF8}", + "${res:AvalonEdit.FileEncoding.EncodingCausesDataLoss.Continue}"); + if (r == 1) { + // continue saving with data loss + MemoryStream ms = new MemoryStream(); + codeEditor.Save(ms); + ms.Position = 0; + ms.WriteTo(stream); + ms.Position = 0; + // Read back the version we just saved to show the data loss to the user (he'll be able to press Undo). + using (StreamReader reader = new StreamReader(ms, codeEditor.Encoding, false)) { + codeEditor.Document.Text = reader.ReadToEnd(); + } + return; + } else { + // unfortunately we don't support cancel within IViewContent.Save, so we'll use the safe choice of UTF-8 instead + codeEditor.Encoding = System.Text.Encoding.UTF8; + codeEditor.Save(stream); + } + } } bool isLoading; @@ -92,7 +120,7 @@ namespace ICSharpCode.AvalonEdit.AddIn codeEditor.PrimaryTextEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(file.FileName)); - codeEditor.Load(stream); + codeEditor.Load(stream); // we set the file name after loading because this will place the fold markers etc. codeEditor.FileName = FileName.Create(file.FileName); BookmarksAttach(); diff --git a/src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/CodeEditor.cs b/src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/CodeEditor.cs index 8ea2c80499..e94998bd4f 100644 --- a/src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/CodeEditor.cs +++ b/src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/CodeEditor.cs @@ -250,6 +250,24 @@ namespace ICSharpCode.AvalonEdit.AddIn /// public bool UseFixedEncoding { get; set; } + public Encoding Encoding { + get { return primaryTextEditor.Encoding; } + set { primaryTextEditor.Encoding = value; } + } + + /// + /// Gets if the document can be saved with the current encoding without losing data. + /// + public bool CanSaveWithCurrentEncoding() + { + Encoding encoding = this.Encoding; + if (encoding == null || Utils.FileReader.IsUnicode(encoding)) + return true; + // not a unicode codepage + string text = document.Text; + return encoding.GetString(encoding.GetBytes(text)) == text; + } + // always use primary text editor for loading/saving // (the file encoding is stored only there) public void Load(Stream stream)