branch: master commit 98df7dbd758c76161955988acd006629357415a9 Author: Jackson Ray Hamilton <jack...@jacksonrayhamilton.com> Commit: Jackson Ray Hamilton <jack...@jacksonrayhamilton.com>
Optimize cli a little bit. --- context-coloring.el | 18 +++------------- tokenizer/cli.js | 54 ++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/context-coloring.el b/context-coloring.el index 47952bb..bef584d 100644 --- a/context-coloring.el +++ b/context-coloring.el @@ -57,7 +57,7 @@ Determines depth at which to cycle through faces again.") ;;; Face utility functions -(defsubst context-coloring-depth-face (depth) +(defsubst context-coloring-level-face (depth) "Return face-name for DEPTH as a string 'context-coloring-depth-DEPTH-face'. For example: 'context-coloring-depth-1-face'." (intern-soft @@ -74,12 +74,6 @@ For example: 'context-coloring-depth-1-face'." (- context-coloring-face-count 1))))) "-face"))) -(defsubst context-coloring-get-point (line column) - (save-excursion - (goto-line line) - (move-to-column column) - (point))) - (defun context-coloring-save-buffer-to-temp () "Save buffer to temp file. Return the name of the temporary file." @@ -105,13 +99,9 @@ Return the name of the temporary file." (json-read-from-string json)))) (with-silent-modifications (dolist (token tokens) - (let* ((line (cdr (assoc 'line token))) - (from (cdr (assoc 'from token))) - (thru (cdr (assoc 'thru token))) - (level (cdr (assoc 'level token))) - (start (context-coloring-get-point line (- from 1))) - (end (context-coloring-get-point line (- thru 1))) - (face (context-coloring-depth-face level))) + (let ((start (cdr (assoc 's token))) + (end (cdr (assoc 'e token))) + (face (context-coloring-level-face (cdr (assoc 'l token))))) (add-text-properties start end `(font-lock-face ,face rear-nonsticky t))))) (delete-file temp-file))) diff --git a/tokenizer/cli.js b/tokenizer/cli.js index 9ac8262..0e799d1 100644 --- a/tokenizer/cli.js +++ b/tokenizer/cli.js @@ -4,7 +4,25 @@ var JSLINT = require('./jslint'), util = require('util'), // Accumulated input. - whole = ''; + whole = '', + + // Acquires the number of accumulated characters after the end of each line. + getTotals = function (file) { + var lines = file.split('\n'), + total = 0, + totals = { + 0: total + }, + i = 0, + length = lines.length; + while (i < length) { + total += lines[i].length + 1; + totals[i + 1] = total; + i += 1; + } + totals[i + 1] = total + 1; + return totals; + }; process.stdin.setEncoding('utf8'); @@ -16,16 +34,24 @@ process.stdin.on('readable', function () { }); process.stdin.on('end', function () { - var data, tokens; + var data, totals, out, i, tokens, length, token, origin, level, total; // Generate a syntax tree for the input. JSLINT(whole); data = JSLINT.data(); + totals = getTotals(whole); + // Minimize an otherwise-circular structure. - tokens = data.tokens.map(function (token) { - var origin = token, - level; + out = []; + i = 0; + tokens = data.tokens; + length = tokens.length; + + while (i < length) { + token = tokens[i]; + + origin = token; // We always consider the function keyword to be "part" of the scope it // creates, even if the name leaks in the case of function statements. @@ -43,14 +69,16 @@ process.stdin.on('end', function () { } else { level = origin.function.level; } + total = totals[token.line - 1]; + + out.push({ + l: level, + s: total + token.from, + e: total + token.thru + }); - return { - from: token.from, - level: level, - line: token.line, - thru: token.thru - }; - }); + i += 1; + } - console.log(JSON.stringify(tokens)); + console.log(JSON.stringify(out)); });