diff --git a/Hover.fs b/Hover.fs index 0c8fe07..1f3a212 100644 --- a/Hover.fs +++ b/Hover.fs @@ -95,19 +95,27 @@ let rec private declaration (slice: Range -> string) (name: string) (forms: SExp | _ -> false | _ -> false - // `(: Type)`, with what may follow the type, such as `#:sync`. + // `(: Type)`, with what may follow the type: a keyword such as `#:sync`, + // or a `(where (Trait %a))`. Shown whole, with the name put in after the + // colon, so that the constraint is in the hover as it is in the source. let nameless (form: SExpr) = match form with - | SList(SAtom { Token = Colon } :: (SList _ as t) :: rest, _) - | SList(SAtom { Token = Colon } :: (SAtom { Token = (Symbol _ | TypeVar _) } as t) :: rest, _) when + | SList(SAtom { Token = Colon } :: (SList _) :: rest, r) + | SList(SAtom { Token = Colon } :: (SAtom { Token = (Symbol _ | TypeVar _) }) :: rest, r) when rest |> List.forall (function | SAtom { Token = Keyword _ } -> true + | SList(SAtom { Token = Symbol "where" } :: _, _) -> true | _ -> false) -> - Some t + Some r | _ -> None + let withName (r: Range) = + let written = slice r + let colon = written.IndexOf ':' + written.Substring(0, colon + 1) + " " + name + written.Substring(colon + 1) + let named = forms |> List.tryPick (fun form -> @@ -122,7 +130,7 @@ let rec private declaration (slice: Range -> string) (name: string) (forms: SExp |> List.pairwise |> List.tryPick (fun (before, form) -> if defines form then - nameless before |> Option.map (fun t -> $"(: %s{name} %s{slice (getRange t)})") + nameless before |> Option.map withName else None) ) diff --git a/test/smoke.py b/test/smoke.py index 5a88158..a476968 100755 --- a/test/smoke.py +++ b/test/smoke.py @@ -347,6 +347,56 @@ def documented(server, tmp): server.diagnostics_after("textDocument/didClose", uri) +CONSTRAINED = """(import (std prelude) + (std datetime)) + +(def/trait (Sized %a) + (: size (-> %a int))) + +(impl (Sized string) + (defun (size s) (string-length s))) + +(: (-> %a int) (where (Sized %a))) +(defun (twice x) (* 2 (size x))) + +(: main (-> (Vec string) int)) +(defun (main args) + (ignore (+milliseconds (now) 5L)) + (twice "ab")) +""" + + +def constrained(server, tmp): + path = os.path.join(tmp, "constrained.bjo") + uri = "file://" + path + with open(path, "w") as f: + f.write(CONSTRAINED) + found = server.diagnostics_after("textDocument/didOpen", uri, CONSTRAINED) + expect("the constrained file checks", found == [], found) + + def hover(needle): + answer = server.send( + "textDocument/hover", + {"textDocument": {"uri": uri}, "position": position_of(CONSTRAINED, needle)}, + request=True, + ) + return answer["result"]["contents"]["value"] if answer["result"] else None + + h = hover("(twice \"ab") + expect( + "hover: a nameless signature with a where clause, named", + h and "(: twice (-> %a int) (where (Sized %a)))" in h and "(defun (twice x))" in h, + h, + ) + h = hover("(+milliseconds") + expect( + "hover: an imported one with a where clause", + h and "(: +milliseconds (-> %a long %a) (where (TimeArith %a)))" in h, + h, + ) + server.diagnostics_after("textDocument/didClose", uri) + + COMPLETING = """(import (std prelude)) (defun (list-unwind xs) xs) @@ -1200,6 +1250,7 @@ with tempfile.TemporaryDirectory() as tmp: symbols_and_definitions(server, tmp) documented(server, tmp) + constrained(server, tmp) completing(server, tmp) locals_(server, tmp) inferred(server, tmp)