1
1
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
+ ]
3
7
4
8
let rangeOfLoc {Location. loc_start; loc_end} =
5
9
Json. Object [(" start" , posOfLexing loc_start); (" end" , posOfLexing loc_end)]
@@ -15,74 +19,62 @@ let dumpLocations state ~package ~file ~extra ~selectPos uri =
15
19
let pos = Utils. cmtLocFromVscode pos in
16
20
match References. locForPos ~extra: {extra with locations} pos with
17
21
| None -> []
18
- | Some l -> [l] )
22
+ | Some l -> [l])
19
23
| None -> locations
20
24
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
32
25
let locationsInfo =
33
26
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
- ~get Module:(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 ~paths ForModule:package.pathsForModule
52
- ~file ~get Uri:(State. fileForUri state)
53
- ~get Module:(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
+ ~get Module:(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 ~paths ForModule:package.pathsForModule
45
+ ~file ~get Uri:(State. fileForUri state)
46
+ ~get Module:(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)))
86
78
in
87
79
Json. stringify (Json. Array locationsInfo)
88
80
@@ -101,18 +93,18 @@ let dump files =
101
93
let state = TopTypes. empty () in
102
94
files
103
95
|> 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 ~select Pos 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 ~select Pos uri
106
+ in
107
+ print_endline result)
116
108
117
109
let complete ~path ~line ~char ~currentFile =
118
110
let state = TopTypes. empty () in
@@ -125,7 +117,8 @@ let complete ~path ~line ~char ~currentFile =
125
117
" []"
126
118
| Ok (package , full ) ->
127
119
let maybeText = Files. readFile currentFile in
128
- NewCompletions. computeCompletions ~full ~maybe Text ~package ~pos: (line, char ) ~state
120
+ NewCompletions. computeCompletions ~full ~maybe Text ~package
121
+ ~pos: (line, char ) ~state
129
122
|> List. map Protocol. stringifyCompletionItem
130
123
|> Protocol. array
131
124
in
@@ -146,46 +139,43 @@ let hover state ~file ~line ~char ~extra ~package =
146
139
let locationsInfo =
147
140
locations
148
141
|> 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
- ~get Module:(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 ~paths ForModule:package.pathsForModule
170
- ~file ~get Uri:(State. fileForUri state)
171
- ~get Module:(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
+ ~get Module:(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 ~paths ForModule:package.pathsForModule
161
+ ~file ~get Uri:(State. fileForUri state)
162
+ ~get Module:(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 )
189
179
in
190
180
match locationsInfo with
191
181
| [] -> Protocol. null
@@ -197,8 +187,7 @@ let hover ~path ~line ~char =
197
187
let uri = Uri2. fromPath filePath in
198
188
let result =
199
189
match State. getFullFromCmt ~state ~uri with
200
- | Error message ->
201
- Protocol. stringifyHover {contents = message}
190
+ | Error message -> Protocol. stringifyHover {contents = message}
202
191
| Ok (package , {file; extra} ) ->
203
192
hover state ~file ~line ~char ~extra ~package
204
193
in
0 commit comments