visual-studio 智能感知如何在 Visual Studio 中工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/835682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:27:26  来源:igfitidea点击:

How does intellisense work in Visual Studio?

visual-studiointellisense

提问by Dervin Thunk

I hope this is a valid question: how does intellisense work in VS2008? I'm after what is known about the algorithm it uses to find the suggestions, when exactly it pops up (the "." is just one obvious trigger), how its behavior can be modified if at all possible, etc.

我希望这是一个有效的问题:智能感知在 VS2008 中是如何工作的?我正在了解它用于查找建议的算法,它何时弹出(“。”只是一个明显的触发器),如果可能的话,如何修改其行为等。

To put this question into context: The main issue I'm trying to resolve is how to activate and deactivate intellisense in portions of the editor screen and how to modify where it searches to populate the suggestion box.

把这个问题放在上下文中:我试图解决的主要问题是如何在编辑器屏幕的某些部分激活和停用智能感知,以及如何修改它搜索的位置以填充建议框。

All information is welcome.

欢迎提供所有信息。

回答by Galwegian

Take a look at this DIY Intellisense articleon CodeProject.

看看这篇关于 CodeProject 的DIY Intellisense 文章

回答by Charlie Martin

It's more fun to reverse-engineer it, though. Let's consider the problem:

不过,对其进行逆向工程更有趣。让我们考虑这个问题:

  • you need to identify the words of interest
  • you need to find the options possible
  • you need to present them
  • 你需要确定感兴趣的词
  • 你需要找到可能的选项
  • 你需要展示它们

Now, the first step means you have to parse the code. You've got the C/C** keywords, you pre-parse the various function and class declarations, and load them into some kind of data structure. Then you parse the code and store the class, variable, etc names and put them in the same data structure.

现在,第一步意味着您必须解析代码。您已经获得了 C/C** 关键字,您预先解析了各种函数和类声明,并将它们加载到某种数据结构中。然后解析代码并存储类、变量等名称,并将它们放在相同的数据结构中。

The second step means you want a data structure which efficiently can search for a partial word and get all the words that have that prefix. You can dothat with regular expressions, but that's not very efficient. An efficient data structure for that kind of search is a trie, which is discussed here on SO.

第二步意味着您需要一个数据结构,它可以有效地搜索部分单词并获取具有该前缀的所有单词。你可以用正则表达式做到这一点,但这不是很有效。这种搜索的有效数据结构是特里树,这里讨论了 SO

Once you have the list of possibilities, you just present it. You probably want to keep a reference to the root of the tree of possibilities so you can search them out in real time as someone types more letters.

一旦你有了可能性列表,你就可以展示它。您可能希望保留对可能性树的根的引用,以便在有人键入更多字母时实时搜索它们。

回答by John Saunders

I haven't seen any text editor in VS that limits where IntelliSense shows up. It's all language specific. If your cursor is located at a point where IntelliSense might contribute to a valid token, that's when it will be used.

我还没有在 VS 中看到任何限制 IntelliSense 显示位置的文本编辑器。这都是特定于语言的。如果您的光标位于 IntelliSense 可能有助于有效令牌的点,那么它将被使用。

I believe there is some interaction with the project system being used, but that's as far as I know. I also believe there is a sample project system in the Visual Studio SDK, and that might give you an idea.

我相信与正在使用的项目系统有一些交互,但据我所知。我也相信 Visual Studio SDK 中有一个示例项目系统,这可能会给您一个想法。

回答by majkinetor

For such cases I sometimes use my own version of InteliSense that I developed for AutoHotKeywhen I want specific behavior. The point of this script is that it can be used with any editor, or basically any control accepting text. It works by recording text input and interpreting it upon syntax file.

对于这种情况,我有时会在需要特定行为时使用我为AutoHotKey开发的自己的 InteliSense 版本。这个脚本的重点是它可以与任何编辑器一起使用,或者基本上任何接受文本的控件。它通过记录文本输入并在语法文件上解释它来工作。

You can perhaps use it as a base for the thing you want to achieve. I used ISense succesifully with several languages that don't have such thing, like Csoundor even batch scripts. It will be possible to extend it to support C# using input monitoring in combination with Reflection.

您或许可以将其用作您想要实现的目标的基础。我成功地将 ISense 与几种没有这种东西的语言一起使用,比如Csound甚至批处理脚本。可以使用输入监视与反射相结合来扩展它以支持 C#。

Anyway, with AHK you can even control VS intelissense by "taking" the list of items it presents and filter it, or similar things. You may have some small problems with process boundaries but nothing that cant be fixed.

无论如何,使用 AHK,您甚至可以通过“获取”它显示的项目列表并对其进行过滤或类似的事情来控制 VS intelissense。您可能在流程边界方面遇到一些小问题,但没有什么是无法修复的。

The intellisense ius generally, AFAIK, implemented using different methods. I read that Delphi is so fast that it implements isense by recompiling the project on each token and thats the reason C++ Builder didn't have isense as its compiling very slow.

智能感知 ius 通常是 AFAIK,使用不同的方法实现。我读到 Delphi 非常快,它通过在每个令牌上重新编译项目来实现 isense,这就是 C++ Builder 没有 isense 的原因,因为它的编译速度非常慢。

回答by Serapth

As to your how to change where it looks question, short answer is, you can't. Intellisense for the most part is provided by reflection of assemblies included in your project ( and some other tricks with C++ ). What you are getting is a result of VS processing through all the assemblies you have included and all assemblies from the GAC.

至于你如何改变它看起来的问题,简短的回答是,你不能。大多数情况下,智能感知是由项目中包含的程序集的反射提供的(以及 C++ 的一些其他技巧)。您得到的是 VS 处理您包含的所有程序集以及来自 GAC 的所有程序集的结果。

That said, if you want to provide explicit intellisense results from a project you are working on, look into IVsContextualIntellisenseFilterProvider

也就是说,如果您想从您正在处理的项目中提供明确的智能感知结果,请查看IVsContextualIntellisenseFilterProvider

Finally, for some insight into the behind the scenes process, check this blog post

最后,要深入了解幕后流程,请查看此博客文章

回答by Peter D

Eclipse also has this feature and it is an open source project. Why not check out how Eclipse does it by actually looking at the code?

Eclipse 也有这个特性,它是一个开源项目。为什么不通过实际查看代码来了解 Eclipse 是如何做到的呢?

回答by Adam Markowitz

This question is too broad. Since there are a number of different languages the VS IDE supports out of the box AND there are N number of DSL and IDE enhancements that support alternative intellisense this implies a number of answers. If you are speaking about C# specifically then See the Tools | Options | Text Editor | C# | Intellisense area to see the available options of completion options. As far as the algorithm[s] used, you would be looking for the metadata of assemblies, copious caching of type members, MRU list for last member chosen for specific type, etc. If you have a more specific question, I'd suggest you clarify.

这个问题太宽泛了。由于 VS IDE 支持多种不同的语言,并且有 N 种支持替代智能感知的 DSL 和 IDE 增强功能,这意味着许多答案。如果您专门谈论 C#,那么请参阅工具 | 选项 | 文本编辑器 | C# | Intellisense 区域查看完成选项的可用选项。就使用的算法而言,您将寻找程序集的元数据、类型成员的大量缓存、为特定类型选择的最后一个成员的 MRU 列表等。如果您有更具体的问题,我建议你澄清一下。

See the example of a DSL (ironpython) and its implementation here.

此处查看 DSL (ironpython) 示例及其实现。