文本编辑器一般如何实现? [英] How are text editors generally implemented?

查看:374
本文介绍了文本编辑器一般如何实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可能会让我听起来很笨.那是因为我.

This question is probably going to make me sound pretty clueless. That's because I am.

我只是在想,如果我假设对设计自己的文本编辑器GUI控件,小部件或任何您想调用的内容感兴趣(我不是),我该怎么办?

I'm just thinking, if I were hypothetically interested in designing my own text editor GUI control, widget, or whatever you want to call it (which I'm not), how would I even do it?

对于像我这样的新手来说,诱惑是将文本编辑器的内容以字符串的形式存储,这似乎非常昂贵(不是我太熟悉一种语言/平台之间的字符串实现有何不同)然后是下一个;但是我知道,例如,在.NET中,它们是不可变的,因此频繁的操作(例如您需要在文本编辑器中支持的操作)将非常浪费,以非常快的速度构造一个接一个的字符串实例继承).

The temptation to a novice such as myself would be to store the content of the text editor in the form of a string, which seems quite costly (not that I'm too familiar with how string implementations differ between one language/platform and the next; but I know that in .NET, for example, they're immutable, so frequent manipulation such as what you'd need to support in a text editor would be magnificently wasteful, constructing one string instance after another in very rapid succession).

大概使用了一些包含文本的可变数据结构;但是弄清楚这种结构看起来是什么样的,这给我带来了一些挑战.随机访问会很好(无论如何,我都会 -毕竟,您不希望用户能够跳转到文本中的任何位置吗?),但是随后我想知道这样做的代价例如,导航到大文件中间的某个位置并立即开始输入.同样,新手方法(例如,您将文本存储为可调整大小的字符数组)会导致性能很差,我想,因为用户键入的每个字符都会有大量数据需要转移"结束.

Presumably some mutable data structure containing text is used instead; but figuring out what this structure might look like strikes me as a bit of a challenge. Random access would be good (I would think, anyway—after all, don't you want the user to be able to jump around to anywhere in the text?), but then I wonder about the cost of, say, navigating to somewhere in the middle of a huge document and starting to type immediately. Again, the novice approach (say you store the text as a resizeable array of characters) would lead to very poor performance, I'm thinking, as with every character typed by the user there would be a huge amount of data to "shift" over.

因此,如果我不得不猜测,我假设文本编辑器采用某种结构,将文本分解成较小的部分(行,也许是?),这些部分分别包含具有随机访问权限的字符数组,并且哪个本身可以作为离散块随机访问.即使似乎也一定是一个相当大的过度简化,即使它从一开始就非常接近.

So if I had to make a guess, I'd suppose that text editors employ some sort of structure that breaks the text down into smaller pieces (lines, maybe?), which individually comprise character arrays with random access, and which are themselves randomly accessible as discrete chunks. Even that seems like it must be a rather monstrous oversimplification, though, if it is even remotely close to begin with.

当然,我也意识到,可能没有 一种实现文本编辑器的标准"方式;也许从一个编辑器到另一个编辑器,它的变化很大.但是我认为,由于显然已经解决了很多次这个问题,多年来,也许已经出现了一种相对普遍的方法.

Of course I also realize that there may not be a "standard" way that text editors are implemented; maybe it varies dramatically from one editor to another. But I figured, since it's clearly a problem that's been tackled many, many times, perhaps a relatively common approach has surfaced over the years.

无论如何,我只是想知道外面是否有人对此主题有所了解.就像我说的那样,我绝对不是要编写自己的文本编辑器.我很好奇.

Anyway, I'm just interested to know if anyone out there has some knowledge on this topic. Like I said, I'm definitely not looking to write my own text editor; I'm just curious.

推荐答案

一种常见的技术(尤其是在较旧的编辑器中)称为拆分缓冲区.基本上,您将文本拆分"为光标之前的所有内容和光标之后的所有内容.之前的所有内容都在缓冲区的开头.之后的所有内容都在缓冲区的末尾.

One technique that's common (especially in older editors) is called a split buffer. Basically, you "break" the text into everything before the cursor and everything after the cursor. Everything before goes at the beginning of the buffer. Everything after goes at the end of the buffer.

当用户键入文本时,它会在不移动任何数据的情况下进入两者之间的空白处.当用户移动光标时,可以将适当数量的文本从中断"的一侧移到另一侧.通常,在一个区域中有很多移动,因此通常一次只移动少量文本.最大的例外是,如果您具有转到第XXX行"的功能.

When the user types in text, it goes into the empty space in between without moving any data. When the user moves the cursor, you move the appropriate amount of text from one side of the "break" to the other. Typically there's a lot of moving around a single area, so you're usually only moving small amounts of text at a time. The biggest exception is if you have a "go to line xxx" kind of capability.

Charles Crowley写了一个更完整的对该主题的讨论.您可能还想看看文本编辑技术,其中涵盖了分割缓冲区(和其他可能性).

Charles Crowley has written a much more complete discussion of the topic. You might also want to look at The Craft of Text Editing, which covers split buffers (and other possibilities) in much greater depth.

这篇关于文本编辑器一般如何实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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