Skip to content

Commit

Permalink
Refactor matchpairs to allow for pairs other than <,>
Browse files Browse the repository at this point in the history
Refs #4855
  • Loading branch information
J-Fields committed Aug 31, 2021
1 parent 19c2a02 commit c0474b3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 24 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,8 @@
},
"vim.matchpairs": {
"type": "string",
"markdownDescription": "Characters that form pairs. The % command jumps from one to the other. (currently only <:>)"
"markdownDescription": "Characters that form pairs. The % command jumps from one to the other. Only character pairs are allowed that are different, thus you cannot jump between two double quotes. The characters must be separated by a colon. The pairs must be separated by a comma.",
"default": "(:),{:},[:]"
},
"vim.camelCaseMotion.enable": {
"type": "boolean",
Expand Down
4 changes: 2 additions & 2 deletions src/actions/motion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1748,11 +1748,11 @@ class MoveToMatchingBracket extends BaseMovement {

for (let col = position.character; col < lineText.length; col++) {
const currentChar = lineText[col];
const pairing = PairMatcher.getPairing(currentChar);
const pairing = PairMatcher.getPercentPairing(currentChar);

// we need to check pairing, because with text: bla |bla < blub > blub
// this for loop will walk over bla and check for a pairing till it finds <
if (pairing && pairing.matchesWithPercentageMotion) {
if (pairing) {
// We found an opening char, now move to the matching closing char
return (
PairMatcher.nextPairedChar(
Expand Down
42 changes: 25 additions & 17 deletions src/common/matching/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export type Pairing = {
match: string;
isNextMatchForward: boolean;
directionless?: boolean;
matchesWithPercentageMotion?: boolean;
};

/**
Expand All @@ -17,18 +16,18 @@ export class PairMatcher {
static pairings: {
[key: string]: Pairing;
} = {
'(': { match: ')', isNextMatchForward: true, matchesWithPercentageMotion: true },
'{': { match: '}', isNextMatchForward: true, matchesWithPercentageMotion: true },
'[': { match: ']', isNextMatchForward: true, matchesWithPercentageMotion: true },
')': { match: '(', isNextMatchForward: false, matchesWithPercentageMotion: true },
'}': { match: '{', isNextMatchForward: false, matchesWithPercentageMotion: true },
']': { match: '[', isNextMatchForward: false, matchesWithPercentageMotion: true },
'(': { match: ')', isNextMatchForward: true },
'{': { match: '}', isNextMatchForward: true },
'[': { match: ']', isNextMatchForward: true },
')': { match: '(', isNextMatchForward: false },
'}': { match: '{', isNextMatchForward: false },
']': { match: '[', isNextMatchForward: false },

// These characters can't be used for "%"-based matching, but are still
// useful for text objects.
// matchesWithPercentageMotion can be overwritten with configuration.matchpairs
'<': { match: '>', isNextMatchForward: true, matchesWithPercentageMotion: false },
'>': { match: '<', isNextMatchForward: false, matchesWithPercentageMotion: false },
'<': { match: '>', isNextMatchForward: true },
'>': { match: '<', isNextMatchForward: false },
// These are useful for deleting closing and opening quotes, but don't seem to negatively
// affect how text objects such as `ci"` work, which was my worry.
'"': { match: '"', isNextMatchForward: false, directionless: true },
Expand Down Expand Up @@ -116,15 +115,24 @@ export class PairMatcher {
}
}

static getPairing(pair: string): Pairing {
const pairing = this.pairings[pair];
if (pairing !== undefined && pairing.matchesWithPercentageMotion === false) {
// we look up config if it overwrites matchesWithPercentageMotion setting
pairing.matchesWithPercentageMotion = configuration.matchpairs.includes(pair);
return pairing;
} else {
return pairing;
static getPercentPairing(char: string): Pairing | undefined {
for (const pairing of configuration.matchpairs.split(',')) {
const components = pairing.split(':');
if (components.length === 2) {
if (components[0] === char) {
return {
match: components[1],
isNextMatchForward: true,
};
} else if (components[1] === char) {
return {
match: components[0],
isNextMatchForward: false,
};
}
}
}
return undefined;
}

static nextPairedChar(
Expand Down
2 changes: 1 addition & 1 deletion src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class Configuration implements IConfiguration {

autoindent = true;

matchpairs = '';
matchpairs = '(:),{:},[:]';

joinspaces = true;

Expand Down
6 changes: 4 additions & 2 deletions src/configuration/iconfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,10 @@ export interface IConfiguration {
iskeyword: string;

/**
* addional chars for pairwise jumping with %.
* for now only: < >
* Characters that form pairs. The % command jumps from one to the other.
* Only character pairs are allowed that are different, thus you cannot jump between two double quotes.
* The characters must be separated by a colon.
* The pairs must be separated by a comma.
*/
matchpairs: string;

Expand Down
2 changes: 1 addition & 1 deletion test/testConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class Configuration implements IConfiguration {
number = true;
relativenumber = false;
iskeyword = '/\\()"\':,.;<>~!@#$%^&*|+=[]{}`?-';
matchpairs = '';
matchpairs = '(:),{:},[:]';
visualstar = false;
mouseSelectionGoesIntoVisualMode = true;
changeWordIncludesWhitespace = false;
Expand Down

0 comments on commit c0474b3

Please sign in to comment.