@@ -82,21 +82,24 @@ fn on_goto_definition_inner(
8282mod goto_definition_tests {
8383 use std:: panic;
8484
85- use crate :: test_utils:: { self , search_in_file } ;
85+ use crate :: test_utils:: { self , search_in_text } ;
8686 use async_lsp:: lsp_types:: { Position , Range } ;
8787 use tokio:: test;
8888
8989 use super :: * ;
9090
91- async fn expect_goto_for_all_references ( directory : & str , name : & str , definition_index : usize ) {
92- let ( mut state, noir_text_document) = test_utils:: init_lsp_server ( directory) . await ;
93-
94- let ranges = search_in_file ( noir_text_document. path ( ) , name) ;
91+ /// Run goto-definition from every occurrence of `name` in `src` and assert each lands at
92+ /// the `definition_index`-th occurrence. The definition position itself is skipped because
93+ /// goto on a definition does not currently return itself.
94+ async fn expect_goto_for_all_references ( src : & str , name : & str , definition_index : usize ) {
95+ let ranges = search_in_text ( src, name) ;
9596 let expected_range = ranges[ definition_index] ;
9697
98+ let ( mut state, noir_text_document) =
99+ test_utils:: init_lsp_server_with_inline_source ( "document_symbol" , "src/main.nr" , src)
100+ . await ;
101+
97102 for ( index, range) in ranges. iter ( ) . enumerate ( ) {
98- // Ideally "go to" at the definition should return the same location, but this isn't currently
99- // working. But it's also not that important, so we'll keep it for later.
100103 if index == definition_index {
101104 continue ;
102105 }
@@ -127,6 +130,41 @@ mod goto_definition_tests {
127130 }
128131 }
129132
133+ /// Run goto-definition at the `>|<` cursor in `src` and assert the response points within
134+ /// the same file at `expected_range`.
135+ async fn expect_goto_inline ( src : & str , expected_range : Range ) {
136+ let ( mut state, noir_text_document, position, _src) =
137+ test_utils:: init_lsp_server_with_inline_source_and_cursor (
138+ "document_symbol" ,
139+ "src/main.nr" ,
140+ src,
141+ )
142+ . await ;
143+
144+ let params = GotoDefinitionParams {
145+ text_document_position_params : lsp_types:: TextDocumentPositionParams {
146+ text_document : lsp_types:: TextDocumentIdentifier {
147+ uri : noir_text_document. clone ( ) ,
148+ } ,
149+ position,
150+ } ,
151+ work_done_progress_params : Default :: default ( ) ,
152+ partial_result_params : Default :: default ( ) ,
153+ } ;
154+
155+ let response = on_goto_definition_request ( & mut state, params)
156+ . await
157+ . expect ( "Could execute on_goto_definition_request" )
158+ . unwrap_or_else ( || panic ! ( "Didn't get a goto definition response" ) ) ;
159+
160+ if let GotoDefinitionResponse :: Scalar ( location) = response {
161+ assert_eq ! ( location. uri, noir_text_document) ;
162+ assert_eq ! ( location. range, expected_range) ;
163+ } else {
164+ panic ! ( "Expected a scalar response" ) ;
165+ }
166+ }
167+
130168 async fn expect_goto (
131169 directory : & str ,
132170 position : Position ,
@@ -161,15 +199,36 @@ mod goto_definition_tests {
161199
162200 #[ test]
163201 async fn goto_from_function_location_to_declaration ( ) {
164- expect_goto_for_all_references ( "go_to_definition" , "another_function" , 0 ) . await ;
202+ expect_goto_for_all_references (
203+ r#"fn another_function() -> Field {
204+ 1
205+ }
206+
207+ fn main() {
208+ another_function();
209+ another_function();
210+ }
211+ "# ,
212+ "another_function" ,
213+ 0 ,
214+ )
215+ . await ;
165216 }
166217
167218 #[ test]
168219 async fn goto_from_use_as ( ) {
169- expect_goto (
170- "go_to_definition" ,
171- Position { line : 7 , character : 29 } , // The word after `as`,
172- "src/main.nr" ,
220+ expect_goto_inline (
221+ r#"mod foo {
222+ pub fn another_function() -> Field { 1 }
223+ }
224+
225+ use foo::another_function as >|<aliased_function;
226+
227+ fn main() {
228+ let _ = aliased_function();
229+ }
230+ "# ,
231+ // `another_function` in `pub fn another_function`
173232 Range {
174233 start : Position { line : 1 , character : 11 } ,
175234 end : Position { line : 1 , character : 27 } ,
@@ -208,10 +267,14 @@ mod goto_definition_tests {
208267
209268 #[ test]
210269 async fn goto_module_from_use_path ( ) {
211- expect_goto (
212- "go_to_definition" ,
213- Position { line : 6 , character : 4 } , // "foo" in "use foo::another_function;"
214- "src/main.nr" ,
270+ expect_goto_inline (
271+ r#"mod foo {
272+ pub fn another_function() -> Field { 1 }
273+ }
274+
275+ use >|<foo::another_function;
276+ "# ,
277+ // `foo` in `mod foo {`
215278 Range {
216279 start : Position { line : 0 , character : 4 } ,
217280 end : Position { line : 0 , character : 7 } ,
@@ -236,32 +299,37 @@ mod goto_definition_tests {
236299
237300 #[ test]
238301 async fn goto_for_local_variable ( ) {
239- expect_goto_for_all_references ( "local_variable" , "some_var" , 0 ) . await ;
302+ expect_goto_for_all_references (
303+ r#"fn main() {
304+ let some_var = 1;
305+ let _ = some_var + some_var;
306+ }
307+ "# ,
308+ "some_var" ,
309+ 0 ,
310+ )
311+ . await ;
240312 }
241313
242314 #[ test]
243315 async fn goto_at_struct_definition_finds_same_struct ( ) {
244- expect_goto (
245- "go_to_definition" ,
246- Position { line : 21 , character : 7 } , // "Foo" in "struct Foo"
247- "src/main.nr" ,
316+ expect_goto_inline (
317+ "struct >|<Foo {}\n " ,
248318 Range {
249- start : Position { line : 21 , character : 7 } ,
250- end : Position { line : 21 , character : 10 } ,
319+ start : Position { line : 0 , character : 7 } ,
320+ end : Position { line : 0 , character : 10 } ,
251321 } ,
252322 )
253323 . await ;
254324 }
255325
256326 #[ test]
257327 async fn goto_at_trait_definition_finds_same_trait ( ) {
258- expect_goto (
259- "go_to_definition" ,
260- Position { line : 25 , character : 6 } , // "Trait" in "trait Trait"
261- "src/main.nr" ,
328+ expect_goto_inline (
329+ "trait >|<Trait {}\n " ,
262330 Range {
263- start : Position { line : 25 , character : 6 } ,
264- end : Position { line : 25 , character : 11 } ,
331+ start : Position { line : 0 , character : 6 } ,
332+ end : Position { line : 0 , character : 11 } ,
265333 } ,
266334 )
267335 . await ;
@@ -283,22 +351,37 @@ mod goto_definition_tests {
283351
284352 #[ test]
285353 async fn goto_attribute_function ( ) {
286- expect_goto (
287- "go_to_definition" ,
288- Position { line : 31 , character : 3 } , // "attr"
289- "src/main.nr" ,
354+ expect_goto_inline (
355+ r#"#[>|<attr]
356+ pub fn foo() {}
357+
358+ comptime fn attr(_: FunctionDefinition) -> Quoted {
359+ quote { pub fn hello() {} }
360+ }
361+ "# ,
362+ // `attr` in `comptime fn attr(...)`
290363 Range {
291- start : Position { line : 34 , character : 12 } ,
292- end : Position { line : 34 , character : 16 } ,
364+ start : Position { line : 3 , character : 12 } ,
365+ end : Position { line : 3 , character : 16 } ,
293366 } ,
294367 )
295368 . await ;
296369 }
297370
298371 #[ test]
299372 async fn goto_reference_in_doc_comment ( ) {
300- let ( mut state, noir_text_document) = test_utils:: init_lsp_server ( "go_to_definition" ) . await ;
301- let position = Position { line : 38 , character : 10 } ;
373+ let src = r#"struct Foo {}
374+
375+ /// See [F>|<oo].
376+ fn test_doc_comment() {}
377+ "# ;
378+ let ( mut state, noir_text_document, position, _src) =
379+ test_utils:: init_lsp_server_with_inline_source_and_cursor (
380+ "document_symbol" ,
381+ "src/main.nr" ,
382+ src,
383+ )
384+ . await ;
302385
303386 let params = GotoDefinitionParams {
304387 text_document_position_params : lsp_types:: TextDocumentPositionParams {
@@ -320,30 +403,23 @@ mod goto_definition_tests {
320403 } ;
321404 assert_eq ! ( links. len( ) , 1 ) ;
322405 let link = & links[ 0 ] ;
323- assert ! ( link. target_uri. to_string ( ) . ends_with ( "src/main.nr" ) ) ;
406+ assert_eq ! ( link. target_uri, noir_text_document ) ;
324407
325408 // This range is `[Foo]` in the doc comment
326409 assert_eq ! (
327410 link. origin_selection_range,
328411 Some ( Range {
329- start: Position { line: 38 , character: 8 } ,
330- end: Position { line: 38 , character: 13 } ,
412+ start: Position { line: 2 , character: 8 } ,
413+ end: Position { line: 2 , character: 13 } ,
331414 } , )
332415 ) ;
333416
334- assert_eq ! (
335- link. target_range,
336- Range {
337- start: Position { line: 21 , character: 7 } ,
338- end: Position { line: 21 , character: 10 } ,
339- }
340- ) ;
341- assert_eq ! (
342- link. target_selection_range,
343- Range {
344- start: Position { line: 21 , character: 7 } ,
345- end: Position { line: 21 , character: 10 } ,
346- }
347- ) ;
417+ // `Foo` in `struct Foo {}` — line 0, chars 7-10
418+ let foo_def_range = Range {
419+ start : Position { line : 0 , character : 7 } ,
420+ end : Position { line : 0 , character : 10 } ,
421+ } ;
422+ assert_eq ! ( link. target_range, foo_def_range) ;
423+ assert_eq ! ( link. target_selection_range, foo_def_range) ;
348424 }
349425}
0 commit comments