Skip to content

Commit a499951

Browse files
Also prefer functions which return the expected type
1 parent 5812b08 commit a499951

11 files changed

+164
-8
lines changed

compiler-core/src/language_server/completer.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ impl<'a, IO> Completer<'a, IO> {
136136
// type is, which generally is the case.
137137
if expected_type.is_unbound() {
138138
TypeMatch::Unknown
139+
}
140+
// We also want to prioritise functions which return the desired type,
141+
// as often the user's intention will be to write a function call.
142+
else if let Some((_, return_)) = type_.fn_types()
143+
&& expected_type.same_as(&return_)
144+
{
145+
TypeMatch::Matching
139146
} else if expected_type.same_as(type_) {
140147
TypeMatch::Matching
141148
} else {

compiler-core/src/language_server/tests/completion.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,3 +2266,31 @@ pub fn main() -> Bool {
22662266

22672267
assert_completion!(TestProject::for_source(code), Position::new(5, 3));
22682268
}
2269+
2270+
#[test]
2271+
fn prefer_function_which_returns_expected_type() {
2272+
let code = "
2273+
pub fn main() -> Int {
2274+
a
2275+
}
2276+
2277+
fn add(a, b) { a + b }
2278+
fn sub(a, b) { a - b }
2279+
fn addf(a, b) { a +. b }
2280+
";
2281+
2282+
assert_completion!(TestProject::for_source(code), Position::new(2, 3));
2283+
}
2284+
2285+
#[test]
2286+
fn prefer_function_which_returns_expected_generic_type() {
2287+
let code = "
2288+
pub fn main() -> Result(Int, Nil) {
2289+
let result = Ok(12)
2290+
let result2 = Error(True)
2291+
r
2292+
}
2293+
";
2294+
2295+
assert_completion!(TestProject::for_source(code), Position::new(4, 3));
2296+
}

compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__completion__argument_shadowing.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ True
3333
main
3434
kind: Function
3535
detail: fn(Int) -> fn(Float) -> a
36-
sort: 2_main
36+
sort: 02_main
3737
desc: app
3838
edits:
3939
[3:0-3:0]: "main"

compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__completion__local_variable_anonymous_function.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ True
3232
main
3333
kind: Function
3434
detail: fn() -> Int
35-
sort: 2_main
35+
sort: 02_main
3636
desc: app
3737
edits:
3838
[2:34-2:34]: "main"

compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__completion__local_variable_ignore_anonymous_function_args_nested.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ True
3636
add_two
3737
kind: Variable
3838
detail: fn(Int) -> Int
39-
sort: 2_add_two
39+
sort: 02_add_two
4040
desc: app
4141
docs: "A locally defined variable."
4242
edits:
4343
[5:4-5:4]: "add_two"
4444
main
4545
kind: Function
4646
detail: fn() -> Int
47-
sort: 2_main
47+
sort: 02_main
4848
desc: app
4949
edits:
5050
[5:4-5:4]: "main"

compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__completion__local_variable_ignore_anonymous_function_returned.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ True
3535
add_two
3636
kind: Variable
3737
detail: fn(Int) -> Int
38-
sort: 2_add_two
38+
sort: 02_add_two
3939
desc: app
4040
docs: "A locally defined variable."
4141
edits:

compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__completion__local_variable_nested_anonymous_function.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ True
3636
main
3737
kind: Function
3838
detail: fn() -> Int
39-
sort: 2_main
39+
sort: 02_main
4040
desc: app
4141
edits:
4242
[4:36-4:36]: "main"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
source: compiler-core/src/language_server/tests/completion.rs
3+
expression: "\npub fn main() -> Result(Int, Nil) {\n let result = Ok(12)\n let result2 = Error(True)\n r\n}\n"
4+
---
5+
pub fn main() -> Result(Int, Nil) {
6+
let result = Ok(12)
7+
let result2 = Error(True)
8+
r|
9+
}
10+
11+
12+
----- Completion content -----
13+
Error
14+
kind: Constructor
15+
detail: gleam
16+
sort: 04_Error
17+
False
18+
kind: EnumMember
19+
detail: gleam
20+
sort: 4_False
21+
Nil
22+
kind: EnumMember
23+
detail: gleam
24+
sort: 4_Nil
25+
Ok
26+
kind: Constructor
27+
detail: gleam
28+
sort: 04_Ok
29+
True
30+
kind: EnumMember
31+
detail: gleam
32+
sort: 4_True
33+
main
34+
kind: Function
35+
detail: fn() -> Result(Int, Nil)
36+
sort: 02_main
37+
desc: app
38+
edits:
39+
[4:2-4:2]: "main"
40+
result
41+
kind: Variable
42+
detail: Result(Int, a)
43+
sort: 02_result
44+
desc: app
45+
docs: "A locally defined variable."
46+
edits:
47+
[4:2-4:2]: "result"
48+
result2
49+
kind: Variable
50+
detail: Result(a, Bool)
51+
sort: 2_result2
52+
desc: app
53+
docs: "A locally defined variable."
54+
edits:
55+
[4:2-4:2]: "result2"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
source: compiler-core/src/language_server/tests/completion.rs
3+
expression: "\npub fn main() -> Int {\n a\n}\n\nfn add(a, b) { a + b }\nfn sub(a, b) { a - b }\nfn addf(a, b) { a +. b }\n"
4+
---
5+
pub fn main() -> Int {
6+
a|
7+
}
8+
9+
fn add(a, b) { a + b }
10+
fn sub(a, b) { a - b }
11+
fn addf(a, b) { a +. b }
12+
13+
14+
----- Completion content -----
15+
Error
16+
kind: Constructor
17+
detail: gleam
18+
sort: 4_Error
19+
False
20+
kind: EnumMember
21+
detail: gleam
22+
sort: 4_False
23+
Nil
24+
kind: EnumMember
25+
detail: gleam
26+
sort: 4_Nil
27+
Ok
28+
kind: Constructor
29+
detail: gleam
30+
sort: 4_Ok
31+
True
32+
kind: EnumMember
33+
detail: gleam
34+
sort: 4_True
35+
add
36+
kind: Function
37+
detail: fn(Int, Int) -> Int
38+
sort: 02_add
39+
desc: app
40+
edits:
41+
[2:2-2:2]: "add"
42+
addf
43+
kind: Function
44+
detail: fn(Float, Float) -> Float
45+
sort: 2_addf
46+
desc: app
47+
edits:
48+
[2:2-2:2]: "addf"
49+
main
50+
kind: Function
51+
detail: fn() -> Int
52+
sort: 02_main
53+
desc: app
54+
edits:
55+
[2:2-2:2]: "main"
56+
sub
57+
kind: Function
58+
detail: fn(Int, Int) -> Int
59+
sort: 02_sub
60+
desc: app
61+
edits:
62+
[2:2-2:2]: "sub"

compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__completion__prefer_values_matching_expected_type.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ True
3434
main
3535
kind: Function
3636
detail: fn() -> Bool
37-
sort: 2_main
37+
sort: 02_main
3838
desc: app
3939
edits:
4040
[5:2-5:2]: "main"

0 commit comments

Comments
 (0)