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

Commit 5bde442

Browse files
committed
Clean up dumpLocations a bit
1 parent ce245dc commit 5bde442

File tree

1 file changed

+30
-89
lines changed

1 file changed

+30
-89
lines changed

src/EditorSupportCommands.ml

+30-89
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,49 @@
1-
let posOfLexing {Lexing.pos_lnum; pos_cnum; pos_bol} =
2-
Json.Object
3-
[
4-
("line", Json.Number (float_of_int (pos_lnum - 1)));
5-
("character", Json.Number (float_of_int (pos_cnum - pos_bol)));
6-
]
7-
8-
let rangeOfLoc {Location.loc_start; loc_end} =
9-
Json.Object [("start", posOfLexing loc_start); ("end", posOfLexing loc_end)]
10-
11-
let dumpLocations state ~package ~file ~extra ~selectPos uri =
1+
let dumpLocations state ~package ~file ~extra =
122
let locations =
133
extra.SharedTypes.locations
144
|> List.filter (fun (l, _) -> not l.Location.loc_ghost)
155
in
16-
let locations =
17-
match selectPos with
18-
| Some (line, col) -> (
19-
let pos = Utils.protocolLineColToCmtLoc ~line ~col in
20-
match References.locForPos ~extra:{extra with locations} pos with
21-
| None -> []
22-
| Some l -> [l])
23-
| None -> locations
24-
in
25-
let locationsInfo =
26-
locations
27-
|> Utils.filterMap (fun ((location : Location.t), loc) ->
28-
let locIsModule =
29-
match loc with
30-
| SharedTypes.LModule _ | TopLevelModule _ -> true
31-
| TypeDefinition _ | Typed _ | Constant _ | Explanation _ -> false
32-
in
33-
let hoverText =
34-
Hover.newHover ~file
35-
~getModule:(State.fileForModule state ~package)
36-
loc
37-
in
38-
let hover =
39-
match hoverText with
40-
| None -> []
41-
| Some s -> [("hover", Json.String s)]
42-
in
43-
let uriLocOpt =
44-
References.definitionForLoc ~pathsForModule:package.pathsForModule
45-
~file ~getUri:(State.fileForUri state)
46-
~getModule:(State.fileForModule state ~package)
47-
loc
48-
in
49-
let def, skipZero =
50-
match uriLocOpt with
51-
| None -> ([], false)
52-
| Some (uri2, loc) ->
53-
let uriIsCurrentFile = uri = uri2 in
54-
let posIsZero {Lexing.pos_lnum; pos_bol; pos_cnum} =
55-
pos_lnum = 1 && pos_cnum - pos_bol = 0
56-
in
57-
(* Skip if range is all zero, unless it's a module *)
58-
let skipZero =
59-
(not locIsModule) && posIsZero loc.loc_start
60-
&& posIsZero loc.loc_end
61-
in
62-
let range = ("range", rangeOfLoc loc) in
63-
( [
64-
( "definition",
65-
Json.Object
66-
(match uriIsCurrentFile with
67-
| true -> [range]
68-
| false ->
69-
[("uri", Json.String (Uri2.toString uri2)); range]) );
70-
],
71-
skipZero )
72-
in
73-
let skip = skipZero || (hover = [] && def = []) in
74-
match skip with
75-
| true -> None
76-
| false ->
77-
Some (Json.Object ([("range", rangeOfLoc location)] @ hover @ def)))
78-
in
79-
Json.stringify (Json.Array locationsInfo)
80-
81-
(* Split (line,char) from filepath:line:char *)
82-
let splitLineChar pathWithPos =
83-
let mkPos line char = Some (line |> int_of_string, char |> int_of_string) in
84-
match pathWithPos |> String.split_on_char ':' with
85-
| [filePath; line; char] -> (filePath, mkPos line char)
86-
| [drive; rest; line; char] ->
87-
(* c:\... on Windows *)
88-
(drive ^ ":" ^ rest, mkPos line char)
89-
| _ -> (pathWithPos, None)
6+
locations
7+
|> List.map (fun ((location : Location.t), loc) ->
8+
let hoverText =
9+
Hover.newHover ~file
10+
~getModule:(State.fileForModule state ~package)
11+
loc
12+
in
13+
let hover =
14+
match hoverText with None -> "" | Some s -> String.escaped s
15+
in
16+
let uriLocOpt =
17+
References.definitionForLoc ~pathsForModule:package.pathsForModule
18+
~file ~getUri:(State.fileForUri state)
19+
~getModule:(State.fileForModule state ~package)
20+
loc
21+
in
22+
let def =
23+
match uriLocOpt with
24+
| None -> Protocol.null
25+
| Some (uri2, loc) ->
26+
Protocol.stringifyLocation
27+
{uri = Uri2.toString uri2; range = Utils.cmtLocToRange loc}
28+
in
29+
Protocol.stringifyRange (Utils.cmtLocToRange location)
30+
^ "\n Hover: " ^ hover ^ "\n Definition: " ^ def)
31+
|> String.concat "\n\n"
9032

9133
let dump files =
9234
Shared.cacheTypeToString := true;
9335
let state = TopTypes.empty () in
9436
files
95-
|> List.iter (fun pathWithPos ->
96-
let filePath, selectPos = pathWithPos |> splitLineChar in
97-
let filePath = Files.maybeConcat (Unix.getcwd ()) filePath in
37+
|> List.iter (fun path ->
38+
let filePath = Files.maybeConcat (Unix.getcwd ()) path in
9839
let uri = Uri2.fromPath filePath in
9940
let result =
10041
match State.getFullFromCmt ~state ~uri with
10142
| Error message ->
10243
prerr_endline message;
10344
"[]"
10445
| Ok (package, {file; extra}) ->
105-
dumpLocations state ~package ~file ~extra ~selectPos uri
46+
dumpLocations state ~package ~file ~extra
10647
in
10748
print_endline result)
10849

0 commit comments

Comments
 (0)