Skip to content

Commit d3b8c9b

Browse files
authored
checker,vls: support gotodef for fn param and []Type (#26193)
1 parent 4425446 commit d3b8c9b

4 files changed

Lines changed: 124 additions & 4 deletions

File tree

‎vlib/v/checker/fn.v‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,18 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
311311
if node.language == .v {
312312
// Make sure all types are valid
313313
for mut param in node.params {
314+
// handle vls go to definition for parameter types
315+
if c.pref.is_vls && c.pref.linfo.method == .definition {
316+
if c.vls_is_the_node(param.type_pos) {
317+
typ_str := c.table.type_to_str(param.typ)
318+
if np := c.name_pos_gotodef(typ_str) {
319+
if np.file_idx != -1 {
320+
println('${c.table.filelist[np.file_idx]}:${np.line_nr + 1}:${np.col}')
321+
}
322+
exit(0)
323+
}
324+
}
325+
}
314326
if !c.ensure_type_exists(param.typ, param.type_pos) {
315327
return
316328
}

‎vlib/v/checker/struct.v‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,31 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
190190
if c.pref.is_vls && c.pref.linfo.method == .definition {
191191
if c.vls_is_the_node(field.type_pos) {
192192
sym := c.table.sym(field.typ)
193-
pos := sym.info.get_name_pos() or { token.Pos{} }
194-
if pos.file_idx != -1 {
195-
println('${c.table.filelist[pos.file_idx]}:${pos.line_nr + 1}:${pos.col}')
196-
exit(0)
193+
elem_type := match sym.kind {
194+
.array {
195+
(sym.info as ast.Array).elem_type
196+
}
197+
.array_fixed {
198+
(sym.info as ast.ArrayFixed).elem_type
199+
}
200+
else {
201+
ast.Type(0)
202+
}
203+
}
204+
if elem_type == 0 {
205+
pos := sym.info.get_name_pos() or { token.Pos{} }
206+
if pos.file_idx != -1 {
207+
println('${c.table.filelist[pos.file_idx]}:${pos.line_nr + 1}:${pos.col}')
208+
exit(0)
209+
}
210+
} else {
211+
elem_sym := c.table.sym(elem_type)
212+
if np := elem_sym.info.get_name_pos() {
213+
if np.file_idx != -1 {
214+
println('${c.table.filelist[np.file_idx]}:${np.line_nr + 1}:${np.col}')
215+
exit(0)
216+
}
217+
}
197218
}
198219
}
199220
}

‎vlib/v/tests/vls/goto_def_test.v‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,72 @@ const test_cases = [
198198
expected: '${mod1_text_file}:25:1'
199199
description: 'Go to imported enum value definition'
200200
},
201+
// test_operator_overload() tests
202+
TestCase{
203+
name: 'operator_receiver_type'
204+
line: 137
205+
col: 8
206+
expected: '${test_file}:133:7'
207+
description: 'Go to struct definition from operator overload receiver type'
208+
},
209+
TestCase{
210+
name: 'operator_param_type'
211+
line: 137
212+
col: 22
213+
expected: '${test_file}:133:7'
214+
description: 'Go to struct definition from operator overload parameter type'
215+
},
216+
TestCase{
217+
name: 'operator_return_type'
218+
line: 137
219+
col: 33
220+
expected: '${test_file}:133:7'
221+
description: 'Go to struct definition from operator overload return type'
222+
},
223+
TestCase{
224+
name: 'struct_init_field_in_params_left'
225+
line: 138
226+
col: 23
227+
expected: '${test_file}:137:4'
228+
description: 'Go to param definition from struct init variable (left fn param)'
229+
},
230+
TestCase{
231+
name: 'struct_init_field_in_params_right'
232+
line: 138
233+
col: 31
234+
expected: '${test_file}:137:19'
235+
description: 'Go to param definition from struct init variable (right fn param)'
236+
},
237+
TestCase{
238+
name: 'field_selector_in_struct_init'
239+
line: 138
240+
col: 25
241+
expected: '${test_file}:134:1'
242+
description: 'Go to field definition from selector expression in struct init value (a.val)'
243+
},
244+
// test_function_params() tests
245+
TestCase{
246+
name: 'function_param_type'
247+
line: 141
248+
col: 24
249+
expected: '${test_file}:133:7'
250+
description: 'Go to struct definition from function parameter type'
251+
},
252+
// test_array_field_types() tests
253+
TestCase{
254+
name: 'array_elem_type_in_struct_field'
255+
line: 150
256+
col: 10
257+
expected: '${test_file}:145:7'
258+
description: 'Go to element type definition from array type in struct field ([]ArrayElemStruct)'
259+
},
260+
TestCase{
261+
name: 'fixed_array_elem_type_in_struct_field'
262+
line: 151
263+
col: 17
264+
expected: '${test_file}:145:7'
265+
description: 'Go to element type definition from fixed array type in struct field ([5]ArrayElemStruct)'
266+
},
201267
]
202268

203269
fn test_goto_definition() {

‎vlib/v/tests/vls/goto_def_test_data.vv‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,24 @@ fn test_imported_enum() {
129129
imported_e := s.PublicEnum1.a
130130
println(imported_e)
131131
}
132+
133+
struct OpStruct {
134+
val int
135+
}
136+
137+
fn (a OpStruct) + (b OpStruct) OpStruct {
138+
return OpStruct{val: a.val + b.val}
139+
}
140+
141+
fn takes_struct(param OpStruct) string {
142+
return param.str()
143+
}
144+
145+
struct ArrayElemStruct {
146+
value int
147+
}
148+
149+
struct ContainerStruct {
150+
items []ArrayElemStruct
151+
fixed_items [5]ArrayElemStruct
152+
}

0 commit comments

Comments
 (0)