A utility function for chunking code in Sense dashboards.
Common code operations like parsing and syntax highlighting have strong library support. While writing Sense dashboards, however, we found that another code operation we call chunking is often awkward to implement. Code chunking is splitting code up into logical units such as comment and statement blocks, which are the top-level executable units of Sense's interactive dashboards (REPLs). Chunking allows dashboards to display code, comments, results and output in an order that closely matches the source code. As such, the goal is stylistic rather than formal.
// Here are some line comments.// Successive line comments should be grouped together in a chunk.// These chunks are often markdown-formatted and displayed as documentation.
var a = 2;
var b = 2; var c = 3;
/*Here's a block comment. Block comments can be markdown-formatted and displayedas documentation too.*/
varreturn x * x;;
if trueconsole.logsquare3;elseconsole.log'Not displaying the answer.';
Most languages have parsers available that get you most of the way to a chunker. Parsers usually output a sequence of abstract syntax tree nodes, which correspond to the code chunks, and the line and column at which each chunk begins and ends. If you can find such a parser for your language, this package will help you create a chunker relatively quickly. If your parser doesn't make a record of comments, no problem- this module will go back through the code and pick them up.
var// You have to supply this function.// If a syntax error or other error occurs during parsing, it should callcb"the error message"// If the code parses, it should callcbfalsestart: line: 0 column: 0 end: line: 1 column: 3 // the type is "code" by default.start: line: 1 column: 0 end: line: 10 column: 4 type: "blockComment"// Note that the end line and columns are exclusive upper bounds and that// indexing begins at zero, so the statemnt"var x;"// would bestart: line: 0 column: 0 end: line: 1 column: 7var chunk = require'comment-chunk-helper'chunkparser: parserFunclineComment: "//"blockComment: left: "/*" right: "*/";chunkcode cb;// The single argument passed to the callback will be an array// of chunks, that is, an argument of the formtype: "code" or "error" or "comment" or "blockComment"value: "the code, comment, or error message"// Note that the chunker's callback should not take an error// argument. If any error occurs while chunking, the type of// one of the returned chunks should be set to 'error'. That// way the error information is displayed to the user of the// dashboard.