References, and code actions that add a match's clauses and fill holes
textDocument/references and documentHighlight give the uses of the name at point in its module, found as go to definition finds a binding: a local, a top-level definition, a type or a constructor, or an imported name where the module uses it. Uses.fs holds the search, which Rename now shares. textDocument/codeAction offers what the compiler worked out while it checked (Exhaustiveness.observe, Holes.observe, kept by Fixes.fs): at an incomplete match, the clauses it lacks, lined up after its last clause with holes as bodies; at a typed hole, a binding in scope that fits, a constructor of its type with holes for what it takes, or a fun. Version 0.7.0. 🤖 Generated with [ECA](https://eca.dev) Co-Authored-By: eca-agent <git@eca.dev>
This commit is contained in:
parent
853b7c3163
commit
23b6a0e55c
9 changed files with 634 additions and 60 deletions
204
test/smoke.py
204
test/smoke.py
|
|
@ -757,6 +757,208 @@ def renaming(server, tmp):
|
|||
server.diagnostics_after("textDocument/didClose", uri)
|
||||
|
||||
|
||||
SHAPES = """(type (: Shape (Union (: Circle int) (: Square int))))
|
||||
|
||||
(: size (-> Shape int))
|
||||
(defun (size s)
|
||||
(match s
|
||||
((Circle r) r)
|
||||
((Square w) w)))
|
||||
|
||||
(defun (main) (println (int->string (size (Circle 2)))) 0)
|
||||
"""
|
||||
|
||||
|
||||
def referencing(server, tmp):
|
||||
path = os.path.join(tmp, "renaming.bjo")
|
||||
uri = "file://" + path
|
||||
with open(path, "w") as f:
|
||||
f.write(RENAMING)
|
||||
server.diagnostics_after("textDocument/didOpen", uri, RENAMING)
|
||||
|
||||
def ask(method, text, file_uri, needle, offset, nth=0, definition=True):
|
||||
params = {"textDocument": {"uri": file_uri}, "position": position_of(text, needle, nth, offset)}
|
||||
if method == "textDocument/references":
|
||||
params["context"] = {"includeDeclaration": definition}
|
||||
return server.send(method, params, request=True)["result"]
|
||||
|
||||
def at(text, found):
|
||||
"""Each location as the line it is on and what it covers."""
|
||||
lines = text.split("\n")
|
||||
return sorted(
|
||||
(l["range"]["start"]["line"],
|
||||
lines[l["range"]["start"]["line"]][l["range"]["start"]["character"]:l["range"]["end"]["character"]])
|
||||
for l in found or [])
|
||||
|
||||
def lines(text, *needles):
|
||||
return sorted(line_of(text, n) for n in needles)
|
||||
|
||||
found = ask("textDocument/references", RENAMING, uri, "(+ n", 3)
|
||||
expect("references: a local's binding and uses, in an interpolation too, not in quoted data",
|
||||
[l for l, _ in at(RENAMING, found)] == lines(RENAMING, "((n (helper", "${n}", "(+ n")
|
||||
and all(name == "n" for _, name in at(RENAMING, found)),
|
||||
at(RENAMING, found))
|
||||
|
||||
found = ask("textDocument/references", RENAMING, uri, "(+ n", 3, definition=False)
|
||||
expect("references: without the declaration, a local's uses only",
|
||||
[l for l, _ in at(RENAMING, found)] == lines(RENAMING, "${n}", "(+ n"), at(RENAMING, found))
|
||||
|
||||
found = ask("textDocument/references", RENAMING, uri, "(helper 3)", 1)
|
||||
expect("references: a top-level defun, in its doc and signature too",
|
||||
[l for l, _ in at(RENAMING, found)]
|
||||
== lines(RENAMING, "(:doc helper", "(: helper", "(defun (helper", "(helper 3)"),
|
||||
at(RENAMING, found))
|
||||
|
||||
found = ask("textDocument/references", RENAMING, uri, "(defun (area", 8)
|
||||
expect("references: an exported name, in this module",
|
||||
[l for l, _ in at(RENAMING, found)]
|
||||
== lines(RENAMING, "(export area)", "(: area", "(defun (area", "(println (area"),
|
||||
at(RENAMING, found))
|
||||
|
||||
found = ask("textDocument/references", RENAMING, uri, "(length xs)", 1)
|
||||
expect("references: an imported name, where this module uses it",
|
||||
at(RENAMING, found) == [(line_of(RENAMING, "(length xs)"), "length")], at(RENAMING, found))
|
||||
|
||||
found = ask("textDocument/references", RENAMING, uri, "(str greeting", 5)
|
||||
expect("references: a keyword parameter, in its function",
|
||||
[l for l, _ in at(RENAMING, found)] == lines(RENAMING, "#:greeting \"hello\"", "(str greeting"),
|
||||
at(RENAMING, found))
|
||||
|
||||
found = ask("textDocument/documentHighlight", RENAMING, uri, "(+ n", 3)
|
||||
kinds = sorted((h["range"]["start"]["line"], h["kind"]) for h in found or [])
|
||||
expect("highlight: the binding as a write, the uses as reads",
|
||||
kinds == sorted([(line_of(RENAMING, "((n (helper"), 3), (line_of(RENAMING, "${n}"), 2),
|
||||
(line_of(RENAMING, "(+ n"), 2)]),
|
||||
kinds)
|
||||
server.diagnostics_after("textDocument/didClose", uri)
|
||||
|
||||
path = os.path.join(tmp, "shapes.bjo")
|
||||
uri = "file://" + path
|
||||
with open(path, "w") as f:
|
||||
f.write(SHAPES)
|
||||
server.diagnostics_after("textDocument/didOpen", uri, SHAPES)
|
||||
|
||||
found = ask("textDocument/references", SHAPES, uri, "(-> Shape", 4)
|
||||
expect("references: a type, its declaration and the signature that names it",
|
||||
at(SHAPES, found) == sorted([(0, "Shape"), (line_of(SHAPES, "(-> Shape"), "Shape")]), at(SHAPES, found))
|
||||
|
||||
found = ask("textDocument/references", SHAPES, uri, "(Circle 2)", 1)
|
||||
expect("references: a constructor, its case, a pattern and a use",
|
||||
at(SHAPES, found)
|
||||
== sorted([(0, "Circle"), (line_of(SHAPES, "((Circle r)"), "Circle"), (line_of(SHAPES, "(Circle 2)"), "Circle")]),
|
||||
at(SHAPES, found))
|
||||
server.diagnostics_after("textDocument/didClose", uri)
|
||||
|
||||
|
||||
INCOMPLETE = """(type (: Shape (Union (: Circle int) (: Square int) Empty)))
|
||||
|
||||
(: size (-> Shape int))
|
||||
(defun (size s)
|
||||
(match s
|
||||
((Circle r) r)))
|
||||
|
||||
(: pick (-> (Option Shape) int))
|
||||
(defun (pick o)
|
||||
(match o
|
||||
((Some (Circle r)) r)
|
||||
(None 0)))
|
||||
|
||||
(defun (main) (println (int->string (+ (size Empty) (pick None)))) 0)
|
||||
"""
|
||||
|
||||
HOLES = """(type (: Shape (Union (: Circle int) (: Square int) Empty)))
|
||||
|
||||
(: add-size (-> Shape int int))
|
||||
(defun (add-size s n) n)
|
||||
|
||||
(: total-size (-> (List Shape) int))
|
||||
(defun (total-size shapes)
|
||||
(list-foldl _ 0 shapes))
|
||||
|
||||
(: default-shape (-> Shape))
|
||||
(defun (default-shape) _)
|
||||
|
||||
(defun (main) (println (int->string (total-size (list (default-shape))))) 0)
|
||||
"""
|
||||
|
||||
|
||||
def applied(text, edits):
|
||||
"""`text` with LSP text edits made, the last first."""
|
||||
lines = text.split("\n")
|
||||
offsets = [0]
|
||||
for line in lines:
|
||||
offsets.append(offsets[-1] + len(line) + 1)
|
||||
|
||||
def offset(p):
|
||||
return offsets[p["line"]] + p["character"]
|
||||
|
||||
for e in sorted(edits, key=lambda e: -offset(e["range"]["start"])):
|
||||
a, b = offset(e["range"]["start"]), offset(e["range"]["end"])
|
||||
text = text[:a] + e["newText"] + text[b:]
|
||||
return text
|
||||
|
||||
|
||||
def acting(server, tmp):
|
||||
def open_(name, text):
|
||||
path = os.path.join(tmp, name)
|
||||
uri = "file://" + path
|
||||
with open(path, "w") as f:
|
||||
f.write(text)
|
||||
return uri, server.diagnostics_after("textDocument/didOpen", uri, text)
|
||||
|
||||
def actions(uri, text, needle, offset=1):
|
||||
at = position_of(text, needle, 0, offset)
|
||||
params = {"textDocument": {"uri": uri}, "range": {"start": at, "end": at}, "context": {"diagnostics": []}}
|
||||
return server.send("textDocument/codeAction", params, request=True)["result"] or []
|
||||
|
||||
def edit_of(found, title, uri):
|
||||
chosen = [a for a in found if a["title"] == title]
|
||||
return chosen[0]["edit"]["changes"][uri] if chosen else None
|
||||
|
||||
uri, found = open_("incomplete.bjo", INCOMPLETE)
|
||||
expect("actions: the incomplete matches are reported",
|
||||
len([d for d in found if "does not cover every value" in d["message"]]) == 2, found)
|
||||
|
||||
found = actions(uri, INCOMPLETE, "(match s", 1)
|
||||
first = edit_of(found, "Add the 2 missing clauses", uri)
|
||||
expect("actions: a match lacking two cases is offered both", first is not None, [a["title"] for a in found])
|
||||
fixed = applied(INCOMPLETE, first or [])
|
||||
expect("actions: the clauses go after the last one, lined up with it",
|
||||
" ((Circle r) r)\n ((Square _) _)\n (Empty _))" in fixed, fixed)
|
||||
|
||||
# Both actions are of the text as checked, so both go into it together.
|
||||
found = actions(uri, INCOMPLETE, "(match o", 1)
|
||||
second = edit_of(found, "Add the 2 missing clauses", uri)
|
||||
fixed = applied(INCOMPLETE, (first or []) + (second or []))
|
||||
expect("actions: a nested case is spelled out",
|
||||
" (None 0)\n ((Some (Square _)) _)\n ((Some Empty) _))" in fixed, fixed)
|
||||
|
||||
server.change(uri, fixed, 2)
|
||||
after = server.next_diagnostics(uri)
|
||||
expect("actions: with the clauses added, every match covers its type and the bodies are holes",
|
||||
not any("does not cover" in d["message"] for d in after)
|
||||
and len([d for d in after if d["message"].startswith("Hole at")]) == 4,
|
||||
[d["message"].splitlines()[0] for d in after])
|
||||
server.diagnostics_after("textDocument/didClose", uri)
|
||||
|
||||
uri, found = open_("holes.bjo", HOLES)
|
||||
found = actions(uri, HOLES, "(list-foldl _", 12)
|
||||
titles = [a["title"] for a in found]
|
||||
expect("actions: a hole is offered the binding that fits it, and a fun",
|
||||
"Fill with add-size" in titles and "Fill with (fun (x y) _)" in titles, titles)
|
||||
filled = applied(HOLES, edit_of(found, "Fill with add-size", uri) or [])
|
||||
expect("actions: filling replaces the hole", "(list-foldl add-size 0 shapes)" in filled, filled)
|
||||
|
||||
found = actions(uri, HOLES, "(defun (default-shape) _", 23)
|
||||
titles = [a["title"] for a in found]
|
||||
expect("actions: a hole of a union's type is offered its cases, each once",
|
||||
sorted(titles) == ["Fill with (Circle _)", "Fill with (Square _)", "Fill with Empty"], titles)
|
||||
|
||||
expect("actions: nothing is offered away from a hole", actions(uri, HOLES, "(defun (add-size", 8) == [],
|
||||
actions(uri, HOLES, "(defun (add-size", 8))
|
||||
server.diagnostics_after("textDocument/didClose", uri)
|
||||
|
||||
|
||||
CHAIN_MACROS = """;; Two macros, one expanding to the other.
|
||||
(import (std syntax-match))
|
||||
|
||||
|
|
@ -972,6 +1174,8 @@ with tempfile.TemporaryDirectory() as tmp:
|
|||
locals_(server, tmp)
|
||||
inferred(server, tmp)
|
||||
renaming(server, tmp)
|
||||
referencing(server, tmp)
|
||||
acting(server, tmp)
|
||||
expanding(server, tmp)
|
||||
|
||||
answer = server.send("shutdown", request=True)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue