简体   繁体   English

“转到定义”和“转到参考”如何在 Python 的 vscode 中工作?

[英]How does "Go To Definition" and ”Go to References" works in vscode for Python?

The vscode will record all functions, classes, variables in a table? vscode 会把所有的函数、类、变量都记录在一张表里? Or How does vscode can find definitions and references?或者 vscode 如何找到定义和引用?

I want to know how VScode build this "Go To Definition" function?我想知道VScode如何构建这个“Go To Definition”function?

Go to Definition:转到定义:

If a language supports it, you can go to the definition of a symbol by pressing F12.如果一种语言支持它,您可以按 F12 转到符号的定义。

If you press Ctrl and hover over a symbol, a preview of the declaration will appear:如果您按下 Ctrl 并将鼠标悬停在一个符号上,则会出现声明的预览:

在此处输入图片说明

Tip: You can jump to the definition with Ctrl+Click or open the definition to the side with Ctrl+Alt+Click.提示:您可以使用 Ctrl+单击跳转到定义或使用 Ctrl+Alt+单击将定义打开到侧面。

Go to Type Definition:转到类型定义:

Some languages also support jumping to the type definition of a symbol by running the Go to Type Definition command from either the editor context menu or the Command Palette.某些语言还支持通过从编辑器上下文菜单或命令面板运行转到类型定义命令来跳转到符号的类型定义。 This will take you to the definition of the type of a symbol.这将带您到符号类型的定义。 The command editor.action.goToTypeDefinition is not bound to a keyboard shortcut by default but you can add your own custom keybinding.默认情况下,命令 editor.action.goToTypeDefinition 未绑定到键盘快捷键,但您可以添加自己的自定义键绑定。

the explanation is copy pasted from this page: this解释是从这个页面复制粘贴的: 这个

This is a very complicated process and there is a ton of research on software engineering related to this area.这是一个非常复杂的过程,并且有大量与该领域相关的软件工程研究。 Most IDEs/Extensions usually do a best-effort job, and that is good enough for various use cases.大多数 IDE/扩展通常会尽力而为,这对于各种用例来说已经足够了。 It also helps if one follows a widely used build system, and sticks to widely accepted code organization.如果遵循广泛使用的构建系统并坚持广泛接受的代码组织,这也会有所帮助。

Finding definition of a call is a complicated process and usually involve building AST (Abstract Syntax Tree) of the program and finding where a function is defined.查找调用的定义是一个复杂的过程,通常涉及构建程序的 AST(抽象语法树)并查找 function 的定义位置。 There are two important consideration:有两个重要的考虑:

  1. type system of the language语言的类型系统
  2. organization of code around the build system围绕构建系统组织代码

For (mostly) statically typed languages like C/C++ etc. finding the function declaration is not too difficult but finding definition usually involves finding where the source file (.c/.cpp) file is?对于(主要是)静态类型的语言,如 C/C++ 等。找到 function 声明并不太难,但查找定义通常涉及查找源文件 (.c/.cpp) 文件在哪里? Without the knowledge of build system (cmake, qmake) it is very difficult and hence most IDEs have hard time supporting it.如果没有构建系统(cmake、qmake)的知识,这是非常困难的,因此大多数 IDE 很难支持它。

For languages like rust, where it is customary to follow a rigid system of code organization, simple grepping with some type information is sufficient to get most of the function definitions.对于像 rust 这样的语言,习惯上遵循严格的代码组织系统,使用一些类型信息进行简单的 grepping 就足以获得大多数 function 定义。

For languages with dynamic type (Python, JavaScript), it is very hard to find the right definition and IDEs/Extensions usually support a well known coding style and module system.对于具有动态类型的语言(Python、JavaScript),很难找到正确的定义,并且 IDE/扩展通常支持众所周知的编码风格和模块系统。 TLDR: It helps to follow the crowd in this case. TLDR:在这种情况下,随波逐流是有帮助的。 In my experience, PyCharm does a very good job of finding definition of a function invocation.根据我的经验,PyCharm 在查找 function 调用的定义方面做得非常好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 VSCode 中“转到定义”到 super class 的方法 - How to "go to definition" to a method of super class in VSCode Vscode:由于工作空间大(python),无法定义 go - Vscode: can't go to definition due to large workspace (python) 如何在 vscode (python) 中执行 ctrl+click(go to definition) 时查看库中的源代码 - How to see the source code inside the library when doing ctrl+click(go to definition) in vscode (python) vscode python go 到符号不工作 - vscode python go to symbol not working 与在 vscode 中使用 Go 定义和查看定义命令相关 - related to using Go to Definition and Peek Definition commands in vscode VSCode:从JS url(view url)到Django视图的“转到定义” - VSCode: “Go to definition” from JS url (view url) to the Django view Visual Studio 代码“转到定义”仅适用于同一文件 Python - Visual Studio Code “Go To Definition” only works within the same file Python Go 到 Python 中的定义 Microsoft 扩展在模块中不起作用 - Go to Definition in Python Microsoft Extension is not working in modules 转到 VS Code 中本地 python 包的定义 - Go to definition for local python packages in VS Code VS 代码在“转到定义”工作时显示 pylint 导入错误 - VS code shows pylint import errors while 'go to definition' works
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM