Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 65 additions & 180 deletions check-syntax.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -2,125 +2,15 @@
(require drracket/check-syntax
racket/class
racket/contract/base
racket/match
racket/set
racket/logging
racket/list
racket/string
syntax/modread
racket/sandbox
setup/path-to-relative
racket/contract/region
racket/port
"editor.rkt"
"responses.rkt"
"interfaces.rkt"
"doc-trace.rkt"
"msg-io.rkt"
"responses.rkt"
"scheduler.rkt"
"path-util.rkt")

(define ((error-diagnostics doc-text) exn)
(define msg (exn-message exn))
(cond
;; typed racket support: don't report error summaries
[(string-prefix? msg "Type Checker: Summary") (list)]
[(exn:fail:resource? exn)
(list (Diagnostic #:range (Range #:start (Pos #:line 0 #:char 0)
#:end (Pos #:line 0 #:char 0))
#:severity Diag-Hint
#:source "Expander"
#:message "the expand time has exceeded the 90s limit.\
Check if your macro is infinitely expanding"))]
[(and (exn:fail:read? exn)
;; Looking for the pattern '... version mismatch ... .zo ... raco setup ...'
(regexp-match? #rx"version mismatch.*\\.zo.*raco setup" msg))
(define maybe-error-source-mtchs/f
(regexp-match #rx"in: (.*\\.zo)" msg))
(define maybe-lib-error-source/f
(and maybe-error-source-mtchs/f
(path->relative-string/library (list-ref maybe-error-source-mtchs/f 1))))
(define expanded-msg
(cond
[maybe-lib-error-source/f
(format (string-append
"Failed to load file because it is compiled by a different version of Racket.\n"
" file: ~a\n"
" suggestion: ~a\n"
" complete message:\n\n ~a")
maybe-lib-error-source/f
(cond
[(regexp-match? #rx"<[a-z-]+>/" maybe-lib-error-source/f)
"run `raco setup` to recompile the libraries."]
[(regexp-match #rx"^(.*)compiled/([^/]+)_([^_]+)\\.zo$" maybe-lib-error-source/f)
=> (match-lambda
[(list whole-str base-path filename file-extension)
(format "run `raco make '~a~a.~a'` to recompile it."
base-path
filename
file-extension)])]
[else
(format "try running `raco setup` or `raco make <file>`.")])
msg)]
[else msg]))
;; stub range -- see the comments in exn:missing-module?
(list (Diagnostic #:range (Range #:start (Pos #:line 0 #:char 0)
#:end (Pos #:line 0 #:char 0))
#:severity Diag-Error
#:source "Racket"
#:message expanded-msg))]
[(exn:srclocs? exn)
(define srclocs ((exn:srclocs-accessor exn) exn))
(for/list ([sl (in-list srclocs)])
(match-define (srcloc src line col pos span) sl)
(if (and (number? line) (number? col) (number? span))
(Diagnostic #:range (Range #:start (Pos #:line (sub1 line) #:char col)
#:end (Pos #:line (sub1 line) #:char (+ col span)))
#:severity Diag-Error
#:source "Racket"
#:message msg)
;; Some reader exceptions don't report a position
;; Use end of file as a reasonable guess
(let ([end-of-file (abs-pos->Pos doc-text (send doc-text end-pos))])
(Diagnostic #:range (Range #:start end-of-file
#:end end-of-file)
#:severity Diag-Error
#:source "Racket"
#:message msg))))]
[(exn:missing-module? exn)
;; Hack:
;; We do not have any source location for the offending `require`, but the language
;; server protocol requires a valid range object. So we punt and just highlight the
;; first character.
;; This is very close to DrRacket's behavior: it also has no source location to work with,
;; however it simply doesn't highlight any code.
(define silly-range
(Range #:start (Pos #:line 0 #:char 0) #:end (Pos #:line 0 #:char 0)))
(list (Diagnostic #:range silly-range
#:severity Diag-Error
#:source "Racket"
#:message msg))]
[else (error 'error-diagnostics "unexpected failure: ~a" exn)]))

(define (check-typed-racket-log doc-text log)
(match-define (vector _ msg data _) log)
(when (and (list? data) (not (empty? data)) (syntax? (car data)))
(define prop (syntax-property (car data) 'mouse-over-tooltips))
(when (and prop (list? prop) (not (empty? prop)))
(define-values (start end msg)
(match prop
[(list (vector _ start _ _) (vector _ _ end msg))
(values start end msg)]
[(list (vector _ start end msg))
(values start end msg)]
[else (values #f #f #f)]))
(when (string? msg)
(list (Diagnostic #:range (Range #:start (abs-pos->Pos doc-text start) #:end (abs-pos->Pos doc-text end))
#:severity Diag-Error
#:source "Typed Racket"
#:message msg))))))

(define (get-indenter doc-text)
(define text (send doc-text get-text))
(define (get-indenter text)
(define lang-info
(with-handlers ([exn:fail:read? (lambda (e) 'missing)]
[exn:missing-module? (lambda (e) #f)])
Expand All @@ -132,85 +22,80 @@
; check for a #reader directive at start of file, ignoring comments
; the ^ anchor here matches start-of-string, not start-of-line
(if (regexp-match #rx"^(;[^\n]*\n)*#reader" text)
#f ; most likely a drracket file, use default indentation
; (https://github.com/jeapostrophe/racket-langserver/issues/86)
'missing)]
#f ; most likely a drracket file, use default indentation
; (https://github.com/jeapostrophe/racket-langserver/issues/86)
'missing)]
[else #f]))

(define-syntax-rule (timeout time-sec body)
(with-limits time-sec #f body))
; Struct to hold the result of an expansion.
; pre-stx: the syntax before expansion, result of `read-syntax`
; post-stx: the syntax after expansion, result of `expand`
; logs: the log collected during expansion
(struct/contract ExpandResult
([pre-stx (or/c syntax? exn? eof-object?)]
[post-stx (or/c syntax? exn? #f)]
[logs (listof (vector/c log-level/c string? any/c (or/c symbol? #f)))])
#:transparent)

(define (send-diagnostics uri diag-lst)
(display-message/flush (diagnostics-message uri diag-lst)))

(define (check-syntax uri doc-text)
(define src (uri->path uri))
(define indenter (get-indenter doc-text))
(define ns (make-base-namespace))
(define new-trace (new build-trace% [src src] [doc-text doc-text] [indenter indenter]))
(match-define-values (src-dir _ #f) (split-path src))
(define (expand-source path in ns collector)
(define-values (src-dir _1 _2) (split-path path))
(define-values (add-syntax done)
(make-traversal ns src-dir))

;; Rewind input port and read syntax
(define text (send doc-text get-text))
(define in (open-input-string text))
(port-count-lines! in)
(define valid #f)
(define lang-diag
(if (eq? indenter 'missing)
(list (Diagnostic #:range (Range #:start (Pos #:line 0 #:char 0) #:end (Pos #:line 0 #:char 0))
#:severity Diag-Error
#:source "Racket"
#:message "Missing or invalid #lang line"))
(list)))
(define diags (list))
(define err-diags
(parameterize ([current-annotations new-trace]
[current-namespace ns]
[current-load-relative-directory src-dir])

(parameterize ([current-load-relative-directory src-dir]
[current-namespace ns]
[current-annotations collector])
(define stx
(with-handlers ([(λ _ #t) (λ (exn) exn)])
(with-module-reading-parameterization
(λ () (read-syntax path in)))))

(define expand-logs '())
(define expanded-stx
(with-intercepted-logging
(lambda (l)
(define result (check-typed-racket-log doc-text l))
(when (list? result) (set! diags (append result diags))))
(lambda ()
(with-handlers ([(or/c exn:fail:read?
exn:fail:syntax?
exn:fail:filesystem?
exn:fail:resource?)
(error-diagnostics doc-text)])
(define original-stx (with-module-reading-parameterization
(λ () (read-syntax src in))))
;; 90 seconds limit for possible infinity recursive macro expand
(define stx
;; parameterize current output port to fix side effects of `expand`.
(parameterize ([current-output-port (open-output-string)])
(timeout 90 (expand original-stx))))
(add-syntax stx)
(set! valid #t)
(done)
(send new-trace walk-stx original-stx stx)
(λ (log) (set! expand-logs (cons log expand-logs)))
(λ ()
(with-handlers ([(λ _ #t) (λ (exn) exn)])
(parameterize ([current-output-port (open-output-nowhere)])
(if (syntax? stx)
(expand stx)
#f))))
'info))

(when (syntax? expanded-stx)
(add-syntax expanded-stx)
(done))

(ExpandResult stx expanded-stx expand-logs)))

(list)))
'info)))
;; Struct to hold the result of check-syntax.
(struct/contract CSResult
([trace (is-a?/c build-trace%)]
[text string?]
[succeed? boolean?])
#:transparent)

(define warn-diags (set->list (send new-trace get-warn-diags)))
(define other-diags (append err-diags lang-diag diags))
(send-diagnostics uri (append warn-diags other-diags))
(define (check-syntax uri doc-text ns)
(define path (uri->path uri))
(define text (send doc-text get-text))
(define indenter (get-indenter text))
(define new-trace (new build-trace% [src path] [doc-text doc-text] [indenter indenter]))

(define in (open-input-string text))
(define er (expand-source path in ns new-trace))

(define (task)
(send new-trace walk-text text)
(define new-warn-diags (set->list (send new-trace get-warn-diags)))
;; send a diagnostics to force client send a new code action request
(when (not (equal? new-warn-diags warn-diags))
(send-diagnostics uri (append new-warn-diags other-diags))))
(when valid
(scheduler-push-task! uri 'walk-text task))
(define pre-stx (ExpandResult-pre-stx er))
(define post-stx (ExpandResult-post-stx er))

(if valid new-trace #f))
(send new-trace walk-stx pre-stx post-stx)
(send new-trace walk-log (ExpandResult-logs er))
(CSResult new-trace text (and (syntax? pre-stx) (syntax? post-stx))))

(provide
(struct-out ExpandResult)
(struct-out CSResult)
(contract-out
[check-syntax (-> any/c (is-a?/c lsp-editor%)
(or/c #f (is-a?/c build-trace%)))]))
[expand-source (-> path? input-port? namespace? (is-a?/c syncheck-annotations<%>) ExpandResult?)]
[check-syntax (-> any/c (is-a?/c lsp-editor%) namespace? CSResult?)]))

6 changes: 5 additions & 1 deletion doc-trace.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
(define completions (new completion%))
(define requires (new require%))
(define definitions (new definition% [src src]))
(define diag (new diag% [src src] [doc-text doc-text]))
(define diag (new diag% [src src] [doc-text doc-text] [indenter indenter]))
(define decls (new declaration%))
(define semantic-tokens (new highlight% [src src] [doc-text doc-text]))

Expand Down Expand Up @@ -53,6 +53,10 @@
(for ([s services])
(send s walk-text text)))

(define/public (walk-log text)
(for ([s services])
(send s walk-log text)))

;; Getters
(define/public (get-indenter) indenter)
(define/public (get-warn-diags) (car (send diag get)))
Expand Down
Loading