Skip to content

Commit a754cd0

Browse files
authored
Merge pull request #272 from rescript-association/playground-type-hover
Playground type hover
2 parents f65314c + 932ebad commit a754cd0

10 files changed

+699
-54
lines changed

src/Playground.js

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Playground.res

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,10 +1526,33 @@ let default = () => {
15261526
| _ => []
15271527
}
15281528

1529+
let cmHoverHints = switch compilerState {
1530+
| Ready({result: FinalResult.Comp(Success({type_hints}))}) =>
1531+
Js.Array2.map(type_hints, hint => {
1532+
switch hint {
1533+
| TypeDeclaration({start, end, hint})
1534+
| Binding({start, end, hint})
1535+
| CoreType({start, end, hint})
1536+
| Expression({start, end, hint}) => {
1537+
CodeMirror.HoverHint.start: {
1538+
line: start.line,
1539+
col: start.col,
1540+
},
1541+
end: {
1542+
line: end.line,
1543+
col: end.col,
1544+
},
1545+
hint: hint,
1546+
}
1547+
}
1548+
})
1549+
| _ => []
1550+
}
1551+
15291552
let mode = switch compilerState {
1530-
| Ready({ targetLang: Reason }) => "reason"
1531-
| Ready({ targetLang: Res }) => "rescript"
1532-
| _ => "rescript"
1553+
| Ready({targetLang: Reason}) => "reason"
1554+
| Ready({targetLang: Res}) => "rescript"
1555+
| _ => "rescript"
15331556
}
15341557

15351558
<>
@@ -1559,6 +1582,7 @@ let default = () => {
15591582
minHeight="calc(100vh - 10rem)"
15601583
maxHeight="calc(100vh - 10rem)"
15611584
mode
1585+
hoverHints=cmHoverHints
15621586
errors=cmErrors
15631587
value={editorCode.current}
15641588
onChange={value => {

src/bindings/RescriptCompilerApi.js

Lines changed: 80 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bindings/RescriptCompilerApi.res

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,55 @@ module WarningFlag = {
178178
}
179179
}
180180

181+
module TypeHint = {
182+
type position = {
183+
line: int,
184+
col: int,
185+
}
186+
187+
type data = {
188+
start: position,
189+
end: position,
190+
hint: string,
191+
}
192+
193+
type t =
194+
| TypeDeclaration(data)
195+
| Expression(data)
196+
| Binding(data)
197+
| CoreType(data)
198+
199+
let decodePosition = json => {
200+
open Json.Decode
201+
{
202+
line: field("line", int, json),
203+
col: field("col", int, json),
204+
}
205+
}
206+
207+
let decode = (json): t => {
208+
open Json.Decode
209+
let data = {
210+
start: field("start", decodePosition, json),
211+
end: field("end", decodePosition, json),
212+
hint: field("hint", string, json),
213+
}
214+
215+
switch field("kind", string, json) {
216+
| "expression" => Expression(data)
217+
| "type_declaration" => TypeDeclaration(data)
218+
| "binding" => Binding(data)
219+
| "core_type" => CoreType(data)
220+
| other => raise(DecodeError(`Unknown kind "${other}" type hint`))
221+
}
222+
}
223+
}
224+
181225
module CompileSuccess = {
182226
type t = {
183227
js_code: string,
184228
warnings: array<Warning.t>,
229+
type_hints: array<TypeHint.t>,
185230
time: float, // total compilation time
186231
}
187232

@@ -190,6 +235,7 @@ module CompileSuccess = {
190235
{
191236
js_code: field("js_code", string, json),
192237
warnings: field("warnings", array(Warning.decode), json),
238+
type_hints: withDefault([], field("type_hints", array(TypeHint.decode)), json),
193239
time: time,
194240
}
195241
}

0 commit comments

Comments
 (0)