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