Skip to content

Commit 8232c40

Browse files
committed
Improve mode writing docs a little, document line- and line-background- tokens
1 parent 51a45cc commit 8232c40

File tree

1 file changed

+65
-49
lines changed

1 file changed

+65
-49
lines changed

doc/manual.html

Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,15 +2269,15 @@ <h2>Writing CodeMirror Modes</h2>
22692269
advanced modes can also handle indentation for the language.</p>
22702270

22712271
<p id="defineMode">The mode script should
2272-
call <code>CodeMirror.defineMode</code> to register itself with
2273-
CodeMirror. This function takes two arguments. The first should be
2274-
the name of the mode, for which you should use a lowercase string,
2275-
preferably one that is also the name of the files that define the
2276-
mode (i.e. <code>"xml"</code> is defined in <code>xml.js</code>). The
2277-
second argument should be a function that, given a CodeMirror
2278-
configuration object (the thing passed to
2279-
the <code>CodeMirror</code> function) and an optional mode
2280-
configuration object (as in
2272+
call <code><strong>CodeMirror.defineMode</strong></code> to
2273+
register itself with CodeMirror. This function takes two
2274+
arguments. The first should be the name of the mode, for which you
2275+
should use a lowercase string, preferably one that is also the
2276+
name of the files that define the mode (i.e. <code>"xml"</code> is
2277+
defined in <code>xml.js</code>). The second argument should be a
2278+
function that, given a CodeMirror configuration object (the thing
2279+
passed to the <code>CodeMirror</code> function) and an optional
2280+
mode configuration object (as in
22812281
the <a href="#option_mode"><code>mode</code></a> option), returns
22822282
a mode object.</p>
22832283

@@ -2297,30 +2297,43 @@ <h2>Writing CodeMirror Modes</h2>
22972297
reading a token, and which can be mutated by the tokenizer.</p>
22982298

22992299
<p id="startState">Modes that use a state must define
2300-
a <code>startState</code> method on their mode object. This is a
2301-
function of no arguments that produces a state object to be used
2302-
at the start of a document.</p>
2300+
a <code><strong>startState</strong></code> method on their mode
2301+
object. This is a function of no arguments that produces a state
2302+
object to be used at the start of a document.</p>
23032303

23042304
<p id="token">The most important part of a mode object is
2305-
its <code>token(stream, state)</code> method. All modes must
2306-
define this method. It should read one token from the stream it is
2307-
given as an argument, optionally update its state, and return a
2308-
style string, or <code>null</code> for tokens that do not have to
2309-
be styled. For your styles, you are encouraged to use the
2310-
'standard' names defined in the themes (without
2305+
its <code><strong>token</strong>(stream, state)</code> method. All
2306+
modes must define this method. It should read one token from the
2307+
stream it is given as an argument, optionally update its state,
2308+
and return a style string, or <code>null</code> for tokens that do
2309+
not have to be styled. For your styles, you are encouraged to use
2310+
the 'standard' names defined in the themes (without
23112311
the <code>cm-</code> prefix). If that fails, it is also possible
23122312
to come up with your own and write your own CSS theme file.<p>
23132313

2314+
<p id="token_style_line">A typical token string would
2315+
be <code>"variable"</code> or <code>"comment"</code>. Multiple
2316+
styles can be returned (separated by spaces), for
2317+
example <code>"string error"</code> for a thing that looks like a
2318+
string but is invalid somehow (say, missing its closing quote).
2319+
When a style is prefixed by <code>"line-"</code>
2320+
or <code>"line-background-"</code>, the style will be applied to
2321+
the whole line, analogous to what
2322+
the <a href="#addLineClass"><code>addLineClass</code></a> method
2323+
does—styling the <code>"text"</code> in the simple case, and
2324+
the <code>"background"</code> element
2325+
when <code>"line-background-"</code> is prefixed.</p>
2326+
23142327
<p id="StringStream">The stream object that's passed
23152328
to <code>token</code> encapsulates a line of code (tokens may
23162329
never span lines) and our current position in that line. It has
23172330
the following API:</p>
23182331

23192332
<dl>
2320-
<dt><code><strong></strong><strong>eol</strong>() → boolean</code></dt>
2333+
<dt><code><strong>eol</strong>() → boolean</code></dt>
23212334
<dd>Returns true only if the stream is at the end of the
23222335
line.</dd>
2323-
<dt><code><strong></strong><strong>sol</strong>() → boolean</code></dt>
2336+
<dt><code><strong>sol</strong>() → boolean</code></dt>
23242337
<dd>Returns true only if the stream is at the start of the
23252338
line.</dd>
23262339

@@ -2383,9 +2396,10 @@ <h2>Writing CodeMirror Modes</h2>
23832396

23842397
<p id="blankLine">By default, blank lines are simply skipped when
23852398
tokenizing a document. For languages that have significant blank
2386-
lines, you can define a <code>blankLine(state)</code> method on
2387-
your mode that will get called whenever a blank line is passed
2388-
over, so that it can update the parser state.</p>
2399+
lines, you can define
2400+
a <code><strong>blankLine</strong>(state)</code> method on your
2401+
mode that will get called whenever a blank line is passed over, so
2402+
that it can update the parser state.</p>
23892403

23902404
<p id="copyState">Because state object are mutated, and CodeMirror
23912405
needs to keep valid versions of a state around so that it can
@@ -2395,17 +2409,17 @@ <h2>Writing CodeMirror Modes</h2>
23952409
which hold arrays get a copy of these arrays (since arrays tend to
23962410
be used as mutable stacks). When this is not correct, for example
23972411
because a mode mutates non-array properties of its state object, a
2398-
mode object should define a <code>copyState</code> method,
2399-
which is given a state and should return a safe copy of that
2400-
state.</p>
2412+
mode object should define
2413+
a <code><strong>copyState</strong></code> method, which is given a
2414+
state and should return a safe copy of that state.</p>
24012415

24022416
<p id="indent">If you want your mode to provide smart indentation
24032417
(through the <a href="#indentLine"><code>indentLine</code></a>
24042418
method and the <code>indentAuto</code>
24052419
and <code>newlineAndIndent</code> commands, to which keys can be
24062420
<a href="#option_extraKeys">bound</a>), you must define
2407-
an <code>indent(state, textAfter)</code> method on your mode
2408-
object.</p>
2421+
an <code><strong>indent</strong>(state, textAfter)</code> method
2422+
on your mode object.</p>
24092423

24102424
<p>The indentation method should inspect the given state object,
24112425
and optionally the <code>textAfter</code> string, which contains
@@ -2418,8 +2432,9 @@ <h2>Writing CodeMirror Modes</h2>
24182432

24192433
<p id="mode_comment">To work well with
24202434
the <a href="#addon_comment">commenting addon</a>, a mode may
2421-
define <code>lineComment</code> (string that starts a line
2422-
comment), <code>blockCommentStart</code>, <code>blockCommentEnd</code>
2435+
define <code><strong>lineComment</strong></code> (string that
2436+
starts a line
2437+
comment), <code><strong>blockCommentStart</strong></code>, <code><strong>blockCommentEnd</strong></code>
24232438
(strings that start and end block comments),
24242439
and <code>blockCommentLead</code> (a string to put at the start of
24252440
continued lines in a block comment). All of these are
@@ -2455,13 +2470,14 @@ <h2>Writing CodeMirror Modes</h2>
24552470
state.</p>
24562471

24572472
<p id="innerMode">In a nested mode, it is recommended to add an
2458-
extra method, <code>innerMode</code> which, given a state object,
2459-
returns a <code>{state, mode}</code> object with the inner mode
2460-
and its state for the current position. These are used by utility
2461-
scripts such as the <a href="#addon_closetag">tag closer</a> to
2462-
get context information. Use the <code>CodeMirror.innerMode</code>
2463-
helper function to, starting from a mode and a state, recursively
2464-
walk down to the innermost mode and state.</p>
2473+
extra method, <code><strong>innerMode</strong></code> which, given
2474+
a state object, returns a <code>{state, mode}</code> object with
2475+
the inner mode and its state for the current position. These are
2476+
used by utility scripts such as the <a href="#addon_closetag">tag
2477+
closer</a> to get context information. Use
2478+
the <code>CodeMirror.innerMode</code> helper function to, starting
2479+
from a mode and a state, recursively walk down to the innermost
2480+
mode and state.</p>
24652481

24662482
<p>To make indentation work properly in a nested parser, it is
24672483
advisable to give the <code>startState</code> method of modes that
@@ -2470,25 +2486,25 @@ <h2>Writing CodeMirror Modes</h2>
24702486
parser do this, for example, to allow JavaScript and CSS code
24712487
inside the mixed-mode HTML mode to be properly indented.</p>
24722488

2473-
<p>It is possible, and encouraged, to associate your mode, or a
2474-
certain configuration of your mode, with
2489+
<p id="defineMIME">It is possible, and encouraged, to associate
2490+
your mode, or a certain configuration of your mode, with
24752491
a <a href="http://en.wikipedia.org/wiki/MIME">MIME</a> type. For
24762492
example, the JavaScript mode associates itself
24772493
with <code>text/javascript</code>, and its JSON variant
24782494
with <code>application/json</code>. To do this,
2479-
call <code>CodeMirror.defineMIME(mime, modeSpec)</code>,
2480-
where <code>modeSpec</code> can be a string or object specifying a
2481-
mode, as in the <a href="#option_mode"><code>mode</code></a>
2482-
option.</p>
2495+
call <code><strong>CodeMirror.defineMIME</strong>(mime,
2496+
modeSpec)</code>, where <code>modeSpec</code> can be a string or
2497+
object specifying a mode, as in
2498+
the <a href="#option_mode"><code>mode</code></a> option.</p>
24832499

24842500
<p id="extendMode">Sometimes, it is useful to add or override mode
24852501
object properties from external code.
2486-
The <code>CodeMirror.extendMode</code> can be used to add
2487-
properties to mode objects produced for a specific mode. Its first
2488-
argument is the name of the mode, its second an object that
2489-
specifies the properties that should be added. This is mostly
2490-
useful to add utilities that can later be looked
2491-
up through <a href="#getMode"><code>getMode</code></a>.</p>
2502+
The <code><strong>CodeMirror.extendMode</strong></code> function
2503+
can be used to add properties to mode objects produced for a
2504+
specific mode. Its first argument is the name of the mode, its
2505+
second an object that specifies the properties that should be
2506+
added. This is mostly useful to add utilities that can later be
2507+
looked up through <a href="#getMode"><code>getMode</code></a>.</p>
24922508
</section>
24932509

24942510
</article>

0 commit comments

Comments
 (0)