139 points · by jsnell · 4y ago · 48 comments
It explores the space from a different angle: the paper doesn't mention ropes, but it promotes a data structure called a piece table (or sometimes piece chain, piece tree, ...). These two data structure approaches are orthogonal, in that ropes can be used to implement the piece tables' descriptor sequences.
Some salient points from the paper about the advantages of the piece table:
* All buffers used are either read-only or append-only, so they are easy to cache and work well over a network.
* It follows that items never move once placed in a buffer, which is nice as pointers to items never need updating.
* Implementing undo is easy.
EDIT: this blog post is also nice: https://code.visualstudio.com/blogs/2018/03/23/text-buffer-r...
I'm considering starting a new, similar, series called "The Book of Monoids," which covers some of the same ground but with more of a focus on parallelism (GPU) than incremental computation.
I wrote about this in some detail: https://raphlinus.github.io/xi/2020/06/27/xi-retrospective.h...
Addendum: One thing that excites me about Rust is the potential to cut down on the excessive use of IPC, by letting us implement reusable libraries in a safe language that imposes minimal runtime requirements, then use Rust's powerful metaprogramming features to auto-generate safe language bindings for those libraries. That's one reason I chose Rust for AccessKit. I know diplomat [1] is making some progress in this area.
If you want/need the AST for other analysis I guess it might be worth doing something more complex there. For my use it mattered more to keep the editor minimal.
Worst case scenario is if you open something like a block comment which doesn't close and doesn't hit an error condition which resets the parser state. If you need to consider large files that's a reason to re-parse the buffer lazily in the background...
But, yes, if you write your own parsers you certainly would also need to ensure the state is as compact as possible.
Is this your editor? https://github.com/vidarh/re
Found it through your profile page. Thanks!
E.g. if you try to split the current window, it calls a script that's just expected to be there. My current incarnation of that script uses bspc to split the current window tile (note the README is outdated enough to refer to i3wm which I switched away from a couple of years ago...). It then executes its arguments to open a second client in the open tile that attaches to the same buffer.
With respect to the syntax highlighting I don't think the version on Github is remotely up to date - I believe that variant still depends on simply lexing the visible buffer rather than cache the state, but I may be wrong. Just lexing the visible buffer was my quick and dirty starting point to have something, and works surprisingly well (but it gets imprecise and annoying if you e.g. have start of block comments moving in and out of view, hence the switch to caching the Rouge state to make it precise; the next step is to move the lexing to a separate thread in the server process instead of having the clients run it).
It's also buggy, as I fix only the bugs which bothers me...
Now that I've scared you sufficiently, if you have any questions, my e-mail is also on my profile.
I am not scared, I think dogfooding a text editor is remarkable. But I am worried about the time it takes to get it working and get used to it.
Another thing I was wondering is how easy this is to integrate with compiler frontends. Basically they need the same thing: an AST. And incrementally updating those is a big reason intellij uses so much resources and CPU since it is constantly boiling the oceans to update ASTs all over the place. Any time it gets out of wack (which happens a lot), you are looking at lengthy rebuilds using external tools such as gradle. And of course, Jetbrains is currently working on a new compiler frontend for the Kotlin compiler.
I used to use Eclipse back when I was still doing Java. These days people pull up their nose for that. But it had one powerful feature: a tightly integrated incremental compiler. Intellij is orders of magnitudes slower than Eclispe was.
With intellij, the best case performance of making a 1 character change to a test that you just ran and then re-running it is in the order of seconds. Three seconds would be pretty good. Eclipse was able to do this in well under a second. Compilation would have completed within milliseconds of the character being added. That felt as if it had no delay. Jetbrains never came close to that level of performance. It also never came close to having the same level of consistency. If eclipse told you the source code compiled, it generally did. If intellij tells me I have no errors, I can't trust that to be true until I do a full rebuild. Which can take like half a minute and then often fails for me (because there are obvious syntax errors somewhere). The more changes you make, the more likely it is to be lying about whether things still compile.
So, it's a big problem to tackle. The Kotlin compiler is pretty slow. A full recompilation (without even running tests) can easily take a minute plus on a reasonably sized code base. They are still optimizing it and I'm looking forward to the new compiler frontend they've been working on. But reliable & robust incremental compilation for Kotlin tightly integrated into the IDE would make life a lot more pleasant for me.
Text CRDTs are not very difficult, but representing an AST as a CRDT is an open problem. See [1]. There has thankfully been some progress from HN's own dmulligan, see [2].
[1]: https://writer.zohopublic.com/writer/published/grcwy5c699d67...
So why did TECO use a gap buffer? The gap buffer was an easy way to manage an edit buffer back when machine clock speeds were measured in kilohertz and RAM in kilobytes. There were no fonts (no rendering at all), six or seven bit characters, oh, and the machines were often timeshared.
Likewise, vi is just a visual addition to ed, itself a clone of the Multics qed, itself a clone of qed on earlier machines. Those machines were, by modern standards, equally resource starved.
https://code.visualstudio.com/blogs/2018/03/23/text-buffer-r...
Not sure if this is still the case, but it was as of 2018.
Its really kind of fascinating to me, all the different ways we've come up with over the years just to manipulate text.
One of the things that strikes me is how much effort goes into making these editors work well with absurdly large files, rather than more editors punting on that and having people fall back on specific tools for huge files.
For my own editor I basically decided to ignore large files entirely and fall back on using emacs for the very rare case where I need to open an absurdly large file.
I know that's a luxury Jetbrains doesn't have, because we've come to expect all editors to handle ridiculous sized files well.
But the point being that for reasonably sized files - up to tens of thousands of lines - just an array of strings is more than fast enough.
Even with an editor like my personal one (not really usable for anyone else, though I've started packaging up parts of the code) written in Ruby (which introduces a substantial overhead per string).
I think if I ever decide to make my editor handle really huge files, I'll "just" split them in suitably large chunks and lazily do the necessary processing as needed
That falls apart the moment you need some non-basic feature like matching open/closed brackets/elements, etc.
Or references. Or refactoring ...
Although webstorm can't handle large typescript projects with a lot of computed type as well as vs code.
My own editor was based on Femto, initially. It's now entirely unrecognisable, and I've used it as my main editor for the last couple of years. I'm linking Femto rather than mine because mine is huge compared to Femto, and full of assumptions that only holds for me -- that was part of the purpose for me; I realised my .emacs file had grown to the point where I could literally write an editor in fewer lines.
That's not to say I'm suggesting that most people should write their own editor with the purpose of making it their main editor, because that will likely only work without turning into a massive time suck if you (like me) like your editors spartan and care more about ergonomics in a very simple package than "fancy IDE's". But writing an editor is fun because it can start very simple and you can gradually rediscover or reinvent all kinds of model-view abstractions, and get somewhere usable very quickly.