This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathtab-stop.js
61 lines (55 loc) · 1.75 KB
/
tab-stop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const {Range} = require('atom')
const Insertion = require('./insertion')
// A tab stop:
// * belongs to a snippet
// * has an index (one tab stop per index)
// * has multiple Insertions
class TabStop {
constructor ({ snippet, index, insertions }) {
this.insertions = insertions || []
Object.assign(this, { snippet, index })
}
isValid () {
let any = this.insertions.some(insertion => insertion.isTransformation())
if (!any) return true
let all = this.insertions.every(insertion => insertion.isTransformation())
// If there are any transforming insertions, there must be at least one
// non-transforming insertion to act as the primary.
return !all
}
addInsertion ({ range, substitution, references }) {
let insertion = new Insertion({ range, substitution, references })
let insertions = this.insertions
insertions.push(insertion)
insertions = insertions.sort((i1, i2) => {
return i1.range.start.compare(i2.range.start)
})
let initial = insertions.find(insertion => !insertion.isTransformation())
if (initial) {
insertions.splice(insertions.indexOf(initial), 1)
insertions.unshift(initial)
}
this.insertions = insertions
}
copyWithIndent (indent) {
let { snippet, index, insertions } = this
let newInsertions = insertions.map(insertion => {
let { range, substitution } = insertion
let newRange = Range.fromObject(range, true)
if (newRange.start.row) {
newRange.start.column += indent.length
newRange.end.column += indent.length
}
return new Insertion({
range: newRange,
substitution
})
})
return new TabStop({
snippet,
index,
insertions: newInsertions
})
}
}
module.exports = TabStop