通用智能感知的新的和全面实施 [英] A New and Full Implementation of Generic Intellisense

查看:138
本文介绍了通用智能感知的新的和全面实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感兴趣的是写SQL和C#泛型启用智能感知编辑器(等人。如果可能的话!)。我想这样做在C#作为一个覆盖或扩展WPF的RichTextBox型控制。我知道有许多可用的例子项目,我已经实现了的基本的我自己的版本;但大部分我所遇到的(实际上我自己)的例子仅仅是,基本的。

一对夫妇的code的例子是:


  1. DIY智能感知通过yetanotherchris


  2. <一个href=\"http://www.$c$cproject.com/Articles/29154/$c$cTextBox-another-RichTextBox-control-with-synta\">$c$cTextBox - 与语法高亮和智能感知另一RichTextBox控件通过塔马斯Honfi


我已经然而,发现了智能感知 QueryCommander SQL编辑器通过SQL编辑器中的一个很好的例子的Mikael哈坎森这似乎运作良好。微软必须使用命令关键字的XML库,但我的问题是:如何(详细地)做微软实施其智能感知(如你型智能感知)以及如何努力,会是我创造同样的标准我自己?


编辑 - 答:我已经成功地开发具有基本的智能感知主要是为我自己享受我自己的编辑器控制的一年。我以为我会回来的规定,帮助我用我自己的发展,可用于外的开箱和免费的免费提供的.NET项目的列表:


  1. ICSharp code(的WinForms)


  2. AvalonEdit(WPF)


  3. ScintillaNET(的WinForms)


  4. 查询指挥官[例如智能感知执行(的WinForms)



编辑乙:后15个月有人问我仍然在寻找新的改进的编辑器。这一个是好...


  1. RoslynPAD 是爽!


编辑C:2年的问题上+,我发​​现了以下项目,无论是使用WPF和AvalonEdit支持


  1. codeCompletion为AvalonEdit 使用NRefactory。这个项目是非常好的,有一个完整的实现使用NRefactory智能感知的。


  2. ScriptCS ScriptCS可以很容易地编写,并用简单的文本编辑器执行C#。



解决方案

  

如何(详细地)做微软实施他们作为你型智能感知?


我的可以的它描述给你照顾名称详细的任何级别,但我没有足够的时间超过一个简要的解释了。我将解释我们如何做罗斯林。

首先,我们建立使用数据结构,可以有效地重新present记号流的不可变模型的编辑的,因为很明显的修改是precisely什么有将是一个大量的。

关键洞察力,使之成为有效的持续性再利用是重新present中的字符长度的令牌,但不是他们的字符位置的编辑缓冲区;请记住,在该文件的末尾的令牌将要改变每编辑位置,但令牌的长度不会改变。 您必须不惜一切代价最小化总重lexings的数量,如果你想成为非常大的文件有效。

一旦你有一个可以处理插入和删除操作,而无需重新词法每次建立一个永恒不变的标记流的整个文件的不可变的模型,你就必须做的相同的事情,但对于语法分析。的这是在实践中一个相当困难的问题的。我建议你​​获得计算机科学本科或研究生学位与解析器理论强调,如果你还没有。我们得到人们的帮助,谁做了解析理论的论文,设计算法的特定位博士。

然后,很明显,建立一个语法分析器,可以分析C#。请记住,它有分析的的C#,不是的正确的C#;智能感知有时工作程序是在非编译状态。因此,通过未来与修改语法启动具有良好的差错恢复特性。

好了,现在你已经有了一个解析器,能够有效地做语法分析,而无需重新词法或重新解析什么,但编辑的区域,大部分的时间,这意味着,你可以做按键之间的工作。我忘了提,你当然需要拿出一些机制的不会阻塞UI线程的同时做所有这些分析都应该进行分析比发生两次击键之间的时间需要更长的时间。新的异步/等待的C#5的功能应该有助于这一点。 (我可以从个人的经验告诉你:小心任务和取消标记的增殖如果你不小心,就有可能陷入那里有成千上万的取消任务未决的状态,那就是。还不快的。)

现在,您已经有了一个语法分析,你需要建立一个的语义分析的。因为你只是做智能感知,它并不需要是一个特别复杂的语义分析。 (我们的语义分析必须做适合的正确的程序和不正确的方案正确的错误分析产生code的分析。)不过,当然,再次它必须做打破计划好的语义分析,这也大大增加了复杂性。

我的建议是建立一个顶级语义分析,再次使用,可以从编辑坚持的声明,在源 - code类型的状态编辑不可改变的模式启动。与任何顶级分析交易,是的的语句或前pression:类型声明,指令,命名空间,方法声明,构造函数,析构函数,等等。东西,构成了形的程序时,编译器生成的元数据。

元!我忘了元数据。你需要一个元数据读取器。你需要能够生产智能感知的引用类型库前pressions,效果显着。我建议使用CCI库,元数据的读者,而不是反思。因为你只是做智能感知,显然你并不需要一个元数据的作家。

无论如何,一旦你有一个顶级语义分析,那么你可以写一个声明和-EX pression语义分析,用于分析给定语句的类型前pressions的。要特别注意的名称查找的和的重载的算法。方法类型推断会特别棘手,里面特别LINQ查询

一旦你得到了这一切,智能感知引擎应该很容易;只是在当前光标位置摸出前pression的类型和适当地显示一个下拉。


  

如何努力,会是我创造同样的标准我自己?


好吧,我们已经有了一个团队,把它叫做十人,它可能会采取,称之为5年在一起来完成从开始到结束的整个事情。但是,我们有很多的不仅仅是智能感知引擎更多的事情要做。这也许是工作的只有40%。哦,还有一半的人在工作VB,现在,我想它。但是,这些人平均大概五年或十年做这种工作的经验,所以他们是在它的速度比你会如果你从来没有这样做过。

所以我们说,应该需要大约十到二十年的全职工作,单独工作,建立了C#中的罗斯林质量智能感知引擎,可以在时间做大型项目的可接受-贴近正确分析按键之间。

如果您需要首先做的博士,明显延长。

或者,你可以简单地使用罗斯林,因为那是它是。这将带你大概几个小时,但你不要自己动手的乐趣。它是乐趣!

您可以在这里下载preVIEW发布:

<一个href=\"http://www.microsoft.com/download/en/details.aspx?id=27746\">http://www.microsoft.com/download/en/details.aspx?id=27746

I am interested in writing a generic Intellisense enabled editor for SQL and C# (et al. if possible!). I would like to do this in C# as an overridden or extended WPF richTextBox-type control. I know there are many example projects available and I have implemented a basic version of my own; but most of the examples that I have come across (and indeed my own) are just that, basic.

A couple of code examples are:

  1. DIY Intellisense By yetanotherchris

  2. CodeTextBox - another RichTextBox control with syntax highlighting and Intellisense By Tamas Honfi

I have however, found a great example of an SQL editor with Intellisense QueryCommander SQL Editor By Mikael Håkansson which seems to work well. Microsoft must use a XML library of command keywords, but my question is: How (in detail) do Microsoft implement their Intellisense (as-you-type Intellisense) and how hard would it be for me to create my own of the same standard?


Edit A: A year on and I have managed to develop my own editor control with basic intellisense mainly for my own "enjoyment". I thought I would come back provide a list of freely available .NET projects that helped me with my own development and can be used out-of-the-box and free of charge:

  1. ICSharpCode (WinForms)

  2. AvalonEdit (WPF)

  3. ScintillaNET (WinForms)

  4. Query Commander [for example of intellisense implementation] (WinForms)


Edit B: 15 months after the question was asked I am still looking for new improved editors. This one is nice...

  1. RoslynPAD is cool!


Edit C: 2 years+ on from the question, I have found the following projects, both using WPF and backed by AvalonEdit.

  1. CodeCompletion for AvalonEdit using NRefactory. This project is really nice and has a full implementation of intellisense using NRefactory.

  2. ScriptCS ScriptCS makes it easy to write and execute C# with a simple text editor.

解决方案

How (in detail) do Microsoft implement their as-you-type Intellisense?

I can describe it to any level of detail you care to name, but I don't have the time for more than a brief explanation. I'll explain how we do it in Roslyn.

First, we build an immutable model of the token stream using a data structure that can efficiently represent edits, since obviously edits are precisely what there are going to be a lot of.

The key insight to making it efficient for persistent reuse is to represent the character lengths of the tokens but not their character positions in the edit buffer; remember, a token at the end of the file is going to change position on every edit but the length of the token does not change. You must at all costs minimize the number of total re-lexings if you want to be efficient on extremely large files.

Once you have an immutable model that can handle inserts and deletions to build up an immutable token stream without re-lexing the entire file every time, you then have to do the same thing, but for grammatical analysis. This is in practice a considerably harder problem. I recommend that you obtain an undergraduate or graduate degree in computer science with an emphasis on parser theory if you have not already. We obtained the help of people with PhDs who did their theses on parser theory to design this particular bit of the algorithm.

Then, obviously, build a grammatical analyzer that can analyze C#. Remember, it has to analyze broken C#, not correct C#; IntelliSense has to work while the program is in a non-compiling state. So start by coming up with modifications to the grammar that have good error-recovery characteristics.

OK, so now you've got a parser that can efficiently do grammatical analysis without re-lexing or re-parsing anything but the edited region, most of the time, which means that you can do the work between keystrokes. I forgot to mention, of course you will need to come up with some mechanism to not block the UI thread while doing all of these analyses should the analysis happen to take longer than the time between two keystrokes. The new "async/await" feature of C# 5 should help with that. (I can tell you from personal experience: be careful with the proliferation of tasks and cancellation tokens. If you are careless, it is possible to get into a state where there are tens of thousands of cancelled tasks pending, and that is not fast.)

Now that you've got a grammatical analysis you need to build a semantic analyzer. Since you are only doing IntelliSense, it does not need to be a particularly sophisticated semantic analyzer. (Our semantic analyzer must do an analysis suitable for generating code from correct programs and correct error analysis from incorrect programs.) But of course, again it has to do good semantic analysis on broken programs, which does increase the complexity considerably.

My advice is to start by building a "top level" semantic analyzer, again using an immutable model that can persist the state of the declared-in-source-code types from edit to edit. The top level analyzer deals with anything that is not a statement or expression: type declarations, directives, namespaces, method declarations, constructors, destructors, and so on. The stuff that makes up the "shape" of the program when the compiler generates metadata.

Metadata! I forgot about metadata. You'll need a metadata reader. You need to be able to produce IntelliSense on expressions that refer to types in libraries, obviously. I recommend using the CCI libraries as your metadata reader, and not Reflection. Since you are only doing IntelliSense, obviously you don't need a metadata writer.

Anyway, once you have a top-level semantic analyzer, then you can write a statement-and-expression semantic analyzer that analyzes the types of the expressions in a given statement. Pay particular attention to name lookup and overload resolution algorithms. Method type inference will be particularly tricky, especially inside LINQ queries.

Once you've got all that, an IntelliSense engine should be easy; just work out the type of the expression at the current cursor position and display a dropdown appropriately.

how hard would it be for me to create my own of the same standard?

Well, we've got a team of, call it ten people, and it'll probably take, call it five years all together to get the whole thing done from start to finish. But we have lots more to do than just the IntelliSense engine. That's maybe only 40% of the work. Oh, and half those people work on VB, now that I think about it. But those people have on average probably five or ten years experience in doing this sort of work, so they're faster at it than you will be if you've never done this before.

So let's say it should take you about ten to twenty years of full time work, working alone, to build a Roslyn-quality IntelliSense engine for C# that can do acceptably-close-to-correct analysis of large programs in the time between keystrokes.

Longer if you need to do that PhD first, obviously.

Or, you could simply use Roslyn, since that's what it's for. That'll take you probably a few hours, but you don't get the fun of doing it yourself. And it is fun!

You can download the preview release here:

http://www.microsoft.com/download/en/details.aspx?id=27746

这篇关于通用智能感知的新的和全面实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆