|
| 1 | +const {Range} = require('atom') |
| 2 | +const TabStopList = require('./tab-stop-list') |
| 3 | + |
| 4 | +module.exports = class Snippet { |
| 5 | + constructor({name, prefix, bodyText, description, descriptionMoreURL, rightLabelHTML, leftLabel, leftLabelHTML, bodyTree}) { |
| 6 | + this.name = name |
| 7 | + this.prefix = prefix |
| 8 | + this.bodyText = bodyText |
| 9 | + this.description = description |
| 10 | + this.descriptionMoreURL = descriptionMoreURL |
| 11 | + this.rightLabelHTML = rightLabelHTML |
| 12 | + this.leftLabel = leftLabel |
| 13 | + this.leftLabelHTML = leftLabelHTML |
| 14 | + this.tabStopList = new TabStopList(this) |
| 15 | + this.body = this.extractTabStops(bodyTree) |
| 16 | + } |
| 17 | + |
| 18 | + extractTabStops (bodyTree) { |
| 19 | + const bodyText = [] |
| 20 | + let row = 0 |
| 21 | + let column = 0 |
| 22 | + |
| 23 | + // recursive helper function; mutates vars above |
| 24 | + let extractTabStops = bodyTree => { |
| 25 | + for (let segment of bodyTree) { |
| 26 | + if (segment.index != null) { |
| 27 | + let {index, content, substitution} = segment |
| 28 | + if (index === 0) { index = Infinity; } |
| 29 | + const start = [row, column] |
| 30 | + extractTabStops(content) |
| 31 | + const range = new Range(start, [row, column]) |
| 32 | + |
| 33 | + const tabStop = this.tabStopList.findOrCreate({ |
| 34 | + index, |
| 35 | + snippet: this |
| 36 | + }) |
| 37 | + |
| 38 | + tabStop.addInsertion({ range, substitution }) |
| 39 | + |
| 40 | + } else if (typeof segment === 'string') { |
| 41 | + bodyText.push(segment) |
| 42 | + var segmentLines = segment.split('\n') |
| 43 | + column += segmentLines.shift().length |
| 44 | + let nextLine |
| 45 | + |
| 46 | + while ((nextLine = segmentLines.shift()) != null) { |
| 47 | + row += 1 |
| 48 | + column = nextLine.length |
| 49 | + } |
| 50 | + |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + extractTabStops(bodyTree) |
| 56 | + this.lineCount = row + 1 |
| 57 | + this.insertions = this.tabStopList.getInsertions() |
| 58 | + |
| 59 | + return bodyText.join('') |
| 60 | + } |
| 61 | +} |
0 commit comments