fku*_*kov 8 javascript emacs node.js read-eval-print-loop ecmascript-6
Clojure在像这样的语言中Scheme,我确实很喜欢在 REPL 驱动模式下编写代码,当您在编辑器中编写一段代码时(Emacs在我的例子中),将其发送到您的 REPL,使用它,然后返回到编辑器,修复发现问题并再次将代码发送到 REPL。
我尝试对 做同样的事情Node.js,如果我限制自己只使用ES5语法,它会起作用。但如果我使用、和 等ES6功能,我预计会在重新评估我的声明时出现错误:constletclass
> let foo = 1;
> let foo = 2;
TypeError: Identifier 'foo' has already been declared
Run Code Online (Sandbox Code Playgroud)
当我重新评估我的代码时,是否有任何Node.js REPL参数,或者可能是 patched REPLs,甚至是一些神奇的模式会清除现有的声明?Emacs这样我就能够以Node.js这种方式编写代码,而无需不断考虑我正在使用哪种语法和/或需要REPL在每次重新评估时手动重新启动。
如果您正在使用nodejs-repl,您可以eval( M-:)以下代码:
(with-current-buffer "*nodejs*"
(setq kill-buffer-query-functions (delq 'process-kill-buffer-query-function kill-buffer-query-functions))
(kill-process nil comint-ptyp)
(kill-buffer-and-window)
(run-with-timer 0.01 nil (lambda () (nodejs-repl))
))
Run Code Online (Sandbox Code Playgroud)
这不是最佳解决方案,但其工作原理如下:
nodejs-repl如果您想使用另一个 REPL 运行它,只需将缓冲区名称和命令更改为“重新运行”即可。
希望能帮助到你,
此致
这是一个更合适的解决方案,将其添加到您的.emacs或到nodejs-repl.el:
(defun nodejs-repl-restart ()
"restart the nodejs REPL"
(interactive)
(defvar nodejs-repl-code
(concat "process.stdout.columns = %d;" "require('repl').start('%s', null, null, true, false)"))
(with-current-buffer "*nodejs*"
(kill-process nil comint-ptyp)
(run-with-timer 0.01 nil (lambda ()
(setq nodejs-repl-prompt-re (format nodejs-repl-prompt-re-format nodejs-repl-prompt nodejs-repl-prompt))
(with-current-buffer "*nodejs*"
(apply 'make-comint nodejs-repl-process-name nodejs-repl-command nil `("-e" ,(format nodejs-repl-code (window-width) nodejs-repl-prompt)))
(nodejs-repl-mode) (erase-buffer) )))))
Run Code Online (Sandbox Code Playgroud)
它的作用几乎相同,但它不会杀死缓冲区,而是将其擦除。