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

Commit 2a4f450

Browse files
committed
Remove dedup from dump command.
1 parent 4b9ef35 commit 2a4f450

File tree

1 file changed

+109
-120
lines changed

1 file changed

+109
-120
lines changed

src/EditorSupportCommands.ml

Lines changed: 109 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
let posOfLexing {Lexing.pos_lnum; pos_cnum; pos_bol} =
2-
Json.Object [("line", Json.Number (float_of_int (pos_lnum - 1))); ("character", Json.Number (float_of_int (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+
]
37

48
let rangeOfLoc {Location.loc_start; loc_end} =
59
Json.Object [("start", posOfLexing loc_start); ("end", posOfLexing loc_end)]
@@ -15,74 +19,62 @@ let dumpLocations state ~package ~file ~extra ~selectPos uri =
1519
let pos = Utils.cmtLocFromVscode pos in
1620
match References.locForPos ~extra:{extra with locations} pos with
1721
| None -> []
18-
| Some l -> [l] )
22+
| Some l -> [l])
1923
| None -> locations
2024
in
21-
let dedupTable = Hashtbl.create 1 in
22-
let dedupHover hover i =
23-
let isCandidate = String.length hover > 10 in
24-
if isCandidate then (
25-
match Hashtbl.find_opt dedupTable hover with
26-
| Some n -> Json.String ("#" ^ string_of_int n)
27-
| None ->
28-
Hashtbl.replace dedupTable hover i;
29-
Json.String hover )
30-
else Json.String hover
31-
in
3225
let locationsInfo =
3326
locations
34-
|> Utils.filterMapIndex (fun i ((location : Location.t), loc) ->
35-
let locIsModule =
36-
match loc with
37-
| SharedTypes.LModule _ | TopLevelModule _ -> true
38-
| TypeDefinition _ | Typed _ | Constant _ | Explanation _ -> false
39-
in
40-
let hoverText =
41-
Hover.newHover ~file
42-
~getModule:(State.fileForModule state ~package)
43-
loc
44-
in
45-
let hover =
46-
match hoverText with
47-
| None -> []
48-
| Some s -> [("hover", dedupHover s i)]
49-
in
50-
let uriLocOpt =
51-
References.definitionForLoc ~pathsForModule:package.pathsForModule
52-
~file ~getUri:(State.fileForUri state)
53-
~getModule:(State.fileForModule state ~package)
54-
loc
55-
in
56-
let def, skipZero =
57-
match uriLocOpt with
58-
| None -> ([], false)
59-
| Some (uri2, loc) ->
60-
let uriIsCurrentFile = uri = uri2 in
61-
let posIsZero {Lexing.pos_lnum; pos_bol; pos_cnum} =
62-
pos_lnum = 1 && pos_cnum - pos_bol = 0
63-
in
64-
(* Skip if range is all zero, unless it's a module *)
65-
let skipZero =
66-
(not locIsModule) && loc.loc_start |> posIsZero
67-
&& loc.loc_end |> posIsZero
68-
in
69-
let range = ("range", rangeOfLoc loc) in
70-
(
71-
[
72-
("definition",
73-
Json.Object
74-
(match uriIsCurrentFile with
75-
| true -> [range]
76-
| false -> [("uri", Json.String (Uri2.toString uri2)); range])
77-
)
78-
],
79-
skipZero
80-
)
81-
in
82-
let skip = skipZero || (hover = [] && def = []) in
83-
match skip with
84-
| true -> None
85-
| false -> Some (Json.Object ([("range", rangeOfLoc location)] @ hover @ def)))
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) && loc.loc_start |> posIsZero
60+
&& loc.loc_end |> posIsZero
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)))
8678
in
8779
Json.stringify (Json.Array locationsInfo)
8880

@@ -101,18 +93,18 @@ let dump files =
10193
let state = TopTypes.empty () in
10294
files
10395
|> List.iter (fun pathWithPos ->
104-
let filePath, selectPos = pathWithPos |> splitLineChar in
105-
let filePath = Files.maybeConcat (Unix.getcwd ()) filePath in
106-
let uri = Uri2.fromPath filePath in
107-
let result =
108-
match State.getFullFromCmt ~state ~uri with
109-
| Error message ->
110-
prerr_endline message;
111-
"[]"
112-
| Ok (package, {file; extra}) ->
113-
dumpLocations state ~package ~file ~extra ~selectPos uri
114-
in
115-
print_endline result)
96+
let filePath, selectPos = pathWithPos |> splitLineChar in
97+
let filePath = Files.maybeConcat (Unix.getcwd ()) filePath in
98+
let uri = Uri2.fromPath filePath in
99+
let result =
100+
match State.getFullFromCmt ~state ~uri with
101+
| Error message ->
102+
prerr_endline message;
103+
"[]"
104+
| Ok (package, {file; extra}) ->
105+
dumpLocations state ~package ~file ~extra ~selectPos uri
106+
in
107+
print_endline result)
116108

117109
let complete ~path ~line ~char ~currentFile =
118110
let state = TopTypes.empty () in
@@ -125,7 +117,8 @@ let complete ~path ~line ~char ~currentFile =
125117
"[]"
126118
| Ok (package, full) ->
127119
let maybeText = Files.readFile currentFile in
128-
NewCompletions.computeCompletions ~full ~maybeText ~package ~pos:(line, char) ~state
120+
NewCompletions.computeCompletions ~full ~maybeText ~package
121+
~pos:(line, char) ~state
129122
|> List.map Protocol.stringifyCompletionItem
130123
|> Protocol.array
131124
in
@@ -146,46 +139,43 @@ let hover state ~file ~line ~char ~extra ~package =
146139
let locationsInfo =
147140
locations
148141
|> Utils.filterMap (fun (_, loc) ->
149-
let locIsModule =
150-
match loc with
151-
| SharedTypes.LModule _ | TopLevelModule _ -> true
152-
| TypeDefinition _ | Typed _ | Constant _ | Explanation _ -> false
153-
in
154-
let hoverText =
155-
Hover.newHover ~file
156-
~getModule:(State.fileForModule state ~package)
157-
loc
158-
in
159-
let hover =
160-
match hoverText with
161-
| None -> None
162-
| Some s ->
163-
let open Protocol in
164-
Some {
165-
contents = s
166-
}
167-
in
168-
let uriLocOpt =
169-
References.definitionForLoc ~pathsForModule:package.pathsForModule
170-
~file ~getUri:(State.fileForUri state)
171-
~getModule:(State.fileForModule state ~package)
172-
loc
173-
in
174-
let skipZero =
175-
match uriLocOpt with
176-
| None -> false
177-
| Some (_, loc) ->
178-
let posIsZero {Lexing.pos_lnum; pos_bol; pos_cnum} =
179-
pos_lnum = 1 && pos_cnum - pos_bol = 0
180-
in
181-
(* Skip if range is all zero, unless it's a module *)
182-
(not locIsModule) && loc.loc_start |> posIsZero
183-
&& loc.loc_end |> posIsZero
184-
in
185-
match hover with
186-
| Some hover when not skipZero -> Some hover
187-
| _ -> None
188-
)
142+
let locIsModule =
143+
match loc with
144+
| SharedTypes.LModule _ | TopLevelModule _ -> true
145+
| TypeDefinition _ | Typed _ | Constant _ | Explanation _ -> false
146+
in
147+
let hoverText =
148+
Hover.newHover ~file
149+
~getModule:(State.fileForModule state ~package)
150+
loc
151+
in
152+
let hover =
153+
match hoverText with
154+
| None -> None
155+
| Some s ->
156+
let open Protocol in
157+
Some {contents = s}
158+
in
159+
let uriLocOpt =
160+
References.definitionForLoc ~pathsForModule:package.pathsForModule
161+
~file ~getUri:(State.fileForUri state)
162+
~getModule:(State.fileForModule state ~package)
163+
loc
164+
in
165+
let skipZero =
166+
match uriLocOpt with
167+
| None -> false
168+
| Some (_, loc) ->
169+
let posIsZero {Lexing.pos_lnum; pos_bol; pos_cnum} =
170+
pos_lnum = 1 && pos_cnum - pos_bol = 0
171+
in
172+
(* Skip if range is all zero, unless it's a module *)
173+
(not locIsModule) && loc.loc_start |> posIsZero
174+
&& loc.loc_end |> posIsZero
175+
in
176+
match hover with
177+
| Some hover when not skipZero -> Some hover
178+
| _ -> None)
189179
in
190180
match locationsInfo with
191181
| [] -> Protocol.null
@@ -197,8 +187,7 @@ let hover ~path ~line ~char =
197187
let uri = Uri2.fromPath filePath in
198188
let result =
199189
match State.getFullFromCmt ~state ~uri with
200-
| Error message ->
201-
Protocol.stringifyHover {contents = message}
190+
| Error message -> Protocol.stringifyHover {contents = message}
202191
| Ok (package, {file; extra}) ->
203192
hover state ~file ~line ~char ~extra ~package
204193
in

0 commit comments

Comments
 (0)