branch: master commit 2b8bc0540394f1b72148c8fbdaeacfe8d9d749d5 Author: Jackson Ray Hamilton <jack...@jacksonrayhamilton.com> Commit: Jackson Ray Hamilton <jack...@jacksonrayhamilton.com>
Remove buggy tail optimization. Move stdin cruft to cli. --- bin/cli.js | 23 ++++++++++++++ bin/scopifier | 3 ++ context-coloring.el | 5 +-- scopifier.js | 80 ++++++++++++++++++++------------------------------ 4 files changed, 60 insertions(+), 51 deletions(-) diff --git a/bin/cli.js b/bin/cli.js new file mode 100644 index 0000000..4b2b90e --- /dev/null +++ b/bin/cli.js @@ -0,0 +1,23 @@ +/*jslint node: true */ + +// Reads a JavaScript file from stdin. + +// Writes an array of `[level, start, end]' tokens to stdout. + +'use strict'; + +var scopifier = require('../scopifier'), + whole = ''; + +process.stdin.setEncoding('utf8'); + +process.stdin.on('readable', function () { + var chunk = process.stdin.read(); + if (chunk !== null) { + whole += chunk; + } +}); + +process.stdin.on('end', function () { + console.log(JSON.stringify(scopifier(whole))); +}); diff --git a/bin/scopifier b/bin/scopifier new file mode 100755 index 0000000..9610016 --- /dev/null +++ b/bin/scopifier @@ -0,0 +1,3 @@ +#!/usr/bin/env node +'use strict'; +require('./cli.js'); \ No newline at end of file diff --git a/context-coloring.el b/context-coloring.el index 7d1fb02..3b871a8 100644 --- a/context-coloring.el +++ b/context-coloring.el @@ -127,7 +127,7 @@ For example: \"context-coloring-depth-1-face\"." "This file's directory.") (defconst context-coloring-scopifier-path - (expand-file-name "./scopifier.js" context-coloring-path) + (expand-file-name "./bin/scopifier" context-coloring-path) "Path to the external scopifier executable.") (defconst context-coloring-delay 0.25 @@ -171,8 +171,7 @@ calling FUNCTION with the parsed list of tokens." ;; Start the process. (setq context-coloring-scopifier-process - (start-process-shell-command "scopifier" nil - (concat "node " context-coloring-scopifier-path))) + (start-process-shell-command "scopifier" nil context-coloring-scopifier-path)) (let ((output "") (buffer context-coloring-buffer) diff --git a/scopifier.js b/scopifier.js index 3b2338d..7bdbe37 100644 --- a/scopifier.js +++ b/scopifier.js @@ -4,36 +4,31 @@ var escope = require('escope'), esprima = require('esprima'), + + // Given an array of definitions, determines if a definition already exists + // for a given range. (escope detects variables twice if they are declared + // and initialized simultaneously; this filters them.) isDefined = function (definitions, range) { return definitions.some(function (definition) { // Check for identical definitions. return definition[1] === range[0] && definition[2] === range[1]; }); - }, - whole = ''; - -process.stdin.setEncoding('utf8'); - -process.stdin.on('readable', function () { - var chunk = process.stdin.read(); - if (chunk !== null) { - whole += chunk; - } -}); + }; -process.stdin.on('end', function () { +// Given code, returns an array of `[level, start, end]' tokens for +// context-coloring. +module.exports = function (code) { var ast, analyzedScopes, scopes = [], symbols = [], - comments = [], - continuous, + comments, emacsified; // Gracefully handle parse errors by doing nothing. try { - ast = esprima.parse(whole, { + ast = esprima.parse(code, { comment: true, range: true }); @@ -107,38 +102,27 @@ process.stdin.on('end', function () { } }); - ast.comments.forEach(function (comment) { - var range = comment.range; - comments.push([ - -1, - range[0], - range[1] - ]); - }); - - continuous = symbols.concat(comments).sort(function (a, b) { - return a[1] - b[1]; - }); - - continuous = continuous.slice(1).reduce(function (soFar, token) { - var previous = soFar[soFar.length - 1]; - // Detect same-color exact tail ends (nothing else is safe to join). - if (previous[0] === token[0] && - previous[2] === token[1] - 1) { - previous[2] = token[2]; - return soFar; - } - soFar.push(token); - return soFar; - }, continuous.slice(0, 1)); - - emacsified = scopes.concat(continuous); + comments = ast.comments + .map(function (comment) { + var range = comment.range; + return [ + -1, + range[0], + range[1] + ]; + }); - emacsified.forEach(function (instruction) { - // Emacs starts counting from 1. - instruction[1] += 1; - instruction[2] += 1; - }); + emacsified = scopes + .concat(symbols) + .concat(comments) + .map(function (token) { + // Emacs starts counting from 1. + return [ + token[0], + token[1] + 1, + token[2] + 1 + ]; + }); - console.log(JSON.stringify(emacsified)); -}); + return emacsified; +};