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

Commit

Permalink
Split definition into a dedicated api
Browse files Browse the repository at this point in the history
Just like we did for hover
  • Loading branch information
chenglou committed Apr 12, 2021
1 parent 1b3baf8 commit 541f6a9
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/EditorSupportCommands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,65 @@ let hover ~path ~line ~char =
hover state ~file ~line ~char ~extra ~package
in
print_endline result

let definition state ~file ~line ~char ~extra ~package =
let open TopTypes in
let locations =
extra.SharedTypes.locations
|> List.filter (fun (l, _) -> not l.Location.loc_ghost)
in
let locations =
let pos = (line, char) in
let pos = Utils.cmtLocFromVscode pos in
match References.locForPos ~extra:{extra with locations} pos with
| None -> []
| Some l -> [l]
in
let locationsInfo =
locations
|> Utils.filterMap (fun (_, loc) ->
let locIsModule =
match loc with
| SharedTypes.LModule _ | TopLevelModule _ -> true
| TypeDefinition _ | Typed _ | Constant _ | Explanation _ -> false
in
let uriLocOpt =
References.definitionForLoc ~pathsForModule:package.pathsForModule
~file ~getUri:(State.fileForUri state)
~getModule:(State.fileForModule state ~package)
loc
in
let def, skipZero =
match uriLocOpt with
| None -> (None, false)
| Some (uri2, loc) ->
let posIsZero {Lexing.pos_lnum; pos_bol; pos_cnum} =
pos_lnum = 1 && pos_cnum - pos_bol = 0
in
(* Skip if range is all zero, unless it's a module *)
let skipZero =
(not locIsModule) && loc.loc_start |> posIsZero
&& loc.loc_end |> posIsZero
in
let open Protocol in
( Some {uri = Uri2.toString uri2; range = Utils.cmtLocToRange loc},
skipZero )
in
let skip = skipZero || def = None in
match skip with true -> None | false -> def)
in
match locationsInfo with
| [] -> Protocol.null
| head :: _ -> Protocol.stringifyLocation head

let definition ~path ~line ~char =
let state = TopTypes.empty () in
let filePath = Files.maybeConcat (Unix.getcwd ()) path in
let uri = Uri2.fromPath filePath in
let result =
match State.getFullFromCmt ~state ~uri with
| Error _message -> Protocol.null
| Ok (package, {file; extra}) ->
definition state ~file ~line ~char ~extra ~package
in
print_endline result
28 changes: 28 additions & 0 deletions src/Protocol.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
let array l = "[" ^ (String.concat ", " l) ^ "]"

type position = {
line: int;
character: int;
}

type range = {
start: position;
end_: position;
}

type markupContent = {
kind: string;
value: string;
Expand All @@ -17,6 +27,19 @@ type hover = {
contents: string;
}

type location = {
uri: string;
range: range;
}

let stringifyPosition p =
Printf.sprintf {|{"line": "%i", "character": "%i"}|} p.line p.character

let stringifyRange r =
Printf.sprintf {|{"start": "%s", "end": "%s"}|}
(stringifyPosition r.start)
(stringifyPosition r.end_)

let stringifyMarkupContent (m: markupContent) =
Printf.sprintf {|{"kind": "%s", "value": "%s"}|}
m.kind (String.escaped m.value)
Expand All @@ -39,4 +62,9 @@ let stringifyHover h =
Printf.sprintf {|{"contents": "%s"}|}
(String.escaped h.contents)

let stringifyLocation h =
Printf.sprintf {|{"uri": "%s", "range": "%s"}|}
(String.escaped h.uri)
(stringifyRange h.range)

let null = "null"
6 changes: 6 additions & 0 deletions src/RescriptEditorSupport.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ Options:
hover: get inferred type for Foo.res at line 10 column 2:

rescript-editor-support.exe hover src/Foo.res 10 2

definition: get inferred type for Foo.res at line 10 column 2:

rescript-editor-support.exe definition src/Foo.res 10 2
|}

let showHelp () = prerr_endline help
Expand All @@ -53,6 +57,8 @@ let main () =
~char:(int_of_string char) ~currentFile
| _opts, ["hover"; path; line; char] ->
EditorSupportCommands.hover ~path ~line:(int_of_string line) ~char:(int_of_string char)
| _opts, ["definition"; path; line; char] ->
EditorSupportCommands.definition ~path ~line:(int_of_string line) ~char:(int_of_string char)
| _ ->
showHelp ();
exit 1
Expand Down
10 changes: 10 additions & 0 deletions src/Utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ let endsWith s suffix =

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

let cmtLocToPosition {Lexing.pos_lnum; pos_cnum; pos_bol} = Protocol.{
line = pos_lnum - 1;
character = pos_cnum - pos_bol;
}

let cmtLocToRange {Location.loc_start; loc_end} = Protocol.{
start = cmtLocToPosition loc_start;
end_ = cmtLocToPosition loc_end;
}

let locWithinLoc inner outer =
let open Location in
inner.loc_start.pos_cnum >= outer.loc_start.pos_cnum
Expand Down

0 comments on commit 541f6a9

Please sign in to comment.