Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
perf(format): add fast path using json stringify/parse
  • Loading branch information
H4ad committed Jan 13, 2024
commit 1107adaffdb4dc158570a7d963fe7a0c8f13a482
18 changes: 18 additions & 0 deletions src/impl/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ export function format(documentText: string, range: Range | undefined, options:
}
const eol = getEOL(options, documentText);

if (eol === '\n' && options.insertSpaces && !options.keepLines && rangeStart === 0 && rangeEnd === documentText.length) {
try {
let formatted = JSON.stringify(JSON.parse(documentText), null, options?.tabSize || 4);

if (options.insertFinalNewline) {
formatted += '\n';
}

return [{
offset: 0,
length: documentText.length,
content: formatted
}];
} catch (e) {
// ignore because there is an error on JSON and we will try format until we reach that error.
}
}

let numberLineBreaks = 0;

let indentLevel = 0;
Expand Down