Signature help, formatting, and a fix that writes a missing signature

- textDocument/signatureHelp: the function whose call the cursor is in,
  with the parameter that is being written marked. A definition shows its
  head as written; an import, a trait's method and a builtin show their
  type. It reads tokens as far as the cursor, so a call that is not closed
  has help.
- textDocument/formatting: the whole text as bjo fmt lays it out. The
  server runs bjo -q fmt - on the buffer's text.
- A quick fix at a function with no signature writes the one its body
  gives it, on a line above the definition
  (CheckDecl.observeMissingSignature, in the compiler).
- The server says version 0.8.0.

Needs the compiler at its commit "CheckDecl.observeMissingSignature".

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-11-25 08:01:34 +01:00
parent d421ec9363
commit ce426b94df
8 changed files with 695 additions and 11 deletions

View file

@ -971,6 +971,126 @@ def ranges(server, tmp):
)
HELPED = """(import (std prelude))
(: (-> int int (#:scale int) (#:label string) int))
(defun (area width height #:scale 1 #:label "a")
(* scale (* width height)))
(: (-> int #:rest int int))
(defun (total first #:rest more) first)
(defun (main)
(println (int->string (area 2 3 #:scale 4)))
(println (int->string (total 1 2 3)))
(println (int->string (string-length (string-append "a" "b"))))
(println (int->string (area 2 (if #t 3 4))))
0)
"""
def helping(server, tmp):
"""Signature help: the function of the call the cursor is in, and which
of its parameters is being written."""
path = os.path.join(tmp, "helped.bjo")
uri = "file://" + path
with open(path, "w") as f:
f.write(HELPED)
found = server.diagnostics_after("textDocument/didOpen", uri, HELPED)
errors = [d for d in found if d["severity"] == 1]
expect("signature help: the file checks", errors == [], json.dumps(errors, indent=1))
def help_after(needle, nth=0):
at = position_of(HELPED, needle, nth, len(needle))
return server.send("textDocument/signatureHelp", {"textDocument": {"uri": uri}, "position": at}, request=True)["result"]
def marked(h):
"""The text of the parameter that is marked, or None."""
if not h or "activeParameter" not in h:
return None
signature = h["signatures"][0]
first, last = signature["parameters"][h["activeParameter"]]["label"]
return signature["label"][first:last]
h = help_after("(area ")
expect(
"signature help: a definition's head as written",
h and h["signatures"][0]["label"] == '(area width height #:scale 1 #:label "a")',
h,
)
expect("signature help: the first parameter", marked(h) == "width", h)
expect("signature help: its declaration is the documentation",
h and "(: area (-> int int (#:scale int) (#:label string) int))" in h["signatures"][0]["documentation"]["value"], h)
expect("signature help: the second parameter", marked(help_after("(area 2 ")) == "height")
expect("signature help: while an argument is written", marked(help_after("(area 2")) == "width")
expect("signature help: the value of a keyword", marked(help_after("(area 2 3 #:scale ")) == "#:scale 1")
expect("signature help: the keyword itself", marked(help_after("(area 2 3 #:scale")) == "#:scale 1")
expect("signature help: after a keyword and its value, none is marked", marked(help_after("(area 2 3 #:scale 4")) == "#:scale 1")
expect("signature help: a rest parameter", marked(help_after("(total 1 2 ")) == "#:rest more")
h = help_after('(string-append "a" ')
expect(
"signature help: the innermost call, of a function of the prelude",
h and "string-append" in h["signatures"][0]["label"] and h.get("activeParameter") == 1,
h,
)
h = help_after("(area 2 (if #t ")
expect(
"signature help: a special form is passed over",
h and h["signatures"][0]["label"].startswith("(area ") and marked(h) == "height",
h,
)
h = help_after("(defun (main)")
expect("signature help: none outside a call", h is None, h)
UNFORMATTED = """(import (std prelude))
(defun (main)
(println "a")
0)
"""
FORMATTED = """(import (std prelude))
(defun (main)
(println "a")
0)
"""
def formatting(server, tmp):
"""Formatting: the whole text, as `bjo fmt` lays it out."""
path = os.path.join(tmp, "formatted.bjo")
uri = "file://" + path
with open(path, "w") as f:
f.write(UNFORMATTED)
server.diagnostics_after("textDocument/didOpen", uri, UNFORMATTED)
def formatted():
return server.send(
"textDocument/formatting",
{"textDocument": {"uri": uri}, "options": {"tabSize": 2, "insertSpaces": True}},
request=True,
)
answer = formatted()
edits = answer.get("result")
expect("formatting: one edit", edits is not None and len(edits) == 1, answer)
if edits:
expect("formatting: of the whole text", edits[0]["range"] == {"start": {"line": 0, "character": 0}, "end": {"line": 5, "character": 0}}, edits)
expect("formatting: as bjo fmt lays it out", edits[0]["newText"] == FORMATTED, edits[0]["newText"])
server.change(uri, FORMATTED, 2)
server.next_diagnostics(uri)
answer = formatted()
expect("formatting: no edit for a text that is laid out", answer.get("result") == [], answer)
server.change(uri, "(defun (main)\n", 3)
server.next_diagnostics(uri)
answer = formatted()
expect("formatting: a text that does not read is refused, with why", "never closed" in answer.get("error", {}).get("message", ""), answer)
def fields(server, tmp):
path = os.path.join(tmp, "fields.bjo")
uri = "file://" + path
@ -1279,6 +1399,29 @@ def applied(text, edits):
return text
UNSIGNED = """(import (std prelude))
(defun (double n)
(* n 2))
(defun (label n #:prefix "n = ")
(str prefix (int->string n)))
(defun (pick first #:rest others)
(if (> (array-length others) 0) (array-ref others 0) first))
(defun (shout text)
(string-upcase text))
(defun (broken n)
(string-length n 1 2 3))
(defun (main)
(println (int->string (double 2)))
0)
"""
def acting(server, tmp):
def open_(name, text):
path = os.path.join(tmp, name)
@ -1355,6 +1498,50 @@ def acting(server, tmp):
" (x #:when (some? x) 1)\n (None _)\n ((Some _) _)))" in fixed, fixed)
server.diagnostics_after("textDocument/didClose", uri)
# A function with no signature is offered the one its body gives it.
uri, found = open_("unsigned.bjo", UNSIGNED)
expect("actions: each function with no signature is reported",
len([d for d in found if "requires a type signature" in d["message"]]) == 5, [d["message"] for d in found])
def signature_of(needle):
found = actions(uri, UNSIGNED, needle, 1)
titles = [a["title"] for a in found if a["title"].startswith("Write the signature ")]
return titles[0][len("Write the signature "):] if titles else None, found
written, found = signature_of("(defun (double")
# `(* n 2)`: an int of any unit. The variable is one that inference
# made, and it is written as `%a`.
expect("actions: a signature with a variable that inference made", written == "(: (-> (int %a) (int %a)))", written)
fixed = applied(UNSIGNED, edit_of(found, "Write the signature " + (written or ""), uri) or [])
expect("actions: the signature goes on a line above the definition",
"\n(: (-> (int %a) (int %a)))\n(defun (double n)\n" in fixed, fixed)
written, _ = signature_of("(defun (label")
expect("actions: a keyword parameter in a signature", written == "(: (-> int (#:prefix string) string))", written)
written, _ = signature_of("(defun (pick")
expect("actions: a rest parameter in a signature", written == "(: (-> %a #:rest %a %a))", written)
written, found = signature_of("(defun (shout")
fixed = applied(UNSIGNED, edit_of(found, "Write the signature " + (written or ""), uri) or [])
expect("actions: an indented definition gets its signature at its column",
"\n (: (-> string string))\n (defun (shout text)\n" in fixed, fixed)
written, _ = signature_of("(defun (broken")
expect("actions: a body that does not check gives no signature", written is None, written)
_, found = signature_of("(* n 2)")
expect("actions: the signature is not offered in the body", found == [], found)
# With the signatures written, the file has only the error it had.
fixed = UNSIGNED
edits = []
for needle in ["(defun (double", "(defun (label", "(defun (pick", "(defun (shout"]:
written, found = signature_of(needle)
edits += edit_of(found, "Write the signature " + (written or ""), uri) or []
fixed = applied(UNSIGNED, edits)
server.change(uri, fixed, 2)
after = server.next_diagnostics(uri)
expect("actions: with the signatures written, those functions check",
len([d for d in after if "requires a type signature" in d["message"]]) == 1,
[d["message"].splitlines()[0] for d in after])
server.diagnostics_after("textDocument/didClose", uri)
CHAIN_MACROS = """;; Two macros, one expanding to the other.
(import (std syntax-match))
@ -1575,6 +1762,8 @@ with tempfile.TemporaryDirectory() as tmp:
inferred(server, tmp)
fields(server, tmp)
ranges(server, tmp)
helping(server, tmp)
formatting(server, tmp)
renaming(server, tmp)
referencing(server, tmp)
acting(server, tmp)