diff --git a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/FormatXmlCommand.cs b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/FormatXmlCommand.cs new file mode 100644 index 0000000000..8ea281248b --- /dev/null +++ b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/FormatXmlCommand.cs @@ -0,0 +1,30 @@ +// +// +// +// +// $Revision$ +// + +using ICSharpCode.Core; +using ICSharpCode.SharpDevelop.Gui; +using System.IO; +using System.Xml; + +namespace ICSharpCode.XmlEditor +{ + /// + /// Pretty prints the xml. + /// + public class FormatXmlCommand : AbstractMenuCommand + { + public override void Run() + { + // Find active XmlView. + XmlView xmlView = WorkbenchSingleton.Workbench.ActiveWorkbenchWindow.ActiveViewContent as XmlView; + if (xmlView != null) { + xmlView.FormatXml(); + } + } + } +} + diff --git a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs index db51b5346e..ee3652d7ed 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs @@ -239,6 +239,22 @@ namespace ICSharpCode.XmlEditor } } + /// + /// Pretty prints the xml. + /// + public void FormatXml() + { + TaskService.ClearExceptCommentTasks(); + + if (IsWellFormed) { + string xml = SimpleFormat(IndentedFormat(Text)); + xmlEditor.Document.Replace(0, xmlEditor.Document.TextLength, xml); + UpdateFolding(); + } else { + ShowErrorList(); + } + } + #region IEditable interface public IClipboardHandler ClipboardHandler { @@ -730,7 +746,6 @@ namespace ICSharpCode.XmlEditor view.LoadContent(xml); view.WorkbenchWindow.SelectWindow(); } - } /// @@ -775,27 +790,31 @@ namespace ICSharpCode.XmlEditor /// A pretty print version of the specified xml. If the /// string is not well formed xml the original string is returned. /// - static string IndentedFormat(string xml) + string IndentedFormat(string xml) { string indentedText = String.Empty; - try - { + try { XmlTextReader reader = new XmlTextReader(new StringReader(xml)); reader.WhitespaceHandling = WhitespaceHandling.None; StringWriter indentedXmlWriter = new StringWriter(); XmlTextWriter writer = new XmlTextWriter(indentedXmlWriter); - writer.Indentation = 1; - writer.IndentChar = '\t'; + if (xmlEditor.TextEditorProperties.ConvertTabsToSpaces) { + writer.Indentation = xmlEditor.TextEditorProperties.TabIndent; + writer.IndentChar = ' '; + ; + } else { + writer.Indentation = 1; + writer.IndentChar = '\t'; + } writer.Formatting = Formatting.Indented; writer.WriteNode(reader, false); writer.Flush(); indentedText = indentedXmlWriter.ToString(); } - catch(Exception) - { + catch(Exception) { indentedText = xml; } @@ -807,14 +826,11 @@ namespace ICSharpCode.XmlEditor /// bool IsWellFormed { get { - try - { + try { XmlDocument Document = new XmlDocument( ); Document.LoadXml(Text); return true; - } - catch(XmlException ex) - { + } catch(XmlException ex) { string fileName = FileName; if (fileName == null || fileName.Length == 0) { fileName = TitleName; @@ -830,8 +846,7 @@ namespace ICSharpCode.XmlEditor /// bool IsValidXsl(string xml) { - try - { + try { WorkbenchSingleton.Workbench.GetPad(typeof(CompilerMessageView)).BringPadToFront(); StringReader reader = new StringReader(xml); @@ -841,9 +856,7 @@ namespace ICSharpCode.XmlEditor xslTransform.Load(doc, XsltSettings.Default, new XmlUrlResolver()); return true; - } - catch(XsltCompileException ex) - { + } catch(XsltCompileException ex) { string message = String.Empty; if(ex.InnerException != null) @@ -856,13 +869,9 @@ namespace ICSharpCode.XmlEditor } AddTask(StylesheetFileName, message, ex.LineNumber - 1, ex.LinePosition - 1, TaskType.Error); - } - catch(XsltException ex) - { + } catch(XsltException ex) { AddTask(StylesheetFileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1, TaskType.Error); - } - catch(XmlException ex) - { + } catch(XmlException ex) { AddTask(StylesheetFileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1, TaskType.Error); } diff --git a/src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.addin b/src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.addin index 835f69040b..765a563c31 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.addin +++ b/src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.addin @@ -82,6 +82,10 @@ class="ICSharpCode.XmlEditor.RunXslTransformCommand" shortcut="Control|Shift|T"/> + + diff --git a/src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.csproj b/src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.csproj index 122c23603e..605264fa39 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.csproj +++ b/src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.csproj @@ -92,6 +92,7 @@ Configuration\GlobalAssemblyInfo.cs +