-
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathselectedTextHoverProvider.ts
More file actions
85 lines (74 loc) · 2.16 KB
/
selectedTextHoverProvider.ts
File metadata and controls
85 lines (74 loc) · 2.16 KB
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
import { d } from "@pretty-ts-errors/utils";
import { prettifyDiagnosticForHover } from "@pretty-ts-errors/vscode-formatter";
import {
ExtensionContext,
ExtensionMode,
MarkdownString,
languages,
window,
} from "vscode";
import { createConverter } from "vscode-languageclient/lib/common/codeConverter";
import { formattedDiagnosticsStore } from "../formattedDiagnosticsStore";
import { enabledCommands } from "../commands/enabledCommands";
/**
* Register an hover provider in debug only.
* It format selected text and help test things visually easier.
*/
export function registerSelectedTextHoverProvider(context: ExtensionContext) {
if (context.extensionMode !== ExtensionMode.Development) {
return;
}
const converter = createConverter();
context.subscriptions.push(
languages.registerHoverProvider(
{
language: "typescript",
pattern: "**/test/**/*.ts",
},
{
async provideHover(document, position) {
const editor = window.activeTextEditor;
if (!editor) {
return;
}
const range = document.getWordRangeAtPosition(position);
const message = editor ? document.getText(editor.selection) : "";
if (!range || !message) {
return null;
}
const lspDiagnostic = converter.asDiagnostic({
message,
range,
severity: 0,
source: "ts",
code: 1337,
});
const markdown = new MarkdownString(
debugHoverHeader + (await prettifyDiagnosticForHover(lspDiagnostic))
);
markdown.isTrusted = { enabledCommands };
markdown.supportHtml = true;
formattedDiagnosticsStore.set(document.uri.fsPath, [
{
range,
contents: [markdown],
lspDiagnostic,
},
]);
return {
contents: [markdown],
};
},
}
)
);
}
const debugHoverHeader = d /*html*/ `
<span style="color:#f96363;">
<span class="codicon codicon-debug"></span>
Formatted selected text (debug only)
</span>
<br>
<hr>
<p></p>
`;