Skip to content

v.fmt #

fn fmt #

fn fmt(file ast.File, mut table ast.Table, pref_ &pref.Preferences, is_debug bool, options FmtOptions) string

enum CommentsLevel #

enum CommentsLevel {

struct AttrsOptions #

struct AttrsOptions {
	same_line bool

struct CommentsOptions #

struct CommentsOptions {
	has_nl    bool = true
	same_line bool
	level     CommentsLevel
	prev_line int = -1

CommentsOptions defines the way comments are going to be written- has_nl: adds an newline at the end of a list of comments

  • same_line: line comments will be on the same line as the last statement
  • level: either .keep (don't indent), or .indent (increment indentation)
  • prev_line: the line number of the previous token to save linebreaks

struct Fmt #

struct Fmt {
	pref &pref.Preferences = unsafe { nil }
pub mut:
	file               ast.File
	table              &ast.Table = unsafe { nil }
	is_debug           bool
	out                strings.Builder
	out_imports        strings.Builder
	indent             int
	empty_line         bool
	line_len           int    // the current line length, Note: it counts \t as 4 spaces, and starts at 0 after f.writeln
	buffering          bool   // disables line wrapping for exprs that will be analyzed later
	par_level          int    // how many parentheses are put around the current expression
	array_init_break   []bool // line breaks after elements in hierarchy level of multi dimensional array
	array_init_depth   int    // current level of hierarchy in array init
	single_line_if     bool
	cur_mod            string
	did_imports        bool
	import_pos         int // position of the imports in the resulting string
	auto_imports       map[string]bool   // potentially hidden imports(`sync` when using channels) and preludes(when embedding files)
	used_imports       map[string]bool   // to remove unused imports
	import_syms_used   map[string]bool   // to remove unused import symbols
	mod2alias          map[string]string // for `import time as t`, will contain: 'time'=>'t'
	mod2syms           map[string]string // import time { now } ''=>'now'
	use_short_fn_args  bool
	single_line_fields bool   // should struct fields be on a single line
	it_name            string // the name to replace `it` with
	in_lambda_depth    int
	inside_const       bool
	inside_unsafe      bool
	inside_comptime_if bool
	is_assign          bool
	is_index_expr      bool
	is_mbranch_expr    bool // match a { x...y { } }
	is_struct_init     bool
	fn_scope           &ast.Scope = unsafe { nil }
	wsinfix_depth      int
	format_state       FormatState
	source_text        string // can be set by `echo "println('hi')" | v fmt`, i.e. when processing source not from a file, but from stdin. In this case, it will contain the entire input text. You can use f.file.path otherwise, and read from that file.

fn (Fmt) alias_type_decl #

fn (mut f Fmt) alias_type_decl(node ast.AliasTypeDecl)

fn (Fmt) anon_fn #

fn (mut f Fmt) anon_fn(node ast.AnonFn)

fn (Fmt) array_decompose #

fn (mut f Fmt) array_decompose(node ast.ArrayDecompose)

=== Specific Expr methods ===//

fn (Fmt) array_init #

fn (mut f Fmt) array_init(node ast.ArrayInit)

fn (Fmt) as_cast #

fn (mut f Fmt) as_cast(node ast.AsCast)

fn (Fmt) assert_stmt #

fn (mut f Fmt) assert_stmt(node ast.AssertStmt)

=== Specific Stmt methods ===//

fn (Fmt) assign_stmt #

fn (mut f Fmt) assign_stmt(node ast.AssignStmt)

fn (Fmt) assoc #

fn (mut f Fmt) assoc(node ast.Assoc)

fn (Fmt) at_expr #

fn (mut f Fmt) at_expr(node ast.AtExpr)

fn (Fmt) attrs #

fn (mut f Fmt) attrs(attrs []ast.Attr)

fn (Fmt) block #

fn (mut f Fmt) block(node ast.Block)

fn (Fmt) branch_stmt #

fn (mut f Fmt) branch_stmt(node ast.BranchStmt)

fn (Fmt) calculate_alignment #

fn (mut f Fmt) calculate_alignment(fields []ast.StructField, mut field_aligns []AlignInfo, mut comment_aligns []AlignInfo, mut default_expr_aligns []AlignInfo, mut field_types []string)

fn (Fmt) call_args #

fn (mut f Fmt) call_args(args []ast.CallArg)

fn (Fmt) call_expr #

fn (mut f Fmt) call_expr(node ast.CallExpr)

fn (Fmt) cast_expr #

fn (mut f Fmt) cast_expr(node ast.CastExpr)

fn (Fmt) chan_init #

fn (mut f Fmt) chan_init(mut node ast.ChanInit)

fn (Fmt) char_literal #

fn (mut f Fmt) char_literal(node ast.CharLiteral)

fn (Fmt) comment #

fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions)

fn (Fmt) comments #

fn (mut f Fmt) comments(comments []ast.Comment, options CommentsOptions)

fn (Fmt) comments_after_last_field #

fn (mut f Fmt) comments_after_last_field(comments []ast.Comment)

fn (Fmt) comments_before_field #

fn (mut f Fmt) comments_before_field(comments []ast.Comment)

fn (Fmt) comptime_call #

fn (mut f Fmt) comptime_call(node ast.ComptimeCall)

fn (Fmt) comptime_for #

fn (mut f Fmt) comptime_for(node ast.ComptimeFor)

fn (Fmt) comptime_selector #

fn (mut f Fmt) comptime_selector(node ast.ComptimeSelector)

fn (Fmt) concat_expr #

fn (mut f Fmt) concat_expr(node ast.ConcatExpr)

fn (Fmt) const_decl #

fn (mut f Fmt) const_decl(node ast.ConstDecl)

fn (Fmt) debugger_stmt #

fn (mut f Fmt) debugger_stmt(node ast.DebuggerStmt)

fn (Fmt) defer_stmt #

fn (mut f Fmt) defer_stmt(node ast.DeferStmt)

fn (Fmt) dump_expr #

fn (mut f Fmt) dump_expr(node ast.DumpExpr)

fn (Fmt) enum_decl #

fn (mut f Fmt) enum_decl(node ast.EnumDecl)

fn (Fmt) enum_val #

fn (mut f Fmt) enum_val(node ast.EnumVal)

fn (Fmt) expr #

fn (mut f Fmt) expr(node_ ast.Expr)

=== General Expr-related methods and helpers ===//

fn (Fmt) expr_stmt #

fn (mut f Fmt) expr_stmt(node ast.ExprStmt)

fn (Fmt) fn_decl #

fn (mut f Fmt) fn_decl(node ast.FnDecl)

fn (Fmt) fn_type_decl #

fn (mut f Fmt) fn_type_decl(node ast.FnTypeDecl)

fn (Fmt) for_c_stmt #

fn (mut f Fmt) for_c_stmt(node ast.ForCStmt)

fn (Fmt) for_in_stmt #

fn (mut f Fmt) for_in_stmt(node ast.ForInStmt)

fn (Fmt) for_stmt #

fn (mut f Fmt) for_stmt(node ast.ForStmt)

fn (Fmt) get_source_lines #

fn (mut f Fmt) get_source_lines() []string

fn (Fmt) global_decl #

fn (mut f Fmt) global_decl(node ast.GlobalDecl)

fn (Fmt) go_expr #

fn (mut f Fmt) go_expr(node ast.GoExpr)

fn (Fmt) goto_label #

fn (mut f Fmt) goto_label(node ast.GotoLabel)

fn (Fmt) goto_stmt #

fn (mut f Fmt) goto_stmt(node ast.GotoStmt)

fn (Fmt) hash_stmt #

fn (mut f Fmt) hash_stmt(node ast.HashStmt)

fn (Fmt) ident #

fn (mut f Fmt) ident(node ast.Ident)

fn (Fmt) if_expr #

fn (mut f Fmt) if_expr(node ast.IfExpr)

fn (Fmt) if_guard_expr #

fn (mut f Fmt) if_guard_expr(node ast.IfGuardExpr)

fn (Fmt) imp_stmt_str #

fn (f Fmt) imp_stmt_str(imp ast.Import) string

fn (Fmt) import_comments #

fn (mut f Fmt) import_comments(comments []ast.Comment, options CommentsOptions)

fn (Fmt) imports #

fn (mut f Fmt) imports(imports []ast.Import)

fn (Fmt) index_expr #

fn (mut f Fmt) index_expr(node ast.IndexExpr)

fn (Fmt) infix_expr #

fn (mut f Fmt) infix_expr(node ast.InfixExpr)

fn (Fmt) interface_decl #

fn (mut f Fmt) interface_decl(node ast.InterfaceDecl)

fn (Fmt) interface_field #

fn (mut f Fmt) interface_field(field ast.StructField, field_align AlignInfo)

fn (Fmt) interface_method #

fn (mut f Fmt) interface_method(method ast.FnDecl)

fn (Fmt) is_ref_type #

fn (mut f Fmt) is_ref_type(node ast.IsRefType)

fn (Fmt) likely #

fn (mut f Fmt) likely(node ast.Likely)

fn (Fmt) lock_expr #

fn (mut f Fmt) lock_expr(node ast.LockExpr)

fn (Fmt) map_init #

fn (mut f Fmt) map_init(node ast.MapInit)

fn (Fmt) mark_import_as_used #

fn (mut f Fmt) mark_import_as_used(name string)

fn (Fmt) mark_types_import_as_used #

fn (mut f Fmt) mark_types_import_as_used(typ ast.Type)

=== Import-related methods ===//

fn (Fmt) match_expr #

fn (mut f Fmt) match_expr(node ast.MatchExpr)

fn (Fmt) module_stmt #

fn (mut f Fmt) module_stmt(mod ast.Module)

fn (Fmt) no_cur_mod #

fn (mut f Fmt) no_cur_mod(typename string) string

fn (Fmt) node_str #

fn (mut f Fmt) node_str(node ast.Node) string

fn (Fmt) offset_of #

fn (mut f Fmt) offset_of(node ast.OffsetOf)

fn (Fmt) or_expr #

fn (mut f Fmt) or_expr(node ast.OrExpr)

fn (Fmt) par_expr #

fn (mut f Fmt) par_expr(node ast.ParExpr)

fn (Fmt) postfix_expr #

fn (mut f Fmt) postfix_expr(node ast.PostfixExpr)

fn (Fmt) prefix_expr #

fn (mut f Fmt) prefix_expr(node ast.PrefixExpr)

fn (Fmt) process_file_imports #

fn (mut f Fmt) process_file_imports(file &ast.File)

fn (Fmt) range_expr #

fn (mut f Fmt) range_expr(node ast.RangeExpr)

fn (Fmt) remove_new_line #

fn (mut f Fmt) remove_new_line(cfg RemoveNewLineConfig) string

When the removal action actually occurs, the string of the last line after the removal is returned

fn (Fmt) return_stmt #

fn (mut f Fmt) return_stmt(node ast.Return)

fn (Fmt) select_expr #

fn (mut f Fmt) select_expr(node ast.SelectExpr)

fn (Fmt) selector_expr #

fn (mut f Fmt) selector_expr(node ast.SelectorExpr)

fn (Fmt) set_current_module_name #

fn (mut f Fmt) set_current_module_name(cmodname string)

=== Module handling helper methods ===//

fn (Fmt) short_module #

fn (mut f Fmt) short_module(name string) string => bar.fn()

fn (Fmt) single_line_attrs #

fn (mut f Fmt) single_line_attrs(attrs []ast.Attr, options AttrsOptions)

fn (Fmt) size_of #

fn (mut f Fmt) size_of(node ast.SizeOf)

fn (Fmt) spawn_expr #

fn (mut f Fmt) spawn_expr(node ast.SpawnExpr)

fn (Fmt) sql_expr #

fn (mut f Fmt) sql_expr(node ast.SqlExpr)

fn (Fmt) sql_stmt #

fn (mut f Fmt) sql_stmt(node ast.SqlStmt)

fn (Fmt) sql_stmt_line #

fn (mut f Fmt) sql_stmt_line(node ast.SqlStmtLine)

fn (Fmt) stmt #

fn (mut f Fmt) stmt(node ast.Stmt)

fn (Fmt) stmts #

fn (mut f Fmt) stmts(stmts []ast.Stmt)

=== General Stmt-related methods and helpers ===//

fn (Fmt) string_inter_literal #

fn (mut f Fmt) string_inter_literal(node ast.StringInterLiteral)

fn (Fmt) string_literal #

fn (mut f Fmt) string_literal(node ast.StringLiteral)

fn (Fmt) struct_decl #

fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool)

fn (Fmt) struct_init #

fn (mut f Fmt) struct_init(node ast.StructInit)

fn (Fmt) sum_type_decl #

fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl)

fn (Fmt) type_decl #

fn (mut f Fmt) type_decl(node ast.TypeDecl)

fn (Fmt) type_expr #

fn (mut f Fmt) type_expr(node ast.TypeNode)

fn (Fmt) type_of #

fn (mut f Fmt) type_of(node ast.TypeOf)

fn (Fmt) unsafe_expr #

fn (mut f Fmt) unsafe_expr(node ast.UnsafeExpr)

fn (Fmt) vfmt_off #

fn (mut f Fmt) vfmt_off(off_line int)

fn (Fmt) vfmt_on #

fn (mut f Fmt) vfmt_on(on_line int)

fn (Fmt) wrap_infix #

fn (mut f Fmt) wrap_infix(start_pos int, start_len int, is_cond bool)

fn (Fmt) wrap_long_line #

fn (mut f Fmt) wrap_long_line(penalty_idx int, add_indent bool) bool

fn (Fmt) write #

fn (mut f Fmt) write(s string)

=== Basic buffer write operations ===//

fn (Fmt) writeln #

fn (mut f Fmt) writeln(s string)

struct FmtOptions #

struct FmtOptions {
	source_text string

struct RemoveNewLineConfig #

struct RemoveNewLineConfig {
	imports_buffer bool // Work on f.out_imports instead of f.out