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 pathsnippet.js
71 lines (65 loc) · 2.17 KB
/
snippet.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
62
63
64
65
66
67
68
69
70
71
const {Range} = require('atom')
const TabStopList = require('./tab-stop-list')
function tabStopsReferencedWithinTabStopContent (segment) {
const results = []
for (const item of segment) {
if (item.index) {
results.push(item.index, ...tabStopsReferencedWithinTabStopContent(item.content))
}
}
return new Set(results)
}
module.exports = class Snippet {
constructor({name, prefix, bodyText, description, descriptionMoreURL, rightLabelHTML, leftLabel, leftLabelHTML, bodyTree}) {
this.name = name
this.prefix = prefix
this.bodyText = bodyText
this.description = description
this.descriptionMoreURL = descriptionMoreURL
this.rightLabelHTML = rightLabelHTML
this.leftLabel = leftLabel
this.leftLabelHTML = leftLabelHTML
this.tabStopList = new TabStopList(this)
this.body = this.extractTabStops(bodyTree)
}
extractTabStops (bodyTree) {
const bodyText = []
let row = 0
let column = 0
// recursive helper function; mutates vars above
let extractTabStops = bodyTree => {
for (const segment of bodyTree) {
if (segment.index != null) {
let {index, content, substitution} = segment
if (index === 0) { index = Infinity; }
const start = [row, column]
extractTabStops(content)
const referencedTabStops = tabStopsReferencedWithinTabStopContent(content)
const range = new Range(start, [row, column])
const tabStop = this.tabStopList.findOrCreate({
index,
snippet: this
})
tabStop.addInsertion({
range,
substitution,
references: Array.from(referencedTabStops)
})
} else if (typeof segment === 'string') {
bodyText.push(segment)
var segmentLines = segment.split('\n')
column += segmentLines.shift().length
let nextLine
while ((nextLine = segmentLines.shift()) != null) {
row += 1
column = nextLine.length
}
}
}
}
extractTabStops(bodyTree)
this.lineCount = row + 1
this.insertions = this.tabStopList.getInsertions()
return bodyText.join('')
}
}