-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstatic.ts
More file actions
66 lines (64 loc) · 1.88 KB
/
Copy pathstatic.ts
File metadata and controls
66 lines (64 loc) · 1.88 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
import type { NodeQuery } from "@ec-ts/twoslash";
import {
type AnnotationRenderOptions,
ExpressiveCodeAnnotation,
type ExpressiveCodeLine,
} from "@expressive-code/core";
import { type Element, h } from "@expressive-code/core/hast";
import { getTextWidthInPixels } from "../helpers/index.ts";
import type { RenderJSDocs } from "../types.ts";
/**
* Represents a static annotation for Twoslash.
* Extends the ExpressiveCodeAnnotation class.
*/
export class TwoslashStaticAnnotation extends ExpressiveCodeAnnotation {
readonly name = "twoslash-static-annotation";
/**
* Creates an instance of TwoslashStaticAnnotation.
*
* @param hover - The hover information for the node.
* @param line - The line of code associated with the annotation.
* @param includeJsDoc - A flag indicating whether to include JSDoc comments.
* @param query - The query information for the node.
*/
constructor(
readonly query: NodeQuery,
readonly line: ExpressiveCodeLine,
readonly codeType: Element,
readonly renderedDocs: RenderJSDocs,
) {
super({
inlineRange: {
columnStart: line.text.length,
columnEnd: line.text.length + query.length,
},
});
}
/**
* Renders the static annotation.
* @param nodesToTransform - The nodes to transform with the error box annotation.
* @returns An array of transformed nodes with the error box annotation.
*/
render({ nodesToTransform }: AnnotationRenderOptions) {
return nodesToTransform.map((node) => {
return h("span.twoslash-noline", [
node,
h(
"div.twoslash-static",
{
style: {
"margin-left": `${getTextWidthInPixels(this.query.character)}px`,
},
},
[
h("div.twoslash-static-container.not-content", [
h("code.twoslash-popup-code", [h("span.twoslash-popup-code-type", this.codeType)]),
this.renderedDocs.docs,
this.renderedDocs.tags,
]),
],
),
]);
});
}
}