Skip to content

Commit e9fdc8f

Browse files
authored
fix(resolving/completion): for methods on T? (#229)
Fixes #174 Fixes #184
1 parent 3b41d15 commit e9fdc8f

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
========================================================================
2+
Completion for method on T?
3+
========================================================================
4+
primitive Int;
5+
6+
extends fun foo(self: Int) {}
7+
extends fun fooOpt(self: Int?) {}
8+
9+
fun test() {
10+
let a = 10;
11+
a.<caret>;
12+
}
13+
------------------------------------------------------------------------
14+
2 foo(self: Int)
15+
2 fooOpt(self: Int?)
16+
14 call Use as function argument
17+
14 do Create do-until loop
18+
14 if Create if statement
19+
14 let Create variable
20+
14 not Negate expression
21+
14 repeat Create repeat loop

server/src/e2e/suite/testcases/resolve/methods.test

+67
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,73 @@ fun test() {
1414
------------------------------------------------------------------------
1515
8:6 -> 2:12 resolved
1616

17+
========================================================================
18+
Extends method resolve on T? with non Option qualifier
19+
========================================================================
20+
primitive Int;
21+
22+
extends fun add(self: Int?, other: Int): Int {
23+
return self + other;
24+
}
25+
26+
fun test() {
27+
let x: Int = 5;
28+
x.<caret>add(3);
29+
}
30+
------------------------------------------------------------------------
31+
8:6 -> 2:12 resolved
32+
33+
========================================================================
34+
Extends method resolve on T? with Option qualifier
35+
========================================================================
36+
primitive Int;
37+
38+
extends fun add(self: Int?, other: Int): Int {
39+
return self + other;
40+
}
41+
42+
fun test() {
43+
let x: Int? = 5;
44+
x.<caret>add(3);
45+
}
46+
------------------------------------------------------------------------
47+
8:6 -> 2:12 resolved
48+
49+
========================================================================
50+
Extends method resolve on T and T? with the same name
51+
========================================================================
52+
primitive Int;
53+
54+
struct SomeStruct {}
55+
56+
extends fun equal(self: SomeStruct?, other: SomeStruct?): Bool {
57+
if (self == null && other == null) { return true }
58+
if (self == null || other == null) { return false }
59+
return self!!.i == other!!.i && self!!.b == other!!.b;
60+
}
61+
62+
extends fun equal(self: SomeStruct, other: SomeStruct): Bool {
63+
return self.i == other.i && self.b == other.b;
64+
}
65+
66+
contract Test {
67+
receive() {}
68+
69+
get fun test1(): Bool {
70+
let s1 = SomeStruct { i: 42, b: true };
71+
return s1.<caret>equal(s1)
72+
}
73+
74+
get fun test2(): Bool {
75+
let s2 = SomeStruct { i: 42, b: false };
76+
let s3: SomeStruct? = null;
77+
return !s3.<caret>equal(s2);
78+
}
79+
}
80+
------------------------------------------------------------------------
81+
19:18 -> 10:12 resolved
82+
25:19 -> 4:12 resolved
83+
1784
========================================================================
1885
Contract method resolve
1986
========================================================================

server/src/psi/Reference.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,16 @@ export class Reference {
195195
return this.processType(qualifier, qualifierType.innerTy, proc, state)
196196
}
197197

198+
if (!this.processType(qualifier, qualifierType, proc, state)) return false
199+
200+
// process unwrapped T? later to correctly resolve in case of same name method for T and T?
198201
if (qualifierType instanceof OptionTy) {
199202
// show completion and resolve without explicit unwrapping
200203
return this.processType(qualifier, qualifierType.innerTy, proc, state)
201204
}
202205

203-
return this.processType(qualifier, qualifierType, proc, state)
206+
// last resort, trying to find methods of T?
207+
return this.processType(qualifier, new OptionTy(qualifierType), proc, state)
204208
}
205209

206210
private processType(

0 commit comments

Comments
 (0)