mirror of
https://github.com/vlang/v.git
synced 2025-08-03 17:57:59 -04:00
tools: fix vrepl for import mod { f }
(#24340)
This commit is contained in:
parent
880f2169b7
commit
146c766dc9
@ -23,7 +23,7 @@ mut:
|
||||
folder string // the folder in which the repl will write its temporary source files
|
||||
last_output string // the last repl output
|
||||
|
||||
modules []string // all the import modules
|
||||
modules map[string][]string // all the import modules
|
||||
alias map[string]string // all the alias used in the import
|
||||
includes []string // all the #include statements
|
||||
functions []string // all the user function declarations
|
||||
@ -91,7 +91,11 @@ fn new_repl(folder string) Repl {
|
||||
skip_empty: true
|
||||
}
|
||||
folder: folder
|
||||
modules: ['os', 'time', 'math']
|
||||
modules: {
|
||||
'os': []
|
||||
'time': []
|
||||
'math': []
|
||||
}
|
||||
vstartup_lines: vstartup_source
|
||||
// Test file used to check if a function as a void return or a value return.
|
||||
eval_func_lines: vstartup_source
|
||||
@ -205,11 +209,18 @@ fn (r &Repl) is_function_call(line string) bool {
|
||||
// to a sequence of V source code lines
|
||||
fn (r &Repl) import_to_source_code() []string {
|
||||
mut imports_line := []string{}
|
||||
for mod in r.modules {
|
||||
for mod, value in r.modules {
|
||||
mut import_str := 'import ${mod}'
|
||||
if mod in r.alias {
|
||||
import_str += ' as ${r.alias[mod]}'
|
||||
}
|
||||
if value.len > 0 {
|
||||
import_str += '{ '
|
||||
for val in value {
|
||||
import_str += '${val}, '
|
||||
}
|
||||
import_str += '}'
|
||||
}
|
||||
imports_line << endline_if_missed(import_str)
|
||||
}
|
||||
return imports_line
|
||||
@ -312,17 +323,25 @@ fn (mut r Repl) parse_import(line string) {
|
||||
tokens := r.line.fields()
|
||||
// module name
|
||||
mod := tokens[1]
|
||||
if mod !in r.modules {
|
||||
r.modules << mod
|
||||
}
|
||||
// Check if the import contains an alias
|
||||
// import mod_name as alias_mod
|
||||
// set alias
|
||||
if line.contains('as ') && tokens.len >= 4 {
|
||||
alias := tokens[3]
|
||||
if mod !in r.alias {
|
||||
r.alias[mod] = alias
|
||||
}
|
||||
}
|
||||
|
||||
// set value
|
||||
if line.contains('{') && line.contains('}') {
|
||||
values := line.split('{')[1].split('}')[0]
|
||||
for value in values.split(',') {
|
||||
r.modules[mod] << value
|
||||
}
|
||||
} else {
|
||||
if mod !in r.modules {
|
||||
r.modules[mod] = []string{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// clear the screen, then list source code
|
||||
|
4
vlib/v/slow_tests/repl/from_import.repl
Normal file
4
vlib/v/slow_tests/repl/from_import.repl
Normal file
@ -0,0 +1,4 @@
|
||||
import json { encode }
|
||||
encode('123')
|
||||
===output===
|
||||
"123"
|
Loading…
x
Reference in New Issue
Block a user