You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.7 KiB

15 years ago
15 years ago
15 years ago
15 years ago
  1. // Copyright (c) 2011 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. using System;
  19. using System.Collections.Generic;
  20. using System.Windows;
  21. using ICSharpCode.AvalonEdit.Rendering;
  22. namespace ICSharpCode.ILSpy.TextView
  23. {
  24. using Pair = KeyValuePair<int, Lazy<UIElement>>;
  25. /// <summary>
  26. /// Embeds UIElements in the text output.
  27. /// </summary>
  28. sealed class UIElementGenerator : VisualLineElementGenerator, IComparer<Pair>
  29. {
  30. /// <summary>
  31. /// The list of embedded UI elements to be displayed.
  32. /// We store this as a sorted list of (offset, Lazy&lt;UIElement&gt;) pairs.
  33. /// The "Lazy" part is used to create UIElements on demand (and thus on the UI thread, not on the decompiler thread).
  34. /// </summary>
  35. public List<Pair> UIElements;
  36. public override int GetFirstInterestedOffset(int startOffset)
  37. {
  38. if (this.UIElements == null)
  39. return -1;
  40. int r = this.UIElements.BinarySearch(new Pair(startOffset, null), this);
  41. // If the element isn't found, BinarySearch returns the complement of "insertion position".
  42. // We use this to find the next element (if there wasn't any exact match).
  43. if (r < 0)
  44. r = ~r;
  45. if (r < this.UIElements.Count)
  46. return this.UIElements[r].Key;
  47. else
  48. return -1;
  49. }
  50. public override VisualLineElement ConstructElement(int offset)
  51. {
  52. if (this.UIElements == null)
  53. return null;
  54. int r = UIElements.BinarySearch(new Pair(offset, null), this);
  55. if (r >= 0)
  56. return new InlineObjectElement(0, this.UIElements[r].Value.Value);
  57. else
  58. return null;
  59. }
  60. int IComparer<Pair>.Compare(Pair x, Pair y)
  61. {
  62. // Compare (offset,Lazy<UIElement>) pairs by the offset.
  63. // Used in BinarySearch()
  64. return x.Key.CompareTo(y.Key);
  65. }
  66. }
  67. }