Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
}
let firstToken = scanNext();
if (firstToken !== Json.SyntaxKind.EOF) {
let firstTokenStart = scanner.getTokenOffset() + rangeOffset;
let initialIndent = repeat(indentValue, initialIndentLevel);
addEdit(initialIndent, rangeOffset, firstTokenStart);
}
while (firstToken !== Json.SyntaxKind.EOF) {
let firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + rangeOffset;
let secondToken = scanNext();
let replaceContent = '';
while (!lineBreak && (secondToken === Json.SyntaxKind.LineCommentTrivia || secondToken === Json.SyntaxKind.BlockCommentTrivia)) {
// comments on the same line: keep them on the same line, but ignore them otherwise
let commentTokenStart = scanner.getTokenOffset() + rangeOffset;
addEdit(' ', firstTokenEnd, commentTokenStart);
firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + rangeOffset;
replaceContent = secondToken === Json.SyntaxKind.LineCommentTrivia ? newLineAndIndent() : '';
secondToken = scanNext();
}
if (secondToken === Json.SyntaxKind.CloseBraceToken) {
if (firstToken !== Json.SyntaxKind.OpenBraceToken) {
indentLevel--;
replaceContent = newLineAndIndent();
}
} else if (secondToken === Json.SyntaxKind.CloseBracketToken) {
if (firstToken !== Json.SyntaxKind.OpenBracketToken) {
indentLevel--;
function scanNext(): Json.SyntaxKind {
let token = scanner.scan();
lineBreak = false;
while (token === Json.SyntaxKind.Trivia || token === Json.SyntaxKind.LineBreakTrivia) {
lineBreak = lineBreak || (token === Json.SyntaxKind.LineBreakTrivia);
token = scanner.scan();
}
return token;
}
let editOperations: { range: TextBuffer.Range, text: string }[] = [];
export const findItemAtOffset = (
node: ASTNode,
document: TextDocument,
offset: number
) => {
const scanner = JSONParser.createScanner(document.getText(), true)
const children = node.getChildNodes()
for (let i = children.length - 1; i >= 0; i--) {
const child = children[i]
if (offset > child.end) {
scanner.setPosition(child.end)
const token = scanner.scan()
if (
token === JSONParser.SyntaxKind.CommaToken &&
offset >= scanner.getTokenOffset() + scanner.getTokenLength()
) {
return i + 1
}
return i
} else if (offset >= child.start) {
return i
}
}
return 0
}
document: TextDocument,
start: number,
offset: number
) => {
const scanner = JSONParser.createScanner(document.getText(), false)
scanner.setPosition(start)
let token = scanner.scan()
while (
token !== JSONParser.SyntaxKind.EOF &&
scanner.getTokenOffset() + scanner.getTokenLength() < offset
) {
token = scanner.scan()
}
return (
(token === JSONParser.SyntaxKind.LineCommentTrivia ||
token === JSONParser.SyntaxKind.BlockCommentTrivia) &&
scanner.getTokenOffset() <= offset
)
}
export const evaluateSeparatorAfter = (
document: TextDocument,
offset: number
) => {
const scanner = JSONParser.createScanner(document.getText(), true)
scanner.setPosition(offset)
const token = scanner.scan()
switch (token) {
case JSONParser.SyntaxKind.CommaToken:
case JSONParser.SyntaxKind.CloseBraceToken:
case JSONParser.SyntaxKind.CloseBracketToken:
case JSONParser.SyntaxKind.EOF:
return ""
default:
return ""
}
}