@@ -25,26 +25,7 @@ def test_reference() -> None:
2525 path = active_customers_uri .removeprefix ("file://" )
2626 read_file = open (path , "r" ).readlines ()
2727 # Get the string range in the read file
28- reference_range = references [0 ].range
29- start_line = reference_range .start .line
30- end_line = reference_range .end .line
31- start_character = reference_range .start .character
32- end_character = reference_range .end .character
33- # Get the string from the file
34-
35- # If the reference spans multiple lines, handle it accordingly
36- if start_line == end_line :
37- # Reference is on a single line
38- line_content = read_file [start_line ]
39- referenced_text = line_content [start_character :end_character ]
40- else :
41- # Reference spans multiple lines
42- referenced_text = read_file [start_line ][
43- start_character :
44- ] # First line from start_character to end
45- for line_num in range (start_line + 1 , end_line ): # Middle lines (if any)
46- referenced_text += read_file [line_num ]
47- referenced_text += read_file [end_line ][:end_character ] # Last line up to end_character
28+ referenced_text = get_string_from_range (read_file , references [0 ].range )
4829 assert referenced_text == "sushi.customers"
4930
5031
@@ -60,6 +41,32 @@ def test_reference_with_alias() -> None:
6041 references = get_model_definitions_for_a_path (lsp_context , waiter_revenue_by_day_uri )
6142 assert len (references ) == 3
6243
44+ path = waiter_revenue_by_day_uri .removeprefix ("file://" )
45+ read_file = open (path , "r" ).readlines ()
46+
6347 assert references [0 ].uri .endswith ("orders.py" )
48+ assert get_string_from_range (read_file , references [0 ].range ) == "sushi.orders"
6449 assert references [1 ].uri .endswith ("order_items.py" )
50+ assert get_string_from_range (read_file , references [1 ].range ) == "sushi.order_items"
6551 assert references [2 ].uri .endswith ("items.py" )
52+ assert get_string_from_range (read_file , references [2 ].range ) == "sushi.items"
53+
54+
55+ def get_string_from_range (file_lines , range_obj ) -> str :
56+ start_line = range_obj .start .line
57+ end_line = range_obj .end .line
58+ start_character = range_obj .start .character
59+ end_character = range_obj .end .character
60+
61+ # If the reference spans multiple lines, handle it accordingly
62+ if start_line == end_line :
63+ # Reference is on a single line
64+ line_content = file_lines [start_line ]
65+ return line_content [start_character :end_character ]
66+
67+ # Reference spans multiple lines
68+ result = file_lines [start_line ][start_character :] # First line from start_character to end
69+ for line_num in range (start_line + 1 , end_line ): # Middle lines (if any)
70+ result += file_lines [line_num ]
71+ result += file_lines [end_line ][:end_character ] # Last line up to end_character
72+ return result
0 commit comments