Rename, and a hover that leads with the type

textDocument/rename and prepareRename rename a local binding, or a
top-level def or defun the module does not export, with its signature,
doc and the files the module includes; a parameter's (arg ...) in its
function's doc goes with it. A rename that could change what the program
means is refused with the reason: a capture, a keyword parameter, an
exported or foreign name, a name that is not one. The renamed text is
checked first, and one with an error the text before did not have is
refused with it.

A hover's first line, which is what the status bar shows, is the type: a
nameless (: Type) above a definition is found and shown with the name, and
a definition with no signature in the file being edited shows the type the
compiler inferred.

🤖 Generated with [ECA](https://eca.dev)

Co-Authored-By: eca-agent <git@eca.dev>
This commit is contained in:
Linus Björnstam 2026-10-22 16:36:22 +02:00
parent 6a92386761
commit 853b7c3163
6 changed files with 632 additions and 17 deletions

View file

@ -521,6 +521,8 @@ INFERRED = """(import (std prelude))
(def (: m int) (+ k 1))
(def bumped (+ m 1))
bumped)
(def numbers (list 4 5 6))
"""
# One declaration that does not check, and one that does.
@ -559,7 +561,18 @@ def inferred(server, tmp):
h = hover(INFERRED, needle, 0)
expect("inferred hover: " + name, h and wanted in h, h)
h = hover(INFERRED, "(def numbers", 5)
expect("inferred hover: a top-level def with no signature shows the type inferred",
h and "(: numbers (List int))" in h, h)
# The status bar shows a hover's first line, so it has to be the type. The
# prelude writes its signatures nameless, above the definition.
h = hover(INFERRED, "(list-map")
expect(
"hover: a nameless signature, shown with the name, first",
h and h.split("\n")[1].startswith("(: list-map (-> (-?-> %a %b) (List %a) (List %b)))"),
h,
)
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,
@ -628,6 +641,122 @@ def inferred(server, tmp):
server.diagnostics_after("textDocument/didClose", uri)
RENAMING = """(export area)
(:doc helper
(summary "Twice the number.")
(arg x "A number.")
(returns "Twice x."))
(: helper (-> int int))
(defun (helper x) (* 2 x))
(: area (-> int int string))
(defun (area w h)
(def product (* w h))
(defun (twice s) (* 2 s))
(let ((n (helper 3))
(xs (list 1 2 3)))
(println #"n is ${n}")
(println '(n xs))
(let ((count 10))
(println count))
(str (displayed (twice product)) (displayed (+ n (length xs))))))
(: greet (-> string (#:greeting string) string))
(defun (greet who #:greeting "hello") (str greeting ", " who))
(defun (main) (println (area 2 3)) (println (greet "x")) 0)
"""
def renaming(server, tmp):
path = os.path.join(tmp, "renaming.bjo")
uri = "file://" + path
with open(path, "w") as f:
f.write(RENAMING)
found = server.diagnostics_after("textDocument/didOpen", uri, RENAMING)
expect("rename: the file checks", [d for d in found if d["severity"] == 1] == [], found)
def ask(method, needle, offset, new=None):
params = {"textDocument": {"uri": uri}, "position": position_of(RENAMING, needle, 0, offset)}
if new is not None:
params["newName"] = new
return server.send(method, params, request=True)
def renamed(needle, offset, new):
"""The text with the rename applied, or the refusal's message."""
answer = ask("textDocument/rename", needle, offset, new)
if "error" in answer:
return None, answer["error"]["message"]
lines = RENAMING.split("\n")
edits = answer["result"]["changes"].get(uri, [])
for e in sorted(edits, key=lambda e: (-e["range"]["start"]["line"], -e["range"]["start"]["character"])):
a, b = e["range"]["start"], e["range"]["end"]
line = lines[a["line"]]
lines[a["line"]] = line[: a["character"]] + e["newText"] + line[b["character"] :]
return "\n".join(lines), None
prepared = ask("textDocument/prepareRename", "((n (helper", 2)["result"]
expect(
"rename: prepareRename gives the name and where it is",
prepared and prepared["placeholder"] == "n"
and prepared["range"]["start"] == position_of(RENAMING, "((n (helper", 0, 2),
prepared,
)
text, why = renamed("((n (helper", 2, "num")
expect(
"rename: a local, in an interpolation too, and not in quoted data",
text is not None
and "((num (helper 3))" in text
and "${num}" in text
and "'(n xs)" in text
and "(+ num (length xs))" in text,
why or text,
)
text, why = renamed("(def product", 5, "prod")
expect("rename: a def in a body", text is not None and "(def prod (* w h))" in text and "(displayed (twice prod))" in text,
why or text)
text, why = renamed("(defun (helper x)", 8, "double")
expect(
"rename: a top-level defun the module does not export, its signature and doc too",
text is not None
and "(:doc double" in text
and "(: double (-> int int))" in text
and "(defun (double x)" in text
and "((n (double 3))" in text,
why or text,
)
text, why = renamed("(defun (helper x)", 15, "k")
expect("rename: a parameter, and the (arg ...) of its function's doc",
text is not None and '(arg k "A number.")' in text and "(defun (helper k) (* 2 k))" in text,
why or text)
for label, needle, offset, new, wanted in [
("a binding the new name would take a use from", "((n (helper", 2, "xs", "'xs' is bound at"),
("a use of the new name the renamed binding would take", "((n (helper", 2, "count", "'count' is used at"),
("an exported name", "(defun (area w", 8, "size", "is exported at"),
("a name defined elsewhere", "(length xs)", 1, "len", "is defined in prelude.bjo"),
("a keyword parameter", "(str greeting", 5, "hi", "keyword parameter"),
("main", "(defun (main)", 8, "start", "entry point"),
("a name of a definition the module sees", "(defun (helper x)", 8, "length", "is already defined"),
("a name that is not one", "((n (helper", 2, "two words", "is not a name"),
("a rename the check refuses", "(defun (twice s)", 8, "when", "would give the program an error"),
]:
text, why = renamed(needle, offset, new)
expect(f"rename refused: {label}", text is None and why and wanted in why, why or text)
# The renamed text is what the editor ends up with, and it checks.
text, _ = renamed("((n (helper", 2, "num")
server.change(uri, text, 2)
found = server.next_diagnostics(uri)
expect("rename: the renamed text checks", [d for d in found if d["severity"] == 1] == [], found)
server.diagnostics_after("textDocument/didClose", uri)
CHAIN_MACROS = """;; Two macros, one expanding to the other.
(import (std syntax-match))
@ -842,6 +971,7 @@ with tempfile.TemporaryDirectory() as tmp:
completing(server, tmp)
locals_(server, tmp)
inferred(server, tmp)
renaming(server, tmp)
expanding(server, tmp)
answer = server.send("shutdown", request=True)