-
Notifications
You must be signed in to change notification settings - Fork 1
Add CodeLens annotations for Server/Client Components #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yongsk0066
wants to merge
2
commits into
dev-five-git:main
Choose a base branch
from
yongsk0066:feature/codelens
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
좋은 기능인 만큼 기본 값을 true로 하면 어떨까 합니다.