repl: treat middle imports in a more forgiving way

pull/4922/head
Delyan Angelov 2020-05-16 22:03:28 +03:00
parent a3a19e899d
commit 62cb48d252
2 changed files with 38 additions and 21 deletions

View File

@ -12,13 +12,15 @@ import v.util
struct Repl { struct Repl {
mut: mut:
indent int indent int // indentation level
in_func bool in_func bool // are we inside a new custom user function
line string line string // the current line entered by the user
lines []string //
temp_lines []string imports []string // all the import statements
functions_name []string functions []string // all the user function declarations
functions []string functions_name []string // all the user function names
lines []string // all the other lines/statements
temp_lines []string // all the temporary expressions/printlns
} }
fn (r mut Repl) checks() bool { fn (r mut Repl) checks() bool {
@ -58,6 +60,17 @@ fn (r &Repl) function_call(line string) bool {
return false return false
} }
fn (r &Repl) current_source_code(should_add_temp_lines bool) string {
mut all_lines := []string{}
all_lines << r.imports
all_lines << r.functions
all_lines << r.lines
if should_add_temp_lines {
all_lines << r.temp_lines
}
return all_lines.join('\n')
}
fn repl_help() { fn repl_help() {
println(util.full_v_version()) println(util.full_v_version())
println(' println('
@ -143,16 +156,16 @@ fn run_repl(workdir string, vrepl_prefix string) {
} }
r.line = '' r.line = ''
} }
if r.line == 'debug_repl' {
eprintln('repl: $r')
continue
}
if r.line == 'reset' { if r.line == 'reset' {
r = Repl{} r = Repl{}
continue continue
} }
if r.line == 'list' { if r.line == 'list' {
mut all_lines := []string{} source_code := r.current_source_code(true)
all_lines << r.functions
all_lines << r.lines
all_lines << r.temp_lines
source_code := all_lines.join('\n')
println('//////////////////////////////////////////////////////////////////////////////////////') println('//////////////////////////////////////////////////////////////////////////////////////')
println(source_code) println(source_code)
println('//////////////////////////////////////////////////////////////////////////////////////') println('//////////////////////////////////////////////////////////////////////////////////////')
@ -162,7 +175,7 @@ fn run_repl(workdir string, vrepl_prefix string) {
// but don't add this print call to the `lines` array, // but don't add this print call to the `lines` array,
// so that it doesn't get called during the next print. // so that it doesn't get called during the next print.
if r.line.starts_with('print') { if r.line.starts_with('print') {
source_code := r.functions.join('\n') + r.lines.join('\n') + '\n' + r.line + '\n' source_code := r.current_source_code(false) + '\n${r.line}\n'
os.write_file(file, source_code) os.write_file(file, source_code)
s := os.exec('"$vexe" -repl run $file') or { s := os.exec('"$vexe" -repl run $file') or {
rerror(err) rerror(err)
@ -195,10 +208,10 @@ fn run_repl(workdir string, vrepl_prefix string) {
temp_flag = true temp_flag = true
} }
mut temp_source_code := '' mut temp_source_code := ''
if temp_line.starts_with('import') { if temp_line.starts_with('import ') {
temp_source_code = r.functions.join('\n') + temp_line + '\n' temp_source_code = '${temp_line}\n' + r.current_source_code(false)
} else { } else {
temp_source_code = r.functions.join('\n') + r.lines.join('\n') + '\n' + r.temp_lines.join('\n') + '\n' + temp_line + '\n' temp_source_code = r.current_source_code(true) + '\n${temp_line}\n'
} }
os.write_file(temp_file, temp_source_code) os.write_file(temp_file, temp_source_code)
s := os.exec('"$vexe" -repl run $temp_file') or { s := os.exec('"$vexe" -repl run $temp_file') or {
@ -212,11 +225,10 @@ fn run_repl(workdir string, vrepl_prefix string) {
} }
r.temp_lines.delete(0) r.temp_lines.delete(0)
} }
if r.line.starts_with('import') { if r.line.starts_with('import ') {
mut lines := []string{cap: r.lines.len+1} mut imports := r.imports
lines << r.line r.imports = [r.line]
lines << r.lines r.imports << imports
r.lines = lines
} else { } else {
r.lines << r.line r.lines << r.line
} }

View File

@ -39,5 +39,10 @@ fn (p mut Parser) check_unused_imports() {
if output == '' { if output == '' {
return return
} }
if p.pref.is_repl {
// The REPL should be much more liberal, and should not warn about
// unused imports, because they probably will be in the next few lines...
return
}
eprintln('`$p.file_name` warning: the following imports were never used: $output') eprintln('`$p.file_name` warning: the following imports were never used: $output')
} }