A hover shows a signature with a where clause
A nameless (: Type) above a definition was only taken when keywords followed the type, so one ending in (where (Trait %a)) was not, and the hover over +milliseconds or any constrained function had no type. The signature is now shown whole, the name put in after the colon, so its constraint and keywords are in the hover too. 🤖 Generated with [ECA](https://eca.dev) (anthropic/claude-opus-4-8) Co-Authored-By: eca-agent <git@eca.dev>
This commit is contained in:
parent
28341632fd
commit
29977ca301
2 changed files with 64 additions and 5 deletions
18
Hover.fs
18
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)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue