-
Notifications
You must be signed in to change notification settings - Fork 306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Vi-Mode] Supports di' and di" commands (quoted text objects) #3791
Closed
springcomp
wants to merge
2
commits into
PowerShell:master
from
springcomp:feature/quote-text-objects
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System; | ||
using System.Management.Automation; | ||
using System.Text; | ||
|
||
namespace Microsoft.PowerShell | ||
|
@@ -109,5 +110,161 @@ public static int ViFindBeginningOfNextWordObjectBoundary(this StringBuilder buf | |
// Make sure end includes the starting position. | ||
return Math.Max(i, position); | ||
} | ||
|
||
/// <summary> | ||
/// Returns the span of text within the quotes relative to the specified position, in the corresponding logical line. | ||
/// If the position refers to the given start delimiter, the method returns the position immediately. | ||
/// If not, it first attempts to look backwards to find the start delimiter and returns its position if found. | ||
/// Otherwise, it look forwards to find the start delimiter and returns its position if found. | ||
/// Otherwise, it returns (-1, -1). | ||
/// | ||
/// If a start delimiter is found, this method then attempts to find the end delimiter within the logical line. | ||
/// Otherwise, it returns (-1, -1). | ||
/// | ||
/// This method supports VI i' and i" text objects. | ||
/// </summary> | ||
public static (int Start, int End) ViFindSpanOfInnerQuotedTextObjectBoundary(this StringBuilder buffer, char delimiter, int position, int repeated = 1) | ||
{ | ||
// Cursor may be past the end of the buffer when calling this method | ||
// this may happen if the cursor is at the beginning of a new line. | ||
|
||
var pos = Math.Min(position, buffer.Length - 1); | ||
|
||
// restrict this method to the logical line | ||
// corresponding to the given position | ||
|
||
var startOfLine = buffer.GetBeginningOfLogicalLinePos(pos); | ||
var endOfLine = buffer.GetEndOfLogicalLinePos(pos); | ||
|
||
var start = -1; | ||
var end = -1; | ||
|
||
// if on a quote we may be on a beginning or end quote | ||
// we need to parse the line to find out | ||
|
||
if (buffer[pos] == delimiter) | ||
{ | ||
var count = 1; | ||
for (var offset = pos - 1; offset > startOfLine; offset--) | ||
{ | ||
if (buffer[offset] == delimiter) | ||
count++; | ||
} | ||
|
||
// if there are an odd number of quotes up to the current position | ||
// the position refers to the beginning a quoted text | ||
|
||
if (count % 2 == 1) | ||
{ | ||
start = pos; | ||
} | ||
} | ||
|
||
// else look backwards | ||
|
||
if (start == -1) | ||
{ | ||
for (var offset = pos - 1; offset > startOfLine; offset--) | ||
{ | ||
if (buffer[offset] == delimiter) | ||
{ | ||
start = offset; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// if not found, look forwards | ||
|
||
if (start == -1) | ||
{ | ||
for (var offset = pos; offset < endOfLine; offset++) | ||
{ | ||
if (buffer[offset] == delimiter) | ||
{ | ||
start = offset; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// attempts to find the end quote | ||
|
||
if (start != -1 && start < endOfLine) | ||
{ | ||
for (var offset = start + 1; offset < buffer.Length; offset++) | ||
{ | ||
if (buffer[offset] == delimiter) | ||
{ | ||
end = offset; | ||
break; | ||
} | ||
if (buffer[offset] == '\n') | ||
{ | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// adjust span boundaries based upon | ||
// the number of repeatitions | ||
|
||
if (start != -1 && end != -1) | ||
{ | ||
if (repeated > 1) | ||
{ | ||
end++; | ||
} | ||
else | ||
{ | ||
start++; | ||
} | ||
} | ||
|
||
return (start, end); | ||
} | ||
|
||
/// <summary> | ||
/// Returns the position of the beginning of line | ||
/// starting from the specified "current" position. | ||
/// </summary> | ||
/// <param name="current">The position in the current logical line.</param> | ||
internal static int GetBeginningOfLogicalLinePos(this StringBuilder buffer, int current) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems |
||
{ | ||
int i = Math.Max(0, current); | ||
while (i > 0) | ||
{ | ||
if (buffer[--i] == '\n') | ||
{ | ||
i += 1; | ||
break; | ||
} | ||
} | ||
|
||
return i; | ||
} | ||
|
||
/// <summary> | ||
/// Returns the position of the end of the logical line | ||
/// as specified by the "current" position. | ||
/// </summary> | ||
/// <param name="current"></param> | ||
/// <returns></returns> | ||
internal static int GetEndOfLogicalLinePos(this StringBuilder buffer, int current) | ||
{ | ||
var newCurrent = current; | ||
|
||
for (var position = current; position < buffer.Length; position++) | ||
{ | ||
if (buffer[position] == '\n') | ||
{ | ||
break; | ||
} | ||
|
||
newCurrent = position; | ||
} | ||
|
||
return newCurrent; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added by mistake? It doesn't seem to be needed by the code changes in this file.