Inferred types: in hovers, and as inlay hints
A check keeps what the compiler inferred, through Inference.observe, as each use of a name with its type there and each binding with the type it binds. A hover shows (: name type) for a local, and a generic function's type at the use hovered. Inlay hints show the type after each local name where it is bound and no type is written. Hints before the first change since the last check are sent; the ones after it wait for the check, which then asks the client to ask again. A hover runs a check that is due first. 🤖 Generated with [ECA](https://eca.dev) Co-Authored-By: eca-agent <git@eca.dev>
This commit is contained in:
parent
cc62a8a360
commit
471b49d9e4
9 changed files with 571 additions and 20 deletions
119
test/smoke.py
119
test/smoke.py
|
|
@ -502,6 +502,124 @@ def locals_(server, tmp):
|
|||
server.diagnostics_after("textDocument/didClose", uri)
|
||||
|
||||
|
||||
INFERRED = """(import (std prelude))
|
||||
|
||||
(: total (-> (List int) int))
|
||||
(defun (total xs)
|
||||
(let ((pairs (list-map (fun (x) (Tuple x "s")) xs)))
|
||||
(match (first pairs)
|
||||
((Some (Tuple n label)) (+ n (string-count label)))
|
||||
(None (loop (:for y xs) (:acc sum (summing y)) => sum)))))
|
||||
|
||||
(: lengths (-> (Vec string) (Vec int)))
|
||||
(defun (lengths names)
|
||||
(defun (twice s) (* 2 (string-count s)))
|
||||
(vec-map twice names))
|
||||
|
||||
(: annotated (-> int int))
|
||||
(defun (annotated k)
|
||||
(def (: m int) (+ k 1))
|
||||
m)
|
||||
"""
|
||||
|
||||
# One declaration that does not check, and one that does.
|
||||
INFERRED_BROKEN = INFERRED.replace('(Tuple x "s")', '(+ x "s")')
|
||||
|
||||
# The same, with what `pairs` holds changed and not saved.
|
||||
INFERRED_EDITED = INFERRED.replace('(Tuple x "s")', '(Tuple x 2.5)').replace("(string-count label)", "1")
|
||||
|
||||
|
||||
def inferred(server, tmp):
|
||||
path = os.path.join(tmp, "inferred.bjo")
|
||||
uri = "file://" + path
|
||||
with open(path, "w") as f:
|
||||
f.write(INFERRED)
|
||||
found = server.diagnostics_after("textDocument/didOpen", uri, INFERRED)
|
||||
expect("inferred: the file checks", found == [], json.dumps(found, indent=1))
|
||||
|
||||
def hover(text, needle, offset=1, nth=0):
|
||||
at = position_of(text, needle, nth, offset)
|
||||
h = server.send("textDocument/hover", {"textDocument": {"uri": uri}, "position": at}, request=True)["result"]
|
||||
return h and h["contents"]["value"]
|
||||
|
||||
cases = [
|
||||
("a parameter without a type", "xs)\n", "(: xs (List int))"),
|
||||
("a let binding", "pairs (list-map", "(: pairs (List (Tuple int string)))"),
|
||||
("a fun's parameter, at a use", "x \"s\"", "(: x int)"),
|
||||
("a match pattern's variable", "n label)", "(: n int)"),
|
||||
("another, at a use", "label)))", "(: label string)"),
|
||||
("a loop variable", "y xs)", "(: y int)"),
|
||||
("a local defun, at a use", "twice names", "(: twice (-?-> string int))"),
|
||||
("a local defun's parameter", "s)))\n (vec-map", "(: s string)"),
|
||||
]
|
||||
for name, needle, wanted in cases:
|
||||
h = hover(INFERRED, needle, 0)
|
||||
expect("inferred hover: " + name, h and wanted in h, h)
|
||||
|
||||
h = hover(INFERRED, "(list-map")
|
||||
expect(
|
||||
"inferred hover: a generic function, at this use",
|
||||
h and "At this use" in h and "(: list-map (-> (-> int (Tuple int string)) (List int) (List (Tuple int string))))" in h,
|
||||
h,
|
||||
)
|
||||
|
||||
def hints(text):
|
||||
"""Each hint as (the text before it on its line, its label)."""
|
||||
lines = text.split("\n")
|
||||
whole = {"start": {"line": 0, "character": 0}, "end": {"line": len(lines), "character": 0}}
|
||||
found = server.send("textDocument/inlayHint", {"textDocument": {"uri": uri}, "range": whole}, request=True)["result"]
|
||||
return [(lines[h["position"]["line"]][: h["position"]["character"]], h["label"]) for h in found]
|
||||
|
||||
found = hints(INFERRED)
|
||||
labels = {before.split()[-1].lstrip("("): label for before, label in found}
|
||||
expect(
|
||||
"inlay hints: a type after each local's binding",
|
||||
labels.get("pairs") == ": (List (Tuple int string))"
|
||||
and labels.get("x") == ": int"
|
||||
and labels.get("n") == ": int"
|
||||
and labels.get("label") == ": string"
|
||||
and labels.get("y") == ": int"
|
||||
and labels.get("s") == ": string",
|
||||
found,
|
||||
)
|
||||
expect(
|
||||
"inlay hints: none for a top-level function's parameters",
|
||||
"xs" not in labels and "names" not in labels and "k" not in labels,
|
||||
found,
|
||||
)
|
||||
expect("inlay hints: none where the type is written", "m" not in labels, found)
|
||||
|
||||
# An edit not checked yet, between `y` and `s`: the hints before it
|
||||
# stay, and the one after it waits for the check.
|
||||
edited = INFERRED.replace("(defun (lengths names)", "(defun (lengths names)")
|
||||
server.change(uri, edited, 2)
|
||||
labels = {before.split()[-1].lstrip("("): label for before, label in hints(edited)}
|
||||
expect(
|
||||
"inlay hints: before an unchecked edit they stay, after it they wait",
|
||||
labels.get("y") == ": int" and "s" not in labels,
|
||||
labels,
|
||||
)
|
||||
server.next_diagnostics(uri)
|
||||
labels = {before.split()[-1].lstrip("("): label for before, label in hints(edited)}
|
||||
expect("inlay hints: after the check, all of them", labels.get("s") == ": string", labels)
|
||||
|
||||
# A type error in `total`: `lengths` still has its types, and `total`'s
|
||||
# names have none rather than wrong ones.
|
||||
server.change(uri, INFERRED_BROKEN, 3)
|
||||
server.next_diagnostics(uri)
|
||||
h = hover(INFERRED_BROKEN, "twice names", 0)
|
||||
expect("inferred hover: another declaration's error leaves this one's", h and "(: twice (-?-> string int))" in h, h)
|
||||
h = hover(INFERRED_BROKEN, "pairs (list-map", 0)
|
||||
expect("inferred hover: a declaration that failed has none", h and "(: pairs" not in h, h)
|
||||
|
||||
# Hovered straight after typing, before the check would have run: the
|
||||
# hover checks first, and the types are of the text as it stands.
|
||||
server.change(uri, INFERRED_EDITED, 4)
|
||||
h = hover(INFERRED_EDITED, "pairs (list-map", 0)
|
||||
expect("inferred hover: an unsaved edit is checked first", h and "(: pairs (List (Tuple int double)))" in h, h)
|
||||
server.diagnostics_after("textDocument/didClose", uri)
|
||||
|
||||
|
||||
CHAIN_MACROS = """;; Two macros, one expanding to the other.
|
||||
(import (std syntax-match))
|
||||
|
||||
|
|
@ -715,6 +833,7 @@ with tempfile.TemporaryDirectory() as tmp:
|
|||
documented(server, tmp)
|
||||
completing(server, tmp)
|
||||
locals_(server, tmp)
|
||||
inferred(server, tmp)
|
||||
expanding(server, tmp)
|
||||
|
||||
answer = server.send("shutdown", request=True)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue