|
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 = |
12 | 2 | let locations = |
13 | 3 | extra.SharedTypes.locations |
14 | 4 | |> List.filter (fun (l, _) -> not l.Location.loc_ghost) |
15 | 5 | 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" |
90 | 32 |
|
91 | 33 | let dump files = |
92 | 34 | Shared.cacheTypeToString := true; |
93 | 35 | let state = TopTypes.empty () in |
94 | 36 | 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 |
98 | 39 | let uri = Uri2.fromPath filePath in |
99 | 40 | let result = |
100 | 41 | match State.getFullFromCmt ~state ~uri with |
101 | 42 | | Error message -> |
102 | 43 | prerr_endline message; |
103 | 44 | "[]" |
104 | 45 | | Ok (package, {file; extra}) -> |
105 | | - dumpLocations state ~package ~file ~extra ~selectPos uri |
| 46 | + dumpLocations state ~package ~file ~extra |
106 | 47 | in |
107 | 48 | print_endline result) |
108 | 49 |
|
|
0 commit comments