-
Notifications
You must be signed in to change notification settings - Fork 240
/
MainWindow.xaml.cs
236 lines (203 loc) · 9.83 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Xml;
using System.Xml.Schema;
using ICSharpCode.AvalonEdit.CodeCompletion;
using XmlIntelliSense.App.XHelper;
namespace XmlIntelliSense.App
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
textEditor.Text = File.ReadAllText("Sample.xml");
XSD.Text = File.ReadAllText("Schema.xsd");
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(File.ReadAllText("Schema.xsd"))));
schemas.Compile();
XsdInformation = XHelper.XsdParser.AnalyseSchema(schemas);
textEditor.TextChanged += IntelliSense;
textEditor.TextArea.TextEntered += TextArea_TextEntered;
textEditor.TextArea.Caret.PositionChanged += IntelliSense;
}
public List<XsdElementInformation> XsdInformation { get; set; }
private void TextArea_TextEntered(object sender, TextCompositionEventArgs e)
{
var _editor = textEditor;
try
{
switch (e.Text)
{
case ">":
{
//auto-insert closing element
int offset = _editor.CaretOffset;
string s = XHelper.XmlParser.GetElementAtCursor(_editor.Text, offset - 1);
if (!string.IsNullOrWhiteSpace(s) && "!--" != s)
{
if (!XHelper.XmlParser.IsClosingElement(_editor.Text, offset - 1, s))
{
string endElement = "</" + s + ">";
var rightOfCursor = _editor.Text.Substring(offset, Math.Max(0, Math.Min(endElement.Length + 50, _editor.Text.Length) - offset - 1)).TrimStart();
if (!rightOfCursor.StartsWith(endElement))
{
_editor.TextArea.Document.Insert(offset, endElement);
_editor.CaretOffset = offset;
}
}
}
break;
}
case "/":
{
int offset = _editor.CaretOffset;
if (_editor.Text.Length > offset + 2 && _editor.Text[offset] == '>')
{
//remove closing tag if exist
string s = XHelper.XmlParser.GetElementAtCursor(_editor.Text, offset - 1);
if (!string.IsNullOrWhiteSpace(s))
{
//search closing end tag. Element must be empty (whitespace allowed)
//"<hallo> </hallo>" --> enter '/' --> "<hallo/> "
string expectedEndTag = "</" + s + ">";
for (int i = offset + 1; i < _editor.Text.Length - expectedEndTag.Length + 1; i++)
{
if (!char.IsWhiteSpace(_editor.Text[i]))
{
if (_editor.Text.Substring(i, expectedEndTag.Length) == expectedEndTag)
{
//remove already existing endTag
_editor.Document.Remove(i, expectedEndTag.Length);
}
break;
}
}
}
}
break;
}
case "<":
var parentElement = XHelper.XmlParser.GetParentElementPath(_editor.Text);
var elementAutocompleteList = ProvidePossibleElementsAutocomplete(parentElement);
InvokeCompletionWindow(elementAutocompleteList, false);
break;
case " ":
{
var currentElement = XHelper.XmlParser.GetActiveElementStartPath(_editor.Text, _editor.CaretOffset);
var attributeautocompletelist = ProvidePossibleAttributesAutocomplete(currentElement);
InvokeCompletionWindow(attributeautocompletelist, true);
break;
}
}
}
catch (Exception exc)
{
}
if (e.Text.Length > 0)
{
char c = e.Text[0];
if (!(char.IsLetterOrDigit(c) || char.IsPunctuation(c)))
{
e.Handled = true;
}
}
}
private void InvokeCompletionWindow(List<Tuple<string, string>> elementAutocompleteList, bool isAttribute)
{
var completionWindow = new CompletionWindow(textEditor.TextArea);
IList<ICompletionData> data = completionWindow.CompletionList.CompletionData;
if (elementAutocompleteList.Any())
{
foreach (var autocompleteelement in elementAutocompleteList)
{
data.Add(new XmlCompletionData(autocompleteelement.Item1, autocompleteelement.Item2, isAttribute));
}
completionWindow.Show();
completionWindow.Closed += delegate { completionWindow = null; };
}
}
public List<Tuple<string, string>> ProvidePossibleElementsAutocomplete(XmlElementInformation path)
{
List<Tuple<string, string>> result = new List<Tuple<string, string>>();
if (path.IsEmpty)
{
var xsdResultForGivenElementPath = XsdInformation.FirstOrDefault(x => x.IsRoot);
if (xsdResultForGivenElementPath != null)
{
result.Add(new Tuple<string, string>(xsdResultForGivenElementPath.Name, xsdResultForGivenElementPath.DataType));
}
}
else
{
StringBuilder xpath = new StringBuilder();
xpath.Append("/");
foreach (var element in path.Elements)
{
xpath.Append("/" + element.Name);
}
var xsdResultForGivenElementPath = XsdInformation.FirstOrDefault(x => x.XPathLikeKey.ToLowerInvariant() == xpath.ToString().ToLowerInvariant());
if (xsdResultForGivenElementPath != null)
{
foreach (var xsdInformationElement in xsdResultForGivenElementPath.Elements)
{
result.Add(new Tuple<string, string>(xsdInformationElement.Name, xsdInformationElement.DataType));
}
}
}
return result;
}
public List<Tuple<string, string>> ProvidePossibleAttributesAutocomplete(XmlElementInformation path)
{
List<Tuple<string, string>> result = new List<Tuple<string, string>>();
if (path.IsEmpty)
{
var xsdResultForGivenElementPath = XsdInformation.FirstOrDefault(x => x.IsRoot);
if (xsdResultForGivenElementPath != null)
{
foreach (var xsdInformationAttribute in xsdResultForGivenElementPath.Attributes)
{
result.Add(new Tuple<string, string>(xsdInformationAttribute.Name, xsdInformationAttribute.DataType));
}
}
}
else
{
StringBuilder xpath = new StringBuilder();
xpath.Append("/");
foreach (var element in path.Elements)
{
xpath.Append("/" + element.Name);
}
var xsdResultForGivenElementPath = XsdInformation.FirstOrDefault(x => x.XPathLikeKey.ToLowerInvariant() == xpath.ToString().ToLowerInvariant());
if (xsdResultForGivenElementPath != null)
{
foreach (var xsdInformationAttribute in xsdResultForGivenElementPath.Attributes)
{
result.Add(new Tuple<string, string>(xsdInformationAttribute.Name, xsdInformationAttribute.DataType));
}
}
}
return result;
}
private void IntelliSense(object sender, EventArgs eventArgs)
{
//var GetActiveElementStartPath = XmlParser.GetActiveElementStartPath(textEditor.Text, textEditor.TextArea.Caret.Offset);
//var GetParentElementPath = XmlParser.GetParentElementPath(textEditor.Text);
//var GetElementAtCursor = XmlParser.GetElementAtCursor(textEditor.Text, textEditor.TextArea.Caret.Offset);
//StringBuilder builder = new StringBuilder();
//builder.AppendLine("GetActiveElementStartPath: " + GetActiveElementStartPath.ToString());
//builder.AppendLine("GetParentElementPath: " + GetParentElementPath.ToString());
//builder.AppendLine("GetElementAtCursor: " + GetElementAtCursor.ToString());
//this.CurrentPath.Text = builder.ToString();
}
}
}