Skip to content

Commit

Permalink
Handle escaped backslashes
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandella committed Jul 20, 2016
1 parent 363eb70 commit 977e7b5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/matching/quoteMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@ export class QuoteMatcher {
static escapeChar = "\\";

static findOpening(char: string, corpus: string, start: number): number {
// first, search backwards to see if we could be inside a quote
// First, search backwards to see if we could be inside a quote
for (let i = start - 1; i >= 0; i--) {
const currentChar = corpus[i];
if (currentChar === char && corpus[i - 1] !== this.escapeChar) {
if (currentChar === char &&
(corpus[i - 1] !== this.escapeChar ||
corpus[i - 2] === this.escapeChar)) {
return i;
}
}

// Didn't find one behind us, the string may start ahead of us
for (let i = start; i < corpus.length; i++) {
const currentChar = corpus[i];
if (currentChar === char && corpus[i - 1] !== this.escapeChar) {
return i;
}
}
return -1;
// Didn't find one behind us, the string may start ahead of us. This happens
// to be the same logic we use to search forwards.
return this.findClosing(char, corpus, start);
}

static findClosing(char: string, corpus: string, start: number): number {
// Search forwards from start, looking for a non-escaped char
for (let i = start; i < corpus.length; i++) {
const currentChar = corpus[i];
if (currentChar === char && corpus[i - 1] !== this.escapeChar) {
if (currentChar === char &&
(corpus[i - 1] !== this.escapeChar ||
corpus[i - 2] === this.escapeChar)) {
return i;
}
}
Expand Down
16 changes: 16 additions & 0 deletions test/mode/modeNormal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,22 @@ suite("Mode Normal", () => {
endMode: ModeName.Insert
});

newTest({
title: "Can handle 'ci\"' with an escaped backslash",
start: ['one "tw|o \\\\three"'],
keysPressed: 'ci"',
end: ['one "|"'],
endMode: ModeName.Insert
});

newTest({
title: "Can handle 'ci\"' with an escaped backslash on closing quote",
start: ['"\\\\|"'],
keysPressed: 'ci"',
end: ['"|"'],
endMode: ModeName.Insert
});

newTest({
title: "Can handle 'ca\"' starting on the closing quote",
start: ['one "two|"'],
Expand Down

0 comments on commit 977e7b5

Please sign in to comment.