Skip to content

Commit f935ce5

Browse files
committed
Refactor commands: all in one place.
1 parent d8fbdb8 commit f935ce5

File tree

1 file changed

+61
-59
lines changed

1 file changed

+61
-59
lines changed

analysis/src/Commands.ml

Lines changed: 61 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -49,79 +49,81 @@ let completion ~path ~line ~col ~currentFile =
4949
in
5050
print_endline result
5151

52-
let hover ~file ~line ~col ~extra ~package =
53-
let pos = Utils.protocolLineColToCmtLoc ~line ~col in
54-
match References.locItemForPos ~extra pos with
55-
| None -> Protocol.null
56-
| Some locItem -> (
57-
let isModule =
58-
match locItem.locType with
59-
| SharedTypes.LModule _ | TopLevelModule _ -> true
60-
| TypeDefinition _ | Typed _ | Constant _ -> false
61-
in
62-
let uriLocOpt = References.definitionForLocItem ~package ~file locItem in
63-
let skipZero =
64-
match uriLocOpt with
65-
| None -> false
66-
| Some (_, loc) ->
67-
let isInterface = file.uri |> Uri2.isInterface in
68-
let posIsZero {Lexing.pos_lnum; pos_bol; pos_cnum} =
69-
(not isInterface) && pos_lnum = 1 && pos_cnum - pos_bol = 0
70-
in
71-
(* Skip if range is all zero, unless it's a module *)
72-
(not isModule) && posIsZero loc.loc_start && posIsZero loc.loc_end
73-
in
74-
if skipZero then Protocol.null
75-
else
76-
let hoverText = Hover.newHover ~file ~package locItem in
77-
match hoverText with
78-
| None -> Protocol.null
79-
| Some s -> Protocol.stringifyHover {contents = s})
80-
8152
let hover ~path ~line ~col =
8253
let uri = Uri2.fromLocalPath path in
8354
let result =
8455
match ProcessCmt.getFullFromCmt ~uri with
8556
| Error message -> Protocol.stringifyHover {contents = message}
86-
| Ok (package, {file; extra}) -> hover ~file ~line ~col ~extra ~package
57+
| Ok (package, {file; extra}) -> (
58+
let pos = Utils.protocolLineColToCmtLoc ~line ~col in
59+
match References.locItemForPos ~extra pos with
60+
| None -> Protocol.null
61+
| Some locItem -> (
62+
let isModule =
63+
match locItem.locType with
64+
| SharedTypes.LModule _ | TopLevelModule _ -> true
65+
| TypeDefinition _ | Typed _ | Constant _ -> false
66+
in
67+
let uriLocOpt =
68+
References.definitionForLocItem ~package ~file locItem
69+
in
70+
let skipZero =
71+
match uriLocOpt with
72+
| None -> false
73+
| Some (_, loc) ->
74+
let isInterface = file.uri |> Uri2.isInterface in
75+
let posIsZero {Lexing.pos_lnum; pos_bol; pos_cnum} =
76+
(not isInterface) && pos_lnum = 1 && pos_cnum - pos_bol = 0
77+
in
78+
(* Skip if range is all zero, unless it's a module *)
79+
(not isModule) && posIsZero loc.loc_start && posIsZero loc.loc_end
80+
in
81+
if skipZero then Protocol.null
82+
else
83+
let hoverText = Hover.newHover ~file ~package locItem in
84+
match hoverText with
85+
| None -> Protocol.null
86+
| Some s -> Protocol.stringifyHover {contents = s}))
8787
in
88-
print_endline result
8988

90-
let definition ~file ~line ~col ~extra ~package =
91-
let pos = Utils.protocolLineColToCmtLoc ~line ~col in
92-
93-
match References.locItemForPos ~extra pos with
94-
| None -> Protocol.null
95-
| Some locItem -> (
96-
let isModule =
97-
match locItem.locType with
98-
| SharedTypes.LModule _ | TopLevelModule _ -> true
99-
| TypeDefinition _ | Typed _ | Constant _ -> false
100-
in
101-
let uriLocOpt = References.definitionForLocItem ~package ~file locItem in
102-
match uriLocOpt with
103-
| None -> Protocol.null
104-
| Some (uri2, loc) ->
105-
let isInterface = file.uri |> Uri2.isInterface in
106-
let posIsZero {Lexing.pos_lnum; pos_bol; pos_cnum} =
107-
(not isInterface) && pos_lnum = 1 && pos_cnum - pos_bol = 0
108-
in
109-
(* Skip if range is all zero, unless it's a module *)
110-
let skipZero =
111-
(not isModule) && posIsZero loc.loc_start && posIsZero loc.loc_end
112-
in
113-
if skipZero then Protocol.null
114-
else
115-
Protocol.stringifyLocation
116-
{uri = Uri2.toString uri2; range = Utils.cmtLocToRange loc})
89+
print_endline result
11790

11891
let definition ~path ~line ~col =
11992
let uri = Uri2.fromLocalPath path in
12093
let result =
12194
match ProcessCmt.getFullFromCmt ~uri with
12295
| Error _message -> Protocol.null
123-
| Ok (package, {file; extra}) -> definition ~file ~line ~col ~extra ~package
96+
| Ok (package, {file; extra}) -> (
97+
let pos = Utils.protocolLineColToCmtLoc ~line ~col in
98+
99+
match References.locItemForPos ~extra pos with
100+
| None -> Protocol.null
101+
| Some locItem -> (
102+
let isModule =
103+
match locItem.locType with
104+
| SharedTypes.LModule _ | TopLevelModule _ -> true
105+
| TypeDefinition _ | Typed _ | Constant _ -> false
106+
in
107+
let uriLocOpt =
108+
References.definitionForLocItem ~package ~file locItem
109+
in
110+
match uriLocOpt with
111+
| None -> Protocol.null
112+
| Some (uri2, loc) ->
113+
let isInterface = file.uri |> Uri2.isInterface in
114+
let posIsZero {Lexing.pos_lnum; pos_bol; pos_cnum} =
115+
(not isInterface) && pos_lnum = 1 && pos_cnum - pos_bol = 0
116+
in
117+
(* Skip if range is all zero, unless it's a module *)
118+
let skipZero =
119+
(not isModule) && posIsZero loc.loc_start && posIsZero loc.loc_end
120+
in
121+
if skipZero then Protocol.null
122+
else
123+
Protocol.stringifyLocation
124+
{uri = Uri2.toString uri2; range = Utils.cmtLocToRange loc}))
124125
in
126+
125127
print_endline result
126128

127129
let references ~file ~line ~col ~extra ~package =

0 commit comments

Comments
 (0)