diff options
| author | DanielGavin <danielgavin5@hotmail.com> | 2024-06-09 12:55:24 +0200 |
|---|---|---|
| committer | DanielGavin <danielgavin5@hotmail.com> | 2024-06-09 12:55:24 +0200 |
| commit | 33b5b66a53fef0ea3fd9ccec4841290050cd95f4 (patch) | |
| tree | 3e656887b76fed9b1cc1b59bf0addd04f7596f74 | |
| parent | ada14e0c911a11129daafbd486386e761661f40b (diff) | |
Improve reference testing
| -rw-r--r-- | src/server/analysis.odin | 4 | ||||
| -rw-r--r-- | src/server/file_resolve.odin | 4 | ||||
| -rw-r--r-- | src/server/references.odin | 10 | ||||
| -rw-r--r-- | src/server/symbol.odin | 2 | ||||
| -rw-r--r-- | src/testing/testing.odin | 25 | ||||
| -rw-r--r-- | tests/references_test.odin | 116 |
6 files changed, 111 insertions, 50 deletions
diff --git a/src/server/analysis.odin b/src/server/analysis.odin index acd5733..7b4d125 100644 --- a/src/server/analysis.odin +++ b/src/server/analysis.odin @@ -4797,14 +4797,14 @@ get_document_position_node :: proc( case ^Ellipsis: get_document_position(n.expr, position_context) case ^Proc_Lit: - get_document_position(n.type, position_context) - if position_in_node(n.body, position_context.position) { + get_document_position(n.type, position_context) position_context.function = cast(^Proc_Lit)node append(&position_context.functions, position_context.function) get_document_position(n.body, position_context) } else if position_in_node(n.type, position_context.position) { position_context.function = cast(^Proc_Lit)node + get_document_position(n.type, position_context) } case ^Comp_Lit: //only set this for the parent comp literal, since we will need to walk through it to infer types. diff --git a/src/server/file_resolve.odin b/src/server/file_resolve.odin index d6119c5..3c1ed17 100644 --- a/src/server/file_resolve.odin +++ b/src/server/file_resolve.odin @@ -142,7 +142,6 @@ resolve_node :: proc(node: ^ast.Node, data: ^FileResolveData) { case ^Bad_Expr: case ^Ident: data.position_context.identifier = node - if data.flag != .None { if symbol, ok := resolve_location_identifier(data.ast_context, n^); ok { @@ -421,10 +420,9 @@ resolve_node :: proc(node: ^ast.Node, data: ^FileResolveData) { }, } } - } else { - resolve_nodes(n.names, data) } + resolve_nodes(n.names, data) resolve_node(n.type, data) resolve_node(n.default_value, data) case ^Field_List: diff --git a/src/server/references.odin b/src/server/references.odin index a23d968..f82a80e 100644 --- a/src/server/references.odin +++ b/src/server/references.odin @@ -36,9 +36,15 @@ walk_directories :: proc( } if strings.contains(info.name, ".odin") { - slash_path, _ := filepath.to_slash(info.fullpath) + slash_path, _ := filepath.to_slash( + info.fullpath, + context.temp_allocator, + ) if slash_path != document.fullpath { - append(&fullpaths, strings.clone(info.fullpath)) + append( + &fullpaths, + strings.clone(info.fullpath, context.temp_allocator), + ) } } diff --git a/src/server/symbol.odin b/src/server/symbol.odin index 2e9aa27..9e0f154 100644 --- a/src/server/symbol.odin +++ b/src/server/symbol.odin @@ -215,6 +215,7 @@ free_symbol :: proc(symbol: Symbol, allocator: mem.Allocator) { common.free_ast(v.arg_types, allocator) case SymbolStructValue: delete(v.names, allocator) + delete(v.ranges, allocator) common.free_ast(v.types, allocator) case SymbolGenericValue: common.free_ast(v.expr, allocator) @@ -248,6 +249,7 @@ free_symbol :: proc(symbol: Symbol, allocator: mem.Allocator) { case SymbolPackageValue: case SymbolBitFieldValue: delete(v.names, allocator) + delete(v.ranges, allocator) common.free_ast(v.types, allocator) } } diff --git a/src/testing/testing.odin b/src/testing/testing.odin index 2a08b23..210db59 100644 --- a/src/testing/testing.odin +++ b/src/testing/testing.odin @@ -384,42 +384,33 @@ expect_definition_locations :: proc( } } -expect_symbol_location :: proc( +expect_reference_locations :: proc( t: ^testing.T, src: ^Source, - flag: server.ResolveReferenceFlag, expect_locations: []common.Location, ) { setup(src) defer teardown(src) - symbol_and_nodes := server.resolve_entire_file( - src.document, - flag, - context.temp_allocator, - ) - - ok := true + locations, ok := server.get_references(src.document, src.position) - for location in expect_locations { + for expect_location in expect_locations { match := false - for k, v in symbol_and_nodes { - if v.symbol.range == location.range { + for location in locations { + if location.range == expect_location.range { match = true } } if !match { ok = false - log.errorf("Failed to match with location: %v", location) + log.errorf("Failed to match with location: %v", expect_location) } } if !ok { log.error("Received:") - for k, v in symbol_and_nodes { - log.errorf("%v \n", v.symbol) + for location in locations { + log.errorf("%v \n", location) } } - - } diff --git a/tests/references_test.odin b/tests/references_test.odin index aa2579b..cadaff4 100644 --- a/tests/references_test.odin +++ b/tests/references_test.odin @@ -12,56 +12,46 @@ reference_variables_in_function :: proc(t: ^testing.T) { my_function :: proc() { a := 2 b := a - c := 2 + b + c := 2 + b{*} } `, packages = {}, } - test.expect_symbol_location( + test.expect_reference_locations( t, &source, - .Identifier, { { range = { - start = {line = 2, character = 3}, - end = {line = 2, character = 4}, - }, - }, - { - range = { start = {line = 3, character = 3}, end = {line = 3, character = 4}, }, }, { range = { - start = {line = 4, character = 3}, - end = {line = 4, character = 4}, + start = {line = 4, character = 12}, + end = {line = 4, character = 13}, }, }, }, ) } - @(test) reference_variables_in_function_parameters :: proc(t: ^testing.T) { source := test.Source { main = `package test my_function :: proc(a: int) { - b := a - c := 2 + b + b := a{*} } `, packages = {}, } - test.expect_symbol_location( + test.expect_reference_locations( t, &source, - .Identifier, { { range = { @@ -69,6 +59,30 @@ reference_variables_in_function_parameters :: proc(t: ^testing.T) { end = {line = 1, character = 23}, }, }, + }, + ) +} + +@(test) +reference_selectors_in_function :: proc(t: ^testing.T) { + source := test.Source { + main = `package test + My_Struct :: struct { + a: int, + } + + my_function :: proc() { + my: My_Struct + my.a{*} = 2 + } + `, + packages = {}, + } + + test.expect_reference_locations( + t, + &source, + { { range = { start = {line = 2, character = 3}, @@ -77,39 +91,89 @@ reference_variables_in_function_parameters :: proc(t: ^testing.T) { }, { range = { - start = {line = 3, character = 3}, - end = {line = 3, character = 4}, + start = {line = 7, character = 6}, + end = {line = 7, character = 7}, }, }, }, ) } + @(test) -reference_selectors_in_function :: proc(t: ^testing.T) { +reference_field_comp_lit :: proc(t: ^testing.T) { source := test.Source { main = `package test + Foo :: struct { + soo_many_cases: int, + } + My_Struct :: struct { - a: int, + foo: Foo, } - my_function :: proc() { - my: My_Struct - my.a = 2 + my_function :: proc(my_struct: My_Struct) { + my := My_Struct { + foo = {soo_many_cases{*} = 2}, + } } `, packages = {}, } - test.expect_symbol_location( + test.expect_reference_locations( t, &source, - .Field, { { range = { start = {line = 2, character = 3}, - end = {line = 2, character = 4}, + end = {line = 2, character = 17}, + }, + }, + { + range = { + start = {line = 11, character = 11}, + end = {line = 11, character = 25}, + }, + }, + }, + ) +} + +@(test) +reference_field_comp_lit_infer_from_function :: proc(t: ^testing.T) { + source := test.Source { + main = `package test + Foo :: struct { + soo_many_cases: int, + } + + My_Struct :: struct { + foo: Foo, + } + + my_function :: proc(my_struct: My_Struct) { + my_function({foo = {soo_many_cases{*} = 2}}) + } + `, + packages = {}, + } + + test.expect_reference_locations( + t, + &source, + { + { + range = { + start = {line = 2, character = 3}, + end = {line = 2, character = 17}, + }, + }, + { + range = { + start = {line = 10, character = 23}, + end = {line = 10, character = 37}, }, }, }, |