-
Notifications
You must be signed in to change notification settings - Fork 1
/
yaml-utils.js
401 lines (365 loc) · 12 KB
/
yaml-utils.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import * as YAML from 'yaml-ast-parser'
import {safeLoad as loadYaml} from 'yaml-ast-parser'
import IntervalTree from 'node-interval-tree'
/**
* Finds the position [row, column] for a given error within
* the yaml document contents
*
* @param {*} lines the contents of the yaml document, split into lines
* @param {*} error the error object
*/
export function getErrorPosition(lines, error) {
let indent = ''
let row = 0
let prefix = ''
let found = false
let len = lines.length
let maxTrace = error.trace.length
let line = ''
if (error.errorType === 0) {
--maxTrace
}
for (let i=1; i < maxTrace; ++i) {
let part = error.trace[i]
indent = findNextIndent(lines, row, false)
prefix = `${indent}${part.stepName}:`
found = false
for (let j=row; j < len && !found; ++j) {
line = lines[j]
if (line.startsWith(prefix)) {
found = true
row = j
}
}
if ('arrayPos' in part) {
// find the indent of the next line
indent = findNextIndent(lines, row, true)
prefix = `${indent}-`
let foundPos = -1
for (let a=row; a < len && foundPos < part.arrayPos; ++a) {
line = lines[a]
if (line.startsWith(prefix)) {
++foundPos
if (foundPos === part.arrayPos) {
row = a
}
}
}
}
}
return row
}
function findNextIndent(lines, startingLine, isArray) {
for (let i=startingLine+1, len=lines.length; i < len; ++i) {
let line = lines[i]
let trimmed = line.trim()
let beginsArray = trimmed.startsWith('-')
if (trimmed.startsWith('#') || (isArray && !beginsArray) || (!isArray && beginsArray)) {
continue
} else {
let indent = line.substr(0, line.length - trimmed.length)
return indent
}
}
return ''
}
/**
* Finds the position [row, column] for a given path within
* the yaml document contents
*
* @param {*} lines the contents of the yaml document, split into lines
* @param {*} path the path reference
*/
export function getPositionForPath(lines, path) {
let trace = [{}]
for (let segment of path.split(/[.]/g)) {
let parts = segment.split(/\[/)
let step = {stepName: parts[0]}
if (parts.length > 1) {
step.arrayPos = parseInt(parts[1].substr(0, parts[1].length - 1), 10)
}
trace.push(step)
}
return getErrorPosition(lines, {trace: trace, errorType: -1})
}
/**
* Gets the [[path, indent]] for a position within the current document
* as an array of [path, indent] possibilities
*
* @param {*} contents the document text
* @param {*} row the row of the cursor
* @param {*} column the column of the cursor
*/
export function getPathAndIndentForPosition(contents, row, column) {
if (contents) {
let index = new YamlIndex(contents)
return index.lookupPathAndIndent(row, column)
} else {
return {paths: ['.'], indent: ''}
}
}
class YamlIndex {
constructor(contents, row, col) {
this.ast = loadYaml(contents)
let lines = this.lines = contents.split(/\n/g, -1)
this._index = {
range: new IntervalTree(),
nodePos: [],
nodePath: {},
posToRow: [],
}
// create index of pos to row,col
let pos = 0
for (let s = 0, len=lines.length; s < len; ++s) {
this._index.posToRow.push([pos, lines[s].length])
pos += lines[s].length + 1
}
this.visit(this.ast)
}
lookupPathAndIndent(row, column) {
let finalResult = {paths:[], indent:''}
let pos = this._index.posToRow[row][0] + column
let path = '.'
if (this.rowIsBlank(row)) {
// when the position result is on a blank line,
// we may return multiple results
let adjustedRow = row
let line, subline, skippedSequenceElement
do {
--adjustedRow
line = this.lines[adjustedRow]
subline = line.substr(column).match(/^(\s+|\s*-)/)
if (line.substr(column).match(/^\s*-/)) {
skippedSequenceElement = true
}
} while (line.length < column || subline)
if (skippedSequenceElement) {
adjustedRow++
}
pos = this._index.posToRow[adjustedRow][0] + column
let results = this._index.range.search(pos, pos)
for (let r of results) {
let node = r[1]
let resultPath = r[0]
let [startRow,] = this.positionToRowCol(node.startPosition)
let nodeIndent = this.lines[startRow].match(/^([\s]*)/)[1]
if (node.kind === YAML.Kind.SEQ && !node.items) {
nodeIndent = this.lines[startRow-1].match(/^([\s]*)/)[1] + ' '
} else if (node.parent && node.parent.kind === YAML.Kind.SEQ && node.parent.items.indexOf(node) === 0) {
nodeIndent += ' '
} else if (node.parent && node.parent.kind === YAML.Kind.MAPPING && !('kind' in node)) {
nodeIndent += ' '
}
if (column === nodeIndent.length) {
finalResult.paths.push(resultPath)
}
}
} else {
// when the position result is not on a blank line,
// we should be able to return a single result
let results = this._index.range.search(pos, pos)
let bestFit = this.ast.endPosition
for (let r of results) {
let node = r[1]
let diff = node.endPosition - node.startPosition
if (diff < bestFit) {
bestFit = diff
path = r[0]
} else if (diff === bestFit && node.endPosition > pos) {
bestFit = diff
path = r[0]
}
}
finalResult.paths.push(path)
}
finalResult.indent = this.lines[row].match(/^(\s*)/)[1].substr(0, column)
return finalResult
}
visit(node) {
if (node) {
switch (node.kind) {
case YAML.Kind.SCALAR: {
return this.visitScalar(node);
}
case YAML.Kind.MAP: {
return this.visitMap(node);
}
case YAML.Kind.MAPPING: {
return this.visitMapping(node);
}
case YAML.Kind.SEQ: {
return this.visitSequence(node);
}
case YAML.Kind.ANCHOR_REF: {
return this.visitAnchorRef(node);
}
case YAML.Kind.INCLUDE_REF: {
return this.visitIncludeRef(node);
}
default:
}
throw new Error(`Kind, ${node.kind} not implemented.`)
}
}
indexPositionAndPath(path, parent, node, elementIndex) {
path = path || '.'
if (node) {
if (node.endPosition < node.startPosition) {
console.error(`unexpected node start/end combination: ${JSON.stringify(node)}`)
}
} else if (parent.kind === YAML.Kind.SEQ) {
let prevItem = null
if (elementIndex > 0) {
for (let i=elementIndex-1; i >= 0 && prevItem === null; --i) {
prevItem = parent.items[i]
}
}
let startPos = (prevItem ? prevItem.endPosition : parent.startPosition) + 1
let endPos = elementIndex + 1 < parent.items.length ? parent.items[elementIndex+1].startPosition : parent.endPosition
node = {startPosition: startPos, endPosition: endPos, parent: parent}
} else if (parent.kind === YAML.Kind.MAPPING) {
let map = parent.parent
let startPos, endPos
if (elementIndex > 0) {
startPos = map.mappings[elementIndex-1].endPosition
} else {
startPos = parent.endPosition
}
if (elementIndex + 1 < map.mappings.length) {
endPos = map.mappings[elementIndex+1].startPosition
} else {
endPos = map.endPosition
}
node = {startPosition: startPos, endPosition: endPos, parent: parent}
}
if (node.parent && node.parent.kind === YAML.Kind.SEQ && !node._adjustedForSequence) {
// find the position just following the '-' and set start position there
let [r, c] = this.positionToRowCol(node.startPosition)
let elementStart = this.lines[r].indexOf('-') + 1
node.startPosition += (elementStart - c)
node._adjustedForSequence = true
// } else if (node.parent && node.parent.kind === YAML.Kind.MAPPING && !node._adjustedForMapping && !('kind' in node)) {
// // let [r, c] = this.positionToRowCol(node.startPosition)
// node.startPosition += 3
// node.endPosition = Math.max(node.endPosition, node.startPosition)
// node._adjustedForMapping = true
}
if (node !== this.ast) {
this.adjustEndPosition(node, path)
}
this._index.range.insert(node.startPosition, node.endPosition, [path, node])
}
visitScalar(node) {
let path = pathForNode(node) || '.'
this.indexPositionAndPath(path, node.parent, node)
}
visitMapping(node) {
if (node.value) {
this.visit(this.visit(node.value))
} else {
let path = pathForNode(node) + `.${node.key.value}`
this.indexPositionAndPath(path, node, null, node.parent.mappings.indexOf(node))
}
}
visitSequence(node) {
let that = this
let thisPath = pathForNode(node)
node.items.forEach(function(n, index) {
if (n) {
that.visit(n)
} else {
let path = thisPath + `[${index}]`
that.indexPositionAndPath(path, node, null, index)
}
})
this.indexPositionAndPath(thisPath, node.parent, node)
}
visitMap(node) {
node.mappings.map(n => this.visitMapping(n))
let path = pathForNode(node)
this.indexPositionAndPath(path, node.parent, node)
}
visitAnchorRef(nodeAnchorReference) {
}
visitIncludeRef(node) {
}
adjustEndPosition(node, path) {
let [startRow,] = this.positionToRowCol(node.startPosition)
let [endRow, endCol] = this.positionToRowCol(node.endPosition)
if (node.endPosition > node.startPosition) {
if (this.lines[endRow].substr(0, endCol).match(/^\s*$/)) {
node.endPosition -= (endCol + 1)
}
} else if (startRow === endRow && endCol < this.lines[endRow].length) {
node.endPosition += (this.lines[endRow].length - endCol)
}
}
indexFollowingBlank(path, node) {
// if the next row is blank, index it as a next element in this sequence
if (node !== this.ast) {
let [startRow,] = this.positionToRowCol(node.startPosition)
let [endRow, endCol] = this.positionToRowCol(node.endPosition)
let blankRow = 0
// if the node ends the row...
if (this.lines[endRow].substr(endCol).match(/^\s*$/)) {
// and is followed by a blank row, or ends with a blank row...
if (this.rowIsBlank(endRow+1)) {
blankRow = endRow+1
} else if (this.rowIsBlank(endRow)) {
blankRow = endRow
}
if (blankRow > 0) {
let indent = this.lines[startRow].match(/^(\s*).*/)[1]
if (node.items) {
path += `[${node.items.length}]`
}
this.indexPositionAndPath(path,
node,
{
startPosition: Math.min(this.ast.endPosition, node.endPosition + indent.length),
endPosition: Math.max(node.endPosition + indent.length,
this._index.posToRow[blankRow][0] + this.lines[blankRow].length),
}
)
}
}
}
}
rowIsBlank(row) {
return (row < this.lines.length && (this.lines[row].match(/^\s*$/) || this.lines[row] === ''))
}
positionToRowCol(position) {
var max = this._index.posToRow.length - 1
var min = 0
while (min <= max) {
let mid = Math.floor((max + min) / 2)
let [pos, rowLen] = this._index.posToRow[mid]
if (position < pos) {
max = mid - 1
} else if (position > (pos + rowLen)) {
min = mid + 1
} else {
return [mid, position - pos]
}
}
let lastRow = this._index.posToRow[this._index.posToRow.length - 1]
if (position > lastRow[0] + lastRow[1]) {
return [this._index.posToRow.length - 1, lastRow[1]]
}
return null
}
}
function pathForNode(node) {
let path = ''
if (node && node.parent) {
path = pathForNode(node.parent)
if (node.parent.kind === YAML.Kind.SEQ) {
let index = node.parent.items.indexOf(node)
path += `[${index}]`
} else if (node.parent.kind === YAML.Kind.MAPPING) {
let key = node.parent.key.value
path += `.${key}`
}
}
return path
}