Skip to content

Signature help fixes #950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
clean up signature help
  • Loading branch information
zth committed Mar 10, 2024
commit 11796749cef192c10c5c7cb3f6bec300a45c7649
61 changes: 31 additions & 30 deletions analysis/src/SignatureHelp.ml
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
open SharedTypes
type cursorAtArg = Unlabelled of int | Labelled of string

let shouldPrintMainTypeStr typ ~env ~package =
match typ |> Shared.digConstructor with
| Some path -> (
match References.digConstructor ~env ~package path with
| Some (_, {item = {kind = Record _}}) -> false
| _ -> true)
| _ -> false

(* Produces the doc string shown below the signature help for each parameter. *)
let docsForLabel typeExpr ~file ~package ~supportsMarkdownLinks =
let env = QueryEnv.fromFile file in
let types = Hover.findRelevantTypesFromType ~file ~package typeExpr in
let typeString =
if shouldPrintMainTypeStr typeExpr ~env ~package then
Markdown.codeBlock (typeExpr |> Shared.typeToString)
else ""
in
let typeNames = types |> List.map (fun {Hover.name} -> name) in
let typeDefinitions =
types
Expand Down Expand Up @@ -45,9 +31,9 @@ let docsForLabel typeExpr ~file ~package ~supportsMarkdownLinks =
(Shared.declToString ~printNameAsIs:true typeName decl)
^ linkToTypeDefinitionStr)
in
typeString :: typeDefinitions |> String.concat "\n"
typeDefinitions |> String.concat "\n"

let findFunctionType ~currentFile ~debug ~path ~pos ~supportsMarkdownLinks =
let findFunctionType ~currentFile ~debug ~path ~pos =
(* Start by looking at the typed info at the loc of the fn *)
match Cmt.loadFullCmtFromPath ~path with
| None -> None
Expand All @@ -56,21 +42,20 @@ let findFunctionType ~currentFile ~debug ~path ~pos ~supportsMarkdownLinks =
let env = QueryEnv.fromFile file in
let fnFromLocItem =
match References.getLocItem ~full ~pos ~debug:false with
| Some {locType = Typed (_, typeExpr, _)} -> (
| Some {locType = Typed (_, typeExpr, locKind)} -> (
let docstring =
match References.definedForLoc ~file ~package locKind with
| None -> []
| Some (docstring, _) -> docstring
in
if Debug.verbose () then
Printf.printf "[sig_help_fn] Found loc item: %s.\n"
(Shared.typeToString typeExpr);
match
TypeUtils.extractFunctionType2 ~env ~package:full.package typeExpr
with
| args, _tRet, _ when args <> [] ->
Some
( args,
[docsForLabel ~file ~package ~supportsMarkdownLinks typeExpr],
typeExpr,
package,
env,
file )
Some (args, docstring, typeExpr, package, env, file)
| _ -> None)
| None ->
if Debug.verbose () then
Expand Down Expand Up @@ -394,9 +379,7 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
| Some (_, `FunctionCall (argAtCursor, exp, _extractedArgs)) -> (
(* Not looking for the cursor position after this, but rather the target function expression's loc. *)
let pos = exp.pexp_loc |> Loc.end_ in
match
findFunctionType ~currentFile ~debug ~path ~pos ~supportsMarkdownLinks
with
match findFunctionType ~currentFile ~debug ~path ~pos with
| Some (args, docstring, type_expr, package, _env, file) ->
if debug then
Printf.printf "argAtCursor: %s\n"
Expand Down Expand Up @@ -432,6 +415,8 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =

(* Figure out the active parameter *)
let activeParameter = findActiveParameter ~argAtCursor ~args in

let paramUnlabelledArgCount = ref 0 in
Some
{
Protocol.signatures =
Expand All @@ -441,16 +426,32 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
parameters =
parameters
|> List.map (fun (argLabel, start, end_) ->
let paramArgCount = !paramUnlabelledArgCount in
paramUnlabelledArgCount := paramArgCount + 1;
let unlabelledArgCount = ref 0 in
{
Protocol.label = (start, end_);
documentation =
(match
args
|> List.find_opt (fun (lbl, _) ->
lbl = argLabel)
let argCount = !unlabelledArgCount in
unlabelledArgCount := argCount + 1;
match (lbl, argLabel) with
| ( Asttypes.Optional l1,
Asttypes.Optional l2 )
when l1 = l2 ->
true
| Labelled l1, Labelled l2
when l1 = l2 ->
true
| Nolabel, Nolabel
when paramArgCount = argCount ->
true
| _ -> false)
with
| None ->
{Protocol.kind = "markdown"; value = "Nope"}
{Protocol.kind = "markdown"; value = ""}
| Some (_, labelTypExpr) ->
{
Protocol.kind = "markdown";
Expand Down Expand Up @@ -626,7 +627,7 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
documentation = {Protocol.kind = "markdown"; value = ""};
}
:: (fields
|> List.map (fun (_, field, (start, end_)) ->
|> List.map (fun (_, (field : field), (start, end_)) ->
{
Protocol.label =
(baseOffset + start, baseOffset + end_);
Expand Down
31 changes: 29 additions & 2 deletions analysis/tests/src/SignatureHelp.res
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,32 @@ let _deepestTakesPrecedence = [12]->Js.Array2.map(v =>
}
)

let _usesCorrectTypeInfo = [12]->Belt.Array.map(v => v)
// ^she
/** Main docstring here. */
let map = (arr, mapper) => {
Array.map(mapper, arr)
}

let _usesCorrectTypeInfo = [12]->map(v => v)
// ^she

/** Type x... */
type x = {
age?: int,
name?: string,
}

/** Type tt! */
type tt = One

/** Some stuff */
let stuffers = (x: x, y: tt) => {
ignore(x)
ignore(y)
"hello"
}

let _ = stuffers({}, One)
// ^she

let _ = stuffers({}, One)
// ^she
Loading