From 0058b8253dcef7aa674442a2a6055716979374da Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 8 Jun 2020 09:22:10 +0300 Subject: [PATCH] vdoc: fix 'module, comment, import' sequence bug --- cmd/tools/vdoc.v | 11 +++++++++-- vlib/v/doc/doc.v | 39 ++++++++++++++++++++++++++++++++++++--- vlib/v/parser/parser.v | 12 ++++++++++-- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/cmd/tools/vdoc.v b/cmd/tools/vdoc.v index 8ddad56ec2..91b01173a0 100644 --- a/cmd/tools/vdoc.v +++ b/cmd/tools/vdoc.v @@ -403,6 +403,9 @@ fn (cfg DocConfig) gen_plaintext(idx int) string { dcs := cfg.docs[idx] mut pw := strings.new_builder(200) pw.writeln('${dcs.head.content}\n') + if dcs.head.comment.len > 0 { + pw.writeln('// ' + dcs.head.comment.replace('\n', '\n// ') + '\n') + } for cn in dcs.contents { pw.writeln(cn.content) if cn.comment.len > 0 { @@ -553,8 +556,12 @@ fn (mut cfg DocConfig) generate_docs_from_file() { cfg.vprintln('Rendering docs...') if cfg.output_path.len == 0 { outputs := cfg.render() - first := outputs.keys()[0] - println(outputs[first]) + if outputs.size == 0 { + println('No documentation for $dirs') + } else { + first := outputs.keys()[0] + println(outputs[first]) + } } else { if !os.is_dir(cfg.output_path) { cfg.output_path = os.real_path('.') diff --git a/vlib/v/doc/doc.v b/vlib/v/doc/doc.v index 3b6176cbe2..2ebf552d45 100644 --- a/vlib/v/doc/doc.v +++ b/vlib/v/doc/doc.v @@ -40,6 +40,17 @@ pub mut: parent_type string = '' } +pub fn merge_comments(stmts []ast.Stmt) string { + mut res := []string{} + for s in stmts { + if s is ast.Comment { + c := s as ast.Comment + res << c.text.trim_left('|') + } + } + return res.join('\n') +} + pub fn get_comment_block_right_before(stmts []ast.Stmt) string { if stmts.len == 0 { return '' @@ -263,9 +274,17 @@ pub fn (mut d Doc) generate() ?bool { } else if file_ast.mod.name != orig_mod_name { continue } - mut prev_comments := []ast.Stmt{} stmts := file_ast.stmts - for o, stmt in stmts { + // + mut last_import_stmt_idx := 0 + for sidx, stmt in stmts { + if stmt is ast.Import { + last_import_stmt_idx = sidx + } + } + mut prev_comments := []ast.Stmt{} + mut imports_section := true + for sidx, stmt in stmts { //eprintln('stmt typeof: ' + typeof(stmt)) if stmt is ast.Comment { prev_comments << stmt @@ -287,6 +306,20 @@ pub fn (mut d Doc) generate() ?bool { d.head.comment += module_comment continue } + if last_import_stmt_idx > 0 && sidx == last_import_stmt_idx { + // the accumulated comments were interspersed before/between the imports; + // just add them all to the module comment: + import_comments := merge_comments(prev_comments) + if d.head.comment != '' { + d.head.comment += '\n' + } + d.head.comment += import_comments + prev_comments = [] + imports_section = false + } + if stmt is ast.Import { + continue + } signature := d.get_signature(stmt) pos := d.get_pos(stmt) mut name := d.get_name(stmt) @@ -316,7 +349,7 @@ pub fn (mut d Doc) generate() ?bool { } if stmt is ast.ConstDecl { if const_idx == -1 { - const_idx = o + const_idx = sidx } else { node.parent_type = 'Constants' } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 7e6485ca9b..19387c8709 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -129,8 +129,16 @@ fn (mut p Parser) parse() ast.File { module_decl := p.module_decl() stmts << module_decl // imports - for p.tok.kind == .key_import { - stmts << p.import_stmt() + for { + if p.tok.kind == .key_import { + stmts << p.import_stmt() + continue + } + if p.tok.kind == .comment { + stmts << p.comment() + continue + } + break } for { if p.tok.kind == .eof {