Skip to content

Commit 0ef49f0

Browse files
committed
extract logic for finding argument with cursor into its own function, in preparation for handling expressions with pipe
1 parent 417b0ee commit 0ef49f0

File tree

1 file changed

+38
-35
lines changed

1 file changed

+38
-35
lines changed

analysis/src/SignatureHelp.ml

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,43 @@ let signatureHelp ~path ~pos ~currentFile ~debug =
155155
let supportsMarkdownLinks = true in
156156
let foundFunctionApplicationExpr = ref None in
157157
let setFound r = foundFunctionApplicationExpr := Some r in
158+
let searchForArgWithCursor ~args ~exp =
159+
let extractedArgs = extractExpApplyArgs ~args in
160+
let argAtCursor =
161+
let unlabelledArgCount = ref 0 in
162+
extractedArgs
163+
|> List.find_map (fun arg ->
164+
match arg.label with
165+
| None ->
166+
let currentUnlabelledArgCount = !unlabelledArgCount in
167+
unlabelledArgCount := currentUnlabelledArgCount + 1;
168+
(* An argument without a label is just the expression, so we can use that. *)
169+
if arg.exp.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor then
170+
Some (Unlabelled currentUnlabelledArgCount)
171+
else None
172+
| Some {name; posStart; posEnd} -> (
173+
(* Check for the label identifier itself having the cursor *)
174+
match
175+
pos |> CursorPosition.classifyPositions ~posStart ~posEnd
176+
with
177+
| HasCursor -> Some (Labelled name)
178+
| NoCursor | EmptyLoc -> (
179+
(* If we're not in the label, check the exp. Either the exp
180+
exists and has the cursor. Or the exp is a parser recovery
181+
node, in which case we assume that the parser recovery
182+
indicates that the cursor was here. *)
183+
match
184+
( arg.exp.pexp_desc,
185+
arg.exp.pexp_loc
186+
|> CursorPosition.classifyLoc ~pos:posBeforeCursor )
187+
with
188+
| Pexp_extension ({txt = "rescript.exprhole"}, _), _
189+
| _, HasCursor ->
190+
Some (Labelled name)
191+
| _ -> None)))
192+
in
193+
setFound (argAtCursor, exp, extractedArgs)
194+
in
158195
let expr (iterator : Ast_iterator.iterator) (expr : Parsetree.expression) =
159196
(match expr with
160197
(* Look for applying idents, like someIdent(...) *)
@@ -165,41 +202,7 @@ let signatureHelp ~path ~pos ~currentFile ~debug =
165202
when pexp_loc
166203
|> CursorPosition.classifyLoc ~pos:posBeforeCursor
167204
== HasCursor ->
168-
let extractedArgs = extractExpApplyArgs ~args in
169-
let argAtCursor =
170-
let unlabelledArgCount = ref 0 in
171-
extractedArgs
172-
|> List.find_map (fun arg ->
173-
match arg.label with
174-
| None ->
175-
let currentUnlabelledArgCount = !unlabelledArgCount in
176-
unlabelledArgCount := currentUnlabelledArgCount + 1;
177-
(* An argument without a label is just the expression, so we can use that. *)
178-
if arg.exp.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor then
179-
Some (Unlabelled currentUnlabelledArgCount)
180-
else None
181-
| Some {name; posStart; posEnd} -> (
182-
(* Check for the label identifier itself having the cursor *)
183-
match
184-
pos |> CursorPosition.classifyPositions ~posStart ~posEnd
185-
with
186-
| HasCursor -> Some (Labelled name)
187-
| NoCursor | EmptyLoc -> (
188-
(* If we're not in the label, check the exp. Either the exp
189-
exists and has the cursor. Or the exp is a parser recovery
190-
node, in which case we assume that the parser recovery
191-
indicates that the cursor was here. *)
192-
match
193-
( arg.exp.pexp_desc,
194-
arg.exp.pexp_loc
195-
|> CursorPosition.classifyLoc ~pos:posBeforeCursor )
196-
with
197-
| Pexp_extension ({txt = "rescript.exprhole"}, _), _
198-
| _, HasCursor ->
199-
Some (Labelled name)
200-
| _ -> None)))
201-
in
202-
setFound (argAtCursor, exp, extractedArgs)
205+
searchForArgWithCursor ~args ~exp
203206
| _ -> ());
204207
Ast_iterator.default_iterator.expr iterator expr
205208
in

0 commit comments

Comments
 (0)