serve the index over LSP
sex-lsp answers: - hover, - definition, - completion, - symbols, - diagnostics. --expand-lines and sex/expand return the C for a line range. clangd is off unless --c-backend=clangd, and it is asked about names the compiler never parsed.
This commit is contained in:
132
tests/lsp.scm
Normal file
132
tests/lsp.scm
Normal file
@@ -0,0 +1,132 @@
|
||||
;;; The language server speaks LSP. ./sex-lsp is forked onto pipes,
|
||||
;;; because a `process' port has no descriptor to wait on.
|
||||
|
||||
(import scheme
|
||||
(scheme base)
|
||||
(chicken file posix)
|
||||
(chicken port)
|
||||
(chicken process)
|
||||
srfi-13
|
||||
test)
|
||||
|
||||
(define (string->bytes text)
|
||||
(let* ((n (string-length text))
|
||||
(out (make-bytevector n)))
|
||||
(do ((i 0 (+ i 1)))
|
||||
((= i n) out)
|
||||
(bytevector-u8-set! out i (char->integer (string-ref text i))))))
|
||||
|
||||
(define (copied-bytes buf count)
|
||||
(let ((out (make-string count)))
|
||||
(do ((i 0 (+ i 1)))
|
||||
((= i count) out)
|
||||
(string-set! out i (integer->char (bytevector-u8-ref buf i))))))
|
||||
|
||||
(define (fd-write-all fd text)
|
||||
(unless (string=? text "")
|
||||
(let ((n (file-write fd (string->bytes text))))
|
||||
(when (< n (string-length text))
|
||||
(fd-write-all fd (substring text n))))))
|
||||
|
||||
(define (fd-chunk fd n)
|
||||
(let ((got (file-read fd n)))
|
||||
(let ((buf (car got))
|
||||
(count (cadr got)))
|
||||
(and (positive? count) (copied-bytes buf count)))))
|
||||
|
||||
(define (fd-ready? fd)
|
||||
(call-with-values
|
||||
(lambda () (file-select (list fd) '() 4))
|
||||
(lambda (readable writable) (pair? readable))))
|
||||
|
||||
(define (read-some fd n)
|
||||
(let loop ((acc "") (left n))
|
||||
(if (zero? left)
|
||||
acc
|
||||
(let ((chunk (fd-chunk fd left)))
|
||||
(and chunk (loop (string-append acc chunk)
|
||||
(- left (string-length chunk))))))))
|
||||
|
||||
(define (lsp-read fd)
|
||||
(and (fd-ready? fd)
|
||||
(let loop ((line "") (len #f))
|
||||
(let ((b (fd-chunk fd 1)))
|
||||
(and b
|
||||
(if (char=? (string-ref b 0) #\newline)
|
||||
(let ((line (if (and (positive? (string-length line))
|
||||
(char=? (string-ref line
|
||||
(- (string-length line) 1))
|
||||
#\return))
|
||||
(substring line 0 (- (string-length line) 1))
|
||||
line)))
|
||||
(cond
|
||||
((string=? line "") (and len (read-some fd len)))
|
||||
((and (>= (string-length line) 15)
|
||||
(string-ci=? (substring line 0 15) "content-length:"))
|
||||
(loop "" (string->number (string-trim (substring line 15)))))
|
||||
(else (loop "" len))))
|
||||
(loop (string-append line b) len)))))))
|
||||
|
||||
(define (lsp-send fd method id params-json)
|
||||
(let ((body (string-append "{\"jsonrpc\":\"2.0\""
|
||||
(if id (string-append ",\"id\":" (number->string id)) "")
|
||||
",\"method\":\"" method "\",\"params\":" params-json "}")))
|
||||
(fd-write-all fd (string-append "Content-Length: "
|
||||
(number->string (string-length body))
|
||||
"\r\n\r\n" body))))
|
||||
|
||||
(define (spawn-sex-lsp)
|
||||
(call-with-values
|
||||
create-pipe
|
||||
(lambda (from-r from-w)
|
||||
(call-with-values
|
||||
create-pipe
|
||||
(lambda (to-r to-w)
|
||||
(let ((proc (process-fork)))
|
||||
(if (not proc)
|
||||
(begin
|
||||
(duplicate-fileno to-r 0)
|
||||
(duplicate-fileno from-w 1)
|
||||
(process-execute "./sex-lsp" '())
|
||||
(exit 127))
|
||||
(begin
|
||||
(file-close to-r)
|
||||
(file-close from-w)
|
||||
(values proc to-w from-r)))))))))
|
||||
|
||||
(when (file-exists? "./sex-lsp")
|
||||
(test-group "sex-lsp"
|
||||
(call-with-values
|
||||
spawn-sex-lsp
|
||||
(lambda (proc to-lsp from-lsp)
|
||||
(lsp-send to-lsp "initialize" 1 "{\"capabilities\":{}}")
|
||||
(let ((init (lsp-read from-lsp)))
|
||||
(test-assert "initialize offers hover"
|
||||
(and init (string-contains init "hoverProvider"))))
|
||||
(lsp-send to-lsp "initialized" #f "{}")
|
||||
(lsp-send to-lsp "textDocument/didOpen" #f
|
||||
(string-append
|
||||
"{\"textDocument\":{\"uri\":\"file:///tmp/lsp-bar.sex\",\"languageId\":\"sex\",\"version\":1,\"text\":"
|
||||
(with-output-to-string
|
||||
(lambda ()
|
||||
(write "(pub fn bar ((a int)) void\n \"Add once.\"\n (return))\n(nope)\n")))
|
||||
"}}"))
|
||||
(let ((note (lsp-read from-lsp)))
|
||||
(test-assert "the bad form is diagnosed"
|
||||
(and note (string-contains note "unknown top level form"))))
|
||||
(lsp-send to-lsp "textDocument/hover" 2
|
||||
"{\"textDocument\":{\"uri\":\"file:///tmp/lsp-bar.sex\"},\"position\":{\"line\":0,\"character\":10}}")
|
||||
(let ((hover (lsp-read from-lsp)))
|
||||
(test-assert "hover is the prototype and the docstring"
|
||||
(and hover
|
||||
(string-contains hover "fn bar")
|
||||
(string-contains hover "Add once."))))
|
||||
(lsp-send to-lsp "sex/expand" 3
|
||||
"{\"textDocument\":{\"uri\":\"file:///tmp/lsp-bar.sex\"},\"startLine\":1,\"endLine\":3}")
|
||||
(let ((expanded (lsp-read from-lsp)))
|
||||
(test-assert "sex/expand returns the function's C"
|
||||
(and expanded (string-contains expanded "bar"))))
|
||||
(lsp-send to-lsp "shutdown" 4 "null")
|
||||
(lsp-read from-lsp)
|
||||
(lsp-send to-lsp "exit" #f "null")
|
||||
(process-wait proc)))))
|
||||
@@ -13,6 +13,7 @@
|
||||
(include-relative "types.scm")
|
||||
(include-relative "infer.scm")
|
||||
(include-relative "query.scm")
|
||||
(include-relative "lsp.scm")
|
||||
|
||||
;;; Should be the last in the test suite
|
||||
(test-exit)
|
||||
|
||||
Reference in New Issue
Block a user