Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(format): add fast path using json stringify/parse #83

Closed
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
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