|
| 1 | +import { existsSync, readFileSync } from 'fs'; |
| 2 | +import { join, resolve } from 'path'; |
| 3 | +import * as vscode from 'vscode'; |
| 4 | + |
| 5 | +export class CssModuleDefinitionProvider implements vscode.DefinitionProvider { |
| 6 | + async provideDefinition( |
| 7 | + document: vscode.TextDocument, |
| 8 | + position: vscode.Position, |
| 9 | + token: vscode.CancellationToken |
| 10 | + ): Promise<vscode.Definition | null> { |
| 11 | + const wordRange = document.getWordRangeAtPosition(position); |
| 12 | + if (!wordRange) return null; |
| 13 | + |
| 14 | + const word = document.getText(wordRange); |
| 15 | + const text = document.getText(); |
| 16 | + const lineText = document.lineAt(position.line).text; |
| 17 | + |
| 18 | + const regex = /(\w+)\.(\w+)/g; |
| 19 | + let match; |
| 20 | + const matches: { objectName: string; propertyName: string }[] = []; |
| 21 | + |
| 22 | + while ((match = regex.exec(lineText)) !== null) { |
| 23 | + matches.push({ objectName: match[1], propertyName: match[2] }); |
| 24 | + } |
| 25 | + |
| 26 | + const matchData = matches.find(m => m.propertyName === word); |
| 27 | + if (!matchData) return null; |
| 28 | + |
| 29 | + const { objectName, propertyName } = matchData; |
| 30 | + if (propertyName !== word) return null; |
| 31 | + |
| 32 | + const importRegex = new RegExp(`import\\s+${objectName}\\s+from\\s+['"](.*?)['"]`); |
| 33 | + const importMatch = text.match(importRegex); |
| 34 | + if (!importMatch) return null; |
| 35 | + |
| 36 | + const importPath = importMatch[1]; |
| 37 | + const resolvedPath = this.resolveImportPath(document, importPath); |
| 38 | + if (!resolvedPath) return null; |
| 39 | + |
| 40 | + const allowedExtensions = ['.module.css', '.module.scss']; |
| 41 | + if (!allowedExtensions.some(ext => resolvedPath.endsWith(ext))) return null; |
| 42 | + |
| 43 | + const fileContent = readFileSync(resolvedPath, 'utf-8'); |
| 44 | + const lines = fileContent.split('\n'); |
| 45 | + |
| 46 | + const definitions: vscode.Location[] = []; |
| 47 | + |
| 48 | + for (let i = 0; i < lines.length; i++) { |
| 49 | + let line = lines[i].trimStart(); |
| 50 | + if (line.match(new RegExp(`\\.${propertyName}(?![a-zA-Z0-9-_])`))) { |
| 51 | + const definitionUri = vscode.Uri.file(resolvedPath); |
| 52 | + const definitionPosition = new vscode.Position(i, 0); |
| 53 | + definitions.push(new vscode.Location(definitionUri, definitionPosition)); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return definitions; |
| 58 | + } |
| 59 | + |
| 60 | + private resolveImportPath(document: vscode.TextDocument, importPath: string): string | null { |
| 61 | + const workspacePath = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath; |
| 62 | + if (!workspacePath) return null; |
| 63 | + |
| 64 | + const tsConfigPath = join(workspacePath, 'tsconfig.json'); |
| 65 | + let aliasMap: Record<string, string> = {}; |
| 66 | + if (existsSync(tsConfigPath)) { |
| 67 | + const tsConfig = JSON.parse(readFileSync(tsConfigPath, 'utf-8')); |
| 68 | + if (tsConfig.compilerOptions?.paths) { |
| 69 | + aliasMap = this.parseTsConfigPaths(tsConfig.compilerOptions.paths, workspacePath); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + for (const alias in aliasMap) { |
| 74 | + if (importPath.startsWith(alias)) { |
| 75 | + return importPath.replace(alias, aliasMap[alias]); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (importPath.startsWith('.')) { |
| 80 | + const currentDir = join(document.uri.fsPath, '..'); |
| 81 | + return resolve(currentDir, importPath); |
| 82 | + } |
| 83 | + |
| 84 | + return join(workspacePath, importPath); |
| 85 | + } |
| 86 | + |
| 87 | + private parseTsConfigPaths( |
| 88 | + paths: Record<string, string[]>, |
| 89 | + workspacePath: string |
| 90 | + ): Record<string, string> { |
| 91 | + const aliasMap: Record<string, string> = {}; |
| 92 | + for (const alias in paths) { |
| 93 | + const targetPaths = paths[alias]; |
| 94 | + if (targetPaths.length > 0) { |
| 95 | + const cleanedAlias = alias.replace(/\*$/, ''); // "@/*" → "@/" |
| 96 | + const cleanedPath = targetPaths[0].replace(/\*$/, ''); // "./src/*" → "./src/" |
| 97 | + aliasMap[cleanedAlias] = join(workspacePath, cleanedPath); |
| 98 | + } |
| 99 | + } |
| 100 | + return aliasMap; |
| 101 | + } |
| 102 | +} |
0 commit comments