"""Fake rust-analyzer for manager tests. Mimics the on-save flycheck protocol: didChange publishes empty "native" diagnostics at once; didSave starts a flycheck (`$/progress` begin/end), and the real diagnostics (any text containing "oops" is an error) are published shortly after `end`. """ import json, sys, threading, time out_lock = threading.Lock() docs = {} def send(m): b = json.dumps(m).encode() with out_lock: sys.stdout.buffer.write(b"Content-Length: %d\r\n\r\n" % len(b) + b) sys.stdout.buffer.flush() def read(): n = None while True: line = sys.stdin.buffer.readline() if not line: return None line = line.strip() if not line: break if line.lower().startswith(b"content-length:"): n = int(line.split(b":")[1]) return json.loads(sys.stdin.buffer.read(n)) def publish(uri, version, diags): send({"jsonrpc": "2.0", "method": "textDocument/publishDiagnostics", "params": {"uri": uri, "version": version, "diagnostics": diags}}) def progress(kind): send({"jsonrpc": "2.0", "method": "$/progress", "params": {"token": "rust-analyzer/flycheck/0", "value": {"kind": kind}}}) def flycheck(uri): version, text = docs[uri] progress("begin") time.sleep(0.3) progress("end") time.sleep(0.1) bad = [{"range": {"start": {"line": 0, "character": 0}}, "severity": 1, "message": "bad", "source": "fake"}] if "oops" in text else [] publish(uri, version, bad) while True: m = read() if m is None: break method, p = m.get("method"), m.get("params", {}) if method == "initialize": send({"jsonrpc": "2.0", "id": m["id"], "result": {"capabilities": {}}}) elif method == "shutdown": send({"jsonrpc": "2.0", "id": m["id"], "result": None}) elif method == "exit": break elif method in ("textDocument/didOpen", "textDocument/didChange"): td = p["textDocument"] text = td["text"] if method.endswith("didOpen") else p["contentChanges"][0]["text"] docs[td["uri"]] = (td["version"], text) publish(td["uri"], td["version"], []) elif method == "textDocument/didSave": threading.Thread(target=flycheck, args=(p["textDocument"]["uri"],), daemon=True).start()