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 @@ @@ -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 @@ -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
public IClipboardHandler ClipboardHandler {
@ -730,7 +746,6 @@ namespace ICSharpCode.XmlEditor @@ -730,7 +746,6 @@ namespace ICSharpCode.XmlEditor
view.LoadContent(xml);
view.WorkbenchWindow.SelectWindow();
}
}
/// <summary>
@ -775,27 +790,31 @@ namespace ICSharpCode.XmlEditor @@ -775,27 +790,31 @@ namespace ICSharpCode.XmlEditor
/// <returns>A pretty print version of the specified xml. If the
/// string is not well formed xml the original string is returned.
/// </returns>
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 @@ -807,14 +826,11 @@ namespace ICSharpCode.XmlEditor
/// </summary>
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 @@ -830,8 +846,7 @@ namespace ICSharpCode.XmlEditor
/// </summary>
bool IsValidXsl(string xml)
{
try
{
try {
WorkbenchSingleton.Workbench.GetPad(typeof(CompilerMessageView)).BringPadToFront();
StringReader reader = new StringReader(xml);
@ -841,9 +856,7 @@ namespace ICSharpCode.XmlEditor @@ -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 @@ -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);
}

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

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

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

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

Loading…
Cancel
Save