From e76e0a97e1a13d2622c5d220392cf77723f4e4d5 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 2 May 2022 23:48:37 +0300 Subject: [PATCH] vdoc: fix panic on empty `//` comment on `v doc -f html file.v`; turn `expected code block after empty example` to a warning --- cmd/tools/vdoc/html.v | 2 +- .../vdoc/tests/testdata/basic/main.comments.out | 10 ++++++++-- cmd/tools/vdoc/tests/testdata/basic/main.out | 9 +++++++-- cmd/tools/vdoc/tests/testdata/basic/main.v | 9 ++++++++- cmd/tools/vdoc/utils.v | 16 +++++++++++----- vlib/v/doc/node.v | 3 +-- 6 files changed, 36 insertions(+), 13 deletions(-) diff --git a/cmd/tools/vdoc/html.v b/cmd/tools/vdoc/html.v index bbecb8bc13..392567ad80 100644 --- a/cmd/tools/vdoc/html.v +++ b/cmd/tools/vdoc/html.v @@ -303,7 +303,7 @@ fn html_highlight(code string, tb &ast.Table) string { } else if typ == .char { '`$tok.lit`' } else if typ == .comment { - if tok.lit[0] == 1 { '//${tok.lit[1..]}' } else { '//$tok.lit' } + if tok.lit != '' && tok.lit[0] == 1 { '//${tok.lit[1..]}' } else { '//$tok.lit' } } else { tok.lit } diff --git a/cmd/tools/vdoc/tests/testdata/basic/main.comments.out b/cmd/tools/vdoc/tests/testdata/basic/main.comments.out index 326f0c5d0a..5f4a0b4f27 100644 --- a/cmd/tools/vdoc/tests/testdata/basic/main.comments.out +++ b/cmd/tools/vdoc/tests/testdata/basic/main.comments.out @@ -1,7 +1,13 @@ module main const ( - source_root = 'temp' + source_root = 'temp' // some const + another = int(5) // ) +const ( + windowpos_undefined_mask = C.SDL_WINDOWPOS_UNDEFINED_MASK // 0x1FFF0000u + windowpos_undefined = C.SDL_WINDOWPOS_UNDEFINED // +) + Used to indicate that you don't care what the window position is. fn funky() - funky - comment for function below \ No newline at end of file + funky - comment for function below diff --git a/cmd/tools/vdoc/tests/testdata/basic/main.out b/cmd/tools/vdoc/tests/testdata/basic/main.out index 08fe504b85..1f053ec9ec 100644 --- a/cmd/tools/vdoc/tests/testdata/basic/main.out +++ b/cmd/tools/vdoc/tests/testdata/basic/main.out @@ -1,6 +1,11 @@ module main const ( - source_root = 'temp' + source_root = 'temp' // some const + another = int(5) // ) -fn funky() \ No newline at end of file +const ( + windowpos_undefined_mask = C.SDL_WINDOWPOS_UNDEFINED_MASK // 0x1FFF0000u + windowpos_undefined = C.SDL_WINDOWPOS_UNDEFINED // +) +fn funky() diff --git a/cmd/tools/vdoc/tests/testdata/basic/main.v b/cmd/tools/vdoc/tests/testdata/basic/main.v index 9cb66e3498..a274697653 100644 --- a/cmd/tools/vdoc/tests/testdata/basic/main.v +++ b/cmd/tools/vdoc/tests/testdata/basic/main.v @@ -1,5 +1,12 @@ pub const ( - source_root = 'temp' + source_root = 'temp' // some const + another = int(5) // +) + +// Used to indicate that you don't care what the window position is. +pub const ( + windowpos_undefined_mask = C.SDL_WINDOWPOS_UNDEFINED_MASK // 0x1FFF0000u + windowpos_undefined = C.SDL_WINDOWPOS_UNDEFINED // ) // funky - comment for function below diff --git a/cmd/tools/vdoc/utils.v b/cmd/tools/vdoc/utils.v index 50c93581ec..714b78d968 100644 --- a/cmd/tools/vdoc/utils.v +++ b/cmd/tools/vdoc/utils.v @@ -162,7 +162,11 @@ fn color_highlight(code string, tb &ast.Table) string { lit = term.yellow('`$tok.lit`') } .comment { - lit = if tok.lit[0] == 1 { '//${tok.lit[1..]}' } else { '//$tok.lit' } + lit = if tok.lit != '' && tok.lit[0] == 1 { + '//${tok.lit[1..]}' + } else { + '//$tok.lit' + } } .keyword { lit = term.bright_blue(tok.lit) @@ -209,16 +213,18 @@ fn color_highlight(code string, tb &ast.Table) string { } else if next_tok.kind in [.lcbr, .rpar, .eof, .comma, .pipe, .name, .rcbr, .assign, .key_pub, .key_mut, .pipe, .comma] && prev.kind in [.name, .amp, .rsbr, .key_type, .assign, .dot, .question, .rpar, .key_struct, .key_enum, .pipe, .key_interface] - && (tok.lit[0].is_capital() || prev_prev.lit in ['C', 'JS']) { + && ((tok.lit != '' && tok.lit[0].is_capital()) + || prev_prev.lit in ['C', 'JS']) { tok_typ = .symbol - } else if next_tok.kind == .lpar || (!tok.lit[0].is_capital() - && next_tok.kind == .lt && next_tok.pos == tok.pos + tok.lit.len) { + } else if next_tok.kind == .lpar + || (!(tok.lit != '' && tok.lit[0].is_capital()) && next_tok.kind == .lt + && next_tok.pos == tok.pos + tok.lit.len) { tok_typ = .function } else if next_tok.kind == .dot { if tok.lit in ['C', 'JS'] { tok_typ = .prefix } else { - if tok.lit[0].is_capital() { + if tok.lit != '' && tok.lit[0].is_capital() { tok_typ = .symbol } else { tok_typ = .module_ diff --git a/vlib/v/doc/node.v b/vlib/v/doc/node.v index 2e72997a84..490817124a 100644 --- a/vlib/v/doc/node.v +++ b/vlib/v/doc/node.v @@ -62,13 +62,12 @@ pub fn (dc DocNode) merge_comments_without_examples() string { if dc.comments[i].is_multi_line_example() { i++ if i == dc.comments.len || !dc.comments[i].has_triple_backtick() { - eprintln('$dc.file_path:$dc.pos.line_nr: Expected code block after empty example line:') + eprintln('$dc.file_path:$dc.pos.line_nr: warning: expected code block after empty example line:') eprintln('// ```') if i < dc.comments.len { eprintln('Found:') eprintln('//' + dc.comments[i].text[1..]) } - exit(1) } i++ for i < dc.comments.len && !dc.comments[i].has_triple_backtick() {