|
| 1 | +using System; |
| 2 | +using System.Text; |
| 3 | + |
| 4 | +namespace Microsoft.PowerShell |
| 5 | +{ |
| 6 | + internal static class StringBuilderTextObjectExtensions |
| 7 | + { |
| 8 | + private const string WhiteSpace = " \n\t"; |
| 9 | + |
| 10 | + /// <summary> |
| 11 | + /// Returns the position of the beginning of the current word as delimited by white space and delimiters |
| 12 | + /// This method differs from <see cref="ViFindPreviousWordPoint(string)"/>: |
| 13 | + /// - When the cursor location is on the first character of a word, <see cref="ViFindPreviousWordPoint(string)"/> |
| 14 | + /// returns the position of the previous word, whereas this method returns the cursor location. |
| 15 | + /// - When the cursor location is in a word, both methods return the same result. |
| 16 | + /// This method supports VI "iw" text object. |
| 17 | + /// </summary> |
| 18 | + public static int ViFindBeginningOfWordObjectBoundary(this StringBuilder buffer, int position, string wordDelimiters) |
| 19 | + { |
| 20 | + // Cursor may be past the end of the buffer when calling this method |
| 21 | + // this may happen if the cursor is at the beginning of a new line. |
| 22 | + var i = Math.Min(position, buffer.Length - 1); |
| 23 | + |
| 24 | + // If starting on a word consider a text object as a sequence of characters excluding the delimiters, |
| 25 | + // otherwise, consider a word as a sequence of delimiters. |
| 26 | + var delimiters = wordDelimiters; |
| 27 | + var isInWord = buffer.InWord(i, wordDelimiters); |
| 28 | + |
| 29 | + if (isInWord) |
| 30 | + { |
| 31 | + // For the purpose of this method, whitespace character is considered a delimiter. |
| 32 | + delimiters += WhiteSpace; |
| 33 | + } |
| 34 | + else |
| 35 | + { |
| 36 | + char c = buffer[i]; |
| 37 | + if ((wordDelimiters + '\n').IndexOf(c) == -1 && char.IsWhiteSpace(c)) |
| 38 | + { |
| 39 | + // Current position points to a whitespace that is not a newline. |
| 40 | + delimiters = WhiteSpace; |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + delimiters += '\n'; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + var isTextObjectChar = isInWord |
| 49 | + ? (Func<char, bool>)(c => delimiters.IndexOf(c) == -1) |
| 50 | + : c => delimiters.IndexOf(c) != -1; |
| 51 | + |
| 52 | + var beginning = i; |
| 53 | + while (i >= 0 && isTextObjectChar(buffer[i])) |
| 54 | + { |
| 55 | + beginning = i--; |
| 56 | + } |
| 57 | + |
| 58 | + return beginning; |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Finds the position of the beginning of the next word object starting from the specified position. |
| 63 | + /// If positioned on the last word in the buffer, returns buffer length + 1. |
| 64 | + /// This method supports VI "iw" text-object. |
| 65 | + /// iw: "inner word", select words. White space between words is counted too. |
| 66 | + /// </summary> |
| 67 | + public static int ViFindBeginningOfNextWordObjectBoundary(this StringBuilder buffer, int position, string wordDelimiters) |
| 68 | + { |
| 69 | + // Cursor may be past the end of the buffer when calling this method |
| 70 | + // this may happen if the cursor is at the beginning of a new line. |
| 71 | + var i = Math.Min(position, buffer.Length - 1); |
| 72 | + |
| 73 | + // Always skip the first newline character. |
| 74 | + if (buffer[i] == '\n' && i < buffer.Length - 1) |
| 75 | + { |
| 76 | + ++i; |
| 77 | + } |
| 78 | + |
| 79 | + // If starting on a word consider a text object as a sequence of characters excluding the delimiters, |
| 80 | + // otherwise, consider a word as a sequence of delimiters. |
| 81 | + var delimiters = wordDelimiters; |
| 82 | + var isInWord = buffer.InWord(i, wordDelimiters); |
| 83 | + |
| 84 | + if (isInWord) |
| 85 | + { |
| 86 | + delimiters += WhiteSpace; |
| 87 | + } |
| 88 | + else if (char.IsWhiteSpace(buffer[i])) |
| 89 | + { |
| 90 | + delimiters = " \t"; |
| 91 | + } |
| 92 | + |
| 93 | + var isTextObjectChar = isInWord |
| 94 | + ? (Func<char, bool>)(c => delimiters.IndexOf(c) == -1) |
| 95 | + : c => delimiters.IndexOf(c) != -1; |
| 96 | + |
| 97 | + // Try to skip a second newline characters to replicate vim behaviour. |
| 98 | + if (buffer[i] == '\n' && i < buffer.Length - 1) |
| 99 | + { |
| 100 | + ++i; |
| 101 | + } |
| 102 | + |
| 103 | + // Skip to next non-word characters. |
| 104 | + while (i < buffer.Length && isTextObjectChar(buffer[i])) |
| 105 | + { |
| 106 | + ++i; |
| 107 | + } |
| 108 | + |
| 109 | + // Make sure end includes the starting position. |
| 110 | + return Math.Max(i, position); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments