Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 9be72d3

Browse files
committed
pass specs
1 parent c657c92 commit 9be72d3

File tree

8 files changed

+69
-78
lines changed

8 files changed

+69
-78
lines changed

lib/insertion.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,16 @@ function transformText (str, flags) {
4545
}
4646

4747
class Insertion {
48-
constructor ({ range, substitution }) {
48+
constructor ({ range, transformation }) {
4949
this.range = range
50-
this.substitution = substitution
51-
if (substitution) {
52-
if (substitution.replace === undefined) {
53-
substitution.replace = ''
54-
}
55-
this.replacer = this.makeReplacer(substitution.replace)
50+
this.transformation = transformation
51+
if (transformation) {
52+
this.replacer = this.makeReplacer(transformation.replace)
5653
}
5754
}
5855

5956
isTransformation () {
60-
return !!this.substitution
57+
return !!this.transformation
6158
}
6259

6360
makeReplacer (replace) {
@@ -73,8 +70,8 @@ class Insertion {
7370
replace.forEach(token => {
7471
if (typeof token === 'string') {
7572
result.push(transformText(token, flags))
76-
} else if (token.escape) {
77-
ESCAPES[token.escape](flags, result)
73+
} else if (token.modifier) {
74+
ESCAPES[token.modifier](flags, result)
7875
} else if (token.backreference) {
7976
let transformed = transformText(match[token.backreference], flags)
8077
result.push(transformed)
@@ -85,9 +82,9 @@ class Insertion {
8582
}
8683

8784
transform (input) {
88-
let { substitution } = this
89-
if (!substitution) { return input }
90-
return input.replace(substitution.find, this.replacer)
85+
let { transformation } = this
86+
if (!transformation) { return input }
87+
return input.replace(transformation.find, this.replacer)
9188
}
9289
}
9390

lib/snippet-body.pegjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ tabStopContent = c:(tabStopText / escapedTabStop / tabStop / choice / variable /
6565
tabStop = '$' t:(tabStopSimple / tabStopWithoutPlaceholder / tabStopWithPlaceholder / tabStopWithTransform) { return t; }
6666

6767
// The simplest form is just $n for some integer `n`
68-
tabStopSimple = n:integer { return { index: n, content: [] }; }
68+
tabStopSimple = n:integer { return { index: n }; }
6969

7070
// The next simplest form is equivalent to the above, but wrapped in `{}`
71-
tabStopWithoutPlaceholder = '{' n:integer '}' { return { index: n, content: [] }; }
71+
tabStopWithoutPlaceholder = '{' n:integer '}' { return { index: n }; }
7272

7373
// When a ':' follows `n`, the content after the ':' is the placeholder and it can be anything
7474
tabStopWithPlaceholder = '{' n:integer ':' content:tabStopContent '}' { return { index: n, content }; }

lib/snippet.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ module.exports = class Snippet {
2424
let extractTabStops = bodyTree => {
2525
for (const segment of bodyTree) {
2626
if (segment.index != null) {
27-
let {index, content, substitution} = segment
27+
let {index, content, transformation} = segment
2828
if (index === 0) { index = Infinity; }
2929
const start = [row, column]
30-
extractTabStops(content)
30+
if (content) { extractTabStops(content); }
3131
const range = new Range(start, [row, column])
3232
const tabStop = this.tabStopList.findOrCreate({
3333
index,
3434
snippet: this
3535
})
36-
tabStop.addInsertion({ range, substitution })
36+
tabStop.addInsertion({ range, transformation })
3737
} else if (typeof segment === 'string') {
3838
bodyText.push(segment)
3939
var segmentLines = segment.split('\n')

lib/tab-stop.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const {Range} = require('atom')
2-
const Insertion = require('./insertion')
2+
const Insertion = require('./insertion').default
33

44
// A tab stop:
55
// * belongs to a snippet
@@ -20,8 +20,8 @@ class TabStop {
2020
return !all
2121
}
2222

23-
addInsertion ({ range, substitution }) {
24-
let insertion = new Insertion({ range, substitution })
23+
addInsertion ({ range, transformation }) {
24+
let insertion = new Insertion({ range, transformation })
2525
let insertions = this.insertions
2626
insertions.push(insertion)
2727
insertions = insertions.sort((i1, i2) => {
@@ -38,15 +38,15 @@ class TabStop {
3838
copyWithIndent (indent) {
3939
let { snippet, index, insertions } = this
4040
let newInsertions = insertions.map(insertion => {
41-
let { range, substitution } = insertion
41+
let { range, transformation } = insertion
4242
let newRange = Range.fromObject(range, true)
4343
if (newRange.start.row) {
4444
newRange.start.column += indent.length
4545
newRange.end.column += indent.length
4646
}
4747
return new Insertion({
4848
range: newRange,
49-
substitution
49+
transformation
5050
})
5151
})
5252

spec/body-parser-spec.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ describe("Snippet Body Parser", () => {
1111
describe("when parsing tab stops", () => {
1212
it("parses simple tab stops", () => {
1313
expectMatch("hello$1world${2}", [
14-
"hello", { index: 1, content: [] }, "world", { index: 2, content: [] },
14+
"hello", { index: 1 }, "world", { index: 2 },
1515
]);
1616
});
1717

1818
it("skips escaped tab stops", () => {
1919
expectMatch("$1 \\$2 $3", [
20-
{ index: 1, content: [] },
20+
{ index: 1 },
2121
" $2 ",
22-
{ index: 3, content: [] },
22+
{ index: 3 },
2323
]);
2424
});
2525

2626
it("only allows non-negative integer stop numbers", () => {
27-
expectMatch("$0", [{ index: 0, content: [] }]);
28-
expectMatch("$99999", [{ index: 99999, content: [] }]);
27+
expectMatch("$0", [{ index: 0 }]);
28+
expectMatch("$99999", [{ index: 99999 }]);
2929
expectMatch("$-1", ["$-1"]);
3030
expectMatch("${-1}", ["${-1}"]);
31-
expectMatch("$1.5", [{ index: 1, content: [] }, ".5"]);
31+
expectMatch("$1.5", [{ index: 1 }, ".5"]);
3232
expectMatch("${1.5}", ["${1.5}"]);
3333
});
3434

@@ -42,7 +42,7 @@ describe("Snippet Body Parser", () => {
4242
{
4343
index: 1,
4444
content: [
45-
{ index: 2, content: [] },
45+
{ index: 2 },
4646
{ variable: "foo" },
4747
{ index: 3, choices: ["a", "b"] },
4848
]
@@ -144,7 +144,7 @@ describe("Snippet Body Parser", () => {
144144
});
145145

146146
it("doesn't allow names to start with a number", () => {
147-
expectMatch("$1foo", [{ index: 1, content: [] }, "foo"]);
147+
expectMatch("$1foo", [{ index: 1 }, "foo"]);
148148
});
149149
});
150150

@@ -156,7 +156,7 @@ describe("Snippet Body Parser", () => {
156156
{
157157
variable: "foo",
158158
content: [
159-
{ index: 2, content: [] },
159+
{ index: 2 },
160160
{ variable: "bar" },
161161
{ index: 3, choices: ["a", "b"] },
162162
]
@@ -242,7 +242,7 @@ describe("Snippet Body Parser", () => {
242242
expectMatch("${1/foo/bar/a}", ["${1/foo/bar/a}"]); // invalid flag
243243
expectMatch("${1/fo)o$1/$bar/}", [
244244
"${1/fo)o",
245-
{ index: 1, content: [] },
245+
{ index: 1 },
246246
"/",
247247
{ variable: "bar" },
248248
"/}"
@@ -433,8 +433,8 @@ describe("Snippet Body Parser", () => {
433433
it("accepts the first character as text and resumes", () => {
434434
expectMatch("${1:${2}${3}", [
435435
"${1:",
436-
{ index: 2, content: [] },
437-
{ index: 3, content: [] }
436+
{ index: 2 },
437+
{ index: 3 }
438438
]);
439439
});
440440
});
@@ -443,7 +443,7 @@ describe("Snippet Body Parser", () => {
443443
it("breaks a snippet body into lines, with each line containing tab stops at the appropriate position", () => {
444444
expectMatch("the quick brown $1fox ${2:jumped ${3:over}\n}the ${4:lazy} dog", [
445445
"the quick brown ",
446-
{ index: 1, content: [] },
446+
{ index: 1 },
447447
"fox ",
448448
{
449449
index: 2,
@@ -492,7 +492,7 @@ describe("Snippet Body Parser", () => {
492492
'<',
493493
{ index: 1, content: ['p'] },
494494
'>',
495-
{ index: 0, content: [] },
495+
{ index: 0 },
496496
'</',
497497
{ index: 1, transformation: { find: /f/, replace: ['F'] } },
498498
'>',
@@ -514,7 +514,7 @@ describe("Snippet Body Parser", () => {
514514
},
515515
},
516516
' ',
517-
{ index: 1, content: [] },
517+
{ index: 1 },
518518
' ',
519519
{ index: 2, content: ['ANOTHER'] },
520520
' ',
@@ -529,7 +529,7 @@ describe("Snippet Body Parser", () => {
529529
},
530530
},
531531
' ',
532-
{ index: 2, content: [] },
532+
{ index: 2 },
533533
]);
534534
});
535535

@@ -548,7 +548,7 @@ describe("Snippet Body Parser", () => {
548548
},
549549
},
550550
'\n',
551-
{ index: 1, content: [] },
551+
{ index: 1 },
552552
]);
553553
});
554554

@@ -557,7 +557,7 @@ describe("Snippet Body Parser", () => {
557557
'<',
558558
{ index: 1, content: ['p'] },
559559
'>',
560-
{ index: 0, content: [] },
560+
{ index: 0 },
561561
'</',
562562
{
563563
index: 1,
@@ -579,7 +579,7 @@ describe("Snippet Body Parser", () => {
579579
'<',
580580
{ index: 1, content: ['p'] },
581581
'>',
582-
{ index: 0, content: [] },
582+
{ index: 0 },
583583
'</',
584584
{
585585
index: 1,
@@ -598,38 +598,38 @@ describe("Snippet Body Parser", () => {
598598

599599
it("parses a snippet with a placeholder that mirrors another tab stop's content", () => {
600600
expectMatch("$4console.${3:log}('${2:$1}', $1);$0", [
601-
{ index: 4, content: [] },
601+
{ index: 4 },
602602
'console.',
603603
{ index: 3, content: ['log'] },
604604
'(\'',
605605
{
606606
index: 2, content: [
607-
{ index: 1, content: [] }
607+
{ index: 1 }
608608
]
609609
},
610610
'\', ',
611-
{ index: 1, content: [] },
611+
{ index: 1 },
612612
');',
613-
{ index: 0, content: [] }
613+
{ index: 0 }
614614
]);
615615
});
616616

617617
it("parses a snippet with a placeholder that mixes text and tab stop references", () => {
618618
expectMatch("$4console.${3:log}('${2:uh $1}', $1);$0", [
619-
{ index: 4, content: [] },
619+
{ index: 4 },
620620
'console.',
621621
{ index: 3, content: ['log'] },
622622
'(\'',
623623
{
624624
index: 2, content: [
625625
'uh ',
626-
{ index: 1, content: [] }
626+
{ index: 1 }
627627
]
628628
},
629629
'\', ',
630-
{ index: 1, content: [] },
630+
{ index: 1 },
631631
');',
632-
{ index: 0, content: [] }
632+
{ index: 0 }
633633
]);
634634
});
635635
});

0 commit comments

Comments
 (0)