Skip to content

Commit d216d75

Browse files
committed
Add xml highlighting to the template editor
1 parent 71ca0c8 commit d216d75

File tree

6 files changed

+144
-29
lines changed

6 files changed

+144
-29
lines changed

StructuredXmlEditor/Data/DataTransformer.cs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,44 @@ public bool TransformDocument(XElement root)
5555
{
5656
var firstNode = pathParts[1];
5757

58-
var potentialStarts = root.Descendants(firstNode);
58+
var potentialStarts = root.Descendants(firstNode).ToList();
5959

6060
var elementPath = ElementPath.Replace(resourceType + "." + firstNode + ".", "");
6161
foreach (var potentialRoot in potentialStarts)
6262
{
63-
var matchingEls = GetElements(potentialRoot, elementPath);
64-
if (matchingEls.Count > 0)
63+
if (pathParts.Length == 2)
6564
{
66-
foreach (var el in matchingEls)
65+
var el = potentialRoot;
66+
var parent = el.Parent;
67+
var transformed = TransformElement(root, el);
68+
69+
foreach (var newEl in transformed)
6770
{
68-
var parent = el.Parent;
69-
var transformed = TransformElement(root, el);
71+
el.AddBeforeSelf(newEl);
72+
}
73+
el.Remove();
7074

71-
foreach (var newEl in transformed)
75+
processed = true;
76+
}
77+
else
78+
{
79+
var matchingEls = GetElements(potentialRoot, elementPath);
80+
if (matchingEls.Count > 0)
81+
{
82+
foreach (var el in matchingEls)
7283
{
73-
el.AddBeforeSelf(newEl);
84+
var parent = el.Parent;
85+
var transformed = TransformElement(root, el);
86+
87+
foreach (var newEl in transformed)
88+
{
89+
el.AddBeforeSelf(newEl);
90+
}
91+
el.Remove();
7492
}
75-
el.Remove();
76-
}
7793

78-
processed = true;
94+
processed = true;
95+
}
7996
}
8097
}
8198
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<SyntaxDefinition name="XML" extensions=".xml;.xsl;.xslt;.xsd;.manifest;.config;.addin;.xshd;.wxs;.wxi;.wxl;.proj;.csproj;.vbproj;.ilproj;.booproj;.build;.xfrm;.targets;.xaml;.xpt;.xft;.map;.wsdl;.disco;.ps1xml;.nuspec" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
2+
<Color foreground="#bfbfbf" name="Comment" exampleText="&lt;!-- comment --&gt;" />
3+
<Color foreground="#e3e3e3" name="CData" exampleText="&lt;![CDATA[data]]&gt;" />
4+
<Color foreground="#e3e3e3" name="DocType" exampleText="&lt;!DOCTYPE rootElement&gt;" />
5+
<Color foreground="#e3e3e3" name="XmlDeclaration" exampleText='&lt;?xml version="1.0"?&gt;' />
6+
<Color foreground="#e32088" name="XmlTag" exampleText='&lt;tag /&gt;' />
7+
<Color foreground="#a3db1f" name="AttributeName" exampleText='&lt;tag attribute="value" /&gt;' />
8+
<Color foreground="#dfe61c" name="AttributeValue" exampleText='&lt;tag attribute="value" /&gt;' />
9+
<Color foreground="Teal" name="Entity" exampleText="index.aspx?a=1&amp;amp;b=2" />
10+
<Color foreground="Olive" name="BrokenEntity" exampleText="index.aspx?a=1&amp;b=2" />
11+
<Color foreground="#acfcf0" name="Pattern" exampleText="{el|path|contents}" />
12+
<Color foreground="#bdff54" name="PatternRoot" exampleText="{el||}" />
13+
<Color foreground="#f3ff45" name="PatternPath" exampleText="{|path|}" />
14+
<Color foreground="#fcb93d" name="PatternContent" exampleText="{||contents}" />
15+
16+
<RuleSet>
17+
<Span color="Comment" multiline="true">
18+
<Begin>&lt;!--</Begin>
19+
<End>--&gt;</End>
20+
</Span>
21+
<Span color="CData" multiline="true">
22+
<Begin>&lt;!\[CDATA\[</Begin>
23+
<End>]]&gt;</End>
24+
</Span>
25+
<Span color="DocType" multiline="true">
26+
<Begin>&lt;!DOCTYPE</Begin>
27+
<End>&gt;</End>
28+
</Span>
29+
<Span color="XmlDeclaration" multiline="true">
30+
<Begin>&lt;\?</Begin>
31+
<End>\?&gt;</End>
32+
</Span>
33+
<Span color="XmlTag" multiline="true">
34+
<Begin>&lt;</Begin>
35+
<End>&gt;</End>
36+
<RuleSet>
37+
<!-- Treat the position before '<' as end, as that's not a valid character
38+
in attribute names and indicates the user forgot a closing quote. -->
39+
<Span color="AttributeValue" multiline="true" ruleSet="EntitySet">
40+
<Begin>"</Begin>
41+
<End>"|(?=&lt;)</End>
42+
</Span>
43+
<Span color="AttributeValue" multiline="true" ruleSet="EntitySet">
44+
<Begin>'</Begin>
45+
<End>'|(?=&lt;)</End>
46+
</Span>
47+
<Rule color="AttributeName">[\d\w_\-\.]+(?=(\s*=))</Rule>
48+
<Rule color="AttributeValue">=</Rule>
49+
</RuleSet>
50+
</Span>
51+
<Import ruleSet="EntitySet"/>
52+
<Span color="Pattern">
53+
<Begin>{</Begin>
54+
<End>}</End>
55+
<RuleSet>
56+
<Span color="PatternPath">
57+
<Begin>\|</Begin>
58+
<End>\|</End>
59+
</Span>
60+
<Rule color="PatternContent">contents|name|refkey</Rule>
61+
<Rule color="PatternRoot">el|root</Rule>
62+
<Rule color="PatternPath">\|[a-zA-Z.\*]*\|</Rule>
63+
</RuleSet>
64+
</Span>
65+
</RuleSet>
66+
67+
<RuleSet name="EntitySet">
68+
<Rule color="Entity">
69+
&amp;
70+
[\w\d\#]+
71+
;
72+
</Rule>
73+
74+
<Rule color="BrokenEntity">
75+
&amp;
76+
[\w\d\#]*
77+
#missing ;
78+
</Rule>
79+
</RuleSet>
80+
</SyntaxDefinition>

StructuredXmlEditor/StructuredXmlEditor.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
<Reference Include="DiffPlex, Version=1.5.0.0, Culture=neutral, PublicKeyToken=1d35e91d1bd7bc0f, processorArchitecture=MSIL">
5151
<HintPath>..\packages\DiffPlex.1.5.0\lib\net40\DiffPlex.dll</HintPath>
5252
</Reference>
53+
<Reference Include="ICSharpCode.AvalonEdit, Version=6.0.1.278, Culture=neutral, PublicKeyToken=9cc39be672370310, processorArchitecture=MSIL">
54+
<HintPath>..\packages\AvalonEdit.6.0.1\lib\net45\ICSharpCode.AvalonEdit.dll</HintPath>
55+
</Reference>
5356
<Reference Include="LibGit2Sharp, Version=0.26.0.0, Culture=neutral, PublicKeyToken=7cbde695407f0333, processorArchitecture=MSIL">
5457
<HintPath>..\packages\LibGit2Sharp.0.26.2\lib\net46\LibGit2Sharp.dll</HintPath>
5558
</Reference>
@@ -565,6 +568,7 @@
565568
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
566569
</None>
567570
<AppDesigner Include="Properties\" />
571+
<EmbeddedResource Include="Resources\DataTransformerSyntax.xshd" />
568572
</ItemGroup>
569573
<ItemGroup>
570574
<None Include="App.config" />

StructuredXmlEditor/Tools/DataTransformerTool.cs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
using DiffPlex;
22
using DiffPlex.DiffBuilder;
33
using DiffPlex.DiffBuilder.Model;
4+
using ICSharpCode.AvalonEdit;
45
using StructuredXmlEditor.Data;
56
using StructuredXmlEditor.View;
67
using System;
78
using System.Collections.Generic;
89
using System.IO;
910
using System.Linq;
11+
using System.Reflection;
1012
using System.Text;
1113
using System.Threading.Tasks;
14+
using System.Windows.Media;
1215
using System.Xml;
1316
using System.Xml.Linq;
1417

@@ -32,18 +35,7 @@ public string ElementPath
3235
private string m_elementPath = "Example.Data";
3336

3437
//-----------------------------------------------------------------------
35-
public string OutputTemplate
36-
{
37-
get { return m_outputTemplate; }
38-
set
39-
{
40-
m_outputTemplate = value;
41-
RaisePropertyChangedEvent();
42-
43-
UpdatePreview();
44-
}
45-
}
46-
private string m_outputTemplate = "{el||}";
38+
public TextEditor TextEditor { get; set; }
4739

4840
//-----------------------------------------------------------------------
4941
private DataTransformer DataTransformer { get; } = new DataTransformer();
@@ -88,6 +80,24 @@ public string ExampleDocument
8880
//-----------------------------------------------------------------------
8981
public DataTransformerTool(Workspace workspace) : base(workspace, "Data Transformer Tool")
9082
{
83+
TextEditor = new TextEditor();
84+
85+
var assembly = Assembly.GetExecutingAssembly();
86+
var resourceName = ("StructuredXmlEditor.Resources.DataTransformerSyntax.xshd");
87+
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
88+
using (XmlTextReader reader = new XmlTextReader(stream))
89+
{
90+
TextEditor.SyntaxHighlighting = ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(reader, ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance);
91+
}
92+
93+
TextEditor.Text = "{el||}";
94+
TextEditor.Foreground = new SolidColorBrush(Color.FromRgb(255, 255, 255));
95+
TextEditor.Background = Brushes.Transparent;
96+
TextEditor.Document.TextChanged += (e, args) =>
97+
{
98+
UpdatePreview();
99+
};
100+
91101
ExampleDocument = "<Example><Data /></Example>";
92102

93103
VisibleByDefault = false;
@@ -99,7 +109,7 @@ public DataTransformerTool(Workspace workspace) : base(workspace, "Data Transfor
99109
public void UpdatePreview()
100110
{
101111
DataTransformer.ElementPaths = ElementPath.Split('\n').Select(e => e.Trim()).ToList();
102-
DataTransformer.OutputTemplate = OutputTemplate;
112+
DataTransformer.OutputTemplate = TextEditor.Text;
103113

104114
TransformError = null;
105115

StructuredXmlEditor/View/CustomControls/DataTransformerToolView.xaml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,14 @@
9595
Grid.Row="2"
9696
Margin="5,5,5,0"
9797
Text="Replace template. Access content with {(el,root) | (path) | (contents,name,refkey)}." />
98-
<local:PromptTextBox
99-
Grid.Row="3"
100-
AcceptsReturn="True"
101-
PromptText="Replace template"
102-
Text="{Binding OutputTemplate, UpdateSourceTrigger=PropertyChanged, Delay=200}" />
98+
<Border
99+
Background="{StaticResource BackgroundEditBrush}"
100+
BorderThickness="1"
101+
BorderBrush="{StaticResource BorderEditBrush}"
102+
Grid.Row="3">
103+
<ContentPresenter
104+
Content="{Binding TextEditor}" />
105+
</Border>
103106

104107
<DockPanel
105108
Grid.Row="4"

StructuredXmlEditor/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="AvalonEdit" version="6.0.1" targetFramework="net461" />
34
<package id="Costura.Fody" version="3.3.3" targetFramework="net461" developmentDependency="true" />
45
<package id="DiffPlex" version="1.5.0" targetFramework="net461" />
56
<package id="Fody" version="4.2.1" targetFramework="net461" developmentDependency="true" />

0 commit comments

Comments
 (0)