aboutsummaryrefslogtreecommitdiffstats
path: root/infrastructure/ace/blog.txt
blob: 0b095a2987518673ff68358aa3b354e15dd80158 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
ACE.js: In-Browser Code Editing Finally Solved

You may have seen them around -- "in-browser code editors" are text
editors that run on the web in your browser, supporting editing,
syntax highlighting, indentation, and other features familiar from a
desktop editor/IDE like Emacs, Eclipse, or TextMate.  Compared to
desktop editors, the in-browser knock-offs I've seen leave me quite
dissatisfied.  I can deal with the lack of power-user features, but
just in terms of basic usability, these web-based editors are
sputtering at the starting line.  On the whole, they feel unresponsive
on small files, they slow to a crawl on large files, and they tend to
have unaddressed quirks, artifacts, and differences from native
appearance and behavior.  Anyway, that's enough negativity; I have a
lot of respect for the authors and their hard work, and in writing ACE
I'm standing on their shoulders.  But I was curious to see if I could
take AppJet's editor to the next level and approach the speed and
robustness of a desktop editor.  After a few months of work, I'm proud
to declare success.

Here's a walk-through of ACE.js, our new in-browser code editor.
ACE.js is a self-contained, cross-browser JavaScript file that allows
a web page to embed scrolling panes of editable code.  Typing text is
snappy and there are no visual artifacts or clutter.
Syntax-highlighting happens live, as you type.  Amazingly, operations
scale up well with document size.  You can paste in 1,000 lines of
text, or more, and after a second or two of thought, the editor is
fully responsive again.  While you keep typing, it unobtrusively
finishes syntax-highlighting the whole buffer in a matter of seconds.
Local edits to the text are just as fast on long documents as on short
ones.  Changes that require large-scale re-highlighting, like opening
or closing a multi-line comment, affect the text in view almost
instantaneously, and then ACE.js quitely goes to work on the rest of
the document over the next few seconds without holding you up.  ACE.js
supports advanced features like Emacs-style unlimited undo, flashing
matching parentheses as you type them or move across them, and basic
auto-indentation.  Full functionality is supported across Internet
Explorer 6+, Firefox 2+, Safari 3+, and Camino 1.5+.

I've emphasized "scalability" a lot so far, by which I mean efficiency
and responsiveness in the face of large documents, because that's the
aspect of ACE that so clearly sets it apart.  But that's only one of
three "axes" that the ultimate editor needs to excel on.

The second is what I'll call "nativity", meaning how close the editor
comes to the gold standard of the desktop editor experience.  This
includes responsiveness (some in-browser editors have sluggish typing
even on small documents), but also things like undo, copy/paste,
selection, arrow-keys, shift-arrow-keys, different input methods and
devices, and so on.

The third axis is "flexibility".  How easy is it to add features?  To
support more languages for the incremental highlighting, different
undo models, different indentation schemes, macros that expand as you
type, auto-complete, go-to-line by number, and whatever else you can
imagine.

Why am I lecturing you on these theoretical principles?  Well, there's
an interesting insight that explains a lot about the existing crop of
editors, and it isn't one you'd hit upon without spending a lot of
time in the trenches (yes, specifically the trenches of writing an
in-browser code editor).  Based on the hodge-podge of facilities that
browsers happen to make available, you basically end up with the
following slogan: "scalability, nativity, flexibility, pick any two".
Based on this, we can classify editors into three types, which I'll
describe briefly from the point of view of someone writing an editor
(like me); if you're not familiar with browser scripting, hopefully
this won't be too hard to get through.

Type I editors sacrifice scalability.  The typical implementation is a
text-area to handle the typing, covered by a block of colored text to
handle the display.  You (the person making the editor) get a boost in
nativity at the start, because you inherit the browser's text
handling, all the stuff I mentioned above, like undo and copy/paste.
The degree of flexibility is also good, because you can style the
colored text independently of the editing in the textarea, and you can
manipulate the textarea easily, such as by assigning a new content
string.  Unfortunately, this scales quite badly with document size.
The good Type I editors are slick and responsive, but pretty limited
in how large files can get.

Type II editors sacrifice nativity.  This is the kind of editor where
you manually manipulate document (DOM) nodes as the user types.  You
have to re-implement the desktop's native editing behavior from
scratch, first putting in support for typing and selecting text (both
by mouse and keyboard), and hopefully eventually supporting undo and
copy/paste with other programs (through some elaborate hack).  This
type is attractive because it offers unlimited flexibility, and
theoretical scalability.  In practice, you end up with mostly
flexibility, because getting the nativity right is such a huge task,
and scalability takes a back seat to general responsiveness.  Type II
editors are fancy but feel slow and quirky.

Type III editors, as you've probably guessed by now, are the category
ACE falls into, and are the ones that sacrifice flexibility, the third
axis, though in fact they make you work for all three axes.  The crazy
idea here, which seems to have originated with Dutch programmer Marijn
Haverbeke, is to take advantage of a browser feature called "design
mode" (or "content editable"), a mode which allows the user to
directly edit an HTML document.  This feature has quietly been added
to all major browsers over time.  In fact, it's what GMail uses to let
users compose rich-text e-mail.  The advantages of basing an editor on
a design-mode buffer are that such a buffer has a full DOM (document
model), which allows arbitrary styling and swapping of parts of the
document, and that native editing operations (selection, copy/paste)
are mapped by the browser onto operations on the DOM.

What's the downside of a Type III editor?  It's a nightmare to
implement!  GMail skirts the issue by only using hard-coded browser
commands like "make the selection bold" or "insert unordered list".
What happens if you stray from this set of basic commands, and access
that alluring DOM directly, without careful preparation?  Disaster!
Building a robust, extensible editor on top of design mode is kind of
like landing a man safely on the moon, where you design the
spacecraft, the mission control computer, and the pressurized suit.
You're constantly trying to insulate yourself from a hostile
environment.

More specifically, any change your editor makes to the design mode DOM
causes the browser to instantly lose track of the selection range or
insertion point if any, the undo history, and basically any other
useful editing state.  (This can be worked around with a lot of
selection manipulation, though IE in particular lacks even a way to
access the current selection relative to the DOM.)  A simple edit like
the user pressing the "enter" key to start a new line may cause the
browser to insert a line break tag (BR), or split a block element into
two, whatever it wishes.  Pasted text is converted into HTML, through
a somewhat arbitrary process, and inserted into the document.  In
fact, the DOM is constantly being modified by the browser in response
to user actions (like typing), and you have to peek at it and
efficiently make sense of it, determine what's changed, incorporate it
into your representation, make any of your own changes (e.g. indent a
line), modify the DOM to display your changes to the user, and have it
all look like nothing special happened.  A more Draconian policy for
dealing with the browser, like trapping all mouse and key events,
would just leave you with a Type II editor.  The key is to treat the
DOM as a hugely complicated I/O device between you and the browser,
and carefully make rules to constrain it.

You also deal with the fact that design mode's "native" behavior is
HTML-oriented, not text-oriented, and the fact that it tends to have a
lot of quirks and bugs.  You don't get scalability for free, either,
you need some clever data structures.  For example, determining the
absolute line number of the blinking insertion point is an
order-of-document-size operation without the aid of efficient data
structures that can be maintained as the document changes.

The plus side is that once you've systematically beat design mode into
submission, you can have an unmatched degree of scalability and
nativity.  From there, it's just a matter of keeping your layers of
abstraction scalable, and being aware of "nativity"-style constraints
(like responsiveness and events), and you can build up to flexibility,
the crown jewel.  The reward is how easy it is to add new editor
features!

Speaking of which, here is a list of current ACE features.  Some of
them are small and just required recognizing their benefit, while
others are more significant.  Some features, like "Emacs-style undo"
and "highlight matching parentheses", are significant despite having
been implemented in a couple hours each, and are signs of what's to
come in the future of ACE.

General
  - Numbered lines
  - Correct vert/horiz scroll-bar behavior
  - Handles large documents (thousands of lines, but see "quirks")
Browser support
  - Internet Explorer 6+, Firefox 2+, Safari 3+, Camino 1.5+
  - Text size scales with page when changed by user
Syntax highlighting
  - Correct JavaScript syntax (including regular expression subtleties)
  - Extensible for other programming languages
  - Incremental rehighlighting with minimal recalculation for each edit
  - Highlight live as you type
  - Moves highlighting task to background if not finished right away
Editing
  - Emacs-style unlimited undo
  - Native copy/paste, within editor and with other programs
  - Highlight matching (or mis-matched) parentheses, brackets, and braces
  - Basic auto-indentation
  - Multi-stroke international characters on Mac
Interface
  - Import/export text string
  - Toggle editor between ACE and textarea
  - Resize, focus, register key event handlers
  - Multiple ACE editors allowed per page
  - Packaged in one JavaScript file
  - Can be created inside an iframe, any domain, any nesting
Future
  - Super-extensible
  - More languages?
  - Smarter auto-indentation?
  - Indent/unindent selection?
  - Auto-complete?
  - Macros?
  - User extensibility, a la Emacs??
Bugs/Quirks
  - Firefox 2/3: Document height and width are limited to 32767 pixels
      (about 2,045 lines of text at default text size) due to Firefox bug,
      not scheduled for resolution for FF3 launch
  
======

>> TO ADD:
  - link to demo
  - what to {say,ask} about {interest, use, licensing, source}?



















ACE.js: In-Browser Code Editing Finally Solved

You may have seen them around -- "in-browser code editors" are text
editors that run over the web in a browser, supporting editing, syntax
highlighting, indentation, and other features familiar from a desktop
editor/IDE like Emacs, Eclipse, or TextMate.  Compared to desktop
editors, the in-browser knock-offs that I've seen leave me very
dissatisfied.  I can deal without the power-user features, but just in
terms of basic usability, these web-based editors are sputtering at
the starting line.  On the whole, they feel unresponsive on small
files, they slow to a crawl on large files, and they tend to have
unaddressed quirks, artifacts, and differences from native appearance
and behavior.  Anyway, that's enough negativity; I have a lot of
respect for the authors and their hard work, and in writing ACE I'm
standing on their shoulders.  But you see, I tend to approach projects
like this with super-high standards, dangerously so even.  Luckily,
after months of work here at AppJet, there's a happy ending.

[[="I tend to approach projects with super-high standards" is a bit awkward - sounds like tooting your own horn too much for my taste=]]

Here's a walk-through of ACE.js, our new in-browser code editor.  It's 
[[="it" the walkthrough? or "it" the browser?=]]
written in self-contained, cross-browser JavaScript that can be
dropped into any web page.  It looks and feels a lot like a "native"
editor, meaning typing is fast and there are no artifacts or clutter.
It does syntax-highlighting live, as you type.  Amazingly, it scales
up with document size.  You can paste in 1,000 lines of text, or more,
and after a second or two of thought, the editor is fully responsive
again.  While you keep typing, it unobtrusively finishes
syntax-highlighting the whole buffer in a matter of seconds.  Local
edits to the text are just as fast on long documents as on short ones.
Changes that require large-scale re-highlighting, like opening or
closing a multi-line comment, affect the text in view almost
instantaneously, and then ACE quitely goes to work on the rest of the
document over the next few seconds without holding you up.  Advanced
features include Emacs-style unlimited undo, flashing matching
parentheses as you type them or move across them, and basic
auto-indentation.  Full functionality is supported on Internet
Explorer 6+, Firefox 2+, Safari 3+, and Camino 1.5+.

I've emphasized "scalability" a lot so far, by which I mean efficiency
and responsiveness in the face of large documents, because that's the
aspect of ACE that so clearly sets it apart.  But that's only one of
three "axes" that the ultimate editor needs to excel on.

The second is what I'll call "nativity", meaning how close the editor
comes to the gold standard of the desktop editor experience.  This
includes responsiveness (some in-browser editors have sluggish typing
even on small documents), but also things like undo, copy/paste,
selection, arrow-keys, shift-arrow-keys, different input methods and
devices, and so on.

The third axis is "flexibility".  How easy is it to add features?  To
support more languages for the incremental highlighting, different
undo models, different indentation schemes, macros that expand as you
type, auto-complete, go-to-line by number, and whatever else you can
imagine.

Why am I lecturing you on these theoretical principles?  Well, there's
an interesting insight that explains a lot about the existing crop of
editors, and it isn't one you'd hit upon without spending a lot of
time in the trenches (yes, specifically the trenches of writing an
in-browser code editor).  Based on the hodge-podge of facilities that
browsers happen to make available, you basically end up with the
following slogan: "scalability, nativity, flexibility, pick any two".
Based on this, we can classify editors into three types, which I'll
describe briefly from the point of view of someone writing an editor
(like me); if you're not familiar with browser scripting, hopefully
this won't be too hard to get through.

[[= I'm not a huge fan of "type I-II-III" nomenclature. It's totally opaque. And in this case, they don't even form a continuum, except for type III being better than I and II. =]]

Type I editors sacrifice scalability.  The typical implementation is a
text-area to handle the typing, covered by a block of colored text to
handle the display.  You (the person making the editor) get a boost in
nativity at the start, because you inherit the browser's text
handling, all the stuff I mentioned above, like undo and copy/paste.
The degree of flexibility is also good, because you can style the
colored text independently of the editing in the textarea, and you can
manipulate the textarea easily, such as by assigning a new content
string.  Unfortunately, this scales quite badly with document size.
The good Type I editors are slick and responsive, but pretty limited
in how large files can get.

Type II editors sacrifice nativity.  This is the kind of editor where
you manually manipulate document (DOM) nodes as the user types.  You
have to re-implement the desktop's native editing behavior from
scratch, first putting in support for typing and selecting text (both
by mouse and keyboard), and hopefully eventually supporting undo and
copy/paste with other programs (through some elaborate hack).  This
type is attractive because it offers unlimited flexibility, and
theoretical scalability.  In practice, you end up with mostly
flexibility, because getting the nativity right is such a huge task,
and scalability takes a back seat to general responsiveness.  Type II
editors are fancy but feel slow and quirky.

Type III editors, as you've probably guessed by now, are the category
ACE falls into, and are the ones that sacrifice flexibility, the third
axis, though in fact they make you work for all three axes.  The crazy
idea here, which seems to have originated with Dutch programmer Marijn
Haverbeke, is to take advantage of a browser feature called "design
mode" (or "content editable"), a mode which allows the user to
directly edit an HTML document.  This feature has quietly been added
to all major browsers over time.  In fact, it's what GMail uses to let
users compose rich-text e-mail.  The advantages of basing an editor on
a design-mode buffer are that such a buffer has a full DOM (document
model), which allows arbitrary styling and swapping of parts of the
document, and that native editing operations (selection, copy/paste)
are mapped by the browser onto operations on the DOM.

[[= It's not clear how Type III editors sacrifice flexibility =]]

What's the downside of a Type III editor?  It's a nightmare to
implement!  GMail skirts the issue by only using hard-coded browser
commands like "make the selection bold" or "insert unordered list".
What happens if you stray from this set of basic commands, and access
that alluring DOM directly, without careful preparation?  Disaster!
Building a robust, extensible editor on top of design mode is kind of
like landing a man safely on the moon, where you design the
spacecraft, the mission control computer, and the pressurized suit.
You're constantly trying to insulate yourself from a hostile
environment.

More specifically, any change your editor makes to the design mode DOM
causes the browser to instantly lose track of the selection range or
insertion point if any, the undo history, and basically any other
useful editing state.  (This can be worked around with a lot of
selection manipulation, though IE in particular lacks even a way to
access the current selection relative to the DOM.)  A simple edit like
the user pressing the "enter" key to start a new line may cause the
browser to insert a line break tag (BR), or split a block element into
two, whatever it wishes.  Pasted text is converted into HTML, through
a somewhat arbitrary process, and inserted into the document.  In
fact, the DOM is constantly being modified by the browser in response
to user actions (like typing), and you have to peek at it and
efficiently make sense of it, determine what's changed, incorporate it
into your representation, make any of your own changes (e.g. indent a
line), modify the DOM to display your changes to the user, and have it
all look like nothing special happened.  A more Draconian policy for
dealing with the browser, like trapping all mouse and key events,
would just leave you with a Type II editor.  The key is to treat the
DOM as a hugely complicated I/O device between you and the browser,
and carefully make rules to constrain it. [[= Genius analgy, IMO. :) =]]

You also deal with the fact that design mode's "native" behavior is
HTML-oriented, not text-oriented, and the fact that it tends to have a
lot of quirks and bugs.  You don't get scalability for free, either,
you need some clever data structures.  For example, determining the
absolute line number of the blinking insertion point is an
order-of-document-size operation without the aid of efficient data
structures that can be maintained as the document changes.

The plus side is that once you've systematically beat design mode into
submission, you can have an unmatched degree of scalability and
nativity.  From there, it's just a matter of keeping your layers of
abstraction scalable, and being aware of "nativity"-style constraints
(like responsiveness and events), and you can build up to flexibility,
the crown jewel.  The reward is how easy it is to add new editor
features! [[= I'm kind of confused here. You said ACE is a type III editor, sacrificing flexibility, but now you say that ACE *has* flexibility. Huh? =]]

Speaking of which, here is a list of current ACE features.  Some of
them are small and just required recognizing their benefit, while
others are more significant. [[= awkward sentence =]] Some features, like "Emacs-style undo"
and "highlight matching parentheses", are significant despite having
been implemented in a couple hours each, and are signs of what's to
come in the future of ACE.