forked from dev-five-git/react-component-lens
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodelens.ts
More file actions
222 lines (187 loc) · 5.43 KB
/
codelens.ts
File metadata and controls
222 lines (187 loc) · 5.43 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import * as vscode from 'vscode'
import type {
ComponentLensAnalyzer,
ComponentUsage,
ScopeConfig,
} from './analyzer'
import { createOpenSignature } from './resolver'
export interface CodeLensConfig {
clientComponent: boolean
enabled: boolean
globalEnabled: boolean
serverComponent: boolean
}
const CODELENS_SCOPE: ScopeConfig = {
declaration: true,
element: true,
export: true,
import: true,
type: true,
}
interface LineGroup {
clients: Set<string>
components: Map<string, string>
servers: Set<string>
}
export class ComponentCodeLensProvider
implements vscode.CodeLensProvider, vscode.Disposable
{
private readonly changeEmitter = new vscode.EventEmitter<void>()
public readonly onDidChangeCodeLenses = this.changeEmitter.event
private config: CodeLensConfig
public constructor(
private readonly analyzer: ComponentLensAnalyzer,
config: CodeLensConfig,
) {
this.config = config
}
public updateConfig(config: CodeLensConfig): void {
this.config = config
this.changeEmitter.fire()
}
public refresh(): void {
this.changeEmitter.fire()
}
public dispose(): void {
this.changeEmitter.dispose()
}
public async provideCodeLenses(
document: vscode.TextDocument,
): Promise<vscode.CodeLens[]> {
if (!this.config.globalEnabled || !this.config.enabled) {
return []
}
if (!this.config.clientComponent && !this.config.serverComponent) {
return []
}
const signature = createOpenSignature(document.version)
const usages = await this.analyzer.analyzeDocument(
document.fileName,
document.getText(),
signature,
CODELENS_SCOPE,
)
return this.buildCodeLenses(document, usages)
}
private async buildCodeLenses(
document: vscode.TextDocument,
usages: ComponentUsage[],
): Promise<vscode.CodeLens[]> {
const lineMap = new Map<number, LineGroup>()
for (let i = 0; i < usages.length; i++) {
const usage = usages[i]!
if (usage.kind === 'client' && !this.config.clientComponent) {
continue
}
if (usage.kind === 'server' && !this.config.serverComponent) {
continue
}
if (usage.ranges.length === 0) {
continue
}
const line = document.positionAt(usage.ranges[0]!.start).line
let entry = lineMap.get(line)
if (!entry) {
entry = {
clients: new Set(),
components: new Map(),
servers: new Set(),
}
lineMap.set(line, entry)
}
if (usage.kind === 'client') {
entry.clients.add(usage.tagName)
} else {
entry.servers.add(usage.tagName)
}
if (!entry.components.has(usage.tagName)) {
entry.components.set(usage.tagName, usage.sourceFilePath)
}
}
const positions = await this.resolveDeclarationPositions(lineMap)
const codeLenses: vscode.CodeLens[] = []
for (const [line, { clients, servers, components }] of lineMap) {
const parts: string[] = []
if (clients.size > 0) {
parts.push('Client Component')
}
if (servers.size > 0) {
parts.push('Server Component')
}
if (parts.length === 0) {
continue
}
const locations = this.buildLocations(components, positions)
const position = new vscode.Position(line, 0)
if (locations.length > 0) {
codeLenses.push(
new vscode.CodeLens(new vscode.Range(position, position), {
arguments: [document.uri, position, locations, 'peek'],
command: 'editor.action.peekLocations',
title: parts.join(' · '),
}),
)
} else {
codeLenses.push(
new vscode.CodeLens(new vscode.Range(position, position), {
command: '',
title: parts.join(' · '),
}),
)
}
}
return codeLenses
}
private async resolveDeclarationPositions(
lineMap: Map<number, LineGroup>,
): Promise<Map<string, vscode.Position>> {
const filesToResolve = new Map<string, Set<string>>()
for (const [, { components }] of lineMap) {
for (const [tagName, sourceFilePath] of components) {
let names = filesToResolve.get(sourceFilePath)
if (!names) {
names = new Set()
filesToResolve.set(sourceFilePath, names)
}
names.add(tagName)
}
}
const positions = new Map<string, vscode.Position>()
await Promise.all(
Array.from(filesToResolve, async ([filePath, names]) => {
for (const name of names) {
const pos = await this.analyzer.findComponentDeclaration(
filePath,
name,
)
if (pos) {
positions.set(
filePath + ':' + name,
new vscode.Position(pos.line, pos.character),
)
}
}
}),
)
return positions
}
private buildLocations(
components: Map<string, string>,
positions: Map<string, vscode.Position>,
): vscode.Location[] {
const seen = new Set<string>()
const locations: vscode.Location[] = []
for (const [tagName, sourceFilePath] of components) {
if (seen.has(sourceFilePath)) {
continue
}
seen.add(sourceFilePath)
const uri = vscode.Uri.file(sourceFilePath)
const pos =
positions.get(sourceFilePath + ':' + tagName) ??
new vscode.Position(0, 0)
locations.push(new vscode.Location(uri, pos))
}
return locations
}
}