Skip to content

Commit c142239

Browse files
authored
Merge pull request #15 from lvivski/main
Prevent `multiword` mode from matching when there's a space immediately after activation key
2 parents 52ca164 + e296163 commit c142239

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/query.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export default function query(text: string, key: string, cursor: number, multiWo
1212
if (keyIndex === -1) return
1313

1414
if (multiWord) {
15+
// Space immediately after activation key
16+
const charAfterKey = text[keyIndex + 1]
17+
if (charAfterKey === ' ') return
18+
1519
// New line the cursor and previous activation key.
1620
const newLineIndex = text.lastIndexOf('\n', cursor - 1)
1721
if (newLineIndex > keyIndex) return

test/keyword-test.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,68 @@ describe('text-expander single word parsing', function() {
5858
})
5959

6060
describe('text-expander multi word parsing', function() {
61-
it('does matches with a space between cursor and activation key', function() {
61+
it('does not match empty text', function() {
62+
const found = query('', ':', 0, true)
63+
assert(found == null)
64+
})
65+
66+
it('does not match without activation key', function() {
67+
const found = query('cat', ':', 3, true)
68+
assert(found == null)
69+
})
70+
71+
it('matches only activation key', function() {
72+
const found = query(':', ':', 1, true)
73+
assert.deepEqual(found, {text: '', position: 1})
74+
})
75+
76+
it('matches trailing activation key', function() {
77+
const found = query('hi :', ':', 4, true)
78+
assert.deepEqual(found, {text: '', position: 4})
79+
})
80+
81+
it('matches start of text', function() {
82+
const found = query(':cat', ':', 4, true)
83+
assert.deepEqual(found, {text: 'cat', position: 1})
84+
})
85+
86+
it('matches end of text', function() {
87+
const found = query('hi :cat', ':', 7, true)
88+
assert.deepEqual(found, {text: 'cat', position: 4})
89+
})
90+
91+
it('matches middle of text', function() {
92+
const found = query('hi :cat bye', ':', 7, true)
93+
assert.deepEqual(found, {text: 'cat', position: 4})
94+
})
95+
96+
it('matches only at word boundary', function() {
97+
const found = query('hi:cat', ':', 6, true)
98+
assert(found == null)
99+
})
100+
101+
it('matches last activation key word', function() {
102+
const found = query('hi :cat bye :dog', ':', 16, true)
103+
assert.deepEqual(found, {text: 'dog', position: 13})
104+
})
105+
106+
it('matches closest activation key word', function() {
107+
const found = query('hi :cat bye :dog', ':', 7, true)
108+
assert.deepEqual(found, {text: 'cat', position: 4})
109+
})
110+
111+
it('matches with a space between cursor and activation key', function() {
62112
const found = query('hi :cat bye', ':', 11, true)
63113
assert.deepEqual(found, {text: 'cat bye', position: 4})
64114
})
115+
116+
it('does not match with a dot between cursor and activation key', function() {
117+
const found = query('hi :cat. bye', ':', 11, true)
118+
assert(found == null)
119+
})
120+
121+
it('does not match with a space between text and activation key', function() {
122+
const found = query('hi : cat bye', ':', 7, true)
123+
assert(found == null)
124+
})
65125
})

0 commit comments

Comments
 (0)