|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using OmniSharp.Extensions.LanguageServer.Protocol.Models; |
| 9 | + |
| 10 | +namespace Microsoft.PowerShell.EditorServices |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// A class that holds the information for a foldable region of text in a document |
| 14 | + /// </summary> |
| 15 | + public class FoldingReference: IComparable<FoldingReference> |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// The zero-based line number from where the folded range starts. |
| 19 | + /// </summary> |
| 20 | + public int StartLine { get; set; } |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// The zero-based character offset from where the folded range starts. If not defined, defaults to the length of the start line. |
| 24 | + /// </summary> |
| 25 | + public int StartCharacter { get; set; } = 0; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// The zero-based line number where the folded range ends. |
| 29 | + /// </summary> |
| 30 | + public int EndLine { get; set; } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// The zero-based character offset before the folded range ends. If not defined, defaults to the length of the end line. |
| 34 | + /// </summary> |
| 35 | + public int EndCharacter { get; set; } = 0; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Describes the kind of the folding range such as `comment' or 'region'. |
| 39 | + /// </summary> |
| 40 | + public FoldingRangeKind? Kind { get; set; } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// A custom comparable method which can properly sort FoldingReference objects |
| 44 | + /// </summary> |
| 45 | + public int CompareTo(FoldingReference that) { |
| 46 | + // Initially look at the start line |
| 47 | + if (this.StartLine < that.StartLine) { return -1; } |
| 48 | + if (this.StartLine > that.StartLine) { return 1; } |
| 49 | + |
| 50 | + // They have the same start line so now consider the end line. |
| 51 | + // The biggest line range is sorted first |
| 52 | + if (this.EndLine > that.EndLine) { return -1; } |
| 53 | + if (this.EndLine < that.EndLine) { return 1; } |
| 54 | + |
| 55 | + // They have the same lines, but what about character offsets |
| 56 | + if (this.StartCharacter < that.StartCharacter) { return -1; } |
| 57 | + if (this.StartCharacter > that.StartCharacter) { return 1; } |
| 58 | + if (this.EndCharacter < that.EndCharacter) { return -1; } |
| 59 | + if (this.EndCharacter > that.EndCharacter) { return 1; } |
| 60 | + |
| 61 | + // They're the same range, but what about kind |
| 62 | + return that.Kind.Value - this.Kind.Value; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// A class that holds a list of FoldingReferences and ensures that when adding a reference that the |
| 68 | + /// folding rules are obeyed, e.g. Only one fold per start line |
| 69 | + /// </summary> |
| 70 | + public class FoldingReferenceList |
| 71 | + { |
| 72 | + private readonly Dictionary<int, FoldingReference> references = new Dictionary<int, FoldingReference>(); |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Return all references in the list |
| 76 | + /// </summary> |
| 77 | + public IEnumerable<FoldingReference> References |
| 78 | + { |
| 79 | + get |
| 80 | + { |
| 81 | + return references.Values; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Adds a FoldingReference to the list and enforces ordering rules e.g. Only one fold per start line |
| 87 | + /// </summary> |
| 88 | + public void SafeAdd(FoldingReference item) |
| 89 | + { |
| 90 | + if (item == null) { return; } |
| 91 | + |
| 92 | + // Only add the item if it hasn't been seen before or it's the largest range |
| 93 | + if (references.TryGetValue(item.StartLine, out FoldingReference currentItem)) |
| 94 | + { |
| 95 | + if (currentItem.CompareTo(item) == 1) { references[item.StartLine] = item; } |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + references[item.StartLine] = item; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Helper method to easily convert the Dictionary Values into an array |
| 105 | + /// </summary> |
| 106 | + public FoldingReference[] ToArray() |
| 107 | + { |
| 108 | + var result = new FoldingReference[references.Count]; |
| 109 | + references.Values.CopyTo(result, 0); |
| 110 | + return result; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments