Skip to content

Commit 2918de8

Browse files
committed
fix Node REPL
1 parent 029ab2e commit 2918de8

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

bin/lips.js

+32-17
Original file line numberDiff line numberDiff line change
@@ -445,16 +445,22 @@ function is_close(token) {
445445
// find matching open parentheses. The last token is always
446446
// a closing parenthesis
447447
function matched_token(code) {
448-
let count = 0;
449-
// true to tokenize return tokens with meta data
450-
return tokenize(code, true).reverse().find(token => {
451-
if (is_open(token.token)) {
452-
count--;
453-
} else if (is_close(token.token)) {
454-
count++;
448+
try {
449+
let count = 0;
450+
// true to tokenize return tokens with meta data
451+
return tokenize(code, true).reverse().find(token => {
452+
if (is_open(token.token)) {
453+
count--;
454+
} else if (is_close(token.token)) {
455+
count++;
456+
}
457+
return is_open(token.token) && count === 0;
458+
});
459+
} catch(e) {
460+
if (!(e instanceof Parser.Unterminated)) {
461+
throw e;
455462
}
456-
return is_open(token.token) && count === 0;
457-
});
463+
}
458464
}
459465

460466
// highlight the open parentheses based on token metadata
@@ -511,7 +517,7 @@ function run_repl(err, rl) {
511517
let prev_line;
512518
const is_emacs = process.env.INSIDE_EMACS;
513519
function is_brackets_mode() {
514-
return cmd.match(brackets_re);
520+
return !!cmd.match(brackets_re);
515521
}
516522
function continue_multiline(code) {
517523
multiline = true;
@@ -549,23 +555,32 @@ function run_repl(err, rl) {
549555
function char_before_cursor() {
550556
return rl.line[rl.cursor - 1];
551557
}
558+
function format_input_code(code) {
559+
if (code) {
560+
// we remove trailing newline from code
561+
code = code.substring(0, code.length - 1);
562+
return scheme(code);
563+
}
564+
}
552565
rl._writeToOutput = function _writeToOutput(string) {
553566
try {
554567
const prefix = multiline ? continue_prompt : prompt;
555568
const current_line = prefix + rl.line;
556569
let code = scheme(string);
557-
const was_bracket = is_brackets_mode();
558-
if (!was_bracket && !is_emacs) {
559-
// we remove trailing newline from cmd
560-
let code_above = cmd && scheme(cmd.substring(0, cmd.length - 1));
570+
const bracket_mode = cmd.match(brackets_re);
571+
const full_copy_paste = bracket_mode?.length == 2;
572+
if ((!bracket_mode || full_copy_paste) && !is_emacs) {
573+
// clean bracket mode markers
574+
const clean_cmd = cmd.replace(brackets_re, '');
575+
let code_above = format_input_code(clean_cmd);
561576
if (char_before_cursor() === ')') {
562577
const substring = rl.line.substring(0, rl.cursor);
563578
const input = prefix + substring;
564579
const token = matched_token(input);
565580
if (token) {
566581
code = mark_paren(code, token);
567-
} else if (cmd) {
568-
const input = cmd + substring;
582+
} else if (clean_cmd) {
583+
const input = clean_cmd + substring;
569584
// we match paren above the current line
570585
// but we need whole code with rl.line
571586
// so we need to ignore rl.line
@@ -636,7 +651,7 @@ function run_repl(err, rl) {
636651
return result;
637652
}).then(function(result) {
638653
if (process.stdin.isTTY) {
639-
if (print(result)) {
654+
if (print(result) || newline) {
640655
// readline doesn't work with not ended lines
641656
// it ignore those, so we end them ourselves
642657
process.stdout.write('\n');

0 commit comments

Comments
 (0)