pyfqname.py determines the fully qualified name for a given Python source file and line number. It resolves the enclosing scopes and reports key metadata:
- fqname: The fully qualified name, e.g.
module.Class.function. - class: The nearest enclosing class name (if any).
- function: The nearest enclosing function/method name (if any).
- location: Repository-relative file path and line number (e.g.
pkg/module.py#L10). - line: The source line text; if it contains backticks, it is rendered as a fenced code block for Markdown safety.
One-time setup to trigger the script with a keybinding:
- Open "Preferences: Open Keyboard Shortcuts (JSON)".
- Add a keybinding entry like:
{ "key": "cmd+alt+c", "command": "workbench.action.terminal.sendSequence", "args": { "text": "python3 -u pyfqname.py ${file} ${lineNumber} ${workspaceFolder}\n" }, "when": "editorTextFocus && editorLangId == python && remoteName =~ /ssh|wsl/" }, - Ensure
scripts/pyfqname.pyis located under thescriptsdirectory. - In Python file, press
cmd + alt + con a Python file to invoke the script.
pyfqname.py 用于在给定 Python 源文件与行号时,定位该行所处的类/函数作用域,并输出其模块的完全限定名(Fully Qualified Name,FQName)、类名、函数名、相对路径位置以及该行内容。
- Python 3.8+(仅依赖标准库
ast、os、sys)
按如下步骤配置快捷键来调用脚本:
1️⃣ 打开「Preferences: Open Keyboard Shortcuts (JSON)」并添加:
{
"key": "cmd+alt+c",
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "python3 -u pyfqname.py ${file} ${lineNumber} ${workspaceFolder}\n"
},
"when": "editorTextFocus && editorLangId == python && remoteName =~ /ssh|wsl/"
},2️⃣ 确保当前脚本位于 scripts 目录下(scripts/pyfqname.py)。
3️⃣ 聚焦到 Python 文件后,按 cmd + alt + c 触发脚本。
- fqname: `train.train_3d.Trainer.fit`
- class: `Trainer`
- function: `fit`
- location: `train/train_3d.py#L123`
- line: `loss = self.compute_loss(batch)`