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