Skip to content

Commit 7f4babd

Browse files
committed
Update with minor fixes and style changes
1 parent e89bd17 commit 7f4babd

6 files changed

+122
-183
lines changed

PSReadLine/StringBuilderCharacterExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Microsoft.PowerShell
44
{
5-
internal static partial class StringBuilderExtensions
5+
internal static class StringBuilderCharacterExtensions
66
{
77
/// <summary>
88
/// Returns true if the character at the specified position is a visible whitespace character.

PSReadLine/StringBuilderLinewiseExtensions.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal Range(int offset, int count)
1414
internal int Count { get; }
1515
}
1616

17-
internal static partial class StringBuilderLinewiseExtensions
17+
internal static class StringBuilderLinewiseExtensions
1818
{
1919
/// <summary>
2020
/// Determines the offset and the length of the fragment
@@ -74,33 +74,23 @@ internal static Range GetRange(this StringBuilder buffer, int lineIndex, int lin
7474
}
7575

7676
/// <summary>
77-
/// Returns true if the specified position
78-
/// is on an empty logical line
77+
/// Returns true if the specified position is on an empty logical line.
7978
/// </summary>
8079
/// <param name="buffer"></param>
8180
/// <param name="cursor"></param>
8281
/// <returns></returns>
8382
public static bool IsLogigalLineEmpty(this StringBuilder buffer, int cursor)
8483
{
8584
// the cursor is on a logical line considered empty if...
86-
8785
return
88-
89-
// the entire buffer is empty (by definition), or
90-
91-
buffer.Length == 0 ||
92-
93-
// the cursor sits at the start of the empty last line,
86+
// the entire buffer is empty (by definition),
87+
buffer.Length == 0
88+
// or the cursor sits at the start of the empty last line,
9489
// meaning that it is past the end of the buffer and the
95-
// last character in the buffer is a newline character, or
96-
97-
(cursor == buffer.Length && buffer[cursor - 1] == '\n') ||
98-
99-
// if the cursor is on a newline character, or
100-
101-
(cursor > 0 && buffer[cursor] == '\n')
102-
103-
;
90+
// last character in the buffer is a newline character,
91+
|| (cursor == buffer.Length && buffer[cursor - 1] == '\n')
92+
// or if the cursor is on a newline character.
93+
|| (cursor > 0 && buffer[cursor] == '\n');
10494
}
10595
}
10696

Lines changed: 93 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,93 @@
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

Comments
 (0)