如何在C#中实现代码折叠

发布于 2024-08-24 02:03:59 字数 260 浏览 3 评论 0原文

我开始在工作中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

山色无中 2024-08-31 02:03:59

为什么不使用现有的 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.

度的依靠╰つ 2024-08-31 02:03:59

我建议你看看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.

爱情眠于流年 2024-08-31 02:03:59

有时嵌入 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.

alt text

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.

小…楫夜泊 2024-08-31 02:03:59

如果您的团队习惯于“Visual Studio 功能”,那么我假设您在办公室使用 Visual Studio。以下是我的建议:

  • 将您的 IDE 基于 Visual Studio,原因如下:
    • 如果可能,请使用 Visual Studio 2010。该 SDK 较 2008 年/之前有了很大改进。
    • 否则请使用 Visual Studio 2008/2005。目前,我所有的商业 IDE 产品仅支持 2005/2008。
    • 如果您的团队使用 Visual Studio,他们会讨厌 Eclipse。在这种情况下,甚至没有考虑的选项除非您选择使用现有 Eclipse 插件,从而节省您创建新 IDE 的时间。
  • 如果您的团队没有使用 Visual Studio 2010,您可以在集成模式中免费使用 Visual Studio 2010 Shell(隔离模式不是您想要的模式)。这样您就可以暂时使用 Visual Studio 2010 作为 IDE,并且如果团队稍后升级到 Visual Studio 2010 的完整版本之一,则适合您语言的 IDE 将干净地集成到完整版本中。 编辑: Visual Studio Shell 基本上是 Visual Studio 的核心,不包含任何特定语言(C#、C++、VB 等)。微软免费提供这个核心,对于有兴趣创建自己的语言支持的人来说,这是一个很好的选择。

请阅读我对以下两个问题的回答:

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:

  • Base your IDE on Visual Studio, for the following reasons:
    • Use Visual Studio 2010 if possible. The SDK is greatly improved from 2008/earlier.
    • Use Visual Studio 2008/2005 otherwise. At the moment, all of my commercial IDE products only support 2005/2008.
    • If your team uses Visual Studio, they will hate Eclipse. Not even an option to consider in this case unless you choose to use an existing Eclipse plug-in, saving you the time of creating a new IDE.
  • If your team isn't using Visual Studio 2010, you can use the Visual Studio 2010 Shell in Integrated Mode for free (Isolated Mode is not what you want). This lets you use Visual Studio 2010 for your IDE for now, and should the team upgrade later to one of the full versions of Visual Studio 2010, the IDE for your language will cleanly integrate into the full version. Edit: Visual Studio Shell is basically the core of Visual Studio without any specific languages (C#, C++, VB, etc.) included. Microsoft provides this core for free, and it's a great option specifically for people interested in creating their own language support.

Read my answers in the following two questions:

软的没边 2024-08-31 02:03:59

编写一个完整的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文