Browse Source

Added format option to the XML menu which will format and indent the entire xml document.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1170 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 20 years ago
parent
commit
80f198e7fa
  1. 30
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/FormatXmlCommand.cs
  2. 57
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs
  3. 4
      src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.addin
  4. 1
      src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.csproj

30
src/AddIns/DisplayBindings/XmlEditor/Project/Src/FormatXmlCommand.cs

@ -0,0 +1,30 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Gui;
using System.IO;
using System.Xml;
namespace ICSharpCode.XmlEditor
{
/// <summary>
/// Pretty prints the xml.
/// </summary>
public class FormatXmlCommand : AbstractMenuCommand
{
public override void Run()
{
// Find active XmlView.
XmlView xmlView = WorkbenchSingleton.Workbench.ActiveWorkbenchWindow.ActiveViewContent as XmlView;
if (xmlView != null) {
xmlView.FormatXml();
}
}
}
}

57
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs

@ -239,6 +239,22 @@ namespace ICSharpCode.XmlEditor
} }
} }
/// <summary>
/// Pretty prints the xml.
/// </summary>
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 #region IEditable interface
public IClipboardHandler ClipboardHandler { public IClipboardHandler ClipboardHandler {
@ -730,7 +746,6 @@ namespace ICSharpCode.XmlEditor
view.LoadContent(xml); view.LoadContent(xml);
view.WorkbenchWindow.SelectWindow(); view.WorkbenchWindow.SelectWindow();
} }
} }
/// <summary> /// <summary>
@ -775,27 +790,31 @@ namespace ICSharpCode.XmlEditor
/// <returns>A pretty print version of the specified xml. If the /// <returns>A pretty print version of the specified xml. If the
/// string is not well formed xml the original string is returned. /// string is not well formed xml the original string is returned.
/// </returns> /// </returns>
static string IndentedFormat(string xml) string IndentedFormat(string xml)
{ {
string indentedText = String.Empty; string indentedText = String.Empty;
try try {
{
XmlTextReader reader = new XmlTextReader(new StringReader(xml)); XmlTextReader reader = new XmlTextReader(new StringReader(xml));
reader.WhitespaceHandling = WhitespaceHandling.None; reader.WhitespaceHandling = WhitespaceHandling.None;
StringWriter indentedXmlWriter = new StringWriter(); StringWriter indentedXmlWriter = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(indentedXmlWriter); XmlTextWriter writer = new XmlTextWriter(indentedXmlWriter);
writer.Indentation = 1; if (xmlEditor.TextEditorProperties.ConvertTabsToSpaces) {
writer.IndentChar = '\t'; writer.Indentation = xmlEditor.TextEditorProperties.TabIndent;
writer.IndentChar = ' ';
;
} else {
writer.Indentation = 1;
writer.IndentChar = '\t';
}
writer.Formatting = Formatting.Indented; writer.Formatting = Formatting.Indented;
writer.WriteNode(reader, false); writer.WriteNode(reader, false);
writer.Flush(); writer.Flush();
indentedText = indentedXmlWriter.ToString(); indentedText = indentedXmlWriter.ToString();
} }
catch(Exception) catch(Exception) {
{
indentedText = xml; indentedText = xml;
} }
@ -807,14 +826,11 @@ namespace ICSharpCode.XmlEditor
/// </summary> /// </summary>
bool IsWellFormed { bool IsWellFormed {
get { get {
try try {
{
XmlDocument Document = new XmlDocument( ); XmlDocument Document = new XmlDocument( );
Document.LoadXml(Text); Document.LoadXml(Text);
return true; return true;
} } catch(XmlException ex) {
catch(XmlException ex)
{
string fileName = FileName; string fileName = FileName;
if (fileName == null || fileName.Length == 0) { if (fileName == null || fileName.Length == 0) {
fileName = TitleName; fileName = TitleName;
@ -830,8 +846,7 @@ namespace ICSharpCode.XmlEditor
/// </summary> /// </summary>
bool IsValidXsl(string xml) bool IsValidXsl(string xml)
{ {
try try {
{
WorkbenchSingleton.Workbench.GetPad(typeof(CompilerMessageView)).BringPadToFront(); WorkbenchSingleton.Workbench.GetPad(typeof(CompilerMessageView)).BringPadToFront();
StringReader reader = new StringReader(xml); StringReader reader = new StringReader(xml);
@ -841,9 +856,7 @@ namespace ICSharpCode.XmlEditor
xslTransform.Load(doc, XsltSettings.Default, new XmlUrlResolver()); xslTransform.Load(doc, XsltSettings.Default, new XmlUrlResolver());
return true; return true;
} } catch(XsltCompileException ex) {
catch(XsltCompileException ex)
{
string message = String.Empty; string message = String.Empty;
if(ex.InnerException != null) if(ex.InnerException != null)
@ -856,13 +869,9 @@ namespace ICSharpCode.XmlEditor
} }
AddTask(StylesheetFileName, message, ex.LineNumber - 1, ex.LinePosition - 1, TaskType.Error); 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); 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); AddTask(StylesheetFileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1, TaskType.Error);
} }

4
src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.addin

@ -82,6 +82,10 @@
class="ICSharpCode.XmlEditor.RunXslTransformCommand" class="ICSharpCode.XmlEditor.RunXslTransformCommand"
shortcut="Control|Shift|T"/> shortcut="Control|Shift|T"/>
</ComplexCondition> </ComplexCondition>
<MenuItem id = "Separator2" type = "Separator"/>
<MenuItem id = "Format"
label = "${res:XML.MainMenu.EditMenu.FormatMenu}"
class = "ICSharpCode.XmlEditor.FormatXmlCommand" />
</MenuItem> </MenuItem>
</Condition> </Condition>
</Path> </Path>

1
src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.csproj

@ -92,6 +92,7 @@
<Compile Include="..\..\..\..\Main\GlobalAssemblyInfo.cs"> <Compile Include="..\..\..\..\Main\GlobalAssemblyInfo.cs">
<Link>Configuration\GlobalAssemblyInfo.cs</Link> <Link>Configuration\GlobalAssemblyInfo.cs</Link>
</Compile> </Compile>
<Compile Include="Src\FormatXmlCommand.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\..\Main\Base\Project\ICSharpCode.SharpDevelop.csproj"> <ProjectReference Include="..\..\..\..\Main\Base\Project\ICSharpCode.SharpDevelop.csproj">

Loading…
Cancel
Save