anduong / til

0 stars 0 forks source link

Challenging projects every programmer should try #39

Open anduong opened 4 years ago

anduong commented 4 years ago

http://web.eecs.utk.edu/~azh/blog/challengingprojects.html

Text Editor We use text editors everyday, but do you know how it really works? Ignoring all of the fancy features that your favorite editor has, how would you implement a textbox that supports a movable text cursor and selecting, inserting, and deleting text? No, you can't use the builtin textbox component from your favorite GUI framework!

The biggest challenge is figuring out how to store the text document in memory. My first thought was to use an array, but that has horrible performance if the user inserts text anywhere other than the end of the document. Luckily, there are some nice data structures to learn to solve this.

Another hurdle was learning how a text cursor behaves in popular editors. For example, if I press the up arrow key with the cursor in the middle of the document, where will the cursor move? Same column? Not if that line is shorter. Keep pressing up. The cursor will snap back to the original column once a line is long enough. It turns out that the cursor has a memory for the column and tries to get back to it. It is these details that I never noticed until I tried to implement it.

After implementing the basic editor, I challenge you to implement two more features: undo/redo and word wrapping. Implementing undo/redo in an efficient way was mind blowing to me! I first tried keeping an array of previous states, then tried the Memento pattern, before finally settling on the Command pattern. Word wrapping forces you to separate the visual aspects of a text line from the memory aspects.

Things to learn:

Data structures for storing the text: array, rope, gap buffer, piece table. Behavior and implementation of the text cursor. Design patterns for undo/redo: memento, command. Abstractions to separate the visual and memory aspects of the text. Further reading:

Text Editor: Data Structures (web) Design and Implementation of a Win32 Text Editor (web) Data Structures and Algorithms in Java (Amazon)