diff options
| author | DanielGavin <danielgavin5@hotmail.com> | 2025-06-27 21:48:40 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-27 21:48:40 +0200 |
| commit | 4eae668a145df223c6cfc7b8929cdd49d436756d (patch) | |
| tree | 107fb0d242639754162e1028e61d18f8dc6bb782 /src | |
| parent | 863ad953c7ce368c001e15339a0785e1aca0bdc5 (diff) | |
| parent | 9bbb4b4450b5310a68aa4497a3125674452037d7 (diff) | |
Merge pull request #678 from BradLewis/feat/proc-hover-comments
Add proc comments to hover information
Diffstat (limited to 'src')
| -rw-r--r-- | src/server/analysis.odin | 9 | ||||
| -rw-r--r-- | src/server/ast.odin | 42 | ||||
| -rw-r--r-- | src/server/collector.odin | 2 | ||||
| -rw-r--r-- | src/server/completion.odin | 8 | ||||
| -rw-r--r-- | src/server/hover.odin | 2 | ||||
| -rw-r--r-- | src/testing/testing.odin | 2 |
6 files changed, 49 insertions, 16 deletions
diff --git a/src/server/analysis.odin b/src/server/analysis.odin index c485a43..b43dce7 100644 --- a/src/server/analysis.odin +++ b/src/server/analysis.odin @@ -1559,6 +1559,7 @@ internal_resolve_type_identifier :: proc(ast_context: ^AstContext, node: ast.Ide } return_symbol.doc = get_doc(global.docs, ast_context.allocator) + return_symbol.comment = get_comment(global.comment) return return_symbol, ok } else { @@ -3764,6 +3765,7 @@ concatenate_raw_symbol_information :: proc(ast_context: ^AstContext, symbol: Sym symbol.name, symbol.signature, symbol.type, + symbol.comment, is_completion, ) } @@ -3774,6 +3776,7 @@ concatenate_raw_string_information :: proc( name: string, signature: string, type: SymbolType, + comment: string, is_completion: bool, ) -> string { pkg := path.base(pkg, false, context.temp_allocator) @@ -3782,6 +3785,12 @@ concatenate_raw_string_information :: proc( return fmt.tprintf("%v: package", name) } else if type == .Keyword && is_completion { return name + } else if type == .Function { + if comment != "" { + return fmt.tprintf("%v\n%v.%v: %v", comment, pkg, name, signature) + } + return fmt.tprintf("%v.%v: %v", pkg, name, signature) + } else { if signature != "" { return fmt.tprintf("%v.%v: %v", pkg, name, signature) diff --git a/src/server/ast.odin b/src/server/ast.odin index 9780537..9abc1e5 100644 --- a/src/server/ast.odin +++ b/src/server/ast.odin @@ -76,6 +76,7 @@ GlobalExpr :: struct { expr: ^ast.Expr, mutable: bool, docs: ^ast.Comment_Group, + comment: ^ast.Comment_Group, attributes: []^ast.Attribute, deprecated: bool, private: parser.Private_Flag, @@ -296,6 +297,7 @@ collect_value_decl :: proc( global_expr := GlobalExpr { mutable = value_decl.is_mutable, docs = value_decl.docs, + comment = get_file_comment(file, value_decl.pos.line), attributes = value_decl.attributes[:], private = file_tags.private, } @@ -477,6 +479,13 @@ get_doc :: proc(comment: ^ast.Comment_Group, allocator: mem.Allocator) -> string return "" } +get_comment :: proc(comment: ^ast.Comment_Group) -> string { + if comment != nil && len(comment.list) > 0 { + return comment.list[0].text + } + return "" +} + free_ast :: proc { free_ast_node, free_ast_array, @@ -1221,22 +1230,27 @@ construct_struct_field_docs :: proc(file: ast.File, v: ^ast.Struct_Type) { } } else { // We need to check the file to see if it contains a line comment as there is no next field - // TODO: linear scan might be a bit slow for files with lots of comments? - for c in file.comments { - if c.pos.line == field.pos.line { - for item, j in c.list { - field.comment = ast.new(ast.Comment_Group, item.pos, parser.end_pos(item)) - if j == len(c.list) - 1 { - field.comment.list = c.list[j:] - } else { - field.comment.list = c.list[j:j + 1] - } - break - } - } - } + field.comment = get_file_comment(file, field.pos.line) } } } +} +// Retrives the comment group from the specified line of the file +get_file_comment :: proc(file: ast.File, line: int) -> ^ast.Comment_Group { + // TODO: linear scan might be a bit slow for files with lots of comments? + for c in file.comments { + if c.pos.line == line { + for item, j in c.list { + comment := ast.new(ast.Comment_Group, item.pos, parser.end_pos(item)) + if j == len(c.list) - 1 { + comment.list = c.list[j:] + } else { + comment.list = c.list[j:j + 1] + } + return comment + } + } + } + return nil } diff --git a/src/server/collector.odin b/src/server/collector.odin index 667e8c8..c59a46c 100644 --- a/src/server/collector.odin +++ b/src/server/collector.odin @@ -610,6 +610,8 @@ collect_symbols :: proc(collection: ^SymbolCollection, file: ast.File, uri: stri symbol.name = get_index_unique_string(collection, name) symbol.type = token_type symbol.doc = get_doc(expr.docs, collection.allocator) + comment := get_file_comment(file, symbol.range.start.line + 1) + symbol.comment = strings.clone(get_comment(comment), collection.allocator) if expr.builtin || strings.contains(uri, "builtin.odin") { symbol.pkg = "$builtin" diff --git a/src/server/completion.odin b/src/server/completion.odin index 62c098c..2e4c0a9 100644 --- a/src/server/completion.odin +++ b/src/server/completion.odin @@ -1215,6 +1215,7 @@ get_identifier_completion :: proc( name: string, type: SymbolType, doc: string, + comment: string, pkg: string, signature: string, flags: SymbolFlags, @@ -1260,6 +1261,7 @@ get_identifier_completion :: proc( type = r.symbol.type, name = r.symbol.name, doc = r.symbol.doc, + comment = r.symbol.comment, flags = r.symbol.flags, signature = r.symbol.signature, pkg = r.symbol.pkg, @@ -1303,6 +1305,7 @@ get_identifier_completion :: proc( type = symbol.type, name = ident.name, doc = symbol.doc, + comment = symbol.comment, flags = symbol.flags, pkg = symbol.pkg, signature = symbol.signature, @@ -1344,6 +1347,7 @@ get_identifier_completion :: proc( type = symbol.type, name = clean_ident(ident.name), doc = symbol.doc, + comment = symbol.comment, flags = symbol.flags, pkg = symbol.pkg, signature = symbol.signature, @@ -1372,6 +1376,7 @@ get_identifier_completion :: proc( type = symbol.type, name = symbol.name, doc = symbol.doc, + comment = symbol.comment, flags = symbol.flags, signature = symbol.signature, pkg = symbol.pkg, @@ -1394,6 +1399,7 @@ get_identifier_completion :: proc( type = symbol.type, name = symbol.name, doc = symbol.doc, + comment = symbol.comment, flags = symbol.flags, signature = symbol.signature, pkg = symbol.pkg, @@ -1416,6 +1422,7 @@ get_identifier_completion :: proc( type = symbol.type, name = symbol.name, doc = symbol.doc, + comment = symbol.comment, flags = symbol.flags, signature = symbol.signature, pkg = symbol.pkg, @@ -1494,6 +1501,7 @@ get_identifier_completion :: proc( result.name, result.signature, result.type, + result.comment, true, ) diff --git a/src/server/hover.odin b/src/server/hover.odin index dd4af97..e69249b 100644 --- a/src/server/hover.odin +++ b/src/server/hover.odin @@ -40,7 +40,7 @@ write_hover_content :: proc(ast_context: ^AstContext, symbol: Symbol) -> MarkupC if cat != "" { content.kind = "markdown" - content.value = fmt.tprintf("```odin\n%v\n```\n%v", cat, symbol.doc) + content.value = fmt.tprintf("```odin\n%v\n```%v", cat, symbol.doc) } else { content.kind = "plaintext" } diff --git a/src/testing/testing.odin b/src/testing/testing.odin index 29fbd6f..4500676 100644 --- a/src/testing/testing.odin +++ b/src/testing/testing.odin @@ -252,7 +252,7 @@ expect_hover :: proc(t: ^testing.T, src: ^Source, expect_hover_string: string) { return } - content_without_markdown := hover.contents.value[8:len(hover.contents.value) - 5] + content_without_markdown, _ := strings.remove(hover.contents.value[8:], "\n```", 1, context.temp_allocator) if content_without_markdown != expect_hover_string { log.errorf("Expected hover string:\n%q, but received:\n%q", expect_hover_string, content_without_markdown) |