Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let ranges: FoldingRange[] = [];
let nestingLevels: number[] = [];
let stack: FoldingRange[] = [];
let prevStart = -1;
let scanner = createScanner(document.getText(), false);
let token = scanner.scan();
function addRange(range: FoldingRange) {
ranges.push(range);
nestingLevels.push(stack.length);
}
while (token !== SyntaxKind.EOF) {
switch (token) {
case SyntaxKind.OpenBraceToken:
case SyntaxKind.OpenBracketToken: {
let startLine = document.positionAt(scanner.getTokenOffset()).line;
let range = { startLine, endLine: startLine, kind: token === SyntaxKind.OpenBraceToken ? 'object' : 'array' };
stack.push(range);
break;
}
case SyntaxKind.CloseBraceToken:
case SyntaxKind.CloseBracketToken: {
let kind = token === SyntaxKind.CloseBraceToken ? 'object' : 'array';
if (stack.length > 0 && stack[stack.length - 1].kind === kind) {
let range = stack.pop();
let line = document.positionAt(scanner.getTokenOffset()).line;
if (range && line > range.startLine + 1 && prevStart !== range.startLine) {
range.endLine = line - 1;
addRange(range);
prevStart = range.startLine;
}
let secondToken: SyntaxKind;
let secondTokenValue: string;
let result = '';
while (firstToken !== SyntaxKind.EOF) {
[secondToken, secondTokenValue] = scanNext();
switch (firstToken) {
case SyntaxKind.OpenBraceToken:
result += ResponseFormatUtility.jsonSpecialTokenMapping[firstToken];
if (secondToken !== SyntaxKind.CloseBraceToken) {
indentLevel++;
result += newLineAndIndent();
}
break;
case SyntaxKind.OpenBracketToken:
result += ResponseFormatUtility.jsonSpecialTokenMapping[firstToken];
if (secondToken !== SyntaxKind.CloseBracketToken) {
indentLevel++;
result += newLineAndIndent();
}
break;
case SyntaxKind.CloseBraceToken:
case SyntaxKind.CloseBracketToken:
case SyntaxKind.NullKeyword:
case SyntaxKind.TrueKeyword:
case SyntaxKind.FalseKeyword:
result += ResponseFormatUtility.jsonSpecialTokenMapping[firstToken];
if (secondToken === SyntaxKind.CloseBraceToken
|| secondToken === SyntaxKind.CloseBracketToken) {
indentLevel--;
result += newLineAndIndent();