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) {
if (typeof start != 'undefined')
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) {
var found = false;
for (var i in lex[state]) {
......@@ -29,18 +39,27 @@ function highlight(lex, istr, start) {
var parts = patt.exec(istr);
var j = 0;
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];
if (result.length > 1)
new_states = result[1];
}
for (var k in clss) {
j++;
if (out.length > 0 && out[out.length-1]['class'] == clss[k])
if (out.length > 0) {
var equal = equal_classes(out[out.length-1]['class'], clss[k]);
if (equal != null) {
out[out.length-1]['class'] = equal;
out[out.length-1]['str'] += parts[j];
else
} else {
out.push({'class': clss[k], 'str': parts[j]});
}
} else {
out.push({'class': clss[k], 'str': parts[j]});
}
}
istr = parts[j+1];
found = true;
......@@ -118,7 +137,7 @@ function highlightClean(func, callback, start) {
};
var paren_depth = 0;
var indent = 0;
var indents = [[0, 'start']];
var i = 0;
return highlightToHTML({
......@@ -127,19 +146,39 @@ function highlightClean(func, callback, start) {
[/(\/\*)/, ['comment'], 'comment'],
],
_append: [
[/(\n[^\n\S]*)/, function(matches) {
[/(\n[^\n\S]*)/, function(matches, states) {
var whitespace = matches[1];
var rest = matches[2];
var old_indent = indent;
indent = whitespace.substring(1).replace('\t',' ').length;
if (indent < old_indent || indent == 0) {
var indent = whitespace.substring(1).replace('\t',' ').length;
var cls = 'whitespace';
if (indent == 0) {
if (!rest.match(/^[\s\n]*where/))
cls = '__reset__arguments__';
return [[cls], 'reset:start'];
} else {
}
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']],
[/(.)/, ['error']]
......
......@@ -24,8 +24,7 @@ whitespace
typevar m
whitespace
keyword where
whitespace \n
whitespace \n
whitespace \n\n
funcname bind
whitespace
punctuation ::
......
......@@ -29,8 +29,7 @@ whitespace
typevar s
whitespace
keyword where
whitespace \n
whitespace \n
whitespace \n\n
funcname textSize
whitespace
punctuation ::
......
......@@ -214,8 +214,7 @@ whitespace
argumentinrhs yin
whitespace
argumentinrhs yout
whitespace \n
whitespace \n
whitespace \n\n
funcname takexnext
whitespace
punctuation [|
......@@ -245,8 +244,7 @@ literal literal-bool False
punctuation ,[|],
funcname funcname-onlyused accu
punctuation )
whitespace \n
whitespace \n
whitespace \n\n
funcname takeynext
whitespace
punctuation [|
......@@ -279,8 +277,7 @@ literal literal-bool False
punctuation ,[|],
funcname funcname-onlyused accu
punctuation )
whitespace \n
whitespace \n
whitespace \n\n
funcname shift
whitespace
argument xout
......
......@@ -465,8 +465,7 @@ whitespace
classname Monoid
whitespace
typevar m
whitespace \n
whitespace \n
whitespace \n\n
comment /**\n * Right-associative fold of a structure.\n * `foldr f z = 'StdList'.{{foldr}} f z {{o}} {{toList}}`\n */
whitespace \n
funcname foldr
......@@ -632,8 +631,7 @@ whitespace
punctuation ->
whitespace
typevar a
whitespace \n
whitespace \n
whitespace \n\n
comment // TODO Cleanify
whitespace \n
comment //instance Ix i => Foldable (Array i)
......@@ -647,8 +645,7 @@ type Const
whitespace
typevar m
punctuation )
whitespace \n
whitespace \n
whitespace \n\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
funcname foldrM
......@@ -692,8 +689,7 @@ whitespace
classname Monad
whitespace
typevar m
whitespace \n
whitespace \n
whitespace \n\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
funcname foldlM
......@@ -737,8 +733,7 @@ whitespace
classname Monad
whitespace
typevar m
whitespace \n
whitespace \n
whitespace \n\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
funcname traverse_
......@@ -781,8 +776,7 @@ whitespace
classname *>
whitespace
typevar f
whitespace \n
whitespace \n
whitespace \n\n
comment /**\n * `for_` is {{`traverse_`}} with its arguments flipped.\n * @type (t a) (a -> f b) -> f () | Foldable, Applicative f\n */
whitespace \n
funcname for_
......@@ -832,8 +826,7 @@ whitespace
classname Monad
whitespace
typevar m
whitespace \n
whitespace \n
whitespace \n\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
funcname forM_
......@@ -879,8 +872,7 @@ whitespace
classname *>
whitespace
typevar f
whitespace \n
whitespace \n
whitespace \n\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
funcname sequence_
......@@ -1014,8 +1006,7 @@ whitespace
classname Foldable
whitespace
typevar t
whitespace \n
whitespace \n
whitespace \n\n
comment /**\n * Map a function over all the elements of a container and concatenate the\n * resulting lists.\n */
whitespace \n
funcname concatMap
......@@ -1048,8 +1039,7 @@ whitespace
classname Foldable
whitespace
typevar t
whitespace \n
whitespace \n
whitespace \n\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
funcname and
......@@ -1071,8 +1061,7 @@ whitespace
classname Foldable
whitespace
typevar t
whitespace \n
whitespace \n
whitespace \n\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
funcname or
......@@ -1094,8 +1083,7 @@ whitespace
classname Foldable
whitespace
typevar t
whitespace \n
whitespace \n
whitespace \n\n
comment /**\n * Determines whether any element of the structure satisfies the predicate.\n */
whitespace \n
funcname any
......@@ -1125,8 +1113,7 @@ whitespace
classname Foldable
whitespace
typevar t
whitespace \n
whitespace \n
whitespace \n\n
comment /**\n * Determines whether all elements of the structure satisfy the predicate.\n */
whitespace \n
funcname all
......@@ -1156,8 +1143,7 @@ whitespace
classname Foldable
whitespace
typevar t
whitespace \n
whitespace \n
whitespace \n\n
comment /**\n * The `sum` function computes the sum of the numbers of a structure.\n */
whitespace \n
funcname sum
......@@ -1191,8 +1177,7 @@ whitespace
classname zero
whitespace
typevar a
whitespace \n
whitespace \n
whitespace \n\n
comment /**\n * The `product` function computes the product of the numbers of a structure.\n */
whitespace \n
funcname product
......@@ -1226,8 +1211,7 @@ whitespace
classname one
whitespace
typevar a
whitespace \n
whitespace \n
whitespace \n\n
comment /**\n * The largest element of a non-empty structure.\n */
whitespace \n
funcname maximum
......@@ -1255,8 +1239,7 @@ whitespace
classname Ord
whitespace
typevar a
whitespace \n
whitespace \n
whitespace \n\n
comment /**\n * The largest element of a non-empty structure with respect to the given\n * lesser-than function.\n */
whitespace \n
funcname maximumBy
......@@ -1288,8 +1271,7 @@ whitespace
classname Foldable
whitespace
typevar t
whitespace \n
whitespace \n
whitespace \n\n
comment /**\n * The least element of a non-empty structure.\n */
whitespace \n
funcname minimum
......@@ -1317,8 +1299,7 @@ whitespace
classname Ord
whitespace
typevar a
whitespace \n
whitespace \n
whitespace \n\n
comment /**\n * The least element of a non-empty structure with respect to the given\n * lesser-than function.\n */
whitespace \n
funcname minimumBy
......@@ -1350,8 +1331,7 @@ whitespace
classname Foldable
whitespace
typevar t
whitespace \n
whitespace \n
whitespace \n\n
comment /**\n * Does the element occur in the structure?\n */
whitespace \n
funcname elem
......@@ -1381,8 +1361,7 @@ whitespace
classname ==
whitespace
typevar a
whitespace \n
whitespace \n
whitespace \n\n
comment /**\n * `notElem` is the negation of {{`elem`}}.\n */
whitespace \n
funcname notElem
......@@ -1412,8 +1391,7 @@ whitespace
classname ==
whitespace
typevar a
whitespace \n
whitespace \n
whitespace \n\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
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