You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

504 lines
25 KiB

7 months ago
  1. <!doctype html>
  2. <title>CodeMirror: Internals</title>
  3. <meta charset="utf-8"/>
  4. <link rel=stylesheet href="docs.css">
  5. <style>dl dl {margin: 0;} .update {color: #d40 !important}</style>
  6. <script src="activebookmark.js"></script>
  7. <div id=nav>
  8. <a href="https://codemirror.net/5"><h1>CodeMirror</h1><img id=logo src="logo.png"></a>
  9. <ul>
  10. <li><a href="../index.html">Home</a>
  11. <li><a href="manual.html">Manual</a>
  12. <li><a href="https://github.com/codemirror/codemirror5">Code</a>
  13. </ul>
  14. <ul>
  15. <li><a href="#top">Introduction</a></li>
  16. <li><a href="#approach">General Approach</a></li>
  17. <li><a href="#input">Input</a></li>
  18. <li><a href="#selection">Selection</a></li>
  19. <li><a href="#update">Intelligent Updating</a></li>
  20. <li><a href="#parse">Parsing</a></li>
  21. <li><a href="#summary">What Gives?</a></li>
  22. <li><a href="#btree">Content Representation</a></li>
  23. <li><a href="#keymap">Key Maps</a></li>
  24. </ul>
  25. </div>
  26. <article>
  27. <h2 id=top>(Re-) Implementing A Syntax-Highlighting Editor in JavaScript</h2>
  28. <p style="font-size: 85%" id="intro">
  29. <strong>Topic:</strong> JavaScript, code editor implementation<br>
  30. <strong>Author:</strong> Marijn Haverbeke<br>
  31. <strong>Date:</strong> March 2nd 2011 (updated November 13th 2011)
  32. </p>
  33. <p style="padding: 0 3em 0 2em"><strong>Caution</strong>: this text was written briefly after
  34. version 2 was initially written. It no longer (even including the
  35. update at the bottom) fully represents the current implementation. I'm
  36. leaving it here as a historic document. For more up-to-date
  37. information, look at the entries
  38. tagged <a href="http://marijnhaverbeke.nl/blog/#cm-internals">cm-internals</a>
  39. on my blog.</p>
  40. <p>This is a followup to
  41. my <a href="https://codemirror.net/5/story.html">Brutal Odyssey to the
  42. Dark Side of the DOM Tree</a> story. That one describes the
  43. mind-bending process of implementing (what would become) CodeMirror 1.
  44. This one describes the internals of CodeMirror 2, a complete rewrite
  45. and rethink of the old code base. I wanted to give this piece another
  46. Hunter Thompson copycat subtitle, but somehow that would be out of
  47. place—the process this time around was one of straightforward
  48. engineering, requiring no serious mind-bending whatsoever.</p>
  49. <p>So, what is wrong with CodeMirror 1? I'd estimate, by mailing list
  50. activity and general search-engine presence, that it has been
  51. integrated into about a thousand systems by now. The most prominent
  52. one, since a few weeks,
  53. being <a href="http://googlecode.blogspot.com/2011/01/make-quick-fixes-quicker-on-google.html">Google
  54. code's project hosting</a>. It works, and it's being used widely.</p>
  55. <p>Still, I did not start replacing it because I was bored. CodeMirror
  56. 1 was heavily reliant on <code>designMode</code>
  57. or <code>contentEditable</code> (depending on the browser). Neither of
  58. these are well specified (HTML5 tries
  59. to <a href="http://www.w3.org/TR/html5/editing.html#contenteditable">specify</a>
  60. their basics), and, more importantly, they tend to be one of the more
  61. obscure and buggy areas of browser functionality—CodeMirror, by using
  62. this functionality in a non-typical way, was constantly running up
  63. against browser bugs. WebKit wouldn't show an empty line at the end of
  64. the document, and in some releases would suddenly get unbearably slow.
  65. Firefox would show the cursor in the wrong place. Internet Explorer
  66. would insist on linkifying everything that looked like a URL or email
  67. address, a behaviour that can't be turned off. Some bugs I managed to
  68. work around (which was often a frustrating, painful process), others,
  69. such as the Firefox cursor placement, I gave up on, and had to tell
  70. user after user that they were known problems, but not something I
  71. could help.</p>
  72. <p>Also, there is the fact that <code>designMode</code> (which seemed
  73. to be less buggy than <code>contentEditable</code> in Webkit and
  74. Firefox, and was thus used by CodeMirror 1 in those browsers) requires
  75. a frame. Frames are another tricky area. It takes some effort to
  76. prevent getting tripped up by domain restrictions, they don't
  77. initialize synchronously, behave strangely in response to the back
  78. button, and, on several browsers, can't be moved around the DOM
  79. without having them re-initialize. They did provide a very nice way to
  80. namespace the library, though—CodeMirror 1 could freely pollute the
  81. namespace inside the frame.</p>
  82. <p>Finally, working with an editable document means working with
  83. selection in arbitrary DOM structures. Internet Explorer (8 and
  84. before) has an utterly different (and awkward) selection API than all
  85. of the other browsers, and even among the different implementations of
  86. <code>document.selection</code>, details about how exactly a selection
  87. is represented vary quite a bit. Add to that the fact that Opera's
  88. selection support tended to be very buggy until recently, and you can
  89. imagine why CodeMirror 1 contains 700 lines of selection-handling
  90. code.</p>
  91. <p>And that brings us to the main issue with the CodeMirror 1
  92. code base: The proportion of browser-bug-workarounds to real
  93. application code was getting dangerously high. By building on top of a
  94. few dodgy features, I put the system in a vulnerable position—any
  95. incompatibility and bugginess in these features, I had to paper over
  96. with my own code. Not only did I have to do some serious stunt-work to
  97. get it to work on older browsers (as detailed in the
  98. previous <a href="https://codemirror.net/5/story.html">story</a>), things
  99. also kept breaking in newly released versions, requiring me to come up
  100. with <em>new</em> scary hacks in order to keep up. This was starting
  101. to lose its appeal.</p>
  102. <section id=approach>
  103. <h2>General Approach</h2>
  104. <p>What CodeMirror 2 does is try to sidestep most of the hairy hacks
  105. that came up in version 1. I owe a lot to the
  106. <a href="http://ace.ajax.org">ACE</a> editor for inspiration on how to
  107. approach this.</p>
  108. <p>I absolutely did not want to be completely reliant on key events to
  109. generate my input. Every JavaScript programmer knows that key event
  110. information is horrible and incomplete. Some people (most awesomely
  111. Mihai Bazon with <a href="http://ymacs.org">Ymacs</a>) have been able
  112. to build more or less functioning editors by directly reading key
  113. events, but it takes a lot of work (the kind of never-ending, fragile
  114. work I described earlier), and will never be able to properly support
  115. things like multi-keystoke international character
  116. input. <a href="#keymap" class="update">[see below for caveat]</a></p>
  117. <p>So what I do is focus a hidden textarea, and let the browser
  118. believe that the user is typing into that. What we show to the user is
  119. a DOM structure we built to represent his document. If this is updated
  120. quickly enough, and shows some kind of believable cursor, it feels
  121. like a real text-input control.</p>
  122. <p>Another big win is that this DOM representation does not have to
  123. span the whole document. Some CodeMirror 1 users insisted that they
  124. needed to put a 30 thousand line XML document into CodeMirror. Putting
  125. all that into the DOM takes a while, especially since, for some
  126. reason, an editable DOM tree is slower than a normal one on most
  127. browsers. If we have full control over what we show, we must only
  128. ensure that the visible part of the document has been added, and can
  129. do the rest only when needed. (Fortunately, the <code>onscroll</code>
  130. event works almost the same on all browsers, and lends itself well to
  131. displaying things only as they are scrolled into view.)</p>
  132. </section>
  133. <section id="input">
  134. <h2>Input</h2>
  135. <p>ACE uses its hidden textarea only as a text input shim, and does
  136. all cursor movement and things like text deletion itself by directly
  137. handling key events. CodeMirror's way is to let the browser do its
  138. thing as much as possible, and not, for example, define its own set of
  139. key bindings. One way to do this would have been to have the whole
  140. document inside the hidden textarea, and after each key event update
  141. the display DOM to reflect what's in that textarea.</p>
  142. <p>That'd be simple, but it is not realistic. For even medium-sized
  143. document the editor would be constantly munging huge strings, and get
  144. terribly slow. What CodeMirror 2 does is put the current selection,
  145. along with an extra line on the top and on the bottom, into the
  146. textarea.</p>
  147. <p>This means that the arrow keys (and their ctrl-variations), home,
  148. end, etcetera, do not have to be handled specially. We just read the
  149. cursor position in the textarea, and update our cursor to match it.
  150. Also, copy and paste work pretty much for free, and people get their
  151. native key bindings, without any special work on my part. For example,
  152. I have emacs key bindings configured for Chrome and Firefox. There is
  153. no way for a script to detect this. <a class="update"
  154. href="#keymap">[no longer the case]</a></p>
  155. <p>Of course, since only a small part of the document sits in the
  156. textarea, keys like page up and ctrl-end won't do the right thing.
  157. CodeMirror is catching those events and handling them itself.</p>
  158. </section>
  159. <section id="selection">
  160. <h2>Selection</h2>
  161. <p>Getting and setting the selection range of a textarea in modern
  162. browsers is trivial—you just use the <code>selectionStart</code>
  163. and <code>selectionEnd</code> properties. On IE you have to do some
  164. insane stuff with temporary ranges and compensating for the fact that
  165. moving the selection by a 'character' will treat \r\n as a single
  166. character, but even there it is possible to build functions that
  167. reliably set and get the selection range.</p>
  168. <p>But consider this typical case: When I'm somewhere in my document,
  169. press shift, and press the up arrow, something gets selected. Then, if
  170. I, still holding shift, press the up arrow again, the top of my
  171. selection is adjusted. The selection remembers where its <em>head</em>
  172. and its <em>anchor</em> are, and moves the head when we shift-move.
  173. This is a generally accepted property of selections, and done right by
  174. every editing component built in the past twenty years.</p>
  175. <p>But not something that the browser selection APIs expose.</p>
  176. <p>Great. So when someone creates an 'upside-down' selection, the next
  177. time CodeMirror has to update the textarea, it'll re-create the
  178. selection as an 'upside-up' selection, with the anchor at the top, and
  179. the next cursor motion will behave in an unexpected way—our second
  180. up-arrow press in the example above will not do anything, since it is
  181. interpreted in exactly the same way as the first.</p>
  182. <p>No problem. We'll just, ehm, detect that the selection is
  183. upside-down (you can tell by the way it was created), and then, when
  184. an upside-down selection is present, and a cursor-moving key is
  185. pressed in combination with shift, we quickly collapse the selection
  186. in the textarea to its start, allow the key to take effect, and then
  187. combine its new head with its old anchor to get the <em>real</em>
  188. selection.</p>
  189. <p>In short, scary hacks could not be avoided entirely in CodeMirror
  190. 2.</p>
  191. <p>And, the observant reader might ask, how do you even know that a
  192. key combo is a cursor-moving combo, if you claim you support any
  193. native key bindings? Well, we don't, but we can learn. The editor
  194. keeps a set known cursor-movement combos (initialized to the
  195. predictable defaults), and updates this set when it observes that
  196. pressing a certain key had (only) the effect of moving the cursor.
  197. This, of course, doesn't work if the first time the key is used was
  198. for extending an inverted selection, but it works most of the
  199. time.</p>
  200. </section>
  201. <section id="update">
  202. <h2>Intelligent Updating</h2>
  203. <p>One thing that always comes up when you have a complicated internal
  204. state that's reflected in some user-visible external representation
  205. (in this case, the displayed code and the textarea's content) is
  206. keeping the two in sync. The naive way is to just update the display
  207. every time you change your state, but this is not only error prone
  208. (you'll forget), it also easily leads to duplicate work on big,
  209. composite operations. Then you start passing around flags indicating
  210. whether the display should be updated in an attempt to be efficient
  211. again and, well, at that point you might as well give up completely.</p>
  212. <p>I did go down that road, but then switched to a much simpler model:
  213. simply keep track of all the things that have been changed during an
  214. action, and then, only at the end, use this information to update the
  215. user-visible display.</p>
  216. <p>CodeMirror uses a concept of <em>operations</em>, which start by
  217. calling a specific set-up function that clears the state and end by
  218. calling another function that reads this state and does the required
  219. updating. Most event handlers, and all the user-visible methods that
  220. change state are wrapped like this. There's a method
  221. called <code>operation</code> that accepts a function, and returns
  222. another function that wraps the given function as an operation.</p>
  223. <p>It's trivial to extend this (as CodeMirror does) to detect nesting,
  224. and, when an operation is started inside an operation, simply
  225. increment the nesting count, and only do the updating when this count
  226. reaches zero again.</p>
  227. <p>If we have a set of changed ranges and know the currently shown
  228. range, we can (with some awkward code to deal with the fact that
  229. changes can add and remove lines, so we're dealing with a changing
  230. coordinate system) construct a map of the ranges that were left
  231. intact. We can then compare this map with the part of the document
  232. that's currently visible (based on scroll offset and editor height) to
  233. determine whether something needs to be updated.</p>
  234. <p>CodeMirror uses two update algorithms—a full refresh, where it just
  235. discards the whole part of the DOM that contains the edited text and
  236. rebuilds it, and a patch algorithm, where it uses the information
  237. about changed and intact ranges to update only the out-of-date parts
  238. of the DOM. When more than 30 percent (which is the current heuristic,
  239. might change) of the lines need to be updated, the full refresh is
  240. chosen (since it's faster to do than painstakingly finding and
  241. updating all the changed lines), in the other case it does the
  242. patching (so that, if you scroll a line or select another character,
  243. the whole screen doesn't have to be
  244. re-rendered). <span class="update">[the full-refresh
  245. algorithm was dropped, it wasn't really faster than the patching
  246. one]</span></p>
  247. <p>All updating uses <code>innerHTML</code> rather than direct DOM
  248. manipulation, since that still seems to be by far the fastest way to
  249. build documents. There's a per-line function that combines the
  250. highlighting, <a href="manual.html#markText">marking</a>, and
  251. selection info for that line into a snippet of HTML. The patch updater
  252. uses this to reset individual lines, the refresh updater builds an
  253. HTML chunk for the whole visible document at once, and then uses a
  254. single <code>innerHTML</code> update to do the refresh.</p>
  255. </section>
  256. <section id="parse">
  257. <h2>Parsers can be Simple</h2>
  258. <p>When I wrote CodeMirror 1, I
  259. thought <a href="https://codemirror.net/5/story.html#parser">interruptible
  260. parsers</a> were a hugely scary and complicated thing, and I used a
  261. bunch of heavyweight abstractions to keep this supposed complexity
  262. under control: parsers
  263. were <a href="http://bob.pythonmac.org/archives/2005/07/06/iteration-in-javascript/">iterators</a>
  264. that consumed input from another iterator, and used funny
  265. closure-resetting tricks to copy and resume themselves.</p>
  266. <p>This made for a rather nice system, in that parsers formed strictly
  267. separate modules, and could be composed in predictable ways.
  268. Unfortunately, it was quite slow (stacking three or four iterators on
  269. top of each other), and extremely intimidating to people not used to a
  270. functional programming style.</p>
  271. <p>With a few small changes, however, we can keep all those
  272. advantages, but simplify the API and make the whole thing less
  273. indirect and inefficient. CodeMirror
  274. 2's <a href="manual.html#modeapi">mode API</a> uses explicit state
  275. objects, and makes the parser/tokenizer a function that simply takes a
  276. state and a character stream abstraction, advances the stream one
  277. token, and returns the way the token should be styled. This state may
  278. be copied, optionally in a mode-defined way, in order to be able to
  279. continue a parse at a given point. Even someone who's never touched a
  280. lambda in his life can understand this approach. Additionally, far
  281. fewer objects are allocated in the course of parsing now.</p>
  282. <p>The biggest speedup comes from the fact that the parsing no longer
  283. has to touch the DOM though. In CodeMirror 1, on an older browser, you
  284. could <em>see</em> the parser work its way through the document,
  285. managing some twenty lines in each 50-millisecond time slice it got. It
  286. was reading its input from the DOM, and updating the DOM as it went
  287. along, which any experienced JavaScript programmer will immediately
  288. spot as a recipe for slowness. In CodeMirror 2, the parser usually
  289. finishes the whole document in a single 100-millisecond time slice—it
  290. manages some 1500 lines during that time on Chrome. All it has to do
  291. is munge strings, so there is no real reason for it to be slow
  292. anymore.</p>
  293. </section>
  294. <section id="summary">
  295. <h2>What Gives?</h2>
  296. <p>Given all this, what can you expect from CodeMirror 2?</p>
  297. <ul>
  298. <li><strong>Small.</strong> the base library is
  299. some <span class="update">45k</span> when minified
  300. now, <span class="update">17k</span> when gzipped. It's smaller than
  301. its own logo.</li>
  302. <li><strong>Lightweight.</strong> CodeMirror 2 initializes very
  303. quickly, and does almost no work when it is not focused. This means
  304. you can treat it almost like a textarea, have multiple instances on a
  305. page without trouble.</li>
  306. <li><strong>Huge document support.</strong> Since highlighting is
  307. really fast, and no DOM structure is being built for non-visible
  308. content, you don't have to worry about locking up your browser when a
  309. user enters a megabyte-sized document.</li>
  310. <li><strong>Extended API.</strong> Some things kept coming up in the
  311. mailing list, such as marking pieces of text or lines, which were
  312. extremely hard to do with CodeMirror 1. The new version has proper
  313. support for these built in.</li>
  314. <li><strong>Tab support.</strong> Tabs inside editable documents were,
  315. for some reason, a no-go. At least six different people announced they
  316. were going to add tab support to CodeMirror 1, none survived (I mean,
  317. none delivered a working version). CodeMirror 2 no longer removes tabs
  318. from your document.</li>
  319. <li><strong>Sane styling.</strong> <code>iframe</code> nodes aren't
  320. really known for respecting document flow. Now that an editor instance
  321. is a plain <code>div</code> element, it is much easier to size it to
  322. fit the surrounding elements. You don't even have to make it scroll if
  323. you do not <a href="../demo/resize.html">want to</a>.</li>
  324. </ul>
  325. <p>On the downside, a CodeMirror 2 instance is <em>not</em> a native
  326. editable component. Though it does its best to emulate such a
  327. component as much as possible, there is functionality that browsers
  328. just do not allow us to hook into. Doing select-all from the context
  329. menu, for example, is not currently detected by CodeMirror.</p>
  330. <p id="changes" style="margin-top: 2em;"><span style="font-weight:
  331. bold">[Updates from November 13th 2011]</span> Recently, I've made
  332. some changes to the codebase that cause some of the text above to no
  333. longer be current. I've left the text intact, but added markers at the
  334. passages that are now inaccurate. The new situation is described
  335. below.</p>
  336. </section>
  337. <section id="btree">
  338. <h2>Content Representation</h2>
  339. <p>The original implementation of CodeMirror 2 represented the
  340. document as a flat array of line objects. This worked well—splicing
  341. arrays will require the part of the array after the splice to be
  342. moved, but this is basically just a simple <code>memmove</code> of a
  343. bunch of pointers, so it is cheap even for huge documents.</p>
  344. <p>However, I recently added line wrapping and code folding (line
  345. collapsing, basically). Once lines start taking up a non-constant
  346. amount of vertical space, looking up a line by vertical position
  347. (which is needed when someone clicks the document, and to determine
  348. the visible part of the document during scrolling) can only be done
  349. with a linear scan through the whole array, summing up line heights as
  350. you go. Seeing how I've been going out of my way to make big documents
  351. fast, this is not acceptable.</p>
  352. <p>The new representation is based on a B-tree. The leaves of the tree
  353. contain arrays of line objects, with a fixed minimum and maximum size,
  354. and the non-leaf nodes simply hold arrays of child nodes. Each node
  355. stores both the amount of lines that live below them and the vertical
  356. space taken up by these lines. This allows the tree to be indexed both
  357. by line number and by vertical position, and all access has
  358. logarithmic complexity in relation to the document size.</p>
  359. <p>I gave line objects and tree nodes parent pointers, to the node
  360. above them. When a line has to update its height, it can simply walk
  361. these pointers to the top of the tree, adding or subtracting the
  362. difference in height from each node it encounters. The parent pointers
  363. also make it cheaper (in complexity terms, the difference is probably
  364. tiny in normal-sized documents) to find the current line number when
  365. given a line object. In the old approach, the whole document array had
  366. to be searched. Now, we can just walk up the tree and count the sizes
  367. of the nodes coming before us at each level.</p>
  368. <p>I chose B-trees, not regular binary trees, mostly because they
  369. allow for very fast bulk insertions and deletions. When there is a big
  370. change to a document, it typically involves adding, deleting, or
  371. replacing a chunk of subsequent lines. In a regular balanced tree, all
  372. these inserts or deletes would have to be done separately, which could
  373. be really expensive. In a B-tree, to insert a chunk, you just walk
  374. down the tree once to find where it should go, insert them all in one
  375. shot, and then break up the node if needed. This breaking up might
  376. involve breaking up nodes further up, but only requires a single pass
  377. back up the tree. For deletion, I'm somewhat lax in keeping things
  378. balanced—I just collapse nodes into a leaf when their child count goes
  379. below a given number. This means that there are some weird editing
  380. patterns that may result in a seriously unbalanced tree, but even such
  381. an unbalanced tree will perform well, unless you spend a day making
  382. strangely repeating edits to a really big document.</p>
  383. </section>
  384. <section id="keymap">
  385. <h2>Keymaps</h2>
  386. <p><a href="#approach">Above</a>, I claimed that directly catching key
  387. events for things like cursor movement is impractical because it
  388. requires some browser-specific kludges. I then proceeded to explain
  389. some awful <a href="#selection">hacks</a> that were needed to make it
  390. possible for the selection changes to be detected through the
  391. textarea. In fact, the second hack is about as bad as the first.</p>
  392. <p>On top of that, in the presence of user-configurable tab sizes and
  393. collapsed and wrapped lines, lining up cursor movement in the textarea
  394. with what's visible on the screen becomes a nightmare. Thus, I've
  395. decided to move to a model where the textarea's selection is no longer
  396. depended on.</p>
  397. <p>So I moved to a model where all cursor movement is handled by my
  398. own code. This adds support for a goal column, proper interaction of
  399. cursor movement with collapsed lines, and makes it possible for
  400. vertical movement to move through wrapped lines properly, instead of
  401. just treating them like non-wrapped lines.</p>
  402. <p>The key event handlers now translate the key event into a string,
  403. something like <code>Ctrl-Home</code> or <code>Shift-Cmd-R</code>, and
  404. use that string to look up an action to perform. To make keybinding
  405. customizable, this lookup goes through
  406. a <a href="manual.html#option_keyMap">table</a>, using a scheme that
  407. allows such tables to be chained together (for example, the default
  408. Mac bindings fall through to a table named 'emacsy', which defines
  409. basic Emacs-style bindings like <code>Ctrl-F</code>, and which is also
  410. used by the custom Emacs bindings).</p>
  411. <p>A new
  412. option <a href="manual.html#option_extraKeys"><code>extraKeys</code></a>
  413. allows ad-hoc keybindings to be defined in a much nicer way than what
  414. was possible with the
  415. old <a href="manual.html#option_onKeyEvent"><code>onKeyEvent</code></a>
  416. callback. You simply provide an object mapping key identifiers to
  417. functions, instead of painstakingly looking at raw key events.</p>
  418. <p>Built-in commands map to strings, rather than functions, for
  419. example <code>"goLineUp"</code> is the default action bound to the up
  420. arrow key. This allows new keymaps to refer to them without
  421. duplicating any code. New commands can be defined by assigning to
  422. the <code>CodeMirror.commands</code> object, which maps such commands
  423. to functions.</p>
  424. <p>The hidden textarea now only holds the current selection, with no
  425. extra characters around it. This has a nice advantage: polling for
  426. input becomes much, much faster. If there's a big selection, this text
  427. does not have to be read from the textarea every time—when we poll,
  428. just noticing that something is still selected is enough to tell us
  429. that no new text was typed.</p>
  430. <p>The reason that cheap polling is important is that many browsers do
  431. not fire useful events on IME (input method engine) input, which is
  432. the thing where people inputting a language like Japanese or Chinese
  433. use multiple keystrokes to create a character or sequence of
  434. characters. Most modern browsers fire <code>input</code> when the
  435. composing is finished, but many don't fire anything when the character
  436. is updated <em>during</em> composition. So we poll, whenever the
  437. editor is focused, to provide immediate updates of the display.</p>
  438. </section>
  439. </article>