Skip to content

Commit 153e11c

Browse files
committed
split reduce back into filter, map. increase cache size. define constant
1 parent 6de49a7 commit 153e11c

File tree

1 file changed

+26
-36
lines changed

1 file changed

+26
-36
lines changed

src/extension.ts

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import { has } from "./utils";
1616
import * as logger from "./logger";
1717

1818
const cache = new Map<string, MarkdownString>();
19-
const CACHE_SIZE_MAX = 100;
19+
const CACHE_SIZE_MAX = 1000;
20+
const supportedDiagnosticSources = ["ts", "ts-plugin", "deno-ts", "js", "glint"];
2021

2122
export function activate(context: ExtensionContext) {
2223
logger.info('activating pretty-ts-errors');
@@ -31,43 +32,31 @@ export function activate(context: ExtensionContext) {
3132
e.uris.forEach((uri) => {
3233
logger.measure(`uri: '${uri.toString()}'`, () => {
3334
const diagnostics = languages.getDiagnostics(uri);
34-
35-
// use a reduce to prevent multiple iterations over the collection
36-
const items = diagnostics
37-
.reduce((items, diagnostic) => {
38-
if (!diagnostic.source || !has(
39-
["ts", "ts-plugin", "deno-ts", "js", "glint"],
40-
diagnostic.source
41-
)) {
42-
return items;
35+
const supportedDiagnostics = diagnostics.filter(diagnostic => diagnostic.source && has(supportedDiagnosticSources, diagnostic.source))
36+
37+
const items = supportedDiagnostics.map(diagnostic => {
38+
let formattedMessage = cache.get(diagnostic.message);
39+
if (!formattedMessage) {
40+
// formatDiagnostic converts message based on LSP Diagnostic type, not VSCode Diagnostic type, so it can be used in other IDEs.
41+
// Here we convert VSCode Diagnostic to LSP Diagnostic to make formatDiagnostic recognize it.
42+
formattedMessage = new MarkdownString(
43+
formatDiagnostic(converter.asDiagnostic(diagnostic), prettify)
44+
);
45+
formattedMessage.isTrusted = true;
46+
formattedMessage.supportHtml = true;
47+
48+
if (cache.size > CACHE_SIZE_MAX) {
49+
const firstCacheKey = cache.keys().next().value!;
50+
cache.delete(firstCacheKey);
4351
}
52+
cache.set(diagnostic.message, formattedMessage);
53+
}
4454

45-
let formattedMessage = cache.get(diagnostic.message);
46-
if (!formattedMessage) {
47-
// formatDiagnostic converts message based on LSP Diagnostic type, not VSCode Diagnostic type, so it can be used in other IDEs.
48-
// Here we convert VSCode Diagnostic to LSP Diagnostic to make formatDiagnostic recognize it.
49-
formattedMessage = new MarkdownString(
50-
formatDiagnostic(converter.asDiagnostic(diagnostic), prettify)
51-
);
52-
formattedMessage.isTrusted = true;
53-
formattedMessage.supportHtml = true;
54-
55-
if (cache.size > CACHE_SIZE_MAX) {
56-
const firstCacheKey = cache.keys().next().value!;
57-
cache.delete(firstCacheKey);
58-
}
59-
60-
cache.set(diagnostic.message, formattedMessage);
61-
}
62-
63-
items.push({
64-
range: diagnostic.range,
65-
contents: [formattedMessage],
66-
});
67-
68-
return items;
69-
}, [] as Hover[]);
70-
55+
return {
56+
range: diagnostic.range,
57+
contents: [formattedMessage],
58+
};
59+
});
7160

7261
if (items.length > 0) {
7362
uriStore.set(uri.fsPath, items);
@@ -103,4 +92,5 @@ export function deactivate() {
10392
cache.clear();
10493
logger.trace('clearing uriStore')
10594
uriStore.clear();
95+
logger.dispose();
10696
}

0 commit comments

Comments
 (0)