From 0975f3bd4cfe41744f534ee8ad519a7942910a95 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 28 Aug 2020 01:54:16 +0300 Subject: [PATCH] vfmt: support `x chan Name` in fn args --- vlib/v/fmt/fmt.v | 13 ++++++++++++- vlib/v/fmt/tests/chan_init_keep.vv | 12 ++++++++++++ vlib/v/table/atypes.v | 11 ++++++----- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 2fd2012afb..4e03611348 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -725,12 +725,20 @@ pub fn (mut f Fmt) prefix_expr_cast_expr(fexpr ast.Expr) { pub fn (f &Fmt) type_to_str(t table.Type) string { mut res := f.table.type_to_str(t) - map_prefix := 'map[string]' cur_mod := f.cur_mod + '.' + // + map_prefix := 'map[string]' has_map_prefix := res.starts_with(map_prefix) if has_map_prefix { res = res.replace(map_prefix, '') } + // + chan_prefix := 'chan ' + has_chan_prefix := res.starts_with(chan_prefix) + if has_chan_prefix { + res = res.replace(chan_prefix, '') + } + // no_symbols := res.trim_left('&[]') should_shorten := no_symbols.starts_with(cur_mod) // @@ -752,6 +760,9 @@ pub fn (f &Fmt) type_to_str(t table.Type) string { if should_shorten { res = res.replace_once(cur_mod, '') } + if has_chan_prefix { + res = chan_prefix + res + } if has_map_prefix { res = map_prefix + res } diff --git a/vlib/v/fmt/tests/chan_init_keep.vv b/vlib/v/fmt/tests/chan_init_keep.vv index 22edab9bc9..4acca35a9d 100644 --- a/vlib/v/fmt/tests/chan_init_keep.vv +++ b/vlib/v/fmt/tests/chan_init_keep.vv @@ -4,6 +4,18 @@ struct FSMEvent { x int } +fn abc(n FSMEvent) { +} + +fn (e FSMEvent) abc(n FSMEvent) { +} + +fn (e FSMEvent) x(ch chan FSMEvent) { +} + +fn produce_events(ch chan FSMEvent) { +} + fn main() { ch_fsm_events := chan FSMEvent{cap: 1000} eprintln('ch_fsm_events.len: $ch_fsm_events.len') diff --git a/vlib/v/table/atypes.v b/vlib/v/table/atypes.v index 2791ec7169..06b52bb19c 100644 --- a/vlib/v/table/atypes.v +++ b/vlib/v/table/atypes.v @@ -810,6 +810,9 @@ pub fn (table &Table) type_to_str(t Type) string { res = res.replace('map_string_', 'map[string]') map_start = 'map[string]' } + if sym.kind == .chan || 'chan_' in res { + res = res.replace('chan_', '') + } // mod.submod.submod2.Type => submod2.Type if res.contains('.') { vals := res.split('.') @@ -826,6 +829,9 @@ pub fn (table &Table) type_to_str(t Type) string { if sym.kind == .map && !res.starts_with('map') { res = map_start + res } + if sym.kind == .chan && !res.starts_with('chan') { + res = 'chan ' + res + } } nr_muls := t.nr_muls() if nr_muls > 0 { @@ -838,11 +844,6 @@ pub fn (table &Table) type_to_str(t Type) string { res = '?' + res } } - /* - if res.starts_with(cur_mod +'.') { - res = res[cur_mod.len+1.. ] - } - */ return res }