-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
extension.ts
96 lines (81 loc) · 2.99 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import {
ExtensionContext,
languages,
MarkdownString,
Range,
window,
} from "vscode";
import { createConverter } from "vscode-languageclient/lib/common/codeConverter";
import { formatDiagnostic } from "./format/formatDiagnostic";
import { prettify } from "./format/prettify";
import { hoverProvider } from "./provider/hoverProvider";
import { registerSelectedTextHoverProvider } from "./provider/selectedTextHoverProvider";
import { uriStore } from "./provider/uriStore";
import { has } from "./utils";
const cache = new Map();
export function activate(context: ExtensionContext) {
const registeredLanguages = new Set<string>();
const converter = createConverter();
registerSelectedTextHoverProvider(context);
context.subscriptions.push(
languages.onDidChangeDiagnostics(async (e) => {
e.uris.forEach((uri) => {
const diagnostics = languages.getDiagnostics(uri);
const items: {
range: Range;
contents: MarkdownString[];
}[] = [];
let hasTsDiagnostic = false;
diagnostics
.filter((diagnostic) =>
diagnostic.source
? has(
["ts", "ts-plugin", "deno-ts", "js", "glint"],
diagnostic.source
)
: false
)
.forEach(async (diagnostic) => {
// formatDiagnostic converts message based on LSP Diagnostic type, not VSCode Diagnostic type, so it can be used in other IDEs.
// Here we convert VSCode Diagnostic to LSP Diagnostic to make formatDiagnostic recognize it.
let formattedMessage = cache.get(diagnostic.message);
if (!formattedMessage) {
const markdownString = new MarkdownString(
formatDiagnostic(converter.asDiagnostic(diagnostic), prettify)
);
markdownString.isTrusted = true;
markdownString.supportHtml = true;
formattedMessage = markdownString;
cache.set(diagnostic.message, formattedMessage);
if (cache.size > 100) {
const firstCacheKey = cache.keys().next().value;
cache.delete(firstCacheKey);
}
}
items.push({
range: diagnostic.range,
contents: [formattedMessage],
});
hasTsDiagnostic = true;
});
uriStore[uri.fsPath] = items;
if (hasTsDiagnostic) {
const editor = window.visibleTextEditors.find(
(editor) => editor.document.uri.toString() === uri.toString()
);
if (editor && !registeredLanguages.has(editor.document.languageId)) {
registeredLanguages.add(editor.document.languageId);
context.subscriptions.push(
languages.registerHoverProvider(
{
language: editor.document.languageId,
},
hoverProvider
)
);
}
}
});
})
);
}