【发布时间】:2020-08-08 13:24:25
【问题描述】:
我需要大致了解如何解决我的问题。我的Scheme based LISP in JavaScript 有 LIPS 的 REPL,我有使用 readline 的 node.js REPL。我需要解决的最后一件事是在我复制粘贴已缩进的代码的 sn-p 时修复双缩进。
我有这样的代码:
var highlight = require('../../prism-cli/index.js'); // temporary local copy
rl._writeToOutput = function _writeToOutput(stringToWrite) {
rl.output.write(highlight(stringToWrite, 'scheme', true));
};
process.stdin.on('keypress', (c, k) => {
setTimeout(function() {
rl._refreshLine(); // force refresh colors
}, 0);
});
boostrap(interp).then(function() {
rl.on('line', function(line) {
code += line + '\n';
try {
if (balanced_parenthesis(code)) {
rl.pause();
prev_eval = prev_eval.then(function() {
var result = run(code, interp);
code = '';
return result;
}).then(function(result) {
if (process.stdin.isTTY) {
print(result);
if (newline) {
// readline don't work with not endend lines
// it ignore those so we end then ourselfs
process.stdout.write("\n");
newline = false;
}
if (multiline) {
rl.setPrompt(prompt);
multiline = false;
}
rl.prompt();
}
rl.resume();
}).catch(function() {
if (process.stdin.isTTY) {
if (multiline) {
rl.setPrompt(prompt);
multiline = false;
}
rl.prompt();
}
});
} else {
multiline = true;
var ind = indent(code, 2, prompt.length - continuePrompt.length);
rl.setPrompt(continuePrompt);
rl.prompt();
var spaces = new Array(ind + 1).join(' ');
if (terminal) {
rl.write(spaces);
} else {
process.stdout.write(spaces);
code += spaces;
}
}
} catch (e) {
console.error(e.message);
code = '';
rl.prompt();
}
});
}).catch(function() {
console.error('Internal Error: boostrap filed');
});
indent 函数计算需要在下一行中添加的空格数,当我手动输入代码时这很好用,但是当我复制粘贴代码时缩进我最终得到双缩进。
我正在寻找解决此问题的解决方案,我对在终端标准输出中编辑文本并在输入文本后删除空格重写每一行的解决方案很好。我可以在每一行之后我可以更正上一行,但不知道如何。
我也想知道是否有办法检测用户是否输入了空格(实际按键)。
我无法检查行的开头是否有空格,因为缩进发生在实际行之前。
【问题讨论】:
标签: javascript node.js readline read-eval-print-loop