|
| 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 | + |
| 8 | +namespace Microsoft.PowerShell.EditorServices |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// A class that holds the information for a foldable region of text in a document |
| 12 | + /// </summary> |
| 13 | + public class FoldingReference: IComparable<FoldingReference> |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// The zero-based line number from where the folded range starts. |
| 17 | + /// </summary> |
| 18 | + public int StartLine { get; set; } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// The zero-based character offset from where the folded range starts. If not defined, defaults to the length of the start line. |
| 22 | + /// </summary> |
| 23 | + public int StartCharacter { get; set; } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// The zero-based line number where the folded range ends. |
| 27 | + /// </summary> |
| 28 | + public int EndLine { get; set; } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// The zero-based character offset before the folded range ends. If not defined, defaults to the length of the end line. |
| 32 | + /// </summary> |
| 33 | + public int EndCharacter { get; set; } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Describes the kind of the folding range such as `comment' or 'region'. |
| 37 | + /// </summary> |
| 38 | + public string Kind { get; set; } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Builds a folding reference for start and end line and character position |
| 42 | + /// </summary> |
| 43 | + public FoldingReference( |
| 44 | + int startLine, |
| 45 | + int startCharacter, |
| 46 | + int endLine, |
| 47 | + int endCharacter, |
| 48 | + string kind) |
| 49 | + { |
| 50 | + this.EndCharacter = endCharacter; |
| 51 | + this.EndLine = endLine; |
| 52 | + this.Kind = kind; |
| 53 | + this.StartCharacter = startCharacter; |
| 54 | + this.StartLine = startLine; |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Builds a folding reference for start and end line |
| 59 | + /// </summary> |
| 60 | + public FoldingReference( |
| 61 | + int startLine, |
| 62 | + int endLine, |
| 63 | + string kind) |
| 64 | + { |
| 65 | + this.EndCharacter = 0; |
| 66 | + this.EndLine = endLine; |
| 67 | + this.Kind = kind; |
| 68 | + this.StartCharacter = 0; |
| 69 | + this.StartLine = startLine; |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// A custom comparable method which can properly sort FoldingReference objects |
| 74 | + /// </summary> |
| 75 | + public int CompareTo(FoldingReference that) { |
| 76 | + // Initially look at the start line |
| 77 | + if (this.StartLine < that.StartLine) { return -1; } |
| 78 | + if (this.StartLine > that.StartLine) { return 1; } |
| 79 | + // They have the same start line so now consider the end line. |
| 80 | + // The biggest line range is sorted first |
| 81 | + if (this.EndLine > that.EndLine) { return -1; } |
| 82 | + if (this.EndLine < that.EndLine) { return 1; } |
| 83 | + // They have the same lines, but what about character offsets |
| 84 | + if (this.StartCharacter < that.StartCharacter) { return -1; } |
| 85 | + if (this.StartCharacter > that.StartCharacter) { return 1; } |
| 86 | + if (this.EndCharacter < that.EndCharacter) { return -1; } |
| 87 | + if (this.EndCharacter > that.EndCharacter) { return 1; } |
| 88 | + // They're the same range, but what about kind |
| 89 | + // Check for nulls |
| 90 | + if ((this.Kind == null) && (that.Kind == null)) { return 0; } |
| 91 | + return this.Kind.CompareTo(that.Kind); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments