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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
|
//
// BaseXmlEditorExtension.cs
//
// Author:
// Michael Hutchinson <mhutchinson@novell.com>
//
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using MonoDevelop.Core;
using MonoDevelop.DesignerSupport;
using MonoDevelop.Projects.Dom;
using MonoDevelop.Ide.Gui.Content;
using MonoDevelop.Ide.CodeCompletion;
using MonoDevelop.Xml.StateEngine;
using MonoDevelop.XmlEditor.Completion;
using MonoDevelop.Ide;
using MonoDevelop.Components;
using Gtk;
namespace MonoDevelop.XmlEditor.Gui
{
public abstract class BaseXmlEditorExtension : CompletionTextEditorExtension, IPathedDocument, IOutlinedDocument
{
DocumentStateTracker<Parser> tracker;
ParsedDocument lastCU;
XDocType docType;
MonoDevelop.Ide.Gui.Components.PadTreeView outlineTreeView;
Gtk.TreeStore outlineTreeStore;
#region Setup and teardown
protected virtual RootState CreateRootState ()
{
return new XmlFreeState ();
}
public override void Initialize ()
{
base.Initialize ();
Parser parser = new Parser (CreateRootState (), false);
tracker = new DocumentStateTracker<Parser> (parser, Editor);
MonoDevelop.Projects.Dom.Parser.ProjectDomService.ParsedDocumentUpdated += OnParseInformationChanged;
if (Document.ParsedDocument != null) {
lastCU = Document.ParsedDocument;
OnParsedDocumentUpdated ();
}
}
public override void Dispose ()
{
if (tracker != null) {
tracker = null;
MonoDevelop.Projects.Dom.Parser.ProjectDomService.ParsedDocumentUpdated
-= OnParseInformationChanged;
base.Dispose ();
}
}
void OnParseInformationChanged (object sender, MonoDevelop.Projects.Dom.ParsedDocumentEventArgs args)
{
if (this.FileName == args.FileName && args.ParsedDocument != null) {
lastCU = args.ParsedDocument;
OnParsedDocumentUpdated ();
}
}
protected virtual void OnParsedDocumentUpdated ()
{
RefreshOutline ();
//use the doctype to select a completion schema
var doc = CU as XmlParsedDocument;
bool found = false;
if (doc != null && doc.XDocument != null) {
foreach (XNode node in doc.XDocument.Nodes) {
if (node is XDocType) {
DocType = (XDocType)node;
found = true;
break;
}
//cannot validly have a doctype after these nodes
if (node is XElement || node is XCData)
break;
}
if (!found)
DocType = null;
}
}
#endregion
#region Convenience accessors
protected ParsedDocument CU {
get { return lastCU; }
}
protected ITextBuffer Buffer {
get {
if (Document == null)
throw new InvalidOperationException ("Editor extension not yet initialized");
return Document.GetContent<ITextBuffer> ();
}
}
protected IEditableTextBuffer EditableBuffer {
get {
if (Document == null)
throw new InvalidOperationException ("Editor extension not yet initialized");
return Document.GetContent<IEditableTextBuffer> ();
}
}
protected DocumentStateTracker<Parser> Tracker {
get { return tracker; }
}
protected string GetBufferText (DomRegion region)
{
MonoDevelop.Ide.Gui.Content.ITextBuffer buf = Buffer;
int start = buf.GetPositionFromLineColumn (region.Start.Line, region.Start.Column);
int end = buf.GetPositionFromLineColumn (region.End.Line, region.End.Column);
if (end > start && start >= 0)
return buf.GetText (start, end);
else
return null;
}
#endregion
protected XDocType DocType {
get { return docType; }
set {
if (docType == value)
return;
docType = value;
OnDocTypeChanged ();
}
}
protected virtual void OnDocTypeChanged ()
{
}
#region Code completion
public override ICompletionDataList CodeCompletionCommand (CodeCompletionContext completionContext)
{
int pos = completionContext.TriggerOffset;
if (pos <= 0)
return null;
int triggerWordLength = 0;
tracker.UpdateEngine ();
return HandleCodeCompletion ((CodeCompletionContext) completionContext, true, ref triggerWordLength);
}
public override ICompletionDataList HandleCodeCompletion (
CodeCompletionContext completionContext, char completionChar, ref int triggerWordLength)
{
int pos = completionContext.TriggerOffset;
char ch = CompletionWidget != null ? CompletionWidget.GetChar (pos - 1) : Editor.GetCharAt (pos - 1);
if (pos > 0 && ch == completionChar) {
tracker.UpdateEngine ();
return HandleCodeCompletion ((CodeCompletionContext) completionContext,
false, ref triggerWordLength);
}
return null;
}
protected virtual ICompletionDataList HandleCodeCompletion (
CodeCompletionContext completionContext, bool forced, ref int triggerWordLength)
{
IEditableTextBuffer buf = this.EditableBuffer;
// completionChar may be a space even if the current char isn't, when ctrl-space is fired t
DomLocation currentLocation = new DomLocation (completionContext.TriggerLine, completionContext.TriggerLineOffset);
char currentChar = completionContext.TriggerOffset < 1? ' ' : buf.GetCharAt (completionContext.TriggerOffset - 1);
char previousChar = completionContext.TriggerOffset < 2? ' ' : buf.GetCharAt (completionContext.TriggerOffset - 2);
LoggingService.LogDebug ("Attempting completion for state '{0}'x{1}, previousChar='{2}',"
+ " currentChar='{3}', forced='{4}'", tracker.Engine.CurrentState,
tracker.Engine.CurrentStateLength, previousChar, currentChar, forced);
//closing tag completion
if (tracker.Engine.CurrentState is XmlFreeState && currentChar == '>') {
//get name of current node in document that's being ended
XElement el = tracker.Engine.Nodes.Peek () as XElement;
if (el != null && el.Region.End >= currentLocation && !el.IsClosed && el.IsNamed) {
string tag = String.Concat ("</", el.Name.FullName, ">");
if (XmlEditorOptions.AutoCompleteElements) {
buf.BeginAtomicUndo ();
buf.InsertText (buf.CursorPosition, tag);
buf.CursorPosition -= tag.Length;
buf.EndAtomicUndo ();
return null;
} else {
CompletionDataList cp = new CompletionDataList ();
cp.Add (new XmlTagCompletionData (tag, 0, true));
return cp;
}
}
return null;
}
// Auto insert '>' when '/' is typed inside tag state (for quick tag closing)
//FIXME: avoid doing this when the next non-whitespace char is ">" or ignore the next ">" typed
if (XmlEditorOptions.AutoInsertFragments && tracker.Engine.CurrentState is XmlTagState && currentChar == '/') {
buf.BeginAtomicUndo ();
buf.InsertText (buf.CursorPosition, ">");
buf.EndAtomicUndo ();
return null;
}
//element completion
if (currentChar == '<' && tracker.Engine.CurrentState is XmlFreeState) {
CompletionDataList list = new CompletionDataList ();
GetElementCompletions (list);
AddCloseTag (list, Tracker.Engine.Nodes);
return list.Count > 0? list : null;
}
//entity completion
if (currentChar == '&' && (tracker.Engine.CurrentState is XmlFreeState ||
tracker.Engine.CurrentState is XmlAttributeValueState))
{
CompletionDataList list = new CompletionDataList ();
//TODO: need to tweak semicolon insertion
list.Add ("apos").Description = "'";
list.Add ("quot").Description = "\"";
list.Add ("lt").Description = "<";
list.Add ("gt").Description = ">";
list.Add ("amp").Description = "&";
//not sure about these "completions". they're more like
//shortcuts than completions but they're pretty useful
list.Add ("'").CompletionText = "apos;";
list.Add ("\"").CompletionText = "quot;";
list.Add ("<").CompletionText = "lt;";
list.Add (">").CompletionText = "gt;";
list.Add ("&").CompletionText = "amp;";
GetEntityCompletions (list);
return list;
}
//doctype completion
if (tracker.Engine.CurrentState is XmlDocTypeState) {
if (tracker.Engine.CurrentStateLength == 1) {
CompletionDataList list = GetDocTypeCompletions ();
if (list != null && list.Count > 0)
return list;
}
return null;
}
//attribute name completion
if ((forced && Tracker.Engine.Nodes.Peek () is IAttributedXObject && !tracker.Engine.Nodes.Peek ().IsEnded)
|| (Tracker.Engine.CurrentState is XmlNameState
&& Tracker.Engine.CurrentState.Parent is XmlAttributeState
&& Tracker.Engine.CurrentStateLength == 1)
) {
IAttributedXObject attributedOb = (Tracker.Engine.Nodes.Peek () as IAttributedXObject) ??
Tracker.Engine.Nodes.Peek (1) as IAttributedXObject;
if (attributedOb == null)
return null;
//attributes
if (attributedOb.Name.IsValid && (forced ||
(char.IsWhiteSpace (previousChar) && char.IsLetter (currentChar))))
{
if (!forced)
triggerWordLength = 1;
var existingAtts = new Dictionary<string,string> (StringComparer.OrdinalIgnoreCase);
foreach (XAttribute att in attributedOb.Attributes) {
existingAtts [att.Name.FullName] = att.Value ?? string.Empty;
}
return GetAttributeCompletions (attributedOb, existingAtts);
}
}
//attribute value completion
//determine whether to trigger completion within attribute values quotes
if ((Tracker.Engine.CurrentState is XmlDoubleQuotedAttributeValueState
|| Tracker.Engine.CurrentState is XmlSingleQuotedAttributeValueState)
//trigger on the opening quote
&& (Tracker.Engine.CurrentStateLength == 0
//or trigger on first letter of value, if unforced
|| (!forced && Tracker.Engine.CurrentStateLength == 1))
)
{
XAttribute att = (XAttribute) Tracker.Engine.Nodes.Peek ();
if (att.IsNamed) {
IAttributedXObject attributedOb = Tracker.Engine.Nodes.Peek (1) as IAttributedXObject;
if (attributedOb == null)
return null;
char next = ' ';
if (completionContext.TriggerOffset < buf.Length)
next = buf.GetCharAt (completionContext.TriggerOffset);
char compareChar = (Tracker.Engine.CurrentStateLength == 0)? currentChar : previousChar;
if ((compareChar == '"' || compareChar == '\'')
&& (next == compareChar || char.IsWhiteSpace (next))
) {
//if triggered by first letter of value, grab that letter
if (Tracker.Engine.CurrentStateLength == 1)
triggerWordLength = 1;
return GetAttributeValueCompletions (attributedOb, att);
}
}
}
// if (Tracker.Engine.CurrentState is XmlFreeState) {
// if (line < 3) {
// cp.Add ("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
// }
if (forced && Tracker.Engine.CurrentState is XmlFreeState) {
CompletionDataList list = new CompletionDataList ();
MonoDevelop.Ide.CodeTemplates.CodeTemplateService.AddCompletionDataForFileName (Document.Name, list);
return list.Count > 0? list : null;
}
return null;
}
protected virtual void GetElementCompletions (CompletionDataList list)
{
}
protected virtual CompletionDataList GetAttributeCompletions (IAttributedXObject attributedOb,
Dictionary<string, string> existingAtts)
{
return null;
}
protected virtual CompletionDataList GetAttributeValueCompletions (IAttributedXObject attributedOb, XAttribute att)
{
return null;
}
protected virtual void GetEntityCompletions (CompletionDataList list)
{
}
protected virtual CompletionDataList GetDocTypeCompletions ()
{
return null;
}
protected IEnumerable<XName> GetParentElementNames (int skip)
{
foreach (XObject obj in tracker.Engine.Nodes) {
if (skip > 0) {
skip--;
continue;
}
XElement el = obj as XElement;
if (el == null || !el.IsNamed)
yield break;
yield return el.Name;
}
}
protected XName GetParentElementName (int skip)
{
foreach (XObject obj in tracker.Engine.Nodes) {
if (skip > 0) {
skip--;
continue;
}
XElement el = obj as XElement;
return el != null? el.Name : new XName ();
}
return new XName ();
}
protected XElement GetParentElement (int skip)
{
foreach (XObject obj in tracker.Engine.Nodes) {
if (skip > 0) {
skip--;
continue;
}
return obj as XElement;
}
return null;
}
protected static void AddCloseTag (CompletionDataList completionList, NodeStack stack)
{
//FIXME: search forward to see if tag's closed already
List<XElement> elements = new List<XElement> ();
foreach (XObject ob in stack) {
XElement el = ob as XElement;
if (el == null)
continue;
if (!el.IsNamed || el.IsClosed)
return;
if (elements.Count == 0) {
string name = el.Name.FullName;
completionList.Add ("/" + name + ">", Gtk.Stock.GoBack,
GettextCatalog.GetString ("Closing tag for '{0}'", name));
} else {
foreach (XElement listEl in elements) {
if (listEl.Name == el.Name)
return;
}
completionList.Add (new XmlMultipleClosingTagCompletionData (el, elements.ToArray ()));
}
elements.Add (el);
}
}
protected static void AddMiscBeginTags (CompletionDataList list)
{
list.Add ("!--", "md-literal", GettextCatalog.GetString ("Comment"));
list.Add ("![CDATA[", "md-literal", GettextCatalog.GetString ("Character data"));
}
#endregion
#region IPathedDocument
PathEntry[] currentPath;
bool pathUpdateQueued = false;
public override void CursorPositionChanged ()
{
if (pathUpdateQueued)
return;
pathUpdateQueued = true;
GLib.Timeout.Add (500, delegate {
pathUpdateQueued = false;
UpdatePath ();
return false;
});
}
public void SelectPath (int depth)
{
SelectPath (depth, false);
}
public void SelectPathContents (int depth)
{
SelectPath (depth, true);
}
void SelectPath (int depth, bool contents)
{
//clone the parser and put it in tree mode
Parser treeParser = this.tracker.Engine.GetTreeParser ();
//locate the node
List<XObject> path = new List<XObject> (treeParser.Nodes);
//note: list is backwards, and we want ignore the root XDocument
XObject ob = path [path.Count - (depth + 2)];
XNode node = ob as XNode;
XElement el = node as XElement;
//hoist this as it may not be cheap to evaluate (P/Invoke), but won't be changing during the loop
int textLen = Editor.Length;
//run the parser until the tag's closed, or we move to its sibling or parent
if (node != null) {
while (node.NextSibling == null &&
treeParser.Position < textLen && treeParser.Nodes.Peek () != ob.Parent)
{
char c = Editor.GetCharAt (treeParser.Position);
treeParser.Push (c);
if (el != null && el.IsClosed && el.ClosingTag.IsComplete)
break;
}
} else {
while (ob.Region.End < ob.Region.Start &&
treeParser.Position < textLen && treeParser.Nodes.Peek () != ob.Parent)
{
char c = Editor.GetCharAt (treeParser.Position);
treeParser.Push (c);
}
}
if (el == null) {
MonoDevelop.Core.LoggingService.LogDebug ("Selecting {0}", ob.Region);
EditorSelect (ob.Region);
}
else if (el.IsClosed) {
MonoDevelop.Core.LoggingService.LogDebug ("Selecting {0}-{1}",
el.Region.Start, el.ClosingTag.Region.End);
if (el.IsSelfClosing)
contents = false;
//pick out the locations, with some offsets to account for the parsing model
DomLocation s = contents? el.Region.End : el.Region.Start;
DomLocation e = contents? el.ClosingTag.Region.Start : el.ClosingTag.Region.End;
EditorSelect (new DomRegion (s, e));
} else {
MonoDevelop.Core.LoggingService.LogDebug ("No end tag found for selection");
}
}
protected void EditorSelect (DomRegion region)
{
int s = Editor.Document.LocationToOffset (region.Start.Line - 1, region.Start.Column - 1);
int e = Editor.Document.LocationToOffset (region.End.Line - 1, region.End.Column - 1);
if (s > -1 && e > s) {
Editor.SetSelection (s, e);
Editor.ScrollTo (s);
}
}
public event EventHandler<DocumentPathChangedEventArgs> PathChanged;
public Gtk.Widget CreatePathWidget (int index)
{
Menu menu = new Menu ();
MenuItem mi = new MenuItem (GettextCatalog.GetString ("Select"));
mi.Activated += delegate {
SelectPath (index);
};
menu.Add (mi);
mi = new MenuItem (GettextCatalog.GetString ("Select contents"));
mi.Activated += delegate {
SelectPathContents (index);
};
menu.Add (mi);
menu.ShowAll ();
return menu;
}
protected void OnPathChanged (PathEntry[] oldPath)
{
if (PathChanged != null)
PathChanged (this, new DocumentPathChangedEventArgs (oldPath));
}
protected XName GetCompleteName ()
{
Debug.Assert (this.tracker.Engine.CurrentState is XmlNameState);
int end = this.tracker.Engine.Position;
int start = end - this.tracker.Engine.CurrentStateLength;
int mid = -1;
int limit = Math.Min (Editor.Length, end + 35);
//try to find the end of the name, but don't go too far
for (; end < limit; end++) {
char c = Editor.GetCharAt (end);
if (c == ':') {
if (mid == -1)
mid = end;
else
break;
} else if (!XmlChar.IsNameChar (c))
break;
}
if (mid > 0 && end > mid + 1) {
return new XName (Editor.GetTextBetween (start, mid), Editor.GetTextBetween (mid + 1, end));
} else {
return new XName (Editor.GetTextBetween (start, end));
}
}
/*
protected XNode GetFullNode ()
{
Parser p = (Parser) ((ICloneable)this.tracker.Engine).Clone ();
//run the parser until the tag's closed, or we move to its sibling or parent
if (node != null) {
while (node.NextSibling == null &&
treeParser.Position < textLen && treeParser.Nodes.Peek () != ob.Parent)
{
char c = Editor.GetCharAt (treeParser.Position);
treeParser.Push (c);
if (el != null && el.IsClosed && el.ClosingTag.IsComplete)
break;
}
}
}
*/
List<XObject> GetCurrentPath ()
{
if (this.tracker == null)
return null;
this.tracker.UpdateEngine ();
List<XObject> path = new List<XObject> (this.tracker.Engine.Nodes);
//remove the root XDocument
path.RemoveAt (path.Count - 1);
//complete incomplete XName if present
if (this.tracker.Engine.CurrentState is XmlNameState && path[0] is INamedXObject) {
path[0] = (XObject) path[0].ShallowCopy ();
XName completeName = GetCompleteName ();
((INamedXObject)path[0]).Name = completeName;
}
path.Reverse ();
return path;
}
void UpdatePath ()
{
List<XObject> l = GetCurrentPath ();
if (l == null)
return;
//build the list
PathEntry[] path = new PathEntry[l.Count];
for (int i = 0; i < l.Count; i++) {
if (l[i].FriendlyPathRepresentation == null) System.Console.WriteLine(l[i].GetType ());
path[i] = new PathEntry (l[i].FriendlyPathRepresentation ?? "<>");
}
PathEntry[] oldPath = currentPath;
currentPath = path;
OnPathChanged (oldPath);
}
public PathEntry[] CurrentPath {
get { return currentPath; }
}
#endregion
#region IOutlinedDocument
bool refreshingOutline = false;
Gtk.Widget IOutlinedDocument.GetOutlineWidget ()
{
if (outlineTreeView != null)
return outlineTreeView;
outlineTreeStore = new Gtk.TreeStore (typeof (object));
outlineTreeView = new MonoDevelop.Ide.Gui.Components.PadTreeView (outlineTreeStore);
System.Reflection.PropertyInfo prop = typeof (Gtk.TreeView).GetProperty ("EnableTreeLines");
if (prop != null)
prop.SetValue (outlineTreeView, true, null);
outlineTreeView.Realized += delegate { refillOutlineStore (); };
InitializeOutlineColumns (outlineTreeView);
outlineTreeView.HeadersVisible = false;
outlineTreeView.Selection.Changed += delegate {
Gtk.TreeIter iter;
if (!outlineTreeView.Selection.GetSelected (out iter))
return;
OutlineSelectionChanged (outlineTreeStore.GetValue (iter, 0));
};
refillOutlineStore ();
var sw = new MonoDevelop.Components.CompactScrolledWindow ();
sw.Add (outlineTreeView);
sw.ShowAll ();
return sw;
}
protected virtual void InitializeOutlineColumns (MonoDevelop.Ide.Gui.Components.PadTreeView outlineTree)
{
outlineTree.TextRenderer.Xpad = 0;
outlineTree.TextRenderer.Ypad = 0;
outlineTree.AppendColumn ("Node", outlineTree.TextRenderer, new Gtk.TreeCellDataFunc (outlineTreeDataFunc));
}
protected virtual void OutlineSelectionChanged (object selection)
{
SelectNode ((XNode)selection);
}
void RefreshOutline ()
{
if (refreshingOutline || outlineTreeView == null )
return;
refreshingOutline = true;
GLib.Timeout.Add (3000, refillOutlineStoreIdleHandler);
}
bool refillOutlineStoreIdleHandler ()
{
refreshingOutline = false;
refillOutlineStore ();
return false;
}
protected virtual void RefillOutlineStore (ParsedDocument doc, Gtk.TreeStore store)
{
XDocument xdoc = ((XmlParsedDocument)doc).XDocument;
if (xdoc == null)
return;
BuildTreeChildren (store, Gtk.TreeIter.Zero, xdoc);
}
void refillOutlineStore ()
{
DispatchService.AssertGuiThread ();
Gdk.Threads.Enter ();
refreshingOutline = false;
if (outlineTreeStore == null || !outlineTreeView.IsRealized)
return;
outlineTreeStore.Clear ();
if (CU != null) {
DateTime start = DateTime.Now;
RefillOutlineStore (CU, outlineTreeStore);
outlineTreeView.ExpandAll ();
outlineTreeView.ExpandAll ();
LoggingService.LogDebug ("Built outline in {0}ms", (DateTime.Now - start).Milliseconds);
}
Gdk.Threads.Leave ();
}
void IOutlinedDocument.ReleaseOutlineWidget ()
{
if (outlineTreeView == null)
return;
Gtk.ScrolledWindow w = (Gtk.ScrolledWindow) outlineTreeView.Parent;
w.Destroy ();
outlineTreeView.Destroy ();
outlineTreeStore.Dispose ();
outlineTreeStore = null;
outlineTreeView = null;
}
static void BuildTreeChildren (Gtk.TreeStore store, Gtk.TreeIter parent, XContainer p)
{
foreach (XNode n in p.Nodes) {
Gtk.TreeIter childIter;
if (!parent.Equals (Gtk.TreeIter.Zero))
childIter = store.AppendValues (parent, n);
else
childIter = store.AppendValues (n);
XContainer c = n as XContainer;
if (c != null && c.FirstChild != null)
BuildTreeChildren (store, childIter, c);
}
}
void outlineTreeDataFunc (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
{
Gtk.CellRendererText txtRenderer = (Gtk.CellRendererText) cell;
XNode n = (XNode) model.GetValue (iter, 0);
txtRenderer.Text = n.FriendlyPathRepresentation;
}
public void SelectNode (XNode n)
{
MonoDevelop.Projects.Dom.DomRegion region = n.Region;
XElement el = n as XElement;
if (el != null && el.IsClosed && el.ClosingTag.Region.End > region.End) {
region.End = el.ClosingTag.Region.End;
}
EditorSelect (region);
}
#endregion
}
}
|