Verified Commit 8cbc9d4a authored by Camil Staps's avatar Camil Staps 🚀

Improved indentation tracking

parent 9528d598
...@@ -19,6 +19,16 @@ function highlight(lex, istr, start) { ...@@ -19,6 +19,16 @@ function highlight(lex, istr, start) {
if (typeof start != 'undefined') if (typeof start != 'undefined')
state = start; state = start;
var equal_classes = function(a, b) {
if (a == b)
return a;
if (a == '__reset__arguments__' && b == 'whitespace')
return a;
if (a == 'whitespace' && b == '__reset__arguments__')
return b;
return null;
};
while (true) { while (true) {
var found = false; var found = false;
for (var i in lex[state]) { for (var i in lex[state]) {
...@@ -29,17 +39,26 @@ function highlight(lex, istr, start) { ...@@ -29,17 +39,26 @@ function highlight(lex, istr, start) {
var parts = patt.exec(istr); var parts = patt.exec(istr);
var j = 0; var j = 0;
if (typeof clss == 'function') { if (typeof clss == 'function') {
var result = clss(parts); var state_list = state_stack.slice();
state_list.push(state);
var result = clss(parts, state_list);
clss = result[0]; clss = result[0];
if (result.length > 1) if (result.length > 1)
new_states = result[1]; new_states = result[1];
} }
for (var k in clss) { for (var k in clss) {
j++; j++;
if (out.length > 0 && out[out.length-1]['class'] == clss[k]) if (out.length > 0) {
out[out.length-1]['str'] += parts[j]; var equal = equal_classes(out[out.length-1]['class'], clss[k]);
else if (equal != null) {
out[out.length-1]['class'] = equal;
out[out.length-1]['str'] += parts[j];
} else {
out.push({'class': clss[k], 'str': parts[j]});
}
} else {
out.push({'class': clss[k], 'str': parts[j]}); out.push({'class': clss[k], 'str': parts[j]});
}
} }
istr = parts[j+1]; istr = parts[j+1];
...@@ -118,7 +137,7 @@ function highlightClean(func, callback, start) { ...@@ -118,7 +137,7 @@ function highlightClean(func, callback, start) {
}; };
var paren_depth = 0; var paren_depth = 0;
var indent = 0; var indents = [[0, 'start']];
var i = 0; var i = 0;
return highlightToHTML({ return highlightToHTML({
...@@ -127,18 +146,38 @@ function highlightClean(func, callback, start) { ...@@ -127,18 +146,38 @@ function highlightClean(func, callback, start) {
[/(\/\*)/, ['comment'], 'comment'], [/(\/\*)/, ['comment'], 'comment'],
], ],
_append: [ _append: [
[/(\n[^\n\S]*)/, function(matches) { [/(\n[^\n\S]*)/, function(matches, states) {
var whitespace = matches[1]; var whitespace = matches[1];
var rest = matches[2]; var rest = matches[2];
var old_indent = indent; var indent = whitespace.substring(1).replace('\t',' ').length;
indent = whitespace.substring(1).replace('\t',' ').length;
if (indent < old_indent || indent == 0) { var cls = 'whitespace';
var cls = 'whitespace';
if (indent == 0) {
if (!rest.match(/^[\s\n]*where/)) if (!rest.match(/^[\s\n]*where/))
cls = '__reset__arguments__'; cls = '__reset__arguments__';
return [[cls], 'reset:start']; return [[cls], 'reset:start'];
} else { }
return [['whitespace']];
var tried_reset_arguments = false;
while (1) {
if (indent > indents[0][0]) {
indents.unshift([indent, states.join(';')]);
return [['whitespace']];
}
if (indent == indents[0][0]) {
indents[0][1] = states.join(';');
return [[cls], 'reset:' + indents[0][1]];
}
indents.shift();
if (!tried_reset_arguments) {
if (!rest.match(/^[\s\n]*where/))
cls = '__reset__arguments__';
tried_reset_arguments = true;
}
} }
}], }],
[/([^\n\S]+)/, ['whitespace']], [/([^\n\S]+)/, ['whitespace']],
......
...@@ -24,8 +24,7 @@ whitespace ...@@ -24,8 +24,7 @@ whitespace
typevar m typevar m
whitespace whitespace
keyword where keyword where
whitespace \n whitespace \n\n
whitespace \n
funcname bind funcname bind
whitespace whitespace
punctuation :: punctuation ::
......
...@@ -29,8 +29,7 @@ whitespace ...@@ -29,8 +29,7 @@ whitespace
typevar s typevar s
whitespace whitespace
keyword where keyword where
whitespace \n whitespace \n\n
whitespace \n
funcname textSize funcname textSize
whitespace whitespace
punctuation :: punctuation ::
......
...@@ -214,8 +214,7 @@ whitespace ...@@ -214,8 +214,7 @@ whitespace
argumentinrhs yin argumentinrhs yin
whitespace whitespace
argumentinrhs yout argumentinrhs yout
whitespace \n whitespace \n\n
whitespace \n
funcname takexnext funcname takexnext
whitespace whitespace
punctuation [| punctuation [|
...@@ -245,8 +244,7 @@ literal literal-bool False ...@@ -245,8 +244,7 @@ literal literal-bool False
punctuation ,[|], punctuation ,[|],
funcname funcname-onlyused accu funcname funcname-onlyused accu
punctuation ) punctuation )
whitespace \n whitespace \n\n
whitespace \n
funcname takeynext funcname takeynext
whitespace whitespace
punctuation [| punctuation [|
...@@ -279,8 +277,7 @@ literal literal-bool False ...@@ -279,8 +277,7 @@ literal literal-bool False
punctuation ,[|], punctuation ,[|],
funcname funcname-onlyused accu funcname funcname-onlyused accu
punctuation ) punctuation )
whitespace \n whitespace \n\n
whitespace \n
funcname shift funcname shift
whitespace whitespace
argument xout argument xout
......
...@@ -465,8 +465,7 @@ whitespace ...@@ -465,8 +465,7 @@ whitespace
classname Monoid classname Monoid
whitespace whitespace
typevar m typevar m
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * Right-associative fold of a structure.\n * `foldr f z = 'StdList'.{{foldr}} f z {{o}} {{toList}}`\n */ comment /**\n * Right-associative fold of a structure.\n * `foldr f z = 'StdList'.{{foldr}} f z {{o}} {{toList}}`\n */
whitespace \n whitespace \n
funcname foldr funcname foldr
...@@ -632,8 +631,7 @@ whitespace ...@@ -632,8 +631,7 @@ whitespace
punctuation -> punctuation ->
whitespace whitespace
typevar a typevar a
whitespace \n whitespace \n\n
whitespace \n
comment // TODO Cleanify comment // TODO Cleanify
whitespace \n whitespace \n
comment //instance Ix i => Foldable (Array i) comment //instance Ix i => Foldable (Array i)
...@@ -647,8 +645,7 @@ type Const ...@@ -647,8 +645,7 @@ type Const
whitespace whitespace
typevar m typevar m
punctuation ) punctuation )
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * Monadic fold over the elements of a structure, associating to the right,\n * i.e. from right to left.\n */ comment /**\n * Monadic fold over the elements of a structure, associating to the right,\n * i.e. from right to left.\n */
whitespace \n whitespace \n
funcname foldrM funcname foldrM
...@@ -692,8 +689,7 @@ whitespace ...@@ -692,8 +689,7 @@ whitespace
classname Monad classname Monad
whitespace whitespace
typevar m typevar m
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * Monadic fold over the elements of a structure, associating to the left, i.e.\n * from left to right.\n */ comment /**\n * Monadic fold over the elements of a structure, associating to the left, i.e.\n * from left to right.\n */
whitespace \n whitespace \n
funcname foldlM funcname foldlM
...@@ -737,8 +733,7 @@ whitespace ...@@ -737,8 +733,7 @@ whitespace
classname Monad classname Monad
whitespace whitespace
typevar m typevar m
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * Map each element of a structure to an action, evaluate these actions from\n * left to right, and ignore the results.\n */ comment /**\n * Map each element of a structure to an action, evaluate these actions from\n * left to right, and ignore the results.\n */
whitespace \n whitespace \n
funcname traverse_ funcname traverse_
...@@ -781,8 +776,7 @@ whitespace ...@@ -781,8 +776,7 @@ whitespace
classname *> classname *>
whitespace whitespace
typevar f typevar f
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * `for_` is {{`traverse_`}} with its arguments flipped.\n * @type (t a) (a -> f b) -> f () | Foldable, Applicative f\n */ comment /**\n * `for_` is {{`traverse_`}} with its arguments flipped.\n * @type (t a) (a -> f b) -> f () | Foldable, Applicative f\n */
whitespace \n whitespace \n
funcname for_ funcname for_
...@@ -832,8 +826,7 @@ whitespace ...@@ -832,8 +826,7 @@ whitespace
classname Monad classname Monad
whitespace whitespace
typevar m typevar m
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * `forM_` is {{`mapM_`}} with its arguments flipped.\n * @type (t a) (a -> m b) -> m () | Foldable t & Monad m\n */ comment /**\n * `forM_` is {{`mapM_`}} with its arguments flipped.\n * @type (t a) (a -> m b) -> m () | Foldable t & Monad m\n */
whitespace \n whitespace \n
funcname forM_ funcname forM_
...@@ -879,8 +872,7 @@ whitespace ...@@ -879,8 +872,7 @@ whitespace
classname *> classname *>
whitespace whitespace
typevar f typevar f
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * Evaluate each monadic action in the structure from left to right, and ignore\n * the results.\n * @type (t (m a)) -> m () | Foldable t & Monad m\n */ comment /**\n * Evaluate each monadic action in the structure from left to right, and ignore\n * the results.\n * @type (t (m a)) -> m () | Foldable t & Monad m\n */
whitespace \n whitespace \n
funcname sequence_ funcname sequence_
...@@ -1014,8 +1006,7 @@ whitespace ...@@ -1014,8 +1006,7 @@ whitespace
classname Foldable classname Foldable
whitespace whitespace
typevar t typevar t
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * Map a function over all the elements of a container and concatenate the\n * resulting lists.\n */ comment /**\n * Map a function over all the elements of a container and concatenate the\n * resulting lists.\n */
whitespace \n whitespace \n
funcname concatMap funcname concatMap
...@@ -1048,8 +1039,7 @@ whitespace ...@@ -1048,8 +1039,7 @@ whitespace
classname Foldable classname Foldable
whitespace whitespace
typevar t typevar t
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * `and` returns the conjunction of a container of {{`Bool`}}s. For the result\n * to be {{`True`}}, the container must be finite; {{`False`}}, however,\n * results from a {{`False`}} value finitely far from the left end.\n */ comment /**\n * `and` returns the conjunction of a container of {{`Bool`}}s. For the result\n * to be {{`True`}}, the container must be finite; {{`False`}}, however,\n * results from a {{`False`}} value finitely far from the left end.\n */
whitespace \n whitespace \n
funcname and funcname and
...@@ -1071,8 +1061,7 @@ whitespace ...@@ -1071,8 +1061,7 @@ whitespace
classname Foldable classname Foldable
whitespace whitespace
typevar t typevar t
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * `or` returns the disjunction of a container of {{`Bool`}}s. For the result\n * to be {{`False`}}, the container must be finite; {{`True`}}, however,\n * results from a {{`True`}} value finitely far from the left end.\n */ comment /**\n * `or` returns the disjunction of a container of {{`Bool`}}s. For the result\n * to be {{`False`}}, the container must be finite; {{`True`}}, however,\n * results from a {{`True`}} value finitely far from the left end.\n */
whitespace \n whitespace \n
funcname or funcname or
...@@ -1094,8 +1083,7 @@ whitespace ...@@ -1094,8 +1083,7 @@ whitespace
classname Foldable classname Foldable
whitespace whitespace
typevar t typevar t
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * Determines whether any element of the structure satisfies the predicate.\n */ comment /**\n * Determines whether any element of the structure satisfies the predicate.\n */
whitespace \n whitespace \n
funcname any funcname any
...@@ -1125,8 +1113,7 @@ whitespace ...@@ -1125,8 +1113,7 @@ whitespace
classname Foldable classname Foldable
whitespace whitespace
typevar t typevar t
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * Determines whether all elements of the structure satisfy the predicate.\n */ comment /**\n * Determines whether all elements of the structure satisfy the predicate.\n */
whitespace \n whitespace \n
funcname all funcname all
...@@ -1156,8 +1143,7 @@ whitespace ...@@ -1156,8 +1143,7 @@ whitespace
classname Foldable classname Foldable
whitespace whitespace
typevar t typevar t
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * The `sum` function computes the sum of the numbers of a structure.\n */ comment /**\n * The `sum` function computes the sum of the numbers of a structure.\n */
whitespace \n whitespace \n
funcname sum funcname sum
...@@ -1191,8 +1177,7 @@ whitespace ...@@ -1191,8 +1177,7 @@ whitespace
classname zero classname zero
whitespace whitespace
typevar a typevar a
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * The `product` function computes the product of the numbers of a structure.\n */ comment /**\n * The `product` function computes the product of the numbers of a structure.\n */
whitespace \n whitespace \n
funcname product funcname product
...@@ -1226,8 +1211,7 @@ whitespace ...@@ -1226,8 +1211,7 @@ whitespace
classname one classname one
whitespace whitespace
typevar a typevar a
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * The largest element of a non-empty structure.\n */ comment /**\n * The largest element of a non-empty structure.\n */
whitespace \n whitespace \n
funcname maximum funcname maximum
...@@ -1255,8 +1239,7 @@ whitespace ...@@ -1255,8 +1239,7 @@ whitespace
classname Ord classname Ord
whitespace whitespace
typevar a typevar a
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * The largest element of a non-empty structure with respect to the given\n * lesser-than function.\n */ comment /**\n * The largest element of a non-empty structure with respect to the given\n * lesser-than function.\n */
whitespace \n whitespace \n
funcname maximumBy funcname maximumBy
...@@ -1288,8 +1271,7 @@ whitespace ...@@ -1288,8 +1271,7 @@ whitespace
classname Foldable classname Foldable
whitespace whitespace
typevar t typevar t
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * The least element of a non-empty structure.\n */ comment /**\n * The least element of a non-empty structure.\n */
whitespace \n whitespace \n
funcname minimum funcname minimum
...@@ -1317,8 +1299,7 @@ whitespace ...@@ -1317,8 +1299,7 @@ whitespace
classname Ord classname Ord
whitespace whitespace
typevar a typevar a
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * The least element of a non-empty structure with respect to the given\n * lesser-than function.\n */ comment /**\n * The least element of a non-empty structure with respect to the given\n * lesser-than function.\n */
whitespace \n whitespace \n
funcname minimumBy funcname minimumBy
...@@ -1350,8 +1331,7 @@ whitespace ...@@ -1350,8 +1331,7 @@ whitespace
classname Foldable classname Foldable
whitespace whitespace
typevar t typevar t
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * Does the element occur in the structure?\n */ comment /**\n * Does the element occur in the structure?\n */
whitespace \n whitespace \n
funcname elem funcname elem
...@@ -1381,8 +1361,7 @@ whitespace ...@@ -1381,8 +1361,7 @@ whitespace
classname == classname ==
whitespace whitespace
typevar a typevar a
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * `notElem` is the negation of {{`elem`}}.\n */ comment /**\n * `notElem` is the negation of {{`elem`}}.\n */
whitespace \n whitespace \n
funcname notElem funcname notElem
...@@ -1412,8 +1391,7 @@ whitespace ...@@ -1412,8 +1391,7 @@ whitespace
classname == classname ==
whitespace whitespace
typevar a typevar a
whitespace \n whitespace \n\n
whitespace \n
comment /**\n * The `find` function takes a predicate and a structure and returns the\n * leftmost element of the structure matching the predicate, or {{`Nothing`}}\n * if there is no such element.\n */ comment /**\n * The `find` function takes a predicate and a structure and returns the\n * leftmost element of the structure matching the predicate, or {{`Nothing`}}\n * if there is no such element.\n */
whitespace \n whitespace \n
funcname find funcname find
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment