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

Commit 39c3687

Browse files
committed
Split definition into a dedicated api
Just like we did for hover
1 parent 1b3baf8 commit 39c3687

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

src/EditorSupportCommands.ml

+62
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,65 @@ let hover ~path ~line ~char =
192192
hover state ~file ~line ~char ~extra ~package
193193
in
194194
print_endline result
195+
196+
let definition state ~file ~line ~char ~extra ~package =
197+
let open TopTypes in
198+
let locations =
199+
extra.SharedTypes.locations
200+
|> List.filter (fun (l, _) -> not l.Location.loc_ghost)
201+
in
202+
let locations =
203+
let pos = (line, char) in
204+
let pos = Utils.cmtLocFromVscode pos in
205+
match References.locForPos ~extra:{extra with locations} pos with
206+
| None -> []
207+
| Some l -> [l]
208+
in
209+
let locationsInfo =
210+
locations
211+
|> Utils.filterMap (fun (_, loc) ->
212+
let locIsModule =
213+
match loc with
214+
| SharedTypes.LModule _ | TopLevelModule _ -> true
215+
| TypeDefinition _ | Typed _ | Constant _ | Explanation _ -> false
216+
in
217+
let uriLocOpt =
218+
References.definitionForLoc ~pathsForModule:package.pathsForModule
219+
~file ~getUri:(State.fileForUri state)
220+
~getModule:(State.fileForModule state ~package)
221+
loc
222+
in
223+
let def, skipZero =
224+
match uriLocOpt with
225+
| None -> (None, false)
226+
| Some (uri2, loc) ->
227+
let posIsZero {Lexing.pos_lnum; pos_bol; pos_cnum} =
228+
pos_lnum = 1 && pos_cnum - pos_bol = 0
229+
in
230+
(* Skip if range is all zero, unless it's a module *)
231+
let skipZero =
232+
(not locIsModule) && loc.loc_start |> posIsZero
233+
&& loc.loc_end |> posIsZero
234+
in
235+
let open Protocol in
236+
( Some {uri = Uri2.toString uri2; range = Utils.cmtLocToRange loc},
237+
skipZero )
238+
in
239+
let skip = skipZero || def = None in
240+
match skip with true -> None | false -> def)
241+
in
242+
match locationsInfo with
243+
| [] -> Protocol.null
244+
| head :: _ -> Protocol.stringifyLocation head
245+
246+
let definition ~path ~line ~char =
247+
let state = TopTypes.empty () in
248+
let filePath = Files.maybeConcat (Unix.getcwd ()) path in
249+
let uri = Uri2.fromPath filePath in
250+
let result =
251+
match State.getFullFromCmt ~state ~uri with
252+
| Error _message -> Protocol.null
253+
| Ok (package, {file; extra}) ->
254+
definition state ~file ~line ~char ~extra ~package
255+
in
256+
print_endline result

src/Protocol.ml

+28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
let array l = "[" ^ (String.concat ", " l) ^ "]"
22

3+
type position = {
4+
line: int;
5+
character: int;
6+
}
7+
8+
type range = {
9+
start: position;
10+
end_: position;
11+
}
12+
313
type markupContent = {
414
kind: string;
515
value: string;
@@ -17,6 +27,19 @@ type hover = {
1727
contents: string;
1828
}
1929

30+
type location = {
31+
uri: string;
32+
range: range;
33+
}
34+
35+
let stringifyPosition p =
36+
Printf.sprintf {|{"line": "%i", "character": "%i"}|} p.line p.character
37+
38+
let stringifyRange r =
39+
Printf.sprintf {|{"start": "%s", "end": "%s"}|}
40+
(stringifyPosition r.start)
41+
(stringifyPosition r.end_)
42+
2043
let stringifyMarkupContent (m: markupContent) =
2144
Printf.sprintf {|{"kind": "%s", "value": "%s"}|}
2245
m.kind (String.escaped m.value)
@@ -39,4 +62,9 @@ let stringifyHover h =
3962
Printf.sprintf {|{"contents": "%s"}|}
4063
(String.escaped h.contents)
4164

65+
let stringifyLocation h =
66+
Printf.sprintf {|{"uri": "%s", "range": "%s"}|}
67+
(String.escaped h.uri)
68+
(stringifyRange h.range)
69+
4270
let null = "null"

src/RescriptEditorSupport.ml

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ Options:
4040
hover: get inferred type for Foo.res at line 10 column 2:
4141

4242
rescript-editor-support.exe hover src/Foo.res 10 2
43+
44+
definition: get inferred type for Foo.res at line 10 column 2:
45+
46+
rescript-editor-support.exe definition src/Foo.res 10 2
4347
|}
4448

4549
let showHelp () = prerr_endline help
@@ -53,6 +57,8 @@ let main () =
5357
~char:(int_of_string char) ~currentFile
5458
| _opts, ["hover"; path; line; char] ->
5559
EditorSupportCommands.hover ~path ~line:(int_of_string line) ~char:(int_of_string char)
60+
| _opts, ["definition"; path; line; char] ->
61+
EditorSupportCommands.definition ~path ~line:(int_of_string line) ~char:(int_of_string char)
5662
| _ ->
5763
showHelp ();
5864
exit 1

src/Utils.ml

+10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ let endsWith s suffix =
4141

4242
let cmtLocFromVscode (line, col) = (line + 1, col)
4343

44+
let cmtLocToPosition {Lexing.pos_lnum; pos_cnum; pos_bol} = Protocol.{
45+
line = pos_lnum - 1;
46+
character = pos_cnum - pos_bol;
47+
}
48+
49+
let cmtLocToRange {Location.loc_start; loc_end} = Protocol.{
50+
start = cmtLocToPosition loc_start;
51+
end_ = cmtLocToPosition loc_end;
52+
}
53+
4454
let locWithinLoc inner outer =
4555
let open Location in
4656
inner.loc_start.pos_cnum >= outer.loc_start.pos_cnum

0 commit comments

Comments
 (0)