caesar-parser
Advanced tools
Comparing version 0.1.2 to 0.2.1
@@ -80,2 +80,7 @@ 'use strict'; | ||
}, { | ||
key: 'hashCode', | ||
value: function hashCode() { | ||
return this._data.hashCode(); | ||
} | ||
}, { | ||
key: 'toJS', | ||
@@ -82,0 +87,0 @@ value: function toJS() { |
@@ -9,3 +9,3 @@ 'use strict'; | ||
var _get = function get(_x7, _x8, _x9) { var _again = true; _function: while (_again) { var object = _x7, property = _x8, receiver = _x9; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x7 = parent; _x8 = property; _x9 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; | ||
var _get = function get(_x9, _x10, _x11) { var _again = true; _function: while (_again) { var object = _x9, property = _x10, receiver = _x11; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x9 = parent; _x10 = property; _x11 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; | ||
@@ -20,4 +20,2 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var _constants = require('./constants'); | ||
var _StringStream = require('./StringStream'); | ||
@@ -27,4 +25,2 @@ | ||
var _immutable = require('immutable'); | ||
var _Context = require('./Context'); | ||
@@ -36,2 +32,6 @@ | ||
var _Doc = require('./Doc'); | ||
var _Doc2 = _interopRequireDefault(_Doc); | ||
var ERR_LIMIT = 3; | ||
@@ -47,2 +47,3 @@ | ||
* tabSize: 4 | ||
* closedErrorStack: true | ||
* } | ||
@@ -59,5 +60,6 @@ */ | ||
this._tabSize = opts.tabSize || 4; | ||
this._closedErrorStack = opts.closedErrorStack || false; | ||
this._initParser(code); | ||
this._delayed = new _utils.Delayed(); | ||
this._readyPause = false; // 触发暂停 | ||
this._parsing = false; // | ||
this._keepOnLines = null; // 前进行数 | ||
@@ -102,5 +104,5 @@ } | ||
}; | ||
var size = this._code.size; | ||
var size = this._doc.size; | ||
var ended = function ended() { | ||
_this._readyPause = false; | ||
_this._parsing = false; | ||
_this._keepOnLines = null; | ||
@@ -113,3 +115,3 @@ }; | ||
var run = function run() { | ||
if (_this._readyPause || _this._keepOnLines === 0) { | ||
if (_this._keepOnLines === 0) { | ||
ended(); | ||
@@ -119,3 +121,3 @@ return res(); | ||
_this._keepOnLines && _this._keepOnLines--; | ||
var str = _this._code.get(_this._row); | ||
var str = _this._doc.getRowContent(_this._row); | ||
var stream = new _StringStream2['default'](str, { tabSize: _this._tabSize, saveStyle: opts.saveStyle }); | ||
@@ -134,2 +136,4 @@ while (!stream.eol()) { | ||
}; | ||
// 开始渲染 | ||
_this._parsing = true; | ||
delayed(run, opts.delayTime); | ||
@@ -153,23 +157,20 @@ })['catch'](function (e) { | ||
afterLines = undefined; | ||
var colors = undefined; | ||
col = col - 1; // backup one col | ||
col = col < 0 ? 0 : col; | ||
var row = this._row; | ||
try { | ||
colors = require('colors'); | ||
} catch (e) { | ||
colors = { black: function black(str) { | ||
return (0, _utils.emptySpace)(str.length); | ||
} }; | ||
colors.bgBlack = colors.red = function (str) { | ||
return str; | ||
}; | ||
if (this._closedErrorStack) { | ||
var _err = new Error(msg); | ||
_err.text = msg; | ||
_err.row = row; | ||
_err.col = col; | ||
throw _err; | ||
} | ||
var colors = require('colors'); | ||
var preRow = row - ERR_LIMIT + 1 < 0 ? 0 : row - ERR_LIMIT + 1; | ||
errorMsg = (0, _utils.emptySpace)(4) + colors.black(this._code.get(row).slice(0, col)) + colors.red(errorMsg + '^ ' + msg); | ||
lines = this._code.slice(preRow, row + 1).map(function (text, index, arr) { | ||
errorMsg = (0, _utils.emptySpace)(4) + colors.black(this._doc.getRowContent(row).slice(0, col)) + colors.red(errorMsg + '^ ' + msg); | ||
lines = this._doc.code.slice(preRow, row + 1).map(function (text, index, arr) { | ||
return row - arr.size + index + 2 + ' | ' + text; | ||
}); | ||
lines.push(errorMsg); | ||
afterLines = this._code.slice(row + 1, row + 1 + ERR_LIMIT).map(function (text, index) { | ||
afterLines = this._doc.code.slice(row + 1, row + 1 + ERR_LIMIT).map(function (text, index) { | ||
return row + index + 2 + ' | ' + text; | ||
@@ -188,3 +189,2 @@ }); | ||
this.ctx = new _Context2['default'](); | ||
this._ctxList.length = 0; | ||
this._row = 0; | ||
@@ -198,20 +198,50 @@ } | ||
this.ctx = new _Context2['default'](); | ||
this._ctxList = []; | ||
this.updateCode(code); | ||
this.updateDoc(code); | ||
} | ||
}, { | ||
key: 'updateCode', | ||
value: function updateCode(code) { | ||
if (typeof code === 'string') { | ||
this._code = new _immutable.List(code.split(_constants.ENDED_REG)); | ||
} else if (Array.isArray(code)) { | ||
this._code = new _immutable.List(code); | ||
} else { | ||
this._code = code; | ||
key: 'updateDoc', | ||
value: function updateDoc(code) { | ||
this._doc = new _Doc2['default'](code); | ||
} | ||
/** | ||
* @param {number} changedRow | ||
*/ | ||
}, { | ||
key: 'saftyChangeDoc', | ||
value: function saftyChangeDoc() { | ||
var changedRow = arguments.length <= 0 || arguments[0] === undefined ? 0 : arguments[0]; | ||
this.pause(); | ||
if (this._row > changedRow) { | ||
this._row = changedRow; | ||
} | ||
} | ||
/** | ||
* @param {function} fn | ||
* @param {number} perLines - 每执行n行调用一次回调 | ||
*/ | ||
}, { | ||
key: 'onParse', | ||
value: function onParse(fn) { | ||
this.on('parse', fn); | ||
var _this2 = this; | ||
var perLines = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1]; | ||
if (!perLines) { | ||
this.on('parse', fn); | ||
} else { | ||
(function () { | ||
var count = 0; | ||
_this2.on('parse', function () { | ||
count++; | ||
// 执行到最后一次时候也需要 | ||
if (_this2.row === _this2.doc.size - 1 || count >= perLines) { | ||
count = 0; | ||
fn(); | ||
} | ||
}); | ||
})(); | ||
} | ||
} | ||
@@ -249,3 +279,3 @@ }, { | ||
value: function highlight(code) { | ||
var _this2 = this; | ||
var _this3 = this; | ||
@@ -256,23 +286,27 @@ var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var startRow = opts.startRow === undefined ? this._row : opts.startRow; | ||
if (this._ctxList.length !== 0 && startRow > 0) { | ||
// const preCtx = this._doc.getRow | ||
var preRow = this._doc.getRow(startRow - 1); | ||
var preCtx = undefined; | ||
if (preRow && (preCtx = preRow.get('context'))) { | ||
// 获取之前的状态 | ||
this.ctx = this._ctxList[startRow - 1] && this._ctxList[startRow - 1].context; | ||
this._ctxList = this._ctxList.slice(0, startRow); | ||
} else if (startRow === 0) { | ||
this.ctx = preCtx; | ||
opts.startRow = startRow; | ||
} else { | ||
this.clearContext(); | ||
opts.startRow = 0; | ||
} | ||
opts.startRow = startRow; | ||
opts.saveStyle = true; | ||
return this._iter(function (stream) { | ||
return this._iter(function (stream, row) { | ||
var ctx = { | ||
context: _this2.ctx, | ||
context: _this3.ctx, | ||
styleStack: stream.styleStack, | ||
indentStack: stream.indentStack | ||
}; | ||
_this2._ctxList.push(ctx); | ||
return ctx; | ||
// update context to doc | ||
_this3._doc.code = _this3._doc.updateRow(row, ctx); | ||
return _this3._doc.getRow(row); | ||
}, opts).then(function () { | ||
_this2._res = _this2.ctx.toJS(); | ||
return _this2._ctxList.map(function (item) { | ||
return item.styleStack; | ||
_this3._res = _this3.ctx.toJS(); | ||
return _this3._doc.code.map(function (item) { | ||
return item.get('styleStack'); | ||
}); | ||
@@ -297,10 +331,13 @@ }); | ||
} | ||
/** | ||
* 暂停 | ||
*/ | ||
}, { | ||
key: 'isParsing', | ||
value: function isParsing() { | ||
return this._parsing; | ||
} | ||
}, { | ||
key: 'pause', | ||
value: function pause() { | ||
this._readyPause = true; | ||
if (this.isParsing()) { | ||
this._delayed.clearTimeout(); | ||
} | ||
} | ||
@@ -342,11 +379,11 @@ | ||
value: function parse(code) { | ||
var _this3 = this; | ||
var _this4 = this; | ||
code && this.updateCode(code); | ||
code && this.updateDoc(code); | ||
this.clearContext(); | ||
return this._iter(function () { | ||
return { context: _this3.ctx }; | ||
return { context: _this4.ctx }; | ||
}).then(function () { | ||
_this3._res = _this3.ctx.toJS(); | ||
return _this3._res; | ||
_this4._res = _this4.ctx.toJS(); | ||
return _this4._res; | ||
}); | ||
@@ -387,2 +424,7 @@ } | ||
} | ||
}, { | ||
key: 'doc', | ||
get: function get() { | ||
return this._doc; | ||
} | ||
}]); | ||
@@ -389,0 +431,0 @@ |
@@ -7,6 +7,10 @@ 'use strict'; | ||
var _id = undefined; | ||
return function (f, _ms) { | ||
function delayed(f, _ms) { | ||
clearTimeout(_id); | ||
_id = setTimeout(f, _ms || ms); | ||
} | ||
delayed.clearTimeout = function () { | ||
clearTimeout(_id); | ||
}; | ||
return delayed; | ||
} | ||
@@ -13,0 +17,0 @@ |
{ | ||
"name": "caesar-parser", | ||
"version": "0.1.2", | ||
"version": "0.2.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -43,2 +43,5 @@ import immutable from 'immutable' | ||
} | ||
hashCode() { | ||
return this._data.hashCode() | ||
} | ||
toJS() { | ||
@@ -45,0 +48,0 @@ const val = this._data.get('value') |
import {EventEmitter} from 'events' | ||
import {ENDED_REG} from './constants' | ||
import StringStream from './StringStream' | ||
import {List} from 'immutable' | ||
import Context from './Context' | ||
import {Delayed, emptySpace} from './utils' | ||
import Doc from './Doc' | ||
const ERR_LIMIT = 3 | ||
@@ -15,2 +14,3 @@ | ||
* tabSize: 4 | ||
* closedErrorStack: true | ||
* } | ||
@@ -21,5 +21,6 @@ */ | ||
this._tabSize = opts.tabSize || 4 | ||
this._closedErrorStack = opts.closedErrorStack || false | ||
this._initParser(code) | ||
this._delayed = new Delayed | ||
this._readyPause = false // 触发暂停 | ||
this._parsing = false // | ||
this._keepOnLines = null // 前进行数 | ||
@@ -51,5 +52,5 @@ } | ||
const delayed = opts.delayed ? this._delayed : function(_fn) { _fn() } | ||
const size = this._code.size | ||
const size = this._doc.size | ||
const ended = () => { | ||
this._readyPause = false | ||
this._parsing = false | ||
this._keepOnLines = null | ||
@@ -62,3 +63,3 @@ } | ||
const run = ()=> { | ||
if (this._readyPause || this._keepOnLines === 0) { | ||
if (this._keepOnLines === 0) { | ||
ended() | ||
@@ -68,3 +69,3 @@ return res() | ||
this._keepOnLines && (this._keepOnLines --) | ||
const str = this._code.get(this._row) | ||
const str = this._doc.getRowContent(this._row) | ||
const stream = new StringStream(str, {tabSize: this._tabSize, saveStyle: opts.saveStyle}) | ||
@@ -83,2 +84,4 @@ while (!stream.eol()) { | ||
} | ||
// 开始渲染 | ||
this._parsing = true | ||
delayed(run, opts.delayTime) | ||
@@ -98,17 +101,18 @@ }).catch((e) => { | ||
let lines, afterLines | ||
let colors | ||
col = col - 1 // backup one col | ||
col = col < 0 ? 0 : col | ||
const row = this._row | ||
try { | ||
colors = require('colors') | ||
} catch (e) { | ||
colors = {black: (str) => { return emptySpace(str.length) }} | ||
colors.bgBlack = colors.red = (str) => { return str } | ||
if (this._closedErrorStack) { | ||
const err = new Error(msg) | ||
err.text = msg | ||
err.row = row | ||
err.col = col | ||
throw err | ||
} | ||
const colors = require('colors') | ||
const preRow = row - ERR_LIMIT + 1 < 0 ? 0 : row - ERR_LIMIT + 1 | ||
errorMsg = emptySpace(4) + colors.black(this._code.get(row).slice(0, col)) + colors.red(errorMsg + '^ ' + msg) | ||
lines = this._code.slice(preRow, row + 1).map((text, index, arr) => (row - arr.size + index + 2) + ' | ' + text ) | ||
errorMsg = emptySpace(4) + colors.black(this._doc.getRowContent(row).slice(0, col)) + colors.red(errorMsg + '^ ' + msg) | ||
lines = this._doc.code.slice(preRow, row + 1).map((text, index, arr) => (row - arr.size + index + 2) + ' | ' + text ) | ||
lines.push(errorMsg) | ||
afterLines = this._code.slice(row + 1, row + 1 + ERR_LIMIT).map((text, index) => (row + index + 2) + ' | ' + text ) | ||
afterLines = this._doc.code.slice(row + 1, row + 1 + ERR_LIMIT).map((text, index) => (row + index + 2) + ' | ' + text ) | ||
lines = lines.concat(afterLines) | ||
@@ -123,3 +127,2 @@ const err = new Error(colors.bgBlack('\n' + lines.join('\n'))) | ||
this.ctx = new Context() | ||
this._ctxList.length = 0 | ||
this._row = 0 | ||
@@ -131,17 +134,35 @@ } | ||
this.ctx = new Context() | ||
this._ctxList = [] | ||
this.updateCode(code) | ||
this.updateDoc(code) | ||
} | ||
updateCode(code) { | ||
if (typeof code === 'string') { | ||
this._code = new List(code.split(ENDED_REG)) | ||
} else if (Array.isArray(code)) { | ||
this._code = new List(code) | ||
updateDoc(code) { | ||
this._doc = new Doc(code) | ||
} | ||
/** | ||
* @param {number} changedRow | ||
*/ | ||
saftyChangeDoc(changedRow = 0) { | ||
this.pause() | ||
if (this._row > changedRow) { | ||
this._row = changedRow | ||
} | ||
} | ||
/** | ||
* @param {function} fn | ||
* @param {number} perLines - 每执行n行调用一次回调 | ||
*/ | ||
onParse(fn, perLines = 0) { | ||
if (!perLines) { | ||
this.on('parse', fn) | ||
} else { | ||
this._code = code | ||
let count = 0 | ||
this.on('parse', ()=> { | ||
count ++ | ||
// 执行到最后一次时候也需要 | ||
if (this.row === this.doc.size - 1 || count >= perLines) { | ||
count = 0 | ||
fn() | ||
} | ||
}) | ||
} | ||
} | ||
onParse(fn) { | ||
this.on('parse', fn) | ||
} | ||
removeParseHandler(fn) { | ||
@@ -170,12 +191,15 @@ this.removeListener('parse', fn) | ||
const startRow = opts.startRow === undefined ? this._row : opts.startRow | ||
if (this._ctxList.length !== 0 && startRow > 0) { | ||
// const preCtx = this._doc.getRow | ||
const preRow = this._doc.getRow(startRow - 1) | ||
let preCtx | ||
if (preRow && (preCtx = preRow.get('context'))) { | ||
// 获取之前的状态 | ||
this.ctx = this._ctxList[startRow - 1] && this._ctxList[startRow - 1].context | ||
this._ctxList = this._ctxList.slice(0, startRow) | ||
} else if (startRow === 0) { | ||
this.ctx = preCtx | ||
opts.startRow = startRow | ||
} else { | ||
this.clearContext() | ||
opts.startRow = 0 | ||
} | ||
opts.startRow = startRow | ||
opts.saveStyle = true | ||
return this._iter((stream)=> { | ||
return this._iter((stream, row)=> { | ||
const ctx = { | ||
@@ -186,7 +210,8 @@ context: this.ctx, | ||
} | ||
this._ctxList.push(ctx) | ||
return ctx | ||
// update context to doc | ||
this._doc.code = this._doc.updateRow(row, ctx) | ||
return this._doc.getRow(row) | ||
}, opts).then(()=> { | ||
this._res = this.ctx.toJS() | ||
return this._ctxList.map((item) => item.styleStack) | ||
return this._doc.code.map((item) => item.get('styleStack')) | ||
}) | ||
@@ -205,7 +230,9 @@ } | ||
} | ||
/** | ||
* 暂停 | ||
*/ | ||
isParsing() { | ||
return this._parsing | ||
} | ||
pause() { | ||
this._readyPause = true | ||
if (this.isParsing()) { | ||
this._delayed.clearTimeout() | ||
} | ||
} | ||
@@ -236,3 +263,3 @@ /** | ||
parse(code) { | ||
code && this.updateCode(code) | ||
code && this.updateDoc(code) | ||
this.clearContext() | ||
@@ -264,4 +291,7 @@ return this._iter(() => {return {context: this.ctx}}).then(()=> { | ||
} | ||
get doc() { | ||
return this._doc | ||
} | ||
} | ||
export default Parser |
function Delayed(ms = 0) { | ||
let _id | ||
return function(f, _ms) { | ||
function delayed(f, _ms) { | ||
clearTimeout(_id) | ||
_id = setTimeout(f, _ms || ms) | ||
} | ||
delayed.clearTimeout = function() { | ||
clearTimeout(_id) | ||
} | ||
return delayed | ||
} | ||
@@ -8,0 +12,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62039
23
2025