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

Commit cc33883

Browse files
committed
Fix hovering on record fields.
Fixes rescript-lang/rescript-vscode#52 Continuation of #47 Show the correct type information, including type definition for record fields. Also, fix issue in the docstring shown. Before it was either misssing (entire record type) or wrong (for record field, it was showing the doc comment of the containing record type). Clean up code in References.
1 parent 98a0f47 commit cc33883

File tree

4 files changed

+70
-66
lines changed

4 files changed

+70
-66
lines changed

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## master
2+
- Fix type shown when hovering on record fields (see https://github.com/rescript-lang/rescript-vscode/issues/52), and doc comments for records.
23
- Fix issue where type variables are printed with global renaming when hovering or autocompleting a module (see https://github.com/rescript-lang/rescript-editor-support/issues/38).
34
- Fix issue where a log file was always created (see https://github.com/rescript-lang/rescript-vscode/issues/47).
45
- Add support for hover on the id of toplevel module definitions (```module Id = ...```).

examples/example-project/src/ZZ.res

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,42 @@ type inline =
3838
| E({x: int, y: string})
3939
| F
4040

41-
module MSig
42-
: {
43-
type rec t = | A (list<s>)
41+
module MSig: {
42+
type rec t = A(list<s>)
4443
and s = list<t>
4544

46-
let x : int
47-
}
48-
= {
49-
type rec t = | A (list<s>)
45+
let x: int
46+
} = {
47+
type rec t = A(list<s>)
5048
and s = list<t>
5149

5250
let x = 14
5351
}
5452

5553
module Impl = {
56-
type rec t = | A (list<s>)
54+
type rec t = A(list<s>)
5755
and s = list<t>
5856

5957
type w = int
6058

6159
let x = 14
6260
}
6361

64-
module Impl2 = { include Impl};
62+
module Impl2 = {
63+
include Impl
64+
}
6565

6666
module D = MSig
6767
module E = Impl
68-
module F = Impl2
68+
module F = Impl2
69+
70+
@ocaml.doc("str docstring")
71+
type str = string
72+
73+
@ocaml.doc("gr docstring")
74+
type gr = {x: int, s: str}
75+
76+
let testRecordFields = (gr: gr) => {
77+
let str = gr.s
78+
str
79+
}

src/rescript-editor-support/Hover.re

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ let showModule =
4949
};
5050
};
5151

52-
open Infix;
5352
let newHover = (~rootUri, ~file: SharedTypes.file, ~getModule, loc) => {
5453
switch (loc) {
5554
| SharedTypes.Explanation(text) => Some(text)
@@ -98,43 +97,46 @@ let newHover = (~rootUri, ~file: SharedTypes.file, ~getModule, loc) => {
9897
| Const_nativeint(_) => "int"
9998
},
10099
)
101-
| Typed(t, _) =>
102-
let typeString = t |> Shared.typeToString;
103-
let extraTypeInfo = {
104-
let env = {Query.file, exported: file.contents.exported};
105-
let%opt path = t |> Shared.digConstructor;
106-
let%opt (_env, {name: {txt}, item: {decl}}) =
107-
digConstructor(~env, ~getModule, path);
108-
Some(decl |> Shared.declToString(txt));
109-
/* TODO type declaration */
110-
/* None */
111-
/* Some(typ.toString()) */
112-
};
113-
114-
let typeString = codeBlock(typeString);
115-
let typeString =
116-
typeString
117-
++ (
100+
| Typed(t, locKind) =>
101+
let fromType = (~docstring, typ) => {
102+
let typeString = codeBlock(typ |> Shared.typeToString);
103+
let extraTypeInfo = {
104+
let env = {Query.file, exported: file.contents.exported};
105+
let%opt path = typ |> Shared.digConstructor;
106+
let%opt (_env, {docstring, name: {txt}, item: {decl}}) =
107+
digConstructor(~env, ~getModule, path);
108+
Some((decl |> Shared.declToString(txt), docstring));
109+
};
110+
let (typeString, docstring) =
118111
switch (extraTypeInfo) {
119-
| None => ""
120-
| Some(extra) => "\n\n" ++ codeBlock(extra)
121-
}
122-
);
123-
124-
Some(
125-
{
126-
let%opt ({docstring}, {uri}, res) =
127-
References.definedForLoc(~file, ~getModule, loc);
112+
| None => (typeString, docstring)
113+
| Some((extra, extraDocstring)) => (
114+
typeString ++ "\n\n" ++ codeBlock(extra),
115+
extraDocstring,
116+
)
117+
};
118+
(Some(typeString), docstring);
119+
};
128120

121+
let parts =
122+
switch (References.definedForLoc(~file, ~getModule, locKind)) {
123+
| None =>
124+
let (typeString, docstring) = t |> fromType(~docstring=None);
125+
[typeString, docstring];
126+
| Some((docstring, {uri}, res)) =>
129127
let uri =
130128
Utils.startsWith(uri, rootUri)
131129
? "<root>" ++ Utils.sliceToEnd(uri, String.length(rootUri)) : uri;
132130

133131
let parts =
134132
switch (res) {
135-
| `Declared => [Some(typeString), docstring]
136-
| `Constructor({cname: {txt}, args}) => [
137-
Some(typeString),
133+
| `Declared =>
134+
let (typeString, docstring) = t |> fromType(~docstring);
135+
[typeString, docstring];
136+
| `Constructor({cname: {txt}, args}) =>
137+
let (typeString, docstring) = t |> fromType(~docstring);
138+
[
139+
typeString,
138140
Some(
139141
codeBlock(
140142
txt
@@ -151,15 +153,15 @@ let newHover = (~rootUri, ~file: SharedTypes.file, ~getModule, loc) => {
151153
),
152154
),
153155
docstring,
154-
]
155-
| `Attribute(_) => [Some(typeString), docstring]
156+
];
157+
| `Attribute({typ}) =>
158+
let (typeString, docstring) = typ |> fromType(~docstring);
159+
[typeString, docstring];
156160
};
157161

158-
let parts = parts @ [Some(uri)];
162+
parts @ [Some(uri)];
163+
};
159164

160-
Some(String.concat("\n\n", parts |> Utils.filterMap(x => x)));
161-
}
162-
|? typeString,
163-
);
165+
Some(String.concat("\n\n", parts |> Utils.filterMap(x => x)));
164166
};
165167
};

src/rescript-editor-support/References.re

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,19 @@ let localReferencesForLoc = (~file, ~extra, loc) =>
8282
}
8383
};
8484

85-
let definedForLoc = (~file, ~getModule, loc) => {
85+
let definedForLoc = (~file, ~getModule, locKind) => {
8686
let inner = (~file, stamp, tip) => {
8787
switch (tip) {
8888
| Constructor(name) =>
8989
let%opt declared =
9090
Query.declaredForTip(~stamps=file.stamps, stamp, tip);
9191
let%opt constructor = Query.getConstructor(file, stamp, name);
92-
Some((declared, file, `Constructor(constructor)));
92+
Some((declared.docstring, file, `Constructor(constructor)));
9393
| Attribute(name) =>
9494
let%opt declared =
9595
Query.declaredForTip(~stamps=file.stamps, stamp, tip);
9696
let%opt attribute = Query.getAttribute(file, stamp, name);
97-
Some((declared, file, `Attribute(attribute)));
97+
Some((declared.docstring, file, `Attribute(attribute)));
9898
| _ =>
9999
maybeLog(
100100
"Trying for declared "
@@ -105,22 +105,15 @@ let definedForLoc = (~file, ~getModule, loc) => {
105105
++ file.uri,
106106
);
107107
let%opt x = Query.declaredForTip(~stamps=file.stamps, stamp, tip);
108-
Some((x, file, `Declared));
108+
Some((x.docstring, file, `Declared));
109109
};
110110
};
111111

112-
switch (loc) {
113-
| Explanation(_)
114-
| Typed(_, NotFound)
115-
| LModule(NotFound)
116-
| TopLevelModule(_)
117-
| Constant(_) => None
118-
| Typed(_, LocalReference(stamp, tip) | Definition(stamp, tip))
119-
| LModule(LocalReference(stamp, tip) | Definition(stamp, tip)) =>
120-
inner(~file, stamp, tip)
121-
| TypeDefinition(_, _, stamp) => inner(~file, stamp, Type)
122-
| LModule(GlobalReference(moduleName, path, tip))
123-
| Typed(_, GlobalReference(moduleName, path, tip)) =>
112+
switch (locKind) {
113+
| NotFound => None
114+
| LocalReference(stamp, tip)
115+
| Definition(stamp, tip) => inner(~file, stamp, tip)
116+
| GlobalReference(moduleName, path, tip) =>
124117
{
125118
maybeLog("Getting global " ++ moduleName);
126119
let%try file =
@@ -146,9 +139,6 @@ let definedForLoc = (~file, ~getModule, loc) => {
146139
Ok(res);
147140
}
148141
|> RResult.toOptionAndLog
149-
/* let%try extra = getExtra(moduleName) |> RResult.orError("Failed to get extra for " ++ env.file.uri); */
150-
/* maybeLog("Finding references for (global) " ++ file.uri ++ " and stamp " ++ string_of_int(stamp) ++ " and tip " ++ tipToString(tip)); */
151-
/* forLocalStamp(~file, ~extra, ~allModules, ~getModule, ~getExtra, stamp, tip) |> RResult.orError("Could not get for local stamp") */
152142
};
153143
};
154144

0 commit comments

Comments
 (0)