1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vdoc: fix 'module, comment, import' sequence bug

This commit is contained in:
Delyan Angelov 2020-06-08 09:22:10 +03:00
parent 11b7b97311
commit 0058b8253d
3 changed files with 55 additions and 7 deletions

View File

@ -403,6 +403,9 @@ fn (cfg DocConfig) gen_plaintext(idx int) string {
dcs := cfg.docs[idx] dcs := cfg.docs[idx]
mut pw := strings.new_builder(200) mut pw := strings.new_builder(200)
pw.writeln('${dcs.head.content}\n') 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 { for cn in dcs.contents {
pw.writeln(cn.content) pw.writeln(cn.content)
if cn.comment.len > 0 { if cn.comment.len > 0 {
@ -553,8 +556,12 @@ fn (mut cfg DocConfig) generate_docs_from_file() {
cfg.vprintln('Rendering docs...') cfg.vprintln('Rendering docs...')
if cfg.output_path.len == 0 { if cfg.output_path.len == 0 {
outputs := cfg.render() outputs := cfg.render()
first := outputs.keys()[0] if outputs.size == 0 {
println(outputs[first]) println('No documentation for $dirs')
} else {
first := outputs.keys()[0]
println(outputs[first])
}
} else { } else {
if !os.is_dir(cfg.output_path) { if !os.is_dir(cfg.output_path) {
cfg.output_path = os.real_path('.') cfg.output_path = os.real_path('.')

View File

@ -40,6 +40,17 @@ pub mut:
parent_type string = '' 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 { pub fn get_comment_block_right_before(stmts []ast.Stmt) string {
if stmts.len == 0 { if stmts.len == 0 {
return '' return ''
@ -263,9 +274,17 @@ pub fn (mut d Doc) generate() ?bool {
} else if file_ast.mod.name != orig_mod_name { } else if file_ast.mod.name != orig_mod_name {
continue continue
} }
mut prev_comments := []ast.Stmt{}
stmts := file_ast.stmts 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)) //eprintln('stmt typeof: ' + typeof(stmt))
if stmt is ast.Comment { if stmt is ast.Comment {
prev_comments << stmt prev_comments << stmt
@ -287,6 +306,20 @@ pub fn (mut d Doc) generate() ?bool {
d.head.comment += module_comment d.head.comment += module_comment
continue 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) signature := d.get_signature(stmt)
pos := d.get_pos(stmt) pos := d.get_pos(stmt)
mut name := d.get_name(stmt) mut name := d.get_name(stmt)
@ -316,7 +349,7 @@ pub fn (mut d Doc) generate() ?bool {
} }
if stmt is ast.ConstDecl { if stmt is ast.ConstDecl {
if const_idx == -1 { if const_idx == -1 {
const_idx = o const_idx = sidx
} else { } else {
node.parent_type = 'Constants' node.parent_type = 'Constants'
} }

View File

@ -129,8 +129,16 @@ fn (mut p Parser) parse() ast.File {
module_decl := p.module_decl() module_decl := p.module_decl()
stmts << module_decl stmts << module_decl
// imports // imports
for p.tok.kind == .key_import { for {
stmts << p.import_stmt() if p.tok.kind == .key_import {
stmts << p.import_stmt()
continue
}
if p.tok.kind == .comment {
stmts << p.comment()
continue
}
break
} }
for { for {
if p.tok.kind == .eof { if p.tok.kind == .eof {