From e0969fca57d1cbf1820dde2167c7aaeb7e1eacf1 Mon Sep 17 00:00:00 2001 From: Chase Johnson Date: Mon, 17 Mar 2025 08:21:56 -0500 Subject: [PATCH] Add `$` as a surround character (#8895) --- src/actions/motion.ts | 16 +++++++++++++++- src/actions/plugins/surround.ts | 7 +++++++ .../plugins/targets/smartQuotesMatcher.ts | 2 +- src/common/matching/matcher.ts | 1 + src/common/matching/quoteMatcher.ts | 2 +- test/plugins/smartQuotes.test.ts | 18 ++++++++++++++++++ 6 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/actions/motion.ts b/src/actions/motion.ts index fa64b623ee6..b5c5d195f6c 100755 --- a/src/actions/motion.ts +++ b/src/actions/motion.ts @@ -2090,7 +2090,7 @@ export class MoveAroundSquareBracket extends MoveInsideCharacter { export abstract class MoveQuoteMatch extends BaseMovement { override modes = [Mode.Normal, Mode.Visual, Mode.VisualBlock]; protected readonly anyQuote: boolean = false; - protected abstract readonly charToMatch: '"' | "'" | '`'; + protected abstract readonly charToMatch: '"' | "'" | '`' | '$'; protected includeQuotes = false; override isJump = true; readonly which: WhichQuotes = 'current'; @@ -2232,6 +2232,20 @@ class MoveInsideSingleQuotes extends MoveQuoteMatch { override includeQuotes = false; } +@RegisterAction +export class MoveInsideDollarSign extends MoveQuoteMatch { + keys = ['i', '$']; + readonly charToMatch = '$'; + override includeQuotes = false; +} + +@RegisterAction +export class MoveAroundDollarSign extends MoveQuoteMatch { + keys = ['a', '$']; + readonly charToMatch = '$'; + override includeQuotes = true; +} + @RegisterAction export class MoveAroundSingleQuotes extends MoveQuoteMatch { keys = ['a', "'"]; diff --git a/src/actions/plugins/surround.ts b/src/actions/plugins/surround.ts index 9ca28fd2ce7..8d00f225d33 100644 --- a/src/actions/plugins/surround.ts +++ b/src/actions/plugins/surround.ts @@ -11,6 +11,7 @@ import { MoveAroundBacktick, MoveAroundCaret, MoveAroundCurlyBrace, + MoveAroundDollarSign, MoveAroundDoubleQuotes, MoveAroundParentheses, MoveAroundSingleQuotes, @@ -499,6 +500,12 @@ class SurroundHelper { removeSpace: false, movement: () => new MoveAroundBacktick(false), }, + $: { + left: '$', + right: '$', + removeSpace: true, + movement: () => new MoveAroundDollarSign(false), + }, '<': { left: '', right: '', removeSpace: false, movement: () => new MoveAroundTag() }, '*': { left: '*', diff --git a/src/actions/plugins/targets/smartQuotesMatcher.ts b/src/actions/plugins/targets/smartQuotesMatcher.ts index 271dba16df7..9ad4c2d49fa 100644 --- a/src/actions/plugins/targets/smartQuotesMatcher.ts +++ b/src/actions/plugins/targets/smartQuotesMatcher.ts @@ -2,7 +2,7 @@ import { Position } from 'vscode'; import { TextDocument } from 'vscode'; import { configuration } from '../../../configuration/configuration'; -type Quote = '"' | "'" | '`'; +type Quote = '"' | "'" | '`' | '$'; enum QuoteMatch { Opening, Closing, diff --git a/src/common/matching/matcher.ts b/src/common/matching/matcher.ts index 0ac4c63f037..ccd7dee99ae 100644 --- a/src/common/matching/matcher.ts +++ b/src/common/matching/matcher.ts @@ -33,6 +33,7 @@ export class PairMatcher { '"': { match: '"', isNextMatchForward: false, directionless: true }, "'": { match: "'", isNextMatchForward: false, directionless: true }, '`': { match: '`', isNextMatchForward: false, directionless: true }, + $: { match: '$', isNextMatchForward: false, directionless: true }, }; private static findPairedChar( diff --git a/src/common/matching/quoteMatcher.ts b/src/common/matching/quoteMatcher.ts index 88d8387283c..82777626958 100644 --- a/src/common/matching/quoteMatcher.ts +++ b/src/common/matching/quoteMatcher.ts @@ -11,7 +11,7 @@ export class QuoteMatcher { private readonly quoteMap: QuoteMatch[] = []; - constructor(quote: '"' | "'" | '`', corpus: string) { + constructor(quote: '"' | "'" | '`' | '$', corpus: string) { let openingQuote = true; // Loop over corpus, marking quotes and respecting escape characters. for (let i = 0; i < corpus.length; i++) { diff --git a/test/plugins/smartQuotes.test.ts b/test/plugins/smartQuotes.test.ts index 3b1bf36a1a9..e5e3d953561 100644 --- a/test/plugins/smartQuotes.test.ts +++ b/test/plugins/smartQuotes.test.ts @@ -33,6 +33,18 @@ suite('smartQuotes plugin', () => { keysPressed: "di'", end: ['aaa "bbb" c \'|\' '], }); + newTest({ + title: 'dollar sign - 1', + start: ['|aaa "bbb" c $d$ '], + keysPressed: "di'", + end: ['aaa "bbb" c $|$ '], + }); + newTest({ + title: 'dollar sign - 2', + start: ['aaa "bbb" |c $d$ '], + keysPressed: "di'", + end: ['aaa "bbb" c $|$ '], + }); newTest({ title: 'backtick - 1', start: ['|aaa "bbb" c `d` '], @@ -63,6 +75,12 @@ suite('smartQuotes plugin', () => { keysPressed: 'diq', end: [' \'aaa\' "bbb" c `|` '], }); + newTest({ + title: 'any-quote - 4', + start: [' \'aaa\' "bbb" |c $d$ '], + keysPressed: 'diq', + end: [' \'aaa\' "bbb" c $|$ '], + }); // test basic usage newTest({ title: 'no quotes at all',