opencode-navigator 0.27.3 → 0.27.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,838 @@
1
+ function createScanner(text,ignoreTrivia=false){const len=text.length
2
+ let pos=0,value="",tokenOffset=0,token=16,lineNumber=0,lineStartOffset=0,tokenLineStartOffset=0,prevTokenLineStartOffset=0,scanError=0
3
+ function scanHexDigits(count,exact){let digits=0
4
+ let value=0
5
+ while(digits<count||!exact){let ch=text.charCodeAt(pos)
6
+ if(ch>=48&&ch<=57){value=value*16 + ch-48
7
+ } else if(ch>=65&&ch<=70){value=value*16 + ch-65 + 10
8
+ } else if(ch>=97&&ch<=102){value=value*16 + ch-97 + 10
9
+ } else{break
10
+ }
11
+ pos++
12
+ digits++
13
+ }
14
+ if(digits<count){value=-1
15
+ }
16
+ return value
17
+ }
18
+ function setPosition(newPosition){pos=newPosition
19
+ value=""
20
+ tokenOffset=0
21
+ token=16
22
+ scanError=0
23
+ }
24
+ function scanNumber(){let start=pos
25
+ if(text.charCodeAt(pos)===48){pos++
26
+ } else{pos++
27
+ while(pos<text.length&&isDigit(text.charCodeAt(pos))){pos++
28
+ }}
29
+ if(pos<text.length&&text.charCodeAt(pos)===46){pos++
30
+ if(pos<text.length&&isDigit(text.charCodeAt(pos))){pos++
31
+ while(pos<text.length&&isDigit(text.charCodeAt(pos))){pos++
32
+ }} else{scanError=3
33
+ return text.substring(start,pos)
34
+ }}
35
+ let end=pos
36
+ if(pos<text.length&&(text.charCodeAt(pos)===69||text.charCodeAt(pos)===101)){pos++
37
+ if(pos<text.length&&text.charCodeAt(pos)===43||text.charCodeAt(pos)===45){pos++
38
+ }
39
+ if(pos<text.length&&isDigit(text.charCodeAt(pos))){pos++
40
+ while(pos<text.length&&isDigit(text.charCodeAt(pos))){pos++
41
+ }
42
+ end=pos
43
+ } else{scanError=3
44
+ }}
45
+ return text.substring(start,end)
46
+ }
47
+ function scanString(){let result="",start=pos
48
+ while(true){if(pos>=len){result += text.substring(start,pos)
49
+ scanError=2
50
+ break
51
+ }
52
+ const ch=text.charCodeAt(pos)
53
+ if(ch===34){result += text.substring(start,pos)
54
+ pos++
55
+ break
56
+ }
57
+ if(ch===92){result += text.substring(start,pos)
58
+ pos++
59
+ if(pos>=len){scanError=2
60
+ break
61
+ }
62
+ const ch2=text.charCodeAt(pos++)
63
+ switch(ch2){case 34:result += '"'
64
+ break
65
+ case 92:result += "\\"
66
+ break
67
+ case 47:result += "/"
68
+ break
69
+ case 98:result += "\b"
70
+ break
71
+ case 102:result += "\f"
72
+ break
73
+ case 110:result += `
74
+ `
75
+ break
76
+ case 114:result += "\r"
77
+ break
78
+ case 116:result += "\t"
79
+ break
80
+ case 117:const ch3=scanHexDigits(4,true)
81
+ if(ch3>=0){result += String.fromCharCode(ch3)
82
+ } else{scanError=4
83
+ }
84
+ break
85
+ default:scanError=5
86
+ }
87
+ start=pos
88
+ continue
89
+ }
90
+ if(ch>=0&&ch<=31){if(isLineBreak(ch)){result += text.substring(start,pos)
91
+ scanError=2
92
+ break
93
+ } else{scanError=6
94
+ }}
95
+ pos++
96
+ }
97
+ return result
98
+ }
99
+ function scanNext(){value=""
100
+ scanError=0
101
+ tokenOffset=pos
102
+ lineStartOffset=lineNumber
103
+ prevTokenLineStartOffset=tokenLineStartOffset
104
+ if(pos>=len){tokenOffset=len
105
+ return token=17
106
+ }
107
+ let code=text.charCodeAt(pos)
108
+ if(isWhiteSpace(code)){do{pos++
109
+ value += String.fromCharCode(code)
110
+ code=text.charCodeAt(pos)
111
+ } while(isWhiteSpace(code))
112
+ return token=15
113
+ }
114
+ if(isLineBreak(code)){pos++
115
+ value += String.fromCharCode(code)
116
+ if(code===13&&text.charCodeAt(pos)===10){pos++
117
+ value += `
118
+ `
119
+ }
120
+ lineNumber++
121
+ tokenLineStartOffset=pos
122
+ return token=14
123
+ }
124
+ switch(code){case 123:pos++
125
+ return token=1
126
+ case 125:pos++
127
+ return token=2
128
+ case 91:pos++
129
+ return token=3
130
+ case 93:pos++
131
+ return token=4
132
+ case 58:pos++
133
+ return token=6
134
+ case 44:pos++
135
+ return token=5
136
+ case 34:pos++
137
+ value=scanString()
138
+ return token=10
139
+ case 47:const start=pos-1
140
+ if(text.charCodeAt(pos + 1)===47){pos += 2
141
+ while(pos<len){if(isLineBreak(text.charCodeAt(pos))){break
142
+ }
143
+ pos++
144
+ }
145
+ value=text.substring(start,pos)
146
+ return token=12
147
+ }
148
+ if(text.charCodeAt(pos + 1)===42){pos += 2
149
+ const safeLength=len-1
150
+ let commentClosed=false
151
+ while(pos<safeLength){const ch=text.charCodeAt(pos)
152
+ if(ch===42&&text.charCodeAt(pos + 1)===47){pos += 2
153
+ commentClosed=true
154
+ break
155
+ }
156
+ pos++
157
+ if(isLineBreak(ch)){if(ch===13&&text.charCodeAt(pos)===10){pos++
158
+ }
159
+ lineNumber++
160
+ tokenLineStartOffset=pos
161
+ }}
162
+ if(!commentClosed){pos++
163
+ scanError=1
164
+ }
165
+ value=text.substring(start,pos)
166
+ return token=13
167
+ }
168
+ value += String.fromCharCode(code)
169
+ pos++
170
+ return token=16
171
+ case 45:value += String.fromCharCode(code)
172
+ pos++
173
+ if(pos===len||!isDigit(text.charCodeAt(pos))){return token=16
174
+ }
175
+ case 48:case 49:case 50:case 51:case 52:case 53:case 54:case 55:case 56:case 57:value += scanNumber()
176
+ return token=11
177
+ default:while(pos<len&&isUnknownContentCharacter(code)){pos++
178
+ code=text.charCodeAt(pos)
179
+ }
180
+ if(tokenOffset!==pos){value=text.substring(tokenOffset,pos)
181
+ switch(value){case "true":return token=8
182
+ case "false":return token=9
183
+ case "null":return token=7
184
+ }
185
+ return token=16
186
+ }
187
+ value += String.fromCharCode(code)
188
+ pos++
189
+ return token=16
190
+ }}
191
+ function isUnknownContentCharacter(code){if(isWhiteSpace(code)||isLineBreak(code)){return false
192
+ }
193
+ switch(code){case 125:case 93:case 123:case 91:case 34:case 58:case 44:case 47:return false
194
+ }
195
+ return true
196
+ }
197
+ function scanNextNonTrivia(){let result
198
+ do{result=scanNext()
199
+ } while(result>=12&&result<=15)
200
+ return result
201
+ }
202
+ return{setPosition,getPosition:()=>pos,scan:ignoreTrivia?scanNextNonTrivia:scanNext,getToken:()=>token,getTokenValue:()=>value,getTokenOffset:()=>tokenOffset,getTokenLength:()=>pos-tokenOffset,getTokenStartLine:()=>lineStartOffset,getTokenStartCharacter:()=>tokenOffset-prevTokenLineStartOffset,getTokenError:()=>scanError
203
+ }}
204
+ function isWhiteSpace(ch){return ch===32||ch===9
205
+ }
206
+ function isLineBreak(ch){return ch===10||ch===13
207
+ }
208
+ function isDigit(ch){return ch>=48&&ch<=57
209
+ }
210
+ var CharacterCodes;
211
+ (function(CharacterCodes){CharacterCodes[CharacterCodes["lineFeed"]=10]="lineFeed"
212
+ CharacterCodes[CharacterCodes["carriageReturn"]=13]="carriageReturn"
213
+ CharacterCodes[CharacterCodes["space"]=32]="space"
214
+ CharacterCodes[CharacterCodes["_0"]=48]="_0"
215
+ CharacterCodes[CharacterCodes["_1"]=49]="_1"
216
+ CharacterCodes[CharacterCodes["_2"]=50]="_2"
217
+ CharacterCodes[CharacterCodes["_3"]=51]="_3"
218
+ CharacterCodes[CharacterCodes["_4"]=52]="_4"
219
+ CharacterCodes[CharacterCodes["_5"]=53]="_5"
220
+ CharacterCodes[CharacterCodes["_6"]=54]="_6"
221
+ CharacterCodes[CharacterCodes["_7"]=55]="_7"
222
+ CharacterCodes[CharacterCodes["_8"]=56]="_8"
223
+ CharacterCodes[CharacterCodes["_9"]=57]="_9"
224
+ CharacterCodes[CharacterCodes["a"]=97]="a"
225
+ CharacterCodes[CharacterCodes["b"]=98]="b"
226
+ CharacterCodes[CharacterCodes["c"]=99]="c"
227
+ CharacterCodes[CharacterCodes["d"]=100]="d"
228
+ CharacterCodes[CharacterCodes["e"]=101]="e"
229
+ CharacterCodes[CharacterCodes["f"]=102]="f"
230
+ CharacterCodes[CharacterCodes["g"]=103]="g"
231
+ CharacterCodes[CharacterCodes["h"]=104]="h"
232
+ CharacterCodes[CharacterCodes["i"]=105]="i"
233
+ CharacterCodes[CharacterCodes["j"]=106]="j"
234
+ CharacterCodes[CharacterCodes["k"]=107]="k"
235
+ CharacterCodes[CharacterCodes["l"]=108]="l"
236
+ CharacterCodes[CharacterCodes["m"]=109]="m"
237
+ CharacterCodes[CharacterCodes["n"]=110]="n"
238
+ CharacterCodes[CharacterCodes["o"]=111]="o"
239
+ CharacterCodes[CharacterCodes["p"]=112]="p"
240
+ CharacterCodes[CharacterCodes["q"]=113]="q"
241
+ CharacterCodes[CharacterCodes["r"]=114]="r"
242
+ CharacterCodes[CharacterCodes["s"]=115]="s"
243
+ CharacterCodes[CharacterCodes["t"]=116]="t"
244
+ CharacterCodes[CharacterCodes["u"]=117]="u"
245
+ CharacterCodes[CharacterCodes["v"]=118]="v"
246
+ CharacterCodes[CharacterCodes["w"]=119]="w"
247
+ CharacterCodes[CharacterCodes["x"]=120]="x"
248
+ CharacterCodes[CharacterCodes["y"]=121]="y"
249
+ CharacterCodes[CharacterCodes["z"]=122]="z"
250
+ CharacterCodes[CharacterCodes["A"]=65]="A"
251
+ CharacterCodes[CharacterCodes["B"]=66]="B"
252
+ CharacterCodes[CharacterCodes["C"]=67]="C"
253
+ CharacterCodes[CharacterCodes["D"]=68]="D"
254
+ CharacterCodes[CharacterCodes["E"]=69]="E"
255
+ CharacterCodes[CharacterCodes["F"]=70]="F"
256
+ CharacterCodes[CharacterCodes["G"]=71]="G"
257
+ CharacterCodes[CharacterCodes["H"]=72]="H"
258
+ CharacterCodes[CharacterCodes["I"]=73]="I"
259
+ CharacterCodes[CharacterCodes["J"]=74]="J"
260
+ CharacterCodes[CharacterCodes["K"]=75]="K"
261
+ CharacterCodes[CharacterCodes["L"]=76]="L"
262
+ CharacterCodes[CharacterCodes["M"]=77]="M"
263
+ CharacterCodes[CharacterCodes["N"]=78]="N"
264
+ CharacterCodes[CharacterCodes["O"]=79]="O"
265
+ CharacterCodes[CharacterCodes["P"]=80]="P"
266
+ CharacterCodes[CharacterCodes["Q"]=81]="Q"
267
+ CharacterCodes[CharacterCodes["R"]=82]="R"
268
+ CharacterCodes[CharacterCodes["S"]=83]="S"
269
+ CharacterCodes[CharacterCodes["T"]=84]="T"
270
+ CharacterCodes[CharacterCodes["U"]=85]="U"
271
+ CharacterCodes[CharacterCodes["V"]=86]="V"
272
+ CharacterCodes[CharacterCodes["W"]=87]="W"
273
+ CharacterCodes[CharacterCodes["X"]=88]="X"
274
+ CharacterCodes[CharacterCodes["Y"]=89]="Y"
275
+ CharacterCodes[CharacterCodes["Z"]=90]="Z"
276
+ CharacterCodes[CharacterCodes["asterisk"]=42]="asterisk"
277
+ CharacterCodes[CharacterCodes["backslash"]=92]="backslash"
278
+ CharacterCodes[CharacterCodes["closeBrace"]=125]="closeBrace"
279
+ CharacterCodes[CharacterCodes["closeBracket"]=93]="closeBracket"
280
+ CharacterCodes[CharacterCodes["colon"]=58]="colon"
281
+ CharacterCodes[CharacterCodes["comma"]=44]="comma"
282
+ CharacterCodes[CharacterCodes["dot"]=46]="dot"
283
+ CharacterCodes[CharacterCodes["doubleQuote"]=34]="doubleQuote"
284
+ CharacterCodes[CharacterCodes["minus"]=45]="minus"
285
+ CharacterCodes[CharacterCodes["openBrace"]=123]="openBrace"
286
+ CharacterCodes[CharacterCodes["openBracket"]=91]="openBracket"
287
+ CharacterCodes[CharacterCodes["plus"]=43]="plus"
288
+ CharacterCodes[CharacterCodes["slash"]=47]="slash"
289
+ CharacterCodes[CharacterCodes["formFeed"]=12]="formFeed"
290
+ CharacterCodes[CharacterCodes["tab"]=9]="tab"
291
+ })(CharacterCodes||(CharacterCodes={}));
292
+ var cachedSpaces=new Array(20).fill(0).map((_,index)=>{return " ".repeat(index)
293
+ })
294
+ var maxCachedValues=200
295
+ var cachedBreakLinesWithSpaces={" ":{"\n":new Array(maxCachedValues).fill(0).map((_,index)=>{return `
296
+ ` + " ".repeat(index)
297
+ }),"\r":new Array(maxCachedValues).fill(0).map((_,index)=>{return "\r" + " ".repeat(index)
298
+ }),"\r\n":new Array(maxCachedValues).fill(0).map((_,index)=>{return `\r
299
+ ` + " ".repeat(index)
300
+ })
301
+ },"\t":{"\n":new Array(maxCachedValues).fill(0).map((_,index)=>{return `
302
+ ` + "\t".repeat(index)
303
+ }),"\r":new Array(maxCachedValues).fill(0).map((_,index)=>{return "\r" + "\t".repeat(index)
304
+ }),"\r\n":new Array(maxCachedValues).fill(0).map((_,index)=>{return `\r
305
+ ` + "\t".repeat(index)
306
+ })
307
+ }}
308
+ var supportedEols=[`
309
+ `,"\r",`\r
310
+ `];
311
+ function format(documentText,range,options){let initialIndentLevel
312
+ let formatText
313
+ let formatTextStart
314
+ let rangeStart
315
+ let rangeEnd
316
+ if(range){rangeStart=range.offset
317
+ rangeEnd=rangeStart + range.length
318
+ formatTextStart=rangeStart
319
+ while(formatTextStart>0&&!isEOL(documentText,formatTextStart-1)){formatTextStart--
320
+ }
321
+ let endOffset=rangeEnd
322
+ while(endOffset<documentText.length&&!isEOL(documentText,endOffset)){endOffset++
323
+ }
324
+ formatText=documentText.substring(formatTextStart,endOffset)
325
+ initialIndentLevel=computeIndentLevel(formatText,options)
326
+ } else{formatText=documentText
327
+ initialIndentLevel=0
328
+ formatTextStart=0
329
+ rangeStart=0
330
+ rangeEnd=documentText.length
331
+ }
332
+ const eol=getEOL(options,documentText)
333
+ const eolFastPathSupported=supportedEols.includes(eol)
334
+ let numberLineBreaks=0
335
+ let indentLevel=0
336
+ let indentValue
337
+ if(options.insertSpaces){indentValue=cachedSpaces[options.tabSize||4]??repeat(cachedSpaces[1],options.tabSize||4)
338
+ } else{indentValue="\t"
339
+ }
340
+ const indentType=indentValue==="\t"?"\t":" "
341
+ let scanner=createScanner(formatText,false)
342
+ let hasError=false
343
+ function newLinesAndIndent(){if(numberLineBreaks>1){return repeat(eol,numberLineBreaks) + repeat(indentValue,initialIndentLevel + indentLevel)
344
+ }
345
+ const amountOfSpaces=indentValue.length*(initialIndentLevel + indentLevel)
346
+ if(!eolFastPathSupported||amountOfSpaces>cachedBreakLinesWithSpaces[indentType][eol].length){return eol + repeat(indentValue,initialIndentLevel + indentLevel)
347
+ }
348
+ if(amountOfSpaces<=0){return eol
349
+ }
350
+ return cachedBreakLinesWithSpaces[indentType][eol][amountOfSpaces]
351
+ }
352
+ function scanNext(){let token=scanner.scan()
353
+ numberLineBreaks=0
354
+ while(token===15||token===14){if(token===14&&options.keepLines){numberLineBreaks += 1
355
+ } else if(token===14){numberLineBreaks=1
356
+ }
357
+ token=scanner.scan()
358
+ }
359
+ hasError=token===16||scanner.getTokenError()!==0
360
+ return token
361
+ }
362
+ const editOperations=[]
363
+ function addEdit(text,startOffset,endOffset){if(!hasError&&(!range||startOffset<rangeEnd&&endOffset>rangeStart)&&documentText.substring(startOffset,endOffset)!==text){editOperations.push({offset:startOffset,length:endOffset-startOffset,content:text})
364
+ }}
365
+ let firstToken=scanNext()
366
+ if(options.keepLines&&numberLineBreaks>0){addEdit(repeat(eol,numberLineBreaks),0,0)
367
+ }
368
+ if(firstToken!==17){let firstTokenStart=scanner.getTokenOffset() + formatTextStart
369
+ let initialIndent=indentValue.length*initialIndentLevel<20&&options.insertSpaces?cachedSpaces[indentValue.length*initialIndentLevel]:repeat(indentValue,initialIndentLevel)
370
+ addEdit(initialIndent,formatTextStart,firstTokenStart)
371
+ }
372
+ while(firstToken!==17){let firstTokenEnd=scanner.getTokenOffset() + scanner.getTokenLength() + formatTextStart
373
+ let secondToken=scanNext()
374
+ let replaceContent=""
375
+ let needsLineBreak=false
376
+ while(numberLineBreaks===0&&(secondToken===12||secondToken===13)){let commentTokenStart=scanner.getTokenOffset() + formatTextStart
377
+ addEdit(cachedSpaces[1],firstTokenEnd,commentTokenStart)
378
+ firstTokenEnd=scanner.getTokenOffset() + scanner.getTokenLength() + formatTextStart
379
+ needsLineBreak=secondToken===12
380
+ replaceContent=needsLineBreak?newLinesAndIndent():""
381
+ secondToken=scanNext()
382
+ }
383
+ if(secondToken===2){if(firstToken!==1){indentLevel--
384
+ }
385
+ if(options.keepLines&&numberLineBreaks>0||!options.keepLines&&firstToken!==1){replaceContent=newLinesAndIndent()
386
+ } else if(options.keepLines){replaceContent=cachedSpaces[1]
387
+ }} else if(secondToken===4){if(firstToken!==3){indentLevel--
388
+ }
389
+ if(options.keepLines&&numberLineBreaks>0||!options.keepLines&&firstToken!==3){replaceContent=newLinesAndIndent()
390
+ } else if(options.keepLines){replaceContent=cachedSpaces[1]
391
+ }} else{switch(firstToken){case 3:case 1:indentLevel++
392
+ if(options.keepLines&&numberLineBreaks>0||!options.keepLines){replaceContent=newLinesAndIndent()
393
+ } else{replaceContent=cachedSpaces[1]
394
+ }
395
+ break
396
+ case 5:if(options.keepLines&&numberLineBreaks>0||!options.keepLines){replaceContent=newLinesAndIndent()
397
+ } else{replaceContent=cachedSpaces[1]
398
+ }
399
+ break
400
+ case 12:replaceContent=newLinesAndIndent()
401
+ break
402
+ case 13:if(numberLineBreaks>0){replaceContent=newLinesAndIndent()
403
+ } else if(!needsLineBreak){replaceContent=cachedSpaces[1]
404
+ }
405
+ break
406
+ case 6:if(options.keepLines&&numberLineBreaks>0){replaceContent=newLinesAndIndent()
407
+ } else if(!needsLineBreak){replaceContent=cachedSpaces[1]
408
+ }
409
+ break
410
+ case 10:if(options.keepLines&&numberLineBreaks>0){replaceContent=newLinesAndIndent()
411
+ } else if(secondToken===6&&!needsLineBreak){replaceContent=""
412
+ }
413
+ break
414
+ case 7:case 8:case 9:case 11:case 2:case 4:if(options.keepLines&&numberLineBreaks>0){replaceContent=newLinesAndIndent()
415
+ } else{if((secondToken===12||secondToken===13)&&!needsLineBreak){replaceContent=cachedSpaces[1]
416
+ } else if(secondToken!==5&&secondToken!==17){hasError=true
417
+ }}
418
+ break
419
+ case 16:hasError=true
420
+ break
421
+ }
422
+ if(numberLineBreaks>0&&(secondToken===12||secondToken===13)){replaceContent=newLinesAndIndent()
423
+ }}
424
+ if(secondToken===17){if(options.keepLines&&numberLineBreaks>0){replaceContent=newLinesAndIndent()
425
+ } else{replaceContent=options.insertFinalNewline?eol:""
426
+ }}
427
+ const secondTokenStart=scanner.getTokenOffset() + formatTextStart
428
+ addEdit(replaceContent,firstTokenEnd,secondTokenStart)
429
+ firstToken=secondToken
430
+ }
431
+ return editOperations
432
+ }
433
+ function repeat(s,count){let result=""
434
+ for(let i=0;i<count; i++){result += s
435
+ }
436
+ return result
437
+ }
438
+ function computeIndentLevel(content,options){let i=0
439
+ let nChars=0
440
+ const tabSize=options.tabSize||4
441
+ while(i<content.length){let ch=content.charAt(i)
442
+ if(ch===cachedSpaces[1]){nChars++
443
+ } else if(ch==="\t"){nChars += tabSize
444
+ } else{break
445
+ }
446
+ i++
447
+ }
448
+ return Math.floor(nChars / tabSize)
449
+ }
450
+ function getEOL(options,text){for(let i=0;i<text.length; i++){const ch=text.charAt(i)
451
+ if(ch==="\r"){if(i + 1<text.length&&text.charAt(i + 1)===`
452
+ `){return `\r
453
+ `
454
+ }
455
+ return "\r"
456
+ } else if(ch===`
457
+ `){return `
458
+ `
459
+ }}
460
+ return options&&options.eol||`
461
+ `
462
+ }
463
+ function isEOL(text,offset){return `\r
464
+ `.indexOf(text.charAt(offset))!==-1
465
+ }
466
+ var ParseOptions;
467
+ (function(ParseOptions){ParseOptions.DEFAULT={allowTrailingComma:false
468
+ }})(ParseOptions||(ParseOptions={}))
469
+ function parseTree2(text,errors=[],options=ParseOptions.DEFAULT){let currentParent={type:"array",offset:-1,length:-1,children:[],parent:undefined}
470
+ function ensurePropertyComplete(endOffset){if(currentParent.type==="property"){currentParent.length=endOffset-currentParent.offset
471
+ currentParent=currentParent.parent
472
+ }}
473
+ function onValue(valueNode){currentParent.children.push(valueNode)
474
+ return valueNode
475
+ }
476
+ const visitor={onObjectBegin:(offset)=>{currentParent=onValue({type:"object",offset,length:-1,parent:currentParent,children:[]})
477
+ },onObjectProperty:(name,offset,length)=>{currentParent=onValue({type:"property",offset,length:-1,parent:currentParent,children:[]})
478
+ currentParent.children.push({type:"string",value:name,offset,length,parent:currentParent})
479
+ },onObjectEnd:(offset,length)=>{ensurePropertyComplete(offset + length)
480
+ currentParent.length=offset + length-currentParent.offset
481
+ currentParent=currentParent.parent
482
+ ensurePropertyComplete(offset + length)
483
+ },onArrayBegin:(offset,length)=>{currentParent=onValue({type:"array",offset,length:-1,parent:currentParent,children:[]})
484
+ },onArrayEnd:(offset,length)=>{currentParent.length=offset + length-currentParent.offset
485
+ currentParent=currentParent.parent
486
+ ensurePropertyComplete(offset + length)
487
+ },onLiteralValue:(value,offset,length)=>{onValue({type:getNodeType(value),offset,length,parent:currentParent,value})
488
+ ensurePropertyComplete(offset + length)
489
+ },onSeparator:(sep,offset,length)=>{if(currentParent.type==="property"){if(sep===":"){currentParent.colonOffset=offset
490
+ } else if(sep===","){ensurePropertyComplete(offset)
491
+ }}},onError:(error,offset,length)=>{errors.push({error,offset,length})
492
+ }}
493
+ visit(text,visitor,options)
494
+ const result=currentParent.children[0]
495
+ if(result){delete result.parent
496
+ }
497
+ return result
498
+ }
499
+ function findNodeAtLocation2(root,path){if(!root){return
500
+ }
501
+ let node=root
502
+ for(let segment of path){if(typeof segment==="string"){if(node.type!=="object"||!Array.isArray(node.children)){return
503
+ }
504
+ let found=false
505
+ for(const propertyNode of node.children){if(Array.isArray(propertyNode.children)&&propertyNode.children[0].value===segment&&propertyNode.children.length===2){node=propertyNode.children[1]
506
+ found=true
507
+ break
508
+ }}
509
+ if(!found){return
510
+ }} else{const index=segment
511
+ if(node.type!=="array"||index<0||!Array.isArray(node.children)||index>=node.children.length){return
512
+ }
513
+ node=node.children[index]
514
+ }}
515
+ return node
516
+ }
517
+ function getNodeValue2(node){switch(node.type){case "array":return node.children.map(getNodeValue2)
518
+ case "object":const obj=Object.create(null)
519
+ for(let prop of node.children){const valueNode=prop.children[1]
520
+ if(valueNode){obj[prop.children[0].value]=getNodeValue2(valueNode)
521
+ }}
522
+ return obj
523
+ case "null":case "string":case "number":case "boolean":return node.value
524
+ default:return
525
+ }}
526
+ function visit(text,visitor,options=ParseOptions.DEFAULT){const _scanner=createScanner(text,false)
527
+ const _jsonPath=[]
528
+ let suppressedCallbacks=0
529
+ function toNoArgVisit(visitFunction){return visitFunction?()=>suppressedCallbacks===0&&visitFunction(_scanner.getTokenOffset(),_scanner.getTokenLength(),_scanner.getTokenStartLine(),_scanner.getTokenStartCharacter()):()=>true
530
+ }
531
+ function toOneArgVisit(visitFunction){return visitFunction?(arg)=>suppressedCallbacks===0&&visitFunction(arg,_scanner.getTokenOffset(),_scanner.getTokenLength(),_scanner.getTokenStartLine(),_scanner.getTokenStartCharacter()):()=>true
532
+ }
533
+ function toOneArgVisitWithPath(visitFunction){return visitFunction?(arg)=>suppressedCallbacks===0&&visitFunction(arg,_scanner.getTokenOffset(),_scanner.getTokenLength(),_scanner.getTokenStartLine(),_scanner.getTokenStartCharacter(),()=>_jsonPath.slice()):()=>true
534
+ }
535
+ function toBeginVisit(visitFunction){return visitFunction?()=>{if(suppressedCallbacks>0){suppressedCallbacks++
536
+ } else{let cbReturn=visitFunction(_scanner.getTokenOffset(),_scanner.getTokenLength(),_scanner.getTokenStartLine(),_scanner.getTokenStartCharacter(),()=>_jsonPath.slice())
537
+ if(cbReturn===false){suppressedCallbacks=1
538
+ }}}:()=>true
539
+ }
540
+ function toEndVisit(visitFunction){return visitFunction?()=>{if(suppressedCallbacks>0){suppressedCallbacks--
541
+ }
542
+ if(suppressedCallbacks===0){visitFunction(_scanner.getTokenOffset(),_scanner.getTokenLength(),_scanner.getTokenStartLine(),_scanner.getTokenStartCharacter())
543
+ }}:()=>true
544
+ }
545
+ const onObjectBegin=toBeginVisit(visitor.onObjectBegin),onObjectProperty=toOneArgVisitWithPath(visitor.onObjectProperty),onObjectEnd=toEndVisit(visitor.onObjectEnd),onArrayBegin=toBeginVisit(visitor.onArrayBegin),onArrayEnd=toEndVisit(visitor.onArrayEnd),onLiteralValue=toOneArgVisitWithPath(visitor.onLiteralValue),onSeparator=toOneArgVisit(visitor.onSeparator),onComment=toNoArgVisit(visitor.onComment),onError=toOneArgVisit(visitor.onError)
546
+ const disallowComments=options&&options.disallowComments
547
+ const allowTrailingComma=options&&options.allowTrailingComma
548
+ function scanNext(){while(true){const token=_scanner.scan()
549
+ switch(_scanner.getTokenError()){case 4:handleError(14)
550
+ break
551
+ case 5:handleError(15)
552
+ break
553
+ case 3:handleError(13)
554
+ break
555
+ case 1:if(!disallowComments){handleError(11)
556
+ }
557
+ break
558
+ case 2:handleError(12)
559
+ break
560
+ case 6:handleError(16)
561
+ break
562
+ }
563
+ switch(token){case 12:case 13:if(disallowComments){handleError(10)
564
+ } else{onComment()
565
+ }
566
+ break
567
+ case 16:handleError(1)
568
+ break
569
+ case 15:case 14:break
570
+ default:return token
571
+ }}}
572
+ function handleError(error,skipUntilAfter=[],skipUntil=[]){onError(error)
573
+ if(skipUntilAfter.length + skipUntil.length>0){let token=_scanner.getToken()
574
+ while(token!==17){if(skipUntilAfter.indexOf(token)!==-1){scanNext()
575
+ break
576
+ } else if(skipUntil.indexOf(token)!==-1){break
577
+ }
578
+ token=scanNext()
579
+ }}}
580
+ function parseString(isValue){const value=_scanner.getTokenValue()
581
+ if(isValue){onLiteralValue(value)
582
+ } else{onObjectProperty(value)
583
+ _jsonPath.push(value)
584
+ }
585
+ scanNext()
586
+ return true
587
+ }
588
+ function parseLiteral(){switch(_scanner.getToken()){case 11:const tokenValue=_scanner.getTokenValue()
589
+ let value=Number(tokenValue)
590
+ if(isNaN(value)){handleError(2)
591
+ value=0
592
+ }
593
+ onLiteralValue(value)
594
+ break
595
+ case 7:onLiteralValue(null)
596
+ break
597
+ case 8:onLiteralValue(true)
598
+ break
599
+ case 9:onLiteralValue(false)
600
+ break
601
+ default:return false
602
+ }
603
+ scanNext()
604
+ return true
605
+ }
606
+ function parseProperty(){if(_scanner.getToken()!==10){handleError(3,[],[2,5])
607
+ return false
608
+ }
609
+ parseString(false)
610
+ if(_scanner.getToken()===6){onSeparator(":")
611
+ scanNext()
612
+ if(!parseValue()){handleError(4,[],[2,5])
613
+ }} else{handleError(5,[],[2,5])
614
+ }
615
+ _jsonPath.pop()
616
+ return true
617
+ }
618
+ function parseObject(){onObjectBegin()
619
+ scanNext()
620
+ let needsComma=false
621
+ while(_scanner.getToken()!==2&&_scanner.getToken()!==17){if(_scanner.getToken()===5){if(!needsComma){handleError(4,[],[])
622
+ }
623
+ onSeparator(",")
624
+ scanNext()
625
+ if(_scanner.getToken()===2&&allowTrailingComma){break
626
+ }} else if(needsComma){handleError(6,[],[])
627
+ }
628
+ if(!parseProperty()){handleError(4,[],[2,5])
629
+ }
630
+ needsComma=true
631
+ }
632
+ onObjectEnd()
633
+ if(_scanner.getToken()!==2){handleError(7,[2],[])
634
+ } else{scanNext()
635
+ }
636
+ return true
637
+ }
638
+ function parseArray(){onArrayBegin()
639
+ scanNext()
640
+ let isFirstElement=true
641
+ let needsComma=false
642
+ while(_scanner.getToken()!==4&&_scanner.getToken()!==17){if(_scanner.getToken()===5){if(!needsComma){handleError(4,[],[])
643
+ }
644
+ onSeparator(",")
645
+ scanNext()
646
+ if(_scanner.getToken()===4&&allowTrailingComma){break
647
+ }} else if(needsComma){handleError(6,[],[])
648
+ }
649
+ if(isFirstElement){_jsonPath.push(0)
650
+ isFirstElement=false
651
+ } else{_jsonPath[_jsonPath.length-1]++
652
+ }
653
+ if(!parseValue()){handleError(4,[],[4,5])
654
+ }
655
+ needsComma=true
656
+ }
657
+ onArrayEnd()
658
+ if(!isFirstElement){_jsonPath.pop()
659
+ }
660
+ if(_scanner.getToken()!==4){handleError(8,[4],[])
661
+ } else{scanNext()
662
+ }
663
+ return true
664
+ }
665
+ function parseValue(){switch(_scanner.getToken()){case 3:return parseArray()
666
+ case 1:return parseObject()
667
+ case 10:return parseString(true)
668
+ default:return parseLiteral()
669
+ }}
670
+ scanNext()
671
+ if(_scanner.getToken()===17){if(options.allowEmptyContent){return true
672
+ }
673
+ handleError(4,[],[])
674
+ return false
675
+ }
676
+ if(!parseValue()){handleError(4,[],[])
677
+ return false
678
+ }
679
+ if(_scanner.getToken()!==17){handleError(9,[],[])
680
+ }
681
+ return true
682
+ }
683
+ function getNodeType(value){switch(typeof value){case "boolean":return "boolean"
684
+ case "number":return "number"
685
+ case "string":return "string"
686
+ case "object":{if(!value){return "null"
687
+ } else if(Array.isArray(value)){return "array"
688
+ }
689
+ return "object"
690
+ }
691
+ default:return "null"
692
+ }}
693
+ function setProperty(text,originalPath,value,options){const path=originalPath.slice()
694
+ const errors=[]
695
+ const root=parseTree2(text,errors)
696
+ let parent=undefined
697
+ let lastSegment=undefined
698
+ while(path.length>0){lastSegment=path.pop()
699
+ parent=findNodeAtLocation2(root,path)
700
+ if(parent===undefined&&value!==undefined){if(typeof lastSegment==="string"){value={[lastSegment]:value}} else{value=[value]
701
+ }} else{break
702
+ }}
703
+ if(!parent){if(value===undefined){throw new Error("Can not delete in empty document")
704
+ }
705
+ return withFormatting(text,{offset:root?root.offset:0,length:root?root.length:0,content:JSON.stringify(value)},options)
706
+ } else if(parent.type==="object"&&typeof lastSegment==="string"&&Array.isArray(parent.children)){const existing=findNodeAtLocation2(parent,[lastSegment])
707
+ if(existing!==undefined){if(value===undefined){if(!existing.parent){throw new Error("Malformed AST")
708
+ }
709
+ const propertyIndex=parent.children.indexOf(existing.parent)
710
+ let removeBegin
711
+ let removeEnd=existing.parent.offset + existing.parent.length
712
+ if(propertyIndex>0){let previous=parent.children[propertyIndex-1]
713
+ removeBegin=previous.offset + previous.length
714
+ } else{removeBegin=parent.offset + 1
715
+ if(parent.children.length>1){let next=parent.children[1]
716
+ removeEnd=next.offset
717
+ }}
718
+ return withFormatting(text,{offset:removeBegin,length:removeEnd-removeBegin,content:""},options)
719
+ } else{return withFormatting(text,{offset:existing.offset,length:existing.length,content:JSON.stringify(value)},options)
720
+ }} else{if(value===undefined){return []
721
+ }
722
+ const newProperty=`${JSON.stringify(lastSegment)}: ${JSON.stringify(value)}`
723
+ const index=options.getInsertionIndex?options.getInsertionIndex(parent.children.map((p)=>p.children[0].value)):parent.children.length
724
+ let edit
725
+ if(index>0){let previous=parent.children[index-1]
726
+ edit={offset:previous.offset + previous.length,length:0,content:"," + newProperty}} else if(parent.children.length===0){edit={offset:parent.offset + 1,length:0,content:newProperty}} else{edit={offset:parent.offset + 1,length:0,content:newProperty + ","}}
727
+ return withFormatting(text,edit,options)
728
+ }} else if(parent.type==="array"&&typeof lastSegment==="number"&&Array.isArray(parent.children)){const insertIndex=lastSegment
729
+ if(insertIndex===-1){const newProperty=`${JSON.stringify(value)}`
730
+ let edit
731
+ if(parent.children.length===0){edit={offset:parent.offset + 1,length:0,content:newProperty}} else{const previous=parent.children[parent.children.length-1]
732
+ edit={offset:previous.offset + previous.length,length:0,content:"," + newProperty}}
733
+ return withFormatting(text,edit,options)
734
+ } else if(value===undefined&&parent.children.length>=0){const removalIndex=lastSegment
735
+ const toRemove=parent.children[removalIndex]
736
+ let edit
737
+ if(parent.children.length===1){edit={offset:parent.offset + 1,length:parent.length-2,content:""}} else if(parent.children.length-1===removalIndex){let previous=parent.children[removalIndex-1]
738
+ let offset=previous.offset + previous.length
739
+ let parentEndOffset=parent.offset + parent.length
740
+ edit={offset,length:parentEndOffset-2-offset,content:""}} else{edit={offset:toRemove.offset,length:parent.children[removalIndex + 1].offset-toRemove.offset,content:""}}
741
+ return withFormatting(text,edit,options)
742
+ } else if(value!==undefined){let edit
743
+ const newProperty=`${JSON.stringify(value)}`
744
+ if(!options.isArrayInsertion&&parent.children.length>lastSegment){const toModify=parent.children[lastSegment]
745
+ edit={offset:toModify.offset,length:toModify.length,content:newProperty}} else if(parent.children.length===0||lastSegment===0){edit={offset:parent.offset + 1,length:0,content:parent.children.length===0?newProperty:newProperty + ","}} else{const index=lastSegment>parent.children.length?parent.children.length:lastSegment
746
+ const previous=parent.children[index-1]
747
+ edit={offset:previous.offset + previous.length,length:0,content:"," + newProperty}}
748
+ return withFormatting(text,edit,options)
749
+ } else{throw new Error(`Can not ${value === undefined ? "remove" : options.isArrayInsertion ? "insert" : "modify"} Array index ${insertIndex} as length is not sufficient`)
750
+ }} else{throw new Error(`Can not add ${typeof lastSegment !== "number" ? "index" : "property"} to parent of type ${parent.type}`)
751
+ }}
752
+ function withFormatting(text,edit,options){if(!options.formattingOptions){return [edit]
753
+ }
754
+ let newText=applyEdit(text,edit)
755
+ let begin=edit.offset
756
+ let end=edit.offset + edit.content.length
757
+ if(edit.length===0||edit.content.length===0){while(begin>0&&!isEOL(newText,begin-1)){begin--
758
+ }
759
+ while(end<newText.length&&!isEOL(newText,end)){end++
760
+ }}
761
+ const edits=format(newText,{offset:begin,length:end-begin},{...options.formattingOptions,keepLines:false})
762
+ for(let i=edits.length-1;i>=0; i--){const edit=edits[i]
763
+ newText=applyEdit(newText,edit)
764
+ begin=Math.min(begin,edit.offset)
765
+ end=Math.max(end,edit.offset + edit.length)
766
+ end += edit.content.length-edit.length
767
+ }
768
+ const editLength=text.length-(newText.length-end)-begin
769
+ return [{offset:begin,length:editLength,content:newText.substring(begin,end)}]
770
+ }
771
+ function applyEdit(text,edit){return text.substring(0,edit.offset) + edit.content + text.substring(edit.offset + edit.length)
772
+ }
773
+ var ScanError;
774
+ (function(ScanError){ScanError[ScanError["None"]=0]="None"
775
+ ScanError[ScanError["UnexpectedEndOfComment"]=1]="UnexpectedEndOfComment"
776
+ ScanError[ScanError["UnexpectedEndOfString"]=2]="UnexpectedEndOfString"
777
+ ScanError[ScanError["UnexpectedEndOfNumber"]=3]="UnexpectedEndOfNumber"
778
+ ScanError[ScanError["InvalidUnicode"]=4]="InvalidUnicode"
779
+ ScanError[ScanError["InvalidEscapeCharacter"]=5]="InvalidEscapeCharacter"
780
+ ScanError[ScanError["InvalidCharacter"]=6]="InvalidCharacter"
781
+ })(ScanError||(ScanError={}))
782
+ var SyntaxKind;
783
+ (function(SyntaxKind){SyntaxKind[SyntaxKind["OpenBraceToken"]=1]="OpenBraceToken"
784
+ SyntaxKind[SyntaxKind["CloseBraceToken"]=2]="CloseBraceToken"
785
+ SyntaxKind[SyntaxKind["OpenBracketToken"]=3]="OpenBracketToken"
786
+ SyntaxKind[SyntaxKind["CloseBracketToken"]=4]="CloseBracketToken"
787
+ SyntaxKind[SyntaxKind["CommaToken"]=5]="CommaToken"
788
+ SyntaxKind[SyntaxKind["ColonToken"]=6]="ColonToken"
789
+ SyntaxKind[SyntaxKind["NullKeyword"]=7]="NullKeyword"
790
+ SyntaxKind[SyntaxKind["TrueKeyword"]=8]="TrueKeyword"
791
+ SyntaxKind[SyntaxKind["FalseKeyword"]=9]="FalseKeyword"
792
+ SyntaxKind[SyntaxKind["StringLiteral"]=10]="StringLiteral"
793
+ SyntaxKind[SyntaxKind["NumericLiteral"]=11]="NumericLiteral"
794
+ SyntaxKind[SyntaxKind["LineCommentTrivia"]=12]="LineCommentTrivia"
795
+ SyntaxKind[SyntaxKind["BlockCommentTrivia"]=13]="BlockCommentTrivia"
796
+ SyntaxKind[SyntaxKind["LineBreakTrivia"]=14]="LineBreakTrivia"
797
+ SyntaxKind[SyntaxKind["Trivia"]=15]="Trivia"
798
+ SyntaxKind[SyntaxKind["Unknown"]=16]="Unknown"
799
+ SyntaxKind[SyntaxKind["EOF"]=17]="EOF"
800
+ })(SyntaxKind||(SyntaxKind={}))
801
+ var parseTree=parseTree2
802
+ var findNodeAtLocation=findNodeAtLocation2
803
+ var getNodeValue=getNodeValue2
804
+ var ParseErrorCode;
805
+ (function(ParseErrorCode){ParseErrorCode[ParseErrorCode["InvalidSymbol"]=1]="InvalidSymbol"
806
+ ParseErrorCode[ParseErrorCode["InvalidNumberFormat"]=2]="InvalidNumberFormat"
807
+ ParseErrorCode[ParseErrorCode["PropertyNameExpected"]=3]="PropertyNameExpected"
808
+ ParseErrorCode[ParseErrorCode["ValueExpected"]=4]="ValueExpected"
809
+ ParseErrorCode[ParseErrorCode["ColonExpected"]=5]="ColonExpected"
810
+ ParseErrorCode[ParseErrorCode["CommaExpected"]=6]="CommaExpected"
811
+ ParseErrorCode[ParseErrorCode["CloseBraceExpected"]=7]="CloseBraceExpected"
812
+ ParseErrorCode[ParseErrorCode["CloseBracketExpected"]=8]="CloseBracketExpected"
813
+ ParseErrorCode[ParseErrorCode["EndOfFileExpected"]=9]="EndOfFileExpected"
814
+ ParseErrorCode[ParseErrorCode["InvalidCommentToken"]=10]="InvalidCommentToken"
815
+ ParseErrorCode[ParseErrorCode["UnexpectedEndOfComment"]=11]="UnexpectedEndOfComment"
816
+ ParseErrorCode[ParseErrorCode["UnexpectedEndOfString"]=12]="UnexpectedEndOfString"
817
+ ParseErrorCode[ParseErrorCode["UnexpectedEndOfNumber"]=13]="UnexpectedEndOfNumber"
818
+ ParseErrorCode[ParseErrorCode["InvalidUnicode"]=14]="InvalidUnicode"
819
+ ParseErrorCode[ParseErrorCode["InvalidEscapeCharacter"]=15]="InvalidEscapeCharacter"
820
+ ParseErrorCode[ParseErrorCode["InvalidCharacter"]=16]="InvalidCharacter"
821
+ })(ParseErrorCode||(ParseErrorCode={}))
822
+ function modify(text,path,value,options){return setProperty(text,path,value,options)
823
+ }
824
+ function applyEdits(text,edits){let sortedEdits=edits.slice(0).sort((a,b)=>{const diff=a.offset-b.offset
825
+ if(diff===0){return a.length-b.length
826
+ }
827
+ return diff
828
+ })
829
+ let lastModifiedOffset=text.length
830
+ for(let i=sortedEdits.length-1;i>=0; i--){let e=sortedEdits[i]
831
+ if(e.offset + e.length<=lastModifiedOffset){text=applyEdit(text,e)
832
+ } else{throw new Error("Overlapping edit")
833
+ }
834
+ lastModifiedOffset=e.offset
835
+ }
836
+ return text
837
+ }
838
+ export{parseTree,findNodeAtLocation,getNodeValue,modify,applyEdits}