Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Rendering/HeightTreeNode.cs

Last change on this file was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 5.0 KB
Line 
1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Collections.Generic;
21using System.Diagnostics;
22
23using ICSharpCode.AvalonEdit.Document;
24
25namespace ICSharpCode.AvalonEdit.Rendering
26{
27  /// <summary>
28  /// A node in the text view's height tree.
29  /// </summary>
30  sealed class HeightTreeNode
31  {
32    internal readonly DocumentLine documentLine;
33    internal HeightTreeLineNode lineNode;
34   
35    internal HeightTreeNode left, right, parent;
36    internal bool color;
37   
38    internal HeightTreeNode()
39    {
40    }
41   
42    internal HeightTreeNode(DocumentLine documentLine, double height)
43    {
44      this.documentLine = documentLine;
45      this.totalCount = 1;
46      this.lineNode = new HeightTreeLineNode(height);
47      this.totalHeight = height;
48    }
49   
50    internal HeightTreeNode LeftMost {
51      get {
52        HeightTreeNode node = this;
53        while (node.left != null)
54          node = node.left;
55        return node;
56      }
57    }
58   
59    internal HeightTreeNode RightMost {
60      get {
61        HeightTreeNode node = this;
62        while (node.right != null)
63          node = node.right;
64        return node;
65      }
66    }
67   
68    /// <summary>
69    /// Gets the inorder successor of the node.
70    /// </summary>
71    internal HeightTreeNode Successor {
72      get {
73        if (right != null) {
74          return right.LeftMost;
75        } else {
76          HeightTreeNode node = this;
77          HeightTreeNode oldNode;
78          do {
79            oldNode = node;
80            node = node.parent;
81            // go up until we are coming out of a left subtree
82          } while (node != null && node.right == oldNode);
83          return node;
84        }
85      }
86    }
87   
88    /// <summary>
89    /// The number of lines in this node and its child nodes.
90    /// Invariant:
91    ///   totalCount = 1 + left.totalCount + right.totalCount
92    /// </summary>
93    internal int totalCount;
94   
95    /// <summary>
96    /// The total height of this node and its child nodes, excluding directly collapsed nodes.
97    /// Invariant:
98    ///   totalHeight = left.IsDirectlyCollapsed ? 0 : left.totalHeight
99    ///               + lineNode.IsDirectlyCollapsed ? 0 : lineNode.Height
100    ///               + right.IsDirectlyCollapsed ? 0 : right.totalHeight
101    /// </summary>
102    internal double totalHeight;
103   
104    /// <summary>
105    /// List of the sections that hold this node collapsed.
106    /// Invariant 1:
107    ///   For each document line in the range described by a CollapsedSection, exactly one ancestor
108    ///   contains that CollapsedSection.
109    /// Invariant 2:
110    ///   A CollapsedSection is contained either in left+middle or middle+right or just middle.
111    /// Invariant 3:
112    ///   Start and end of a CollapsedSection always contain the collapsedSection in their
113    ///   documentLine (middle node).
114    /// </summary>
115    internal List<CollapsedLineSection> collapsedSections;
116   
117    internal bool IsDirectlyCollapsed {
118      get {
119        return collapsedSections != null;
120      }
121    }
122   
123    internal void AddDirectlyCollapsed(CollapsedLineSection section)
124    {
125      if (collapsedSections == null) {
126        collapsedSections = new List<CollapsedLineSection>();
127        totalHeight = 0;
128      }
129      Debug.Assert(!collapsedSections.Contains(section));
130      collapsedSections.Add(section);
131    }
132   
133   
134    internal void RemoveDirectlyCollapsed(CollapsedLineSection section)
135    {
136      Debug.Assert(collapsedSections.Contains(section));
137      collapsedSections.Remove(section);
138      if (collapsedSections.Count == 0) {
139        collapsedSections = null;
140        totalHeight = lineNode.TotalHeight;
141        if (left != null)
142          totalHeight += left.totalHeight;
143        if (right != null)
144          totalHeight += right.totalHeight;
145      }
146    }
147   
148    #if DEBUG
149    public override string ToString()
150    {
151      return "[HeightTreeNode "
152        + documentLine.LineNumber + " CS=" + GetCollapsedSections(collapsedSections)
153        + " Line.CS=" + GetCollapsedSections(lineNode.collapsedSections)
154        + " Line.Height=" + lineNode.height
155        + " TotalHeight=" + totalHeight
156        + "]";
157    }
158   
159    static string GetCollapsedSections(List<CollapsedLineSection> list)
160    {
161      if (list == null)
162        return "{}";
163      return "{" +
164        string.Join(",",
165                    list.ConvertAll(cs=>cs.ID).ToArray())
166        + "}";
167    }
168    #endif
169  }
170}
Note: See TracBrowser for help on using the repository browser.