-
-
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 additional text object commands
This allows for e.g. 'ci(', 'ca"', 'di<' commands.
- Loading branch information
1 parent
db168e3
commit 8bacbcd
Showing
5 changed files
with
255 additions
and
66 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,64 @@ | ||
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, matchesWithPercentageMotion?: boolean }} = { | ||
"(" : { match: ")", nextMatchIsForward: true, matchesWithPercentageMotion: true }, | ||
"{" : { match: "}", nextMatchIsForward: true, matchesWithPercentageMotion: true }, | ||
"[" : { match: "]", nextMatchIsForward: true, matchesWithPercentageMotion: true }, | ||
")" : { match: "(", nextMatchIsForward: false, matchesWithPercentageMotion: true }, | ||
"}" : { match: "{", nextMatchIsForward: false, matchesWithPercentageMotion: true }, | ||
"]" : { match: "[", nextMatchIsForward: false, matchesWithPercentageMotion: true }, | ||
// These characters can't be used for "%"-based matching, but are still | ||
"'" : { match: "'", nextMatchIsForward: true }, | ||
"\"": { match: "\"", nextMatchIsForward: true }, | ||
"<" : { match: ">", nextMatchIsForward: true }, | ||
}; | ||
|
||
static 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 = this.pairings[charToMatch]; | ||
|
||
let stackHeight = closed ? 0 : 1; | ||
let matchedPosition: Position | undefined = undefined; | ||
|
||
for (const { char, pos } of Position.IterateDocument(position, toFind.nextMatchIsForward)) { | ||
if (currentLineOnly && position.line < pos.line) { | ||
break; | ||
} | ||
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
Oops, something went wrong.