Skip to content
This repository was archived by the owner on Apr 24, 2021. It is now read-only.

Commit ed8744f

Browse files
committed
Fix hover on labels with type annotation in components.
Fixes #132.
1 parent 5d5eafa commit ed8744f

File tree

6 files changed

+69
-25
lines changed

6 files changed

+69
-25
lines changed

Changes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
- Add autocomplete for props in JSX components.
55
- Autocomplete: fix issue where `->` autocomplete was overruling `.`. See https://github.com/rescript-lang/rescript-editor-support/issues/99.
66
- Add pipe autocomplete for builtin list, array, string, option types. And for string and array literals.
7-
- Fix hover on labels in component functions with compiler version 9.1.
7+
- Fix hover on labels in component functions with compiler version 9.1, and labels with type annotation.
88

99
## Release 1.0.6 of rescript-vscode
1010
This [commit](https://github.com/rescript-lang/rescript-editor-support/commit/03ee0d97b250474028d4fb08eac81ddb21ccb082) is vendored in [rescript-vscode 1.0.6](https://github.com/rescript-lang/rescript-vscode/releases/tag/1.0.6).

src/References.ml

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ let locsForPos ~extra pos =
1818
extra.locations |> List.filter (fun (loc, _l) -> checkPos pos loc)
1919

2020
let locForPos ~extra pos =
21-
match locsForPos ~extra pos with
21+
let locs = locsForPos ~extra pos in
22+
match locs with
2223
| [(loc1, Typed (_, LocalReference _)); ((loc3, _) as l3)] when loc1 = loc3 ->
23-
(* JSX and compiler combined: *)
24-
(* ~x becomes Props#x *)
25-
(* heuristic for: [Props, x], give loc of `x` *)
24+
(* JSX and compiler combined:
25+
~x becomes Props#x
26+
heuristic for: [Props, x], give loc of `x` *)
2627
Some l3
2728
| [
2829
(loc1, Typed (_, LocalReference _));
@@ -31,18 +32,31 @@ let locForPos ~extra pos =
3132
]
3233
(* For older compiler 9.0 or earlier *)
3334
when loc1 = loc2 && loc2 = loc3 ->
34-
(* JSX and compiler combined: *)
35-
(* ~x becomes Js_OO.unsafe_downgrade(Props)#x *)
36-
(* heuristic for: [Props, unsafe_downgrade, x], give loc of `x` *)
35+
(* JSX and compiler combined:
36+
~x becomes Js_OO.unsafe_downgrade(Props)#x
37+
heuristic for: [Props, unsafe_downgrade, x], give loc of `x` *)
3738
Some l3
39+
| [
40+
((_, Typed (_, LocalReference (_, Value))) as _l1);
41+
((_, Typed (_, Definition (_, Value))) as l2);
42+
] ->
43+
(* JSX on type-annotated labeled (~arg:t):
44+
(~arg:t) becomes Props#arg
45+
Props has the location range of arg:t
46+
arg has the location range of arg
47+
heuristic for: [Props, arg], give loc of `arg` *)
48+
(* Printf.eprintf "l1 %s\nl2 %s\n"
49+
(SharedTypes.locationToString _l1)
50+
(SharedTypes.locationToString l2); *)
51+
Some l2
3852
| [(loc1, _); ((loc2, _) as l); (loc3, _)] when loc1 = loc2 && loc2 = loc3 ->
39-
(* JSX with at most one child *)
40-
(* heuristic for: [makeProps, make, createElement], give the loc of `make` *)
53+
(* JSX with at most one child
54+
heuristic for: [makeProps, make, createElement], give the loc of `make` *)
4155
Some l
4256
| [(loc1, _); (loc2, _); ((loc3, _) as l); (loc4, _)]
4357
when loc1 = loc2 && loc2 = loc3 && loc3 = loc4 ->
44-
(* JSX variadic, e.g. <C> {x} {y} </C> *)
45-
(* heuristic for: [makeProps, React.null, make, createElementVariadic], give the loc of `make` *)
58+
(* JSX variadic, e.g. <C> {x} {y} </C>
59+
heuristic for: [makeProps , React.null, make, createElementVariadic], give the loc of `make` *)
4660
Some l
4761
| l :: _ -> Some l
4862
| _ -> None

src/SharedTypes.ml

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ let getCmt ?(interface = true) p =
2424
match p with
2525
| Impl (c, _) | Intf (c, _) -> c
2626
| IntfAndImpl (cint, _, cimpl, _) -> (
27-
match interface with true -> cint | false -> cimpl )
27+
match interface with true -> cint | false -> cimpl)
2828

2929
type visibilityPath =
3030
| File of Uri2.t * string
@@ -42,9 +42,9 @@ type 't declared = {
4242
deprecated : string option;
4343
docstring : string list;
4444
item : 't;
45-
(* TODO: maybe add a uri? *)
46-
(* scopeType: scope, *)
47-
(* scopeStart: (int, int), *)
45+
(* TODO: maybe add a uri? *)
46+
(* scopeType: scope, *)
47+
(* scopeStart: (int, int), *)
4848
}
4949

5050
let emptyDeclared name =
@@ -95,17 +95,16 @@ type exported = {
9595
types : namedStampMap;
9696
values : namedStampMap;
9797
modules : namedStampMap;
98-
(* constructors: namedStampMap, *)
99-
(* classes: namedStampMap,
100-
classTypes: namedStampMap, *)
98+
(* constructors: namedStampMap, *)
99+
(* classes: namedStampMap,
100+
classTypes: namedStampMap, *)
101101
}
102102

103103
let initExported () =
104104
{
105105
types = Hashtbl.create 10;
106106
values = Hashtbl.create 10;
107-
modules = Hashtbl.create 10;
108-
(* constructors: Hashtbl.create(10), *)
107+
modules = Hashtbl.create 10 (* constructors: Hashtbl.create(10), *);
109108
}
110109

111110
type moduleItem =
@@ -191,7 +190,6 @@ type openTracker = {
191190
mutable used : (path * tip * Location.t) list;
192191
}
193192

194-
(** These are the bits of info that we need to make in-app stuff awesome *)
195193
type extra = {
196194
internalReferences : (int, Location.t list) Hashtbl.t;
197195
externalReferences : (string, (path * tip * Location.t) list) Hashtbl.t;
@@ -201,6 +199,7 @@ type extra = {
201199
(* OPTIMIZE: using a stack to come up with this would cut the computation time of this considerably. *)
202200
opens : (Location.t, openTracker) Hashtbl.t;
203201
}
202+
(** These are the bits of info that we need to make in-app stuff awesome *)
204203

205204
type full = {extra : extra; file : file}
206205

@@ -213,3 +212,27 @@ let initExtra () =
213212
}
214213

215214
let hashList h = Hashtbl.fold (fun a b c -> (a, b) :: c) h []
215+
216+
let locKindToString = function
217+
| LocalReference (_, tip) -> "(LocalReference " ^ tipToString tip ^ ")"
218+
| GlobalReference _ -> "GlobalReference"
219+
| NotFound -> "NotFound"
220+
| Definition (_, tip) -> "(Definition " ^ tipToString tip ^ ")"
221+
222+
let locToString = function
223+
| Typed (e, locKind) ->
224+
"Typed " ^ Shared.typeToString e ^ " " ^ locKindToString locKind
225+
| Constant _ -> "Constant"
226+
| LModule _ -> "LModule"
227+
| TopLevelModule _ -> "TopLevelModule"
228+
| TypeDefinition _ -> "TypeDefinition"
229+
| Explanation _ -> "Explanation"
230+
231+
let locationToString ({Location.loc_start; loc_end}, loc) =
232+
let pos1 = Utils.cmtPosToPosition loc_start in
233+
let pos2 = Utils.cmtPosToPosition loc_end in
234+
Printf.sprintf "%d:%d-%d:%d %s" pos1.line pos1.character pos2.line
235+
pos2.character (locToString loc)
236+
237+
(* for debugging *)
238+
let _ = locationToString

src/Utils.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ let endsWith s suffix =
2626

2727
let protocolLineColToCmtLoc ~line ~col = (line + 1, col)
2828

29-
let cmtLocToPosition {Lexing.pos_lnum; pos_cnum; pos_bol} = Protocol.{
29+
let cmtPosToPosition {Lexing.pos_lnum; pos_cnum; pos_bol} = Protocol.{
3030
line = pos_lnum - 1;
3131
character = pos_cnum - pos_bol;
3232
}
3333

3434
let cmtLocToRange {Location.loc_start; loc_end} = Protocol.{
35-
start = cmtLocToPosition loc_start;
36-
end_ = cmtLocToPosition loc_end;
35+
start = cmtPosToPosition loc_start;
36+
end_ = cmtPosToPosition loc_end;
3737
}
3838

3939
let locWithinLoc inner outer =

test/src/Hover.res

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,8 @@ let functionWithTypeAnnotation : unit => int = () => 1
3636

3737
@react.component
3838
let make = (~name) => React.string(name)
39+
// ^hov
40+
41+
@react.component
42+
let make2 = (~name:string) => React.string(name)
3943
// ^hov

test/src/expected/Hover.res.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ Hover src/Hover.res 33:4
2222
Hover src/Hover.res 37:13
2323
{"contents": "```rescript\nstring\n```"}
2424

25+
Hover src/Hover.res 41:13
26+
{"contents": "```rescript\nstring\n```"}
27+

0 commit comments

Comments
 (0)