Hacker News new | past | comments | ask | show | jobs | submit login

Max line length can be aggregated by using a 1-2-3 element type, for example, using scalar, 2-tuple and 3-tuple:

  open length |
  { open left length, open right length } |
  { open left length, max contained line, open right length }
The cases are obvious depending on the number of newlines in the substring (think of newlines as the commas in the tuples :)

  0 newlines:  open length
  1 newline:   { open left length, open right length } 
  2+ newlines: { open left length, max contained line, open right length }
Merging (summing) two maxline objects is also fairly obvious. There are 3*3 cases: contract (add) adjacent open counts, max with any contained line counts, preserve left and right open lengths.

The maxline ops are (obviously) non-commutative, but are associative, so you can apply them in any grouping, left-to-right, right-to-left, and even in parallel if you feel the need.

At the root of the tree, the left and right open lengths become actual lines (beginning and end of buffer are virtual line delimiters), so to get the global maximum line length, just max the 1, 2, or 3 values in the root maxline summary.

I like to separate newlines from other substrings as leaves in the tree. So the leaf maxlines are just:

  substring:   length
  newline:     {0,0}
Then sum as given above across any number of child nodes.

The Zed blog claims to have such a thing, but didn't explain it. Apologies if it's in one of the repos. I didn't check.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: