|
| 1 | +import * as vscode from 'vscode' |
| 2 | + |
| 3 | +import type { |
| 4 | + ComponentLensAnalyzer, |
| 5 | + ComponentUsage, |
| 6 | + ScopeConfig, |
| 7 | +} from './analyzer' |
| 8 | +import { createOpenSignature } from './resolver' |
| 9 | + |
| 10 | +export interface CodeLensConfig { |
| 11 | + clientComponent: boolean |
| 12 | + enabled: boolean |
| 13 | + globalEnabled: boolean |
| 14 | + serverComponent: boolean |
| 15 | +} |
| 16 | + |
| 17 | +const CODELENS_SCOPE: ScopeConfig = { |
| 18 | + declaration: true, |
| 19 | + element: true, |
| 20 | + export: true, |
| 21 | + import: true, |
| 22 | + type: true, |
| 23 | +} |
| 24 | + |
| 25 | +interface LineGroup { |
| 26 | + clients: Set<string> |
| 27 | + components: Map<string, string> |
| 28 | + servers: Set<string> |
| 29 | +} |
| 30 | + |
| 31 | +export class ComponentCodeLensProvider |
| 32 | + implements vscode.CodeLensProvider, vscode.Disposable |
| 33 | +{ |
| 34 | + private readonly changeEmitter = new vscode.EventEmitter<void>() |
| 35 | + public readonly onDidChangeCodeLenses = this.changeEmitter.event |
| 36 | + |
| 37 | + private config: CodeLensConfig |
| 38 | + |
| 39 | + public constructor( |
| 40 | + private readonly analyzer: ComponentLensAnalyzer, |
| 41 | + config: CodeLensConfig, |
| 42 | + ) { |
| 43 | + this.config = config |
| 44 | + } |
| 45 | + |
| 46 | + public updateConfig(config: CodeLensConfig): void { |
| 47 | + this.config = config |
| 48 | + this.changeEmitter.fire() |
| 49 | + } |
| 50 | + |
| 51 | + public refresh(): void { |
| 52 | + this.changeEmitter.fire() |
| 53 | + } |
| 54 | + |
| 55 | + public dispose(): void { |
| 56 | + this.changeEmitter.dispose() |
| 57 | + } |
| 58 | + |
| 59 | + public async provideCodeLenses( |
| 60 | + document: vscode.TextDocument, |
| 61 | + ): Promise<vscode.CodeLens[]> { |
| 62 | + if (!this.config.globalEnabled || !this.config.enabled) { |
| 63 | + return [] |
| 64 | + } |
| 65 | + |
| 66 | + if (!this.config.clientComponent && !this.config.serverComponent) { |
| 67 | + return [] |
| 68 | + } |
| 69 | + |
| 70 | + const signature = createOpenSignature(document.version) |
| 71 | + const usages = await this.analyzer.analyzeDocument( |
| 72 | + document.fileName, |
| 73 | + document.getText(), |
| 74 | + signature, |
| 75 | + CODELENS_SCOPE, |
| 76 | + ) |
| 77 | + |
| 78 | + return this.buildCodeLenses(document, usages) |
| 79 | + } |
| 80 | + |
| 81 | + private async buildCodeLenses( |
| 82 | + document: vscode.TextDocument, |
| 83 | + usages: ComponentUsage[], |
| 84 | + ): Promise<vscode.CodeLens[]> { |
| 85 | + const lineMap = new Map<number, LineGroup>() |
| 86 | + |
| 87 | + for (let i = 0; i < usages.length; i++) { |
| 88 | + const usage = usages[i]! |
| 89 | + if (usage.kind === 'client' && !this.config.clientComponent) { |
| 90 | + continue |
| 91 | + } |
| 92 | + if (usage.kind === 'server' && !this.config.serverComponent) { |
| 93 | + continue |
| 94 | + } |
| 95 | + |
| 96 | + if (usage.ranges.length === 0) { |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + const line = document.positionAt(usage.ranges[0]!.start).line |
| 101 | + let entry = lineMap.get(line) |
| 102 | + if (!entry) { |
| 103 | + entry = { |
| 104 | + clients: new Set(), |
| 105 | + components: new Map(), |
| 106 | + servers: new Set(), |
| 107 | + } |
| 108 | + lineMap.set(line, entry) |
| 109 | + } |
| 110 | + |
| 111 | + if (usage.kind === 'client') { |
| 112 | + entry.clients.add(usage.tagName) |
| 113 | + } else { |
| 114 | + entry.servers.add(usage.tagName) |
| 115 | + } |
| 116 | + |
| 117 | + if (!entry.components.has(usage.tagName)) { |
| 118 | + entry.components.set(usage.tagName, usage.sourceFilePath) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + const positions = await this.resolveDeclarationPositions(lineMap) |
| 123 | + const codeLenses: vscode.CodeLens[] = [] |
| 124 | + |
| 125 | + for (const [line, { clients, servers, components }] of lineMap) { |
| 126 | + const parts: string[] = [] |
| 127 | + if (clients.size > 0) { |
| 128 | + parts.push('Client Component') |
| 129 | + } |
| 130 | + if (servers.size > 0) { |
| 131 | + parts.push('Server Component') |
| 132 | + } |
| 133 | + |
| 134 | + if (parts.length === 0) { |
| 135 | + continue |
| 136 | + } |
| 137 | + |
| 138 | + const locations = this.buildLocations(components, positions) |
| 139 | + const position = new vscode.Position(line, 0) |
| 140 | + |
| 141 | + if (locations.length > 0) { |
| 142 | + codeLenses.push( |
| 143 | + new vscode.CodeLens(new vscode.Range(position, position), { |
| 144 | + arguments: [document.uri, position, locations, 'peek'], |
| 145 | + command: 'editor.action.peekLocations', |
| 146 | + title: parts.join(' · '), |
| 147 | + }), |
| 148 | + ) |
| 149 | + } else { |
| 150 | + codeLenses.push( |
| 151 | + new vscode.CodeLens(new vscode.Range(position, position), { |
| 152 | + command: '', |
| 153 | + title: parts.join(' · '), |
| 154 | + }), |
| 155 | + ) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return codeLenses |
| 160 | + } |
| 161 | + |
| 162 | + private async resolveDeclarationPositions( |
| 163 | + lineMap: Map<number, LineGroup>, |
| 164 | + ): Promise<Map<string, vscode.Position>> { |
| 165 | + const filesToResolve = new Map<string, Set<string>>() |
| 166 | + |
| 167 | + for (const [, { components }] of lineMap) { |
| 168 | + for (const [tagName, sourceFilePath] of components) { |
| 169 | + let names = filesToResolve.get(sourceFilePath) |
| 170 | + if (!names) { |
| 171 | + names = new Set() |
| 172 | + filesToResolve.set(sourceFilePath, names) |
| 173 | + } |
| 174 | + names.add(tagName) |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + const positions = new Map<string, vscode.Position>() |
| 179 | + |
| 180 | + await Promise.all( |
| 181 | + Array.from(filesToResolve, async ([filePath, names]) => { |
| 182 | + for (const name of names) { |
| 183 | + const pos = await this.analyzer.findComponentDeclaration( |
| 184 | + filePath, |
| 185 | + name, |
| 186 | + ) |
| 187 | + if (pos) { |
| 188 | + positions.set( |
| 189 | + filePath + ':' + name, |
| 190 | + new vscode.Position(pos.line, pos.character), |
| 191 | + ) |
| 192 | + } |
| 193 | + } |
| 194 | + }), |
| 195 | + ) |
| 196 | + |
| 197 | + return positions |
| 198 | + } |
| 199 | + |
| 200 | + private buildLocations( |
| 201 | + components: Map<string, string>, |
| 202 | + positions: Map<string, vscode.Position>, |
| 203 | + ): vscode.Location[] { |
| 204 | + const seen = new Set<string>() |
| 205 | + const locations: vscode.Location[] = [] |
| 206 | + |
| 207 | + for (const [tagName, sourceFilePath] of components) { |
| 208 | + if (seen.has(sourceFilePath)) { |
| 209 | + continue |
| 210 | + } |
| 211 | + seen.add(sourceFilePath) |
| 212 | + |
| 213 | + const uri = vscode.Uri.file(sourceFilePath) |
| 214 | + const pos = |
| 215 | + positions.get(sourceFilePath + ':' + tagName) ?? |
| 216 | + new vscode.Position(0, 0) |
| 217 | + locations.push(new vscode.Location(uri, pos)) |
| 218 | + } |
| 219 | + |
| 220 | + return locations |
| 221 | + } |
| 222 | +} |
0 commit comments