rust-analyzer/lib/lsp-server/examples/manual_test.sh
Shashi Shankar 4541495037 examples: add minimal_lsp.rs and FIFO test script.
* `examples/minimal_lsp.rs` – compact LSP server showing definition,
  completion, hover, rustfmt-based formatting, and dummy diagnostics.
  Advertises UTF-8 offset encoding.

* `examples/manual_test.sh` – FIFO script that streams the canonical
  nine LSP packets so anyone can validate the server from two terminals.

No new runtime deps; `anyhow` stays under [dev-dependencies].
2025-07-17 11:04:14 +02:00

53 lines
2.3 KiB
Bash
Executable file
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# Simple nine-packet LSP test for examples/minimal_lsp.rs
# Usage (two tabs):
#
# mkfifo /tmp/lsp_pipe # one-time setup
# # tab 1 run the server
# cat /tmp/lsp_pipe | cargo run --example minimal_lsp
#
# # tab 2 fire the packets (this script)
# bash examples/manual_test.sh # blocks until server exits
#
# If you dont use a second tab, run the script in the background:
#
# bash examples/manual_test.sh & # writer in background
# cat /tmp/lsp_pipe | cargo run --example minimal_lsp
#
# The script opens /tmp/lsp_pipe for writing (exec 3>) and sends each JSON
# packet with a correct Content-Length header.
#
# One-liner alternative (single terminal, no FIFO):
#
# cargo run --example minimal_lsp <<'EOF'
# … nine packets …
# EOF
#
# Both approaches feed identical bytes to minimal_lsp via stdin.
set -eu
PIPE=${1:-/tmp/lsp_pipe}
mkfifo -m 600 "$PIPE" 2>/dev/null || true # create once, ignore if exists
# open write end so the fifo stays open
exec 3> "$PIPE"
send() {
local body=$1
local len=$(printf '%s' "$body" | wc -c)
printf 'Content-Length: %d\r\n\r\n%s' "$len" "$body" >&3
}
send '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"capabilities":{}}}'
send '{"jsonrpc":"2.0","method":"initialized","params":{}}'
send '{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"file:///tmp/foo.rs","languageId":"rust","version":1,"text":"fn main( ){println!(\"hi\") }"}}}'
send '{"jsonrpc":"2.0","id":2,"method":"textDocument/completion","params":{"textDocument":{"uri":"file:///tmp/foo.rs"},"position":{"line":0,"character":0}}}'
send '{"jsonrpc":"2.0","id":3,"method":"textDocument/hover","params":{"textDocument":{"uri":"file:///tmp/foo.rs"},"position":{"line":0,"character":0}}}'
send '{"jsonrpc":"2.0","id":4,"method":"textDocument/definition","params":{"textDocument":{"uri":"file:///tmp/foo.rs"},"position":{"line":0,"character":0}}}'
send '{"jsonrpc":"2.0","id":5,"method":"textDocument/formatting","params":{"textDocument":{"uri":"file:///tmp/foo.rs"},"options":{"tabSize":4,"insertSpaces":true}}}'
send '{"jsonrpc":"2.0","id":6,"method":"shutdown","params":null}'
send '{"jsonrpc":"2.0","method":"exit","params":null}'
exec 3>&-
echo "Packets sent watch the other terminal for responses."