Skip to content

Commit b5167a7

Browse files
authored
parser: add documentation support in ast.VlsInfo (#25503)
1 parent 5ead7bc commit b5167a7

6 files changed

Lines changed: 385 additions & 78 deletions

File tree

‎vlib/v/ast/table.v‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub mut:
8888
gostmts int // how many `go` statements there were in the parsed files.
8989
// When table.gostmts > 0, __VTHREADS__ is defined, which can be checked with `$if threads {`
9090
enum_decls map[string]EnumDecl
91-
vls_info map[string]VLSInfo
91+
vls_info map[string]VlsInfo
9292
module_deprecated map[string]bool
9393
module_attrs map[string][]Attr // module attributes
9494
builtin_pub_fns map[string]bool
@@ -113,12 +113,10 @@ pub mut:
113113
c_str string
114114
}
115115

116-
pub struct VLSInfo {
116+
pub struct VlsInfo {
117117
pub mut:
118-
pos token.Pos
119-
pre_comments []Comment
120-
comments []Comment
121-
end_comments []Comment
118+
pos token.Pos
119+
doc string // documentation
122120
}
123121

124122
// used by vls to avoid leaks
@@ -2777,6 +2775,6 @@ pub fn (mut t Table) get_veb_result_type_idx() int {
27772775
}
27782776

27792777
@[inline]
2780-
pub fn (mut t Table) register_vls_info(key string, val VLSInfo) {
2778+
pub fn (mut t Table) register_vls_info(key string, val VlsInfo) {
27812779
t.vls_info[key] = val
27822780
}

‎vlib/v/parser/enum.v‎

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
4848
}
4949
p.check(.key_enum)
5050
end_pos := p.tok.pos()
51+
mut comments_before_key_enum := if p.is_vls {
52+
p.cur_comments.clone()
53+
} else {
54+
[]
55+
}
5156
if p.disallow_declarations_in_script_mode() {
5257
return ast.EnumDecl{}
5358
}
@@ -253,20 +258,39 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
253258

254259
if !already_exists {
255260
p.table.register_enum_decl(enum_decl)
256-
if p.pref.is_vls {
261+
if p.is_vls {
257262
key := 'enum_${name}'
258-
val := ast.VLSInfo{
259-
pos: typ_pos
260-
comments: enum_decl_comments
263+
mut has_decl_end_comment := false
264+
if enum_decl.comments.len > 0
265+
&& enum_decl.comments[0].pos.line_nr == enum_decl.pos.line_nr {
266+
// enum MyEnum { // MyEnum end_comment1
267+
comments_before_key_enum << enum_decl.comments[0]
268+
has_decl_end_comment = true
269+
}
270+
val := ast.VlsInfo{
271+
pos: typ_pos
272+
doc: p.keyword_comments_to_string(enum_name, comments_before_key_enum)
261273
}
262274
p.table.register_vls_info(key, val)
263-
for f in fields {
275+
for i, f in fields {
264276
f_key := 'enum_${name}.${f.name}'
265-
f_val := ast.VLSInfo{
266-
pos: f.pos
267-
pre_comments: f.pre_comments
268-
comments: f.comments
269-
end_comments: f.next_comments
277+
f_val := if i == 0 {
278+
first_field_pre_comment := if has_decl_end_comment {
279+
enum_decl.comments[1..].clone()
280+
} else {
281+
enum_decl.comments
282+
}
283+
ast.VlsInfo{
284+
pos: f.pos
285+
doc: p.comments_to_string(first_field_pre_comment) +
286+
p.comments_to_string(f.comments)
287+
}
288+
} else {
289+
ast.VlsInfo{
290+
pos: f.pos
291+
doc: p.comments_to_string(fields[i - 1].next_comments) +
292+
p.comments_to_string(f.comments)
293+
}
270294
}
271295
p.table.register_vls_info(f_key, f_val)
272296
}

‎vlib/v/parser/fn.v‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
301301
p.next()
302302
}
303303
p.check(.key_fn)
304+
mut comments_before_key_fn := if p.is_vls {
305+
p.cur_comments.clone()
306+
} else {
307+
[]
308+
}
304309
comments << p.eat_comments()
305310
p.open_scope()
306311
defer {
@@ -753,16 +758,17 @@ run them via `v file.v` instead',
753758
p.table.register_fn_generic_types(fn_decl.fkey())
754759
}
755760
p.label_names = []
756-
if p.pref.is_vls {
761+
if p.is_vls {
757762
type_str := if (is_method || is_static_type_method) && rec.typ != ast.no_type {
758763
p.table.sym(rec.typ.idx_type()).name.all_after_last('.')
759764
} else {
760765
''
761766
}
762767
key := 'fn_${p.mod}[${type_str}]${short_fn_name}'
763-
val := ast.VLSInfo{
764-
pos: fn_decl.pos
765-
comments: fn_decl.comments // TODO: we need comment just before fn decl
768+
val := ast.VlsInfo{
769+
pos: fn_decl.pos
770+
doc: p.keyword_comments_to_string(short_fn_name, comments_before_key_fn) +
771+
p.comments_to_string(fn_decl.end_comments)
766772
}
767773
p.table.register_vls_info(key, val)
768774
}

0 commit comments

Comments
 (0)