如何在C#中实现代码折叠
我开始在工作中使用 COBOL/BASIC IDE(以取代我们目前拥有的 IDE,这比记事本稍有进步)。它将用 C# 制作。管理层确实对实现一些 Visual Studio 类型的功能很感兴趣,其中一个重要的功能是代码折叠。我查看了 MSDN,但没有看到任何折叠行(或添加展开/折叠按钮)的好方法。
我假设文本区域应该是 RichTextBox。我这里是不是偏离了轨道?我想这可以通过某种修改后的 TreeView 来完成,但这对我来说似乎有点错误。有什么方法可以做到这一点,我只是想念吗?
I'm beginning to work on a COBOL/BASIC IDE at work (to replace the one that we have currently that's a slight step up from Notepad). It'll be made in C#. The management is really interested in implementing some Visual Studio type features, and a big one is code folding. I've looked on MSDN, but I didn't see any good way to collapse lines (or to add the expand/collapse buttons).
I'm assuming that the text area should be a RichTextBox. Am I off track here? I suppose it could be done with some sort of modified TreeView, but that seems a little wrong to me. Is there some way of doing this that I'm just missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为什么不使用现有的 IDE 并对其进行扩展呢?从头开始编写一个语言是一项艰巨的任务(您需要解析器、词法分析器、语法突出显示器等),如果您需要支持多种语言(您提到 COBOL 和 Basic),则更加复杂。
Notepad++ 具有语法着色功能,可以向其中添加语言 - COBOL 是默认安装的语言之一。它支持代码折叠并具有许多插件(您可以编写自己的插件,以满足您的需求)。
编辑:
Eclipse 是另一个具有类似支持的优秀 IDE,并且正如评论中提到的,它有一个 COBOL 插件。
Why not use an existing IDE and extend it? Writing one from scratch is a huge undertaking (you need a parser, lexer, syntax highlighter and more), and is even more complicated if you need to support multiple languages (you mention COBOL and Basic).
Notepad++ has syntax coloring and one can add languages to it - COBOL is one of the ones installed by default. It supports code folding and has many plugins (you can write your own, that will suit your needs).
Edit:
Eclipse is another excellent IDE that has similar support, and as mentioned in the comments has a COBOL plug-in.
我建议你看看SharpDevelop。它是一个非常好的 IDE,内置了许多类似 Visual Studio 的功能。它是用 C# 编写的,完全支持多种语言的代码折叠和语法突出显示。另外,它是 LGPL 许可下的开源软件。因此,如果您不想将应用程序基于 SharpDevelop,那么您仍然可以重用它们的一些控件,例如代码编辑器或窗口工具包。
您应该考虑将 COBOL 语言添加到 SharpDevelop,而不是从头开始。如果您做不到这一点,那么您仍然可以使用 SharpDevelop 代码作为有关如何使良好的 IDE 工作的不错的参考。
I suggest you take a look at SharpDevelop. It's a pretty good IDE with a bunch of Visual Studio like features already built in. It's written in C# and fully supports code folding with syntax highlighting in several languages. Plus, it's Open Source under the LGPL license. So, if you don't want to base your app on SharpDevelop then you can still reuse some of their controls like the code editor or windowing toolkit.
You should consider adding the COBOL language to SharpDevelop instead of starting from scratch. If you can't do this, then you can still use the SharpDevelop code as a decent reference on how to make a good IDE work.
有时嵌入 Eclipse 或成熟的编辑器并不合适。它是杀伤力过大或超重或由于其他原因而错误。我很欣赏其他帖子中建议的第一个倾向,即不要在这里重新发明,但在某些情况下,一个小发明是必要的。例如,用于制作 Stack Overflow 帖子的文本框......既不是 Eclipse,也不是嵌入式 Visual Studio。我想知道为什么?
提出这个问题很重要 - 构建它还是购买它? - 但有时,正确的答案是构建它。
XPathVisualizer 提供了一个用 C# 实现的代码折叠文本编辑器的简单示例,并且基于一个富文本框。不过,它不是 VB - 它是一个 XML 编辑器。但一般原则是适用的。
下面就让我们来看看吧。
为了在用户键入时动态实现 XML 语法着色,它使用单独的后台线程。描述了原因和一些细节在 Stack Overflow 上的单独答案中。
您可以为 COBOL/VB 做类似的事情。 XPathVisualizer 是开源的,已获得 MS-PL 许可,因此您可以浏览和借用。
Sometimes embedding Eclipse or a full-fledged editor is not appropriate. It's overkill or overweight or wrong for some other reason. I appreciate the first inclination suggested in other posts to not re-invent here, but in some cases a small invention is what is necessary. For example, the textbox used to make Stack Overflow posts .... is neither Eclipse nor an embedded Visual Studio. I wonder why?
It's important to ask the question - build it or buy it? - but sometimes, the correct answer is BUILD IT.
XPathVisualizer provides a simple example of a code-folding text editor implemented in C#, and based on a RichTextBox. It's not VB, though - it's an XML editor. But the general principles apply.
Here's a look at it.
To implement XML syntax colorization dynamically, while the user types, it uses a separate background thread. The reasons why and some of the details are described in a separate answer on Stack Overflow.
You could do something similar for your COBOL/VB thing. XPathVisualizer is open source, licensed with MS-PL, so you can browse and borrow.
如果您的团队习惯于“Visual Studio 功能”,那么我假设您在办公室使用 Visual Studio。以下是我的建议:
请阅读我对以下两个问题的回答:
If your team is used to "Visual Studio features," then I'll assume you use Visual Studio there at the office. Here are my suggestions:
Read my answers in the following two questions:
编写一个完整的 IDE 是一项艰巨的任务。我建议尝试找到一个具有您想要的功能的现有 IDE,或者对现有的开源 IDE 进行调整。
回答你的问题:我猜想 Visual Studio IDE 使用从头开始编写的自定义控件,而不是 RichText 控件。
Writing a complete IDE is a HUGE task. I would recommend trying to find an existing one that has what you want, or make adaptions to an existing open source IDE.
To answer your question: I guess that the Visual Studio IDE uses a custom control, written from scratch, rather than a RichText control.