Skip to content

Instantly share code, notes, and snippets.

@chemzqm
Created January 3, 2016 15:53
Show Gist options
  • Save chemzqm/fd1313206c182884efbc to your computer and use it in GitHub Desktop.
Save chemzqm/fd1313206c182884efbc to your computer and use it in GitHub Desktop.
Send mocha error msg back to macvim quickfix
#!/usr/bin/env node
// Warning: clientserver feature required for MacVim, check it by `:echo has('clientserver')` in MacVim
'use strict'
var lstream = require('./lstream')
var stream = new lstream()
var exec = require('child_process').exec
var hasError = false
stream.on('line', function (line) {
if (line.length) {
var s = line.replace(/^[\s]+/, '')
if (/^at/.test(s)) {
var ms = s.match(/^(.*)\s\((.*):(\d+):(\d+)\)/)
if (!ms) {
ms = s.match(/^(.*)\s([^\s]+):(\d+):(\d+)/)
}
var str = ms[2] + ':' + ms[3] + ':' + ms[4] + ' ' + ms[1]
sendMsg(":caddexpr '" + str + "'")
hasError = true
}
}
return line
})
stream.on('finish', function () {
var code = hasError ? 1 : 0
if (hasError) {
process.exit(1)
} else {
sendMsg('ccl')
}
})
function sendMsg(cmd) {
exec('mvim --remote-send "' + cmd + '<cr>"', function (err, stdout, stderr) {
if (err !== null) console.log(err)
if (stderr) console.log(stderr)
})
}
sendMsg(':set errorformat=%f:%l:%c\\ %m')
sendMsg(':cex []')
process.stdin.pipe(stream).pipe(process.stdout)
var stream = require('stream');
var util = require('util');
var LineStream = module.exports = function LineStream(opts) {
if (!(this instanceof LineStream)) return new LineStream(opts);
opts = opts || {};
opts.objectMode = true;
stream.Transform.call(this, opts);
this._buff = '';
};
util.inherits(LineStream, stream.Transform);
LineStream.prototype._transform = function(chunk, encoding, done) {
var data = this._buff + chunk.toString('utf8');
var lines = data.split(/\r?\n|\r(?!\n)/);
this._buff = lines.pop();
var self = this;
lines.forEach(function (line) {
self._line(line + '\n');
});
done();
};
LineStream.prototype._flush = function(done) {
if (this._buff) this._line(this._buff);
done();
};
LineStream.prototype._line = function(line) {
this.push(line);
this.emit('line', line);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment