diff options
| author | Brad Lewis <22850972+BradLewis@users.noreply.github.com> | 2025-07-04 13:03:44 -0400 |
|---|---|---|
| committer | Brad Lewis <22850972+BradLewis@users.noreply.github.com> | 2025-07-04 20:39:32 -0400 |
| commit | 5227e9ff89b0290eefcec945867fa216aca7f51e (patch) | |
| tree | cea97a6f851cb83fe0786beb716b9150dd8fad2d /src | |
| parent | d91f1da376e12f7ea8eef54528dc6f5ace6cf8bb (diff) | |
Add using bit_field information to StructSymbolValue to use for documentation
Diffstat (limited to 'src')
| -rw-r--r-- | src/server/analysis.odin | 2 | ||||
| -rw-r--r-- | src/server/documentation.odin | 24 | ||||
| -rw-r--r-- | src/server/symbol.odin | 68 |
3 files changed, 83 insertions, 11 deletions
diff --git a/src/server/analysis.odin b/src/server/analysis.odin index bfdf5ba..766913d 100644 --- a/src/server/analysis.odin +++ b/src/server/analysis.odin @@ -2836,7 +2836,7 @@ make_symbol_struct_from_ast :: proc( } b := symbol_struct_value_builder_make(symbol, ast_context.allocator) - write_struct_type(ast_context, &b, v^, ident, attributes, -1, inlined) + write_struct_type(ast_context, &b, v, ident, attributes, -1, inlined) symbol = to_symbol(b) return symbol } diff --git a/src/server/documentation.odin b/src/server/documentation.odin index b86147f..be54b40 100644 --- a/src/server/documentation.odin +++ b/src/server/documentation.odin @@ -352,7 +352,17 @@ write_struct_hover :: proc(ast_context: ^AstContext, sb: ^strings.Builder, v: Sy l += len(using_prefix) } if l > longestNameLen { - longestNameLen = len(name) + longestNameLen = l + } + } + + longest_type_len := 0 + type_names := make([dynamic]string, 0, len(v.types), ast_context.allocator) + for t in v.types { + type_name := node_to_string(t) + append(&type_names, type_name) + if len(type_name) > longest_type_len { + longest_type_len = len(type_name) } } @@ -396,6 +406,11 @@ write_struct_hover :: proc(ast_context: ^AstContext, sb: ^strings.Builder, v: Sy if index := v.from_usings[i]; index != using_index { fmt.sbprintf(sb, "\n\t// from `using %s: ", v.names[index]) build_string_node(v.types[index], sb, false) + if backing_type, ok := v.backing_types[index]; ok { + strings.write_string(sb, " (bit_field ") + build_string_node(backing_type, sb, false) + strings.write_string(sb, ")") + } strings.write_string(sb, "`\n") using_index = index } @@ -408,8 +423,11 @@ write_struct_hover :: proc(ast_context: ^AstContext, sb: ^strings.Builder, v: Sy strings.write_string(sb, using_prefix) name_len += len(using_prefix) } - fmt.sbprintf(sb, "%s:%*s", v.names[i], longestNameLen - name_len + 1, "") - build_string_node(v.types[i], sb, false) + fmt.sbprintf(sb, "%s:%*s%s", v.names[i], longestNameLen - name_len + 1, "", type_names[i]) + if bit_size, ok := v.bit_sizes[i]; ok { + fmt.sbprintf(sb, "%*s| ", longest_type_len - len(type_names[i]) + 1, "") + build_string_node(bit_size, sb, false) + } strings.write_string(sb, ",") append_comments(sb, v.comments, i) strings.write_string(sb, "\n") diff --git a/src/server/symbol.odin b/src/server/symbol.odin index e9b9037..d1e88ad 100644 --- a/src/server/symbol.odin +++ b/src/server/symbol.odin @@ -36,6 +36,10 @@ SymbolStructValue :: struct { args: []^ast.Expr, //The arguments in the call expression for poly docs: []^ast.Comment_Group, comments: []^ast.Comment_Group, + + // Extra fields for embedded bit fields via usings + backing_types: map[int]^ast.Expr, // the base type for the bit field + bit_sizes: map[int]^ast.Expr, // the bit size of the bit field field } SymbolBitFieldValue :: struct { @@ -216,6 +220,8 @@ SymbolStructValueBuilder :: struct { unexpanded_usings: [dynamic]int, poly: ^ast.Field_List, poly_names: [dynamic]string, + backing_types: map[int]^ast.Expr, + bit_sizes: map[int]^ast.Expr, } symbol_struct_value_builder_make_none :: proc(allocator := context.allocator) -> SymbolStructValueBuilder { @@ -230,6 +236,8 @@ symbol_struct_value_builder_make_none :: proc(allocator := context.allocator) -> from_usings = make([dynamic]int, allocator), unexpanded_usings = make([dynamic]int, allocator), poly_names = make([dynamic]string, allocator), + backing_types = make(map[int]^ast.Expr, allocator), + bit_sizes = make(map[int]^ast.Expr, allocator), } } @@ -249,6 +257,8 @@ symbol_struct_value_builder_make_symbol :: proc( from_usings = make([dynamic]int, allocator), unexpanded_usings = make([dynamic]int, allocator), poly_names = make([dynamic]string, allocator), + backing_types = make(map[int]^ast.Expr, allocator), + bit_sizes = make(map[int]^ast.Expr, allocator), } } @@ -269,6 +279,8 @@ symbol_struct_value_builder_make_symbol_symbol_struct_value :: proc( from_usings = slice.to_dynamic(v.from_usings, allocator), unexpanded_usings = slice.to_dynamic(v.unexpanded_usings, allocator), poly_names = slice.to_dynamic(v.poly_names, allocator), + backing_types = v.backing_types, + bit_sizes = v.bit_sizes, } } @@ -297,21 +309,22 @@ to_symbol_struct_value :: proc(b: SymbolStructValueBuilder) -> SymbolStructValue unexpanded_usings = b.unexpanded_usings[:], poly = b.poly, poly_names = b.poly_names[:], + backing_types = b.backing_types, + bit_sizes = b.bit_sizes, } } write_struct_type :: proc( ast_context: ^AstContext, b: ^SymbolStructValueBuilder, - v: ast.Struct_Type, + v: ^ast.Struct_Type, ident: ast.Ident, attributes: []^ast.Attribute, base_using_index: int, inlined := false, ) { b.poly = v.poly_params - v := v - construct_struct_field_docs(ast_context.file, &v) + construct_struct_field_docs(ast_context.file, v) for field, i in v.fields.list { for n in field.names { if identifier, ok := n.derived.(^ast.Ident); ok && field.type != nil { @@ -385,12 +398,49 @@ write_symbol_struct_value :: proc( for u in v.unexpanded_usings { append(&b.unexpanded_usings, u+base_index) } + for k, value in v.backing_types { + b.backing_types[k+base_index] = value + } + for k, value in v.bit_sizes { + b.bit_sizes[k+base_index] = value + } for k in v.usings { b.usings[k+base_index] = struct{}{} } expand_usings(ast_context, b) } +write_symbol_bitfield_value :: proc( + ast_context: ^AstContext, + b: ^SymbolStructValueBuilder, + v: SymbolBitFieldValue, + base_using_index: int, +) { + base_index := len(b.names) + for name, i in v.names { + append(&b.names, name) + append(&b.from_usings, base_using_index) + } + for type in v.types { + append(&b.types, type) + } + for range in v.ranges { + append(&b.ranges, range) + } + for doc in v.docs { + append(&b.docs, doc) + } + for comment in v.comments { + append(&b.comments, comment) + } + b.backing_types[base_using_index] = v.backing_type + for bit_size, i in v.bit_sizes { + b.bit_sizes[i+base_index] = bit_size + } + expand_usings(ast_context, b) + +} + expand_usings :: proc(ast_context: ^AstContext, b: ^SymbolStructValueBuilder) { base := len(b.names) - 1 for len(b.unexpanded_usings) > 0 { @@ -409,23 +459,27 @@ expand_usings :: proc(ast_context: ^AstContext, b: ^SymbolStructValueBuilder) { if ident, ok := field_expr.derived.(^ast.Ident); ok { if v, ok := struct_type_from_identifier(ast_context, ident^); ok { - write_struct_type(ast_context, b, v^, ident^, {}, u, true) + write_struct_type(ast_context, b, v, ident^, {}, u, true) } else { clear(&ast_context.recursion_map) if symbol, ok := resolve_type_identifier(ast_context, ident^); ok { if v, ok := symbol.value.(SymbolStructValue); ok { write_symbol_struct_value(ast_context, b, v, u) + } else if v, ok := symbol.value.(SymbolBitFieldValue); ok { + write_symbol_bitfield_value(ast_context, b, v, u) } } } } else if selector, ok := field_expr.derived.(^ast.Selector_Expr); ok { - if s, ok := resolve_selector_expression(ast_context, selector); ok { - if v, ok := s.value.(SymbolStructValue); ok { + if symbol, ok := resolve_selector_expression(ast_context, selector); ok { + if v, ok := symbol.value.(SymbolStructValue); ok { write_symbol_struct_value(ast_context, b, v, u) + } else if v, ok := symbol.value.(SymbolBitFieldValue); ok { + write_symbol_bitfield_value(ast_context, b, v, u) } } } else if v, ok := field_expr.derived.(^ast.Struct_Type); ok { - write_struct_type(ast_context, b, v^, ast_context.field_name, {}, u) + write_struct_type(ast_context, b, v, ast_context.field_name, {}, u) } delete_key(&ast_context.recursion_map, b.types[u]) } |