-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathinlay_hint_service.ts
More file actions
178 lines (137 loc) · 4.48 KB
/
inlay_hint_service.ts
File metadata and controls
178 lines (137 loc) · 4.48 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { InlayHint, InlayHintKind } from "vscode-languageserver/node"
import { TextDocument } from "vscode-languageserver-textdocument"
import { Visitor, isHTMLOpenTagNode, isLiteralNode } from "@herb-tools/core"
import { ParserService } from "./parser_service"
import { lspPosition } from "./range_utils"
import type {
ERBEndNode,
ERBIfNode,
ERBUnlessNode,
ERBBlockNode,
ERBCaseNode,
ERBCaseMatchNode,
ERBWhileNode,
ERBUntilNode,
ERBForNode,
ERBBeginNode,
HTMLElementNode,
HTMLOpenTagNode,
HTMLAttributeNode,
Node,
} from "@herb-tools/core"
type ERBNodeWithEnd = ERBIfNode | ERBUnlessNode | ERBBlockNode | ERBCaseNode | ERBCaseMatchNode | ERBWhileNode | ERBUntilNode | ERBForNode | ERBBeginNode
function labelForERBNode(node: ERBNodeWithEnd): string | null {
const content = node.content?.value?.trim()
if (!content) return null
return `# ${content}`
}
function findAttributeValue(openTag: HTMLOpenTagNode, attributeName: string): string | null {
for (const child of openTag.children) {
if (child.type !== "AST_HTML_ATTRIBUTE_NODE") continue
const attrNode = child as unknown as HTMLAttributeNode
if (!attrNode.name || !attrNode.value) continue
const nameStr = attrNode.name.children
.filter(isLiteralNode)
.map(n => n.content)
.join("")
if (nameStr !== attributeName) continue
const valueStr = attrNode.value.children
.filter(isLiteralNode)
.map(n => n.content)
.join("")
return valueStr
}
return null
}
function labelForHTMLElement(node: HTMLElementNode): string | null {
if (!node.open_tag || !isHTMLOpenTagNode(node.open_tag)) return null
const id = findAttributeValue(node.open_tag, "id")
if (id) return `<!-- #${id} -->`
const className = findAttributeValue(node.open_tag, "class")
if (className) return `<!-- .${className.split(/\s+/).join(".")} -->`
return null
}
export class InlayHintCollector extends Visitor {
public hints: InlayHint[] = []
private addERBEndNodeHint(node: ERBNodeWithEnd): void {
const endNode: ERBEndNode | null = node.end_node
if (!endNode?.tag_closing) return
const label = labelForERBNode(node)
if (!label) return
const endLine = endNode.location.start.line
const nodeLine = node.location.start.line
if (endLine - nodeLine < 2) return
this.hints.push({
position: lspPosition(endNode.tag_closing.location.end),
label: ` ${label}`,
kind: InlayHintKind.Parameter,
paddingLeft: true,
})
}
visitHTMLElementNode(node: HTMLElementNode): void {
if (node.close_tag && node.open_tag) {
const endLine = node.close_tag.location.start.line
const startLine = node.open_tag.location.start.line
if (endLine - startLine >= 2) {
const label = labelForHTMLElement(node)
if (label) {
this.hints.push({
position: lspPosition(node.close_tag.location.end),
label: ` ${label}`,
kind: InlayHintKind.Parameter,
paddingLeft: true,
})
}
}
}
this.visitChildNodes(node)
}
visitERBIfNode(node: ERBIfNode): void {
this.addERBEndNodeHint(node)
this.visitChildNodes(node)
}
visitERBUnlessNode(node: ERBUnlessNode): void {
this.addERBEndNodeHint(node)
this.visitChildNodes(node)
}
visitERBBlockNode(node: ERBBlockNode): void {
this.addERBEndNodeHint(node)
this.visitChildNodes(node)
}
visitERBCaseNode(node: ERBCaseNode): void {
this.addERBEndNodeHint(node)
this.visitChildNodes(node)
}
visitERBCaseMatchNode(node: ERBCaseMatchNode): void {
this.addERBEndNodeHint(node)
this.visitChildNodes(node)
}
visitERBWhileNode(node: ERBWhileNode): void {
this.addERBEndNodeHint(node)
this.visitChildNodes(node)
}
visitERBUntilNode(node: ERBUntilNode): void {
this.addERBEndNodeHint(node)
this.visitChildNodes(node)
}
visitERBForNode(node: ERBForNode): void {
this.addERBEndNodeHint(node)
this.visitChildNodes(node)
}
visitERBBeginNode(node: ERBBeginNode): void {
this.addERBEndNodeHint(node)
this.visitChildNodes(node)
}
}
export class InlayHintService {
private parserService: ParserService
constructor(parserService: ParserService) {
this.parserService = parserService
}
getInlayHints(textDocument: TextDocument): InlayHint[] {
const parseResult = this.parserService.parseDocument(textDocument)
const collector = new InlayHintCollector()
collector.visit(parseResult.document)
return collector.hints
}
}