|
| 1 | +import * as path from 'path'; |
| 2 | +import * as vscode from 'vscode'; |
| 3 | +import { inject, injectable } from 'inversify'; |
| 4 | + |
| 5 | +import { ICommandManager, IWorkspaceService } from '../../common/application/types'; |
| 6 | +import { IExtensionSingleActivationService } from '../../activation/types'; |
| 7 | +import { Commands } from '../../common/constants'; |
| 8 | +import { getSysPath } from '../../common/utils/pythonUtils'; |
| 9 | + |
| 10 | +@injectable() |
| 11 | +export class CopyImportPathCommand implements IExtensionSingleActivationService { |
| 12 | + public readonly supportedWorkspaceTypes = { untrustedWorkspace: true, virtualWorkspace: true }; |
| 13 | + |
| 14 | + constructor( |
| 15 | + @inject(ICommandManager) private readonly commands: ICommandManager, |
| 16 | + @inject(IWorkspaceService) private readonly workspace: IWorkspaceService, |
| 17 | + ) {} |
| 18 | + |
| 19 | + async activate(): Promise<void> { |
| 20 | + this.commands.registerCommand(Commands.CopyImportPath, this.execute, this); |
| 21 | + } |
| 22 | + |
| 23 | + private async execute(fileUri?: vscode.Uri): Promise<void> { |
| 24 | + const uri = fileUri ?? vscode.window.activeTextEditor?.document.uri; |
| 25 | + if (!uri || uri.scheme !== 'file' || !uri.fsPath.endsWith('.py')) { |
| 26 | + void vscode.window.showWarningMessage('No Python file selected for import-path copy.'); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + const importPath = this.resolveImportPath(uri.fsPath); |
| 31 | + await vscode.env.clipboard.writeText(importPath); |
| 32 | + void vscode.window.showInformationMessage(`Copied: ${importPath}`); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Resolves a Python import-style dotted path from an absolute file path. |
| 37 | + * |
| 38 | + * The resolution follows a 3-level fallback strategy: |
| 39 | + * |
| 40 | + * 1. If the file is located under any entry in `sys.path`, the path relative to that entry is used. |
| 41 | + * 2. If the file is located under the current workspace folder, the path relative to the workspace root is used. |
| 42 | + * 3. Otherwise, the import path falls back to the file name (without extension). |
| 43 | + * |
| 44 | + * @param absPath - The absolute path to a `.py` file. |
| 45 | + * @returns The resolved import path in dotted notation (e.g., 'pkg.module'). |
| 46 | + */ |
| 47 | + private resolveImportPath(absPath: string): string { |
| 48 | + // ---------- ① sys.path ---------- |
| 49 | + for (const sysRoot of getSysPath()) { |
| 50 | + if (sysRoot && absPath.startsWith(sysRoot)) { |
| 51 | + return CopyImportPathCommand.toDotted(path.relative(sysRoot, absPath)); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // ---------- ② workspaceFolder ---------- |
| 56 | + const ws = this.workspace.getWorkspaceFolder(vscode.Uri.file(absPath)); |
| 57 | + if (ws && absPath.startsWith(ws.uri.fsPath)) { |
| 58 | + return CopyImportPathCommand.toDotted(path.relative(ws.uri.fsPath, absPath)); |
| 59 | + } |
| 60 | + |
| 61 | + // ---------- ③ fallback ---------- |
| 62 | + return path.basename(absPath, '.py'); |
| 63 | + } |
| 64 | + |
| 65 | + private static toDotted(relPath: string): string { |
| 66 | + return relPath.replace(/\.py$/i, '').split(path.sep).filter(Boolean).join('.'); |
| 67 | + } |
| 68 | +} |
0 commit comments