aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Scripting/Tango.Scripting.Editors/CodeCompletion/OverloadViewer.cs
blob: f70229a8dabef04962012b1c937c7b876d7a3b53 (plain)
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
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Tango.Scripting.Editors.CodeCompletion
{
	/// <summary>
	/// Represents a text between "Up" and "Down" buttons.
	/// </summary>
	public class OverloadViewer : Control
	{
		static OverloadViewer()
		{
			DefaultStyleKeyProperty.OverrideMetadata(typeof(OverloadViewer),
			                                         new FrameworkPropertyMetadata(typeof(OverloadViewer)));
		}
		
		/// <summary>
		/// The text property.
		/// </summary>
		public static readonly DependencyProperty TextProperty =
			DependencyProperty.Register("Text", typeof(string), typeof(OverloadViewer));
		
		/// <summary>
		/// Gets/Sets the text between the Up and Down buttons.
		/// </summary>
		public string Text {
			get { return (string)GetValue(TextProperty); }
			set { SetValue(TextProperty, value); }
		}
		
		/// <inheritdoc/>
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			
			Button upButton = (Button)this.Template.FindName("PART_UP", this);
			upButton.Click += (sender, e) => {
				e.Handled = true;
				ChangeIndex(-1);
			};
			
			Button downButton = (Button)this.Template.FindName("PART_DOWN", this);
			downButton.Click += (sender, e) => {
				e.Handled = true;
				ChangeIndex(+1);
			};
		}
		
		/// <summary>
		/// The ItemProvider property.
		/// </summary>
		public static readonly DependencyProperty ProviderProperty =
			DependencyProperty.Register("Provider", typeof(IOverloadProvider), typeof(OverloadViewer));
		
		/// <summary>
		/// Gets/Sets the item provider.
		/// </summary>
		public IOverloadProvider Provider {
			get { return (IOverloadProvider)GetValue(ProviderProperty); }
			set { SetValue(ProviderProperty, value); }
		}
		
		/// <summary>
		/// Changes the selected index.
		/// </summary>
		/// <param name="relativeIndexChange">The relative index change - usual values are +1 or -1.</param>
		public void ChangeIndex(int relativeIndexChange)
		{
			IOverloadProvider p = this.Provider;
			if (p != null) {
				int newIndex = p.SelectedIndex + relativeIndexChange;
				if (newIndex < 0)
					newIndex = p.Count - 1;
				if (newIndex >= p.Count)
					newIndex = 0;
				p.SelectedIndex = newIndex;
			}
		}
	}
	
	sealed class CollapseIfSingleOverloadConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return ((int)value < 2) ? Visibility.Collapsed : Visibility.Visible;
		}
		
		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}
}
"w"> public static OffsetChangeMap FromSingleElement(OffsetChangeMapEntry entry) { return new OffsetChangeMap(new OffsetChangeMapEntry[] { entry }, true); } bool isFrozen; /// <summary> /// Creates a new OffsetChangeMap instance. /// </summary> public OffsetChangeMap() { } internal OffsetChangeMap(int capacity) : base(new List<OffsetChangeMapEntry>(capacity)) { } private OffsetChangeMap(IList<OffsetChangeMapEntry> entries, bool isFrozen) : base(entries) { this.isFrozen = isFrozen; } /// <summary> /// Gets the new offset where the specified offset moves after this document change. /// </summary> public int GetNewOffset(int offset, AnchorMovementType movementType) { IList<OffsetChangeMapEntry> items = this.Items; int count = items.Count; for (int i = 0; i < count; i++) { offset = items[i].GetNewOffset(offset, movementType); } return offset; } /// <summary> /// Gets whether this OffsetChangeMap is a valid explanation for the specified document change. /// </summary> public bool IsValidForDocumentChange(int offset, int removalLength, int insertionLength) { int endOffset = offset + removalLength; foreach (OffsetChangeMapEntry entry in this) { // check that ChangeMapEntry is in valid range for this document change if (entry.Offset < offset || entry.Offset + entry.RemovalLength > endOffset) return false; endOffset += entry.InsertionLength - entry.RemovalLength; } // check that the total delta matches return endOffset == offset + insertionLength; } /// <summary> /// Calculates the inverted OffsetChangeMap (used for the undo operation). /// </summary> public OffsetChangeMap Invert() { if (this == Empty) return this; OffsetChangeMap newMap = new OffsetChangeMap(this.Count); for (int i = this.Count - 1; i >= 0; i--) { OffsetChangeMapEntry entry = this[i]; // swap InsertionLength and RemovalLength newMap.Add(new OffsetChangeMapEntry(entry.Offset, entry.InsertionLength, entry.RemovalLength)); } return newMap; } /// <inheritdoc/> protected override void ClearItems() { CheckFrozen(); base.ClearItems(); } /// <inheritdoc/> protected override void InsertItem(int index, OffsetChangeMapEntry item) { CheckFrozen(); base.InsertItem(index, item); } /// <inheritdoc/> protected override void RemoveItem(int index) { CheckFrozen(); base.RemoveItem(index); } /// <inheritdoc/> protected override void SetItem(int index, OffsetChangeMapEntry item) { CheckFrozen(); base.SetItem(index, item); } void CheckFrozen() { if (isFrozen) throw new InvalidOperationException("This instance is frozen and cannot be modified."); } /// <summary> /// Gets if this instance is frozen. Frozen instances are immutable and thus thread-safe. /// </summary> public bool IsFrozen { get { return isFrozen; } } /// <summary> /// Freezes this instance. /// </summary> public void Freeze() { isFrozen = true; } } /// <summary> /// An entry in the OffsetChangeMap. /// This represents the offset of a document change (either insertion or removal, not both at once). /// </summary> [Serializable] public struct OffsetChangeMapEntry : IEquatable<OffsetChangeMapEntry> { readonly int offset; // MSB: DefaultAnchorMovementIsBeforeInsertion readonly uint insertionLengthWithMovementFlag; // MSB: RemovalNeverCausesAnchorDeletion; other 31 bits: RemovalLength readonly uint removalLengthWithDeletionFlag; /// <summary> /// The offset at which the change occurs. /// </summary> public int Offset { get { return offset; } } /// <summary> /// The number of characters inserted. /// Returns 0 if this entry represents a removal. /// </summary> public int InsertionLength { get { return (int)(insertionLengthWithMovementFlag & 0x7fffffff); } } /// <summary> /// The number of characters removed. /// Returns 0 if this entry represents an insertion. /// </summary> public int RemovalLength { get { return (int)(removalLengthWithDeletionFlag & 0x7fffffff); } } /// <summary> /// Gets whether the removal should not cause any anchor deletions. /// </summary> public bool RemovalNeverCausesAnchorDeletion { get { return (removalLengthWithDeletionFlag & 0x80000000) != 0; } } /// <summary> /// Gets whether default anchor movement causes the anchor to stay in front of the caret. /// </summary> public bool DefaultAnchorMovementIsBeforeInsertion { get { return (insertionLengthWithMovementFlag & 0x80000000) != 0; } } /// <summary> /// Gets the new offset where the specified offset moves after this document change. /// </summary> public int GetNewOffset(int oldOffset, AnchorMovementType movementType) { int insertionLength = this.InsertionLength; int removalLength = this.RemovalLength; if (!(removalLength == 0 && oldOffset == offset)) { // we're getting trouble (both if statements in here would apply) // if there's no removal and we insert at the offset // -> we'd need to disambiguate by movementType, which is handled after the if // offset is before start of change: no movement if (oldOffset <= offset) return oldOffset; // offset is after end of change: movement by normal delta if (oldOffset >= offset + removalLength) return oldOffset + insertionLength - removalLength; } // we reach this point if // a) the oldOffset is inside the deleted segment // b) there was no removal and we insert at the caret position if (movementType == AnchorMovementType.AfterInsertion) return offset + insertionLength; else if (movementType == AnchorMovementType.BeforeInsertion) return offset; else return this.DefaultAnchorMovementIsBeforeInsertion ? offset : offset + insertionLength; } /// <summary> /// Creates a new OffsetChangeMapEntry instance. /// </summary> public OffsetChangeMapEntry(int offset, int removalLength, int insertionLength) { ThrowUtil.CheckNotNegative(offset, "offset"); ThrowUtil.CheckNotNegative(removalLength, "removalLength"); ThrowUtil.CheckNotNegative(insertionLength, "insertionLength"); this.offset = offset; this.removalLengthWithDeletionFlag = (uint)removalLength; this.insertionLengthWithMovementFlag = (uint)insertionLength; } /// <summary> /// Creates a new OffsetChangeMapEntry instance. /// </summary> public OffsetChangeMapEntry(int offset, int removalLength, int insertionLength, bool removalNeverCausesAnchorDeletion, bool defaultAnchorMovementIsBeforeInsertion) : this(offset, removalLength, insertionLength) { if (removalNeverCausesAnchorDeletion) this.removalLengthWithDeletionFlag |= 0x80000000; if (defaultAnchorMovementIsBeforeInsertion) this.insertionLengthWithMovementFlag |= 0x80000000; } /// <inheritdoc/> public override int GetHashCode() { unchecked { return offset + 3559 * (int)insertionLengthWithMovementFlag + 3571 * (int)removalLengthWithDeletionFlag; } } /// <inheritdoc/> public override bool Equals(object obj) { return obj is OffsetChangeMapEntry && this.Equals((OffsetChangeMapEntry)obj); } /// <inheritdoc/> public bool Equals(OffsetChangeMapEntry other) { return offset == other.offset && insertionLengthWithMovementFlag == other.insertionLengthWithMovementFlag && removalLengthWithDeletionFlag == other.removalLengthWithDeletionFlag; } /// <summary> /// Tests the two entries for equality. /// </summary> public static bool operator ==(OffsetChangeMapEntry left, OffsetChangeMapEntry right) { return left.Equals(right); } /// <summary> /// Tests the two entries for inequality. /// </summary> public static bool operator !=(OffsetChangeMapEntry left, OffsetChangeMapEntry right) { return !left.Equals(right); } } }