From 1c4a7bc3544d355bcd041a0673758bbf072a55f9 Mon Sep 17 00:00:00 2001 From: Brad Lewis <22850972+BradLewis@users.noreply.github.com> Date: Thu, 7 Aug 2025 11:56:37 -0400 Subject: Remove incorrectly added doc comments from symbols --- src/server/analysis.odin | 2 +- src/server/ast.odin | 32 ++++++++++++++++++++------------ src/server/collector.odin | 2 +- src/server/symbol.odin | 6 +++--- 4 files changed, 25 insertions(+), 17 deletions(-) (limited to 'src/server') diff --git a/src/server/analysis.odin b/src/server/analysis.odin index 47722e8..fc5df05 100644 --- a/src/server/analysis.odin +++ b/src/server/analysis.odin @@ -1861,7 +1861,7 @@ resolve_global_identifier :: proc(ast_context: ^AstContext, node: ast.Ident, glo } if global.docs != nil { - return_symbol.doc = get_doc(global.docs, ast_context.allocator) + return_symbol.doc = get_doc(global.name_expr, global.docs, ast_context.allocator) } if global.comment != nil { diff --git a/src/server/ast.odin b/src/server/ast.odin index 326fd2d..04993d0 100644 --- a/src/server/ast.odin +++ b/src/server/ast.odin @@ -473,20 +473,28 @@ get_ast_node_string :: proc(node: ^ast.Node, src: string) -> string { return string(src[node.pos.offset:node.end.offset]) } -get_doc :: proc(comment: ^ast.Comment_Group, allocator: mem.Allocator) -> string { - if comment != nil { - tmp: string +get_doc :: proc(node: ^ast.Expr, comment: ^ast.Comment_Group, allocator: mem.Allocator) -> string { + if comment == nil { + return "" + } - for doc in comment.list { - tmp = strings.concatenate({tmp, "\n", doc.text}, context.temp_allocator) - } + // The odin parser currently incorrectly adds comments that are more than a line above + // the symbol as a doc comment. We do a quick check here to handle that specific case. + if node != nil && comment.list[len(comment.list)-1].pos.line < node.pos.line-1 { + return "" + } - if tmp != "" { - no_lines, _ := strings.replace_all(tmp, "//", "", context.temp_allocator) - no_begin_comments, _ := strings.replace_all(no_lines, "/*", "", context.temp_allocator) - no_end_comments, _ := strings.replace_all(no_begin_comments, "*/", "", context.temp_allocator) - return strings.clone(no_end_comments, allocator) - } + tmp: string + + for doc in comment.list { + tmp = strings.concatenate({tmp, "\n", doc.text}, context.temp_allocator) + } + + if tmp != "" { + no_lines, _ := strings.replace_all(tmp, "//", "", context.temp_allocator) + no_begin_comments, _ := strings.replace_all(no_lines, "/*", "", context.temp_allocator) + no_end_comments, _ := strings.replace_all(no_begin_comments, "*/", "", context.temp_allocator) + return strings.clone(no_end_comments, allocator) } return "" diff --git a/src/server/collector.odin b/src/server/collector.odin index c06879d..eaf1229 100644 --- a/src/server/collector.odin +++ b/src/server/collector.odin @@ -644,7 +644,7 @@ collect_symbols :: proc(collection: ^SymbolCollection, file: ast.File, uri: stri symbol.range = common.get_token_range(expr.name_expr, file.src) symbol.name = get_index_unique_string(collection, name) symbol.type = token_type - symbol.doc = get_doc(expr.docs, collection.allocator) + symbol.doc = get_doc(expr.name_expr, expr.docs, collection.allocator) symbol.uri = get_index_unique_string(collection, uri) comment, _ := get_file_comment(file, symbol.range.start.line + 1) symbol.comment = strings.clone(get_comment(comment), collection.allocator) diff --git a/src/server/symbol.odin b/src/server/symbol.odin index a41a310..147626b 100644 --- a/src/server/symbol.odin +++ b/src/server/symbol.odin @@ -787,7 +787,7 @@ construct_struct_field_symbol :: proc(symbol: ^Symbol, parent_name: string, valu symbol.name = value.names[index] symbol.pkg = parent_name symbol.type = .Field - symbol.doc = get_doc(value.docs[index], context.temp_allocator) + symbol.doc = get_doc(value.types[index], value.docs[index], context.temp_allocator) symbol.comment = get_comment(value.comments[index]) } @@ -797,14 +797,14 @@ construct_bit_field_field_symbol :: proc(symbol: ^Symbol, parent_name: string, v symbol.name = value.names[index] symbol.pkg = parent_name symbol.type = .Field - symbol.doc = get_doc(value.docs[index], context.temp_allocator) + symbol.doc = get_doc(value.types[index], value.docs[index], context.temp_allocator) symbol.comment = get_comment(value.comments[index]) symbol.signature = get_bit_field_field_signature(value, index) } construct_enum_field_symbol :: proc(symbol: ^Symbol, value: SymbolEnumValue, index: int) { symbol.type = .Field - symbol.doc = get_doc(value.docs[index], context.temp_allocator) + symbol.doc = get_doc(nil, value.docs[index], context.temp_allocator) symbol.comment = get_comment(value.comments[index]) symbol.signature = get_enum_field_signature(value, index) } -- cgit v1.2.3