-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement inside-paren and inside-doublequote movement
This allows for e.g. 'ci(' and 'ci"' commands.
- Loading branch information
1 parent
db168e3
commit 80a876e
Showing
4 changed files
with
238 additions
and
64 deletions.
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Position } from './../motion/position'; | ||
|
||
/** | ||
* PairMatcher finds the position matching the given character, respecting nested | ||
* instances of the pair. | ||
*/ | ||
export class PairMatcher { | ||
static pairings: { [key: string]: { match: string, nextMatchIsForward: boolean, limited?: boolean }} = { | ||
"(" : { match: ")", nextMatchIsForward: true }, | ||
"{" : { match: "}", nextMatchIsForward: true }, | ||
"[" : { match: "]", nextMatchIsForward: true }, | ||
")" : { match: "(", nextMatchIsForward: false }, | ||
"}" : { match: "{", nextMatchIsForward: false }, | ||
"]" : { match: "[", nextMatchIsForward: false }, | ||
// These characters can't be used for "%"-based matching, but are still | ||
// useful for text objects. TODO limited is a terrible name. | ||
"'" : { match: "'", nextMatchIsForward: true, limited: true }, | ||
"\"": { match: "\"", nextMatchIsForward: true, limited: true }, | ||
"<" : { match: ">", nextMatchIsForward: true, limited: true }, | ||
}; | ||
|
||
previousChar(position: Position, charToMatch: string, currentLineOnly: boolean = false): Position { | ||
// TODO handle currentLineOnly | ||
for (const { char, pos } of Position.IterateDocument(position, false)) { | ||
if (currentLineOnly && position.line != pos.line) { | ||
// We've advanced past the current line and the caller doesn't want us to | ||
return position; | ||
} | ||
|
||
if (char === charToMatch) { | ||
return pos; | ||
} | ||
} | ||
|
||
return position; | ||
} | ||
|
||
nextPairedChar(position: Position, charToMatch: string, closed: boolean = true, currentLineOnly: boolean = false): Position { | ||
/** | ||
* We do a fairly basic implementation that only tracks the state of the type of | ||
* character you're over and its pair (e.g. "[" and "]"). This is similar to | ||
* what Vim does. | ||
* | ||
* It can't handle strings very well - something like "|( ')' )" where | is the | ||
* cursor will cause it to go to the ) in the quotes, even though it should skip over it. | ||
* | ||
* PRs welcomed! (TODO) | ||
* Though ideally VSC implements https://github.com/Microsoft/vscode/issues/7177 | ||
*/ | ||
const toFind = PairMatcher.pairings[charToMatch]; | ||
|
||
let stackHeight = closed ? 0 : 1; | ||
let matchedPosition: Position | undefined = undefined; | ||
// TODO respect currentLineOnly | ||
|
||
for (const { char, pos } of Position.IterateDocument(position, toFind.nextMatchIsForward)) { | ||
if (char === charToMatch && charToMatch !== toFind.match) { | ||
stackHeight++; | ||
} | ||
|
||
if (char === toFind.match) { | ||
stackHeight--; | ||
} | ||
|
||
if (stackHeight === 0) { | ||
matchedPosition = pos; | ||
|
||
break; | ||
} | ||
} | ||
|
||
if (matchedPosition) { | ||
return matchedPosition; | ||
} | ||
|
||
// TODO(bell) | ||
return position; | ||
} | ||
} |
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