Ensure --lint can print validation messages from initialization.

This commit is contained in:
Phil Hagelberg 2025-02-26 23:56:44 -08:00
parent d23d36a414
commit 029aecd7cb

View File

@ -1,6 +1,7 @@
(require :fennel) (require :fennel)
(local dispatch (require :fennel-ls.dispatch)) (local dispatch (require :fennel-ls.dispatch))
(local json-rpc (require :fennel-ls.json-rpc)) (local json-rpc (require :fennel-ls.json-rpc))
(local files (require :fennel-ls.files))
(fn print-diagnostic [filename message ?range] (fn print-diagnostic [filename message ?range]
(print (: "%s:%s:%s: %s" :format filename (print (: "%s:%s:%s: %s" :format filename
@ -9,28 +10,31 @@
(+ (or (?. ?range :start :line) 0) 1) (+ (or (?. ?range :start :line) 0) 1)
(or (?. ?range :start :character) "?") message))) (or (?. ?range :start :character) "?") message)))
(fn initialize [server]
(let [params {:id 1
:jsonrpc "2.0"
:method "initialize"
:params {:capabilities {:general {:positionEncodings [:utf-8]}}
:clientInfo {:name "fennel-ls"}
:rootUri "file://."}}]
(each [_ response (ipairs (dispatch.handle* server params))]
(case response
{:method "window/showMessage" :params {: message}}
(print "WARN:" message)))))
(λ lint [filenames] (λ lint [filenames]
"non-interactive mode that gets executed from CLI with --lint. "non-interactive mode that gets executed from CLI with --lint.
runs lints on each file, then formats and prints them" runs lints on each file, then formats and prints them"
(local files (require :fennel-ls.files)) (let [lint (require :fennel-ls.lint)
(local lint (require :fennel-ls.lint)) server (doto {} initialize)]
(let [server (doto {}
(dispatch.handle* {:id 1
:jsonrpc "2.0"
:method "initialize"
:params {:capabilities {:general {:positionEncodings [:utf-8]}}
:clientInfo {:name "fennel-ls"}
:rootUri "file://."}}))]
(var should-err? false) (var should-err? false)
(each [_ filename (ipairs filenames)] (each [_ filename (ipairs filenames)]
(let [file (files.get-by-uri server (.. "file://" filename))] (let [file (files.get-by-uri server (.. "file://" filename))]
(lint.add-lint-diagnostics server file) (lint.add-lint-diagnostics server file)
(each [_ {: message : range} (ipairs file.diagnostics)] (each [_ {: message : range} (ipairs file.diagnostics)]
(print-diagnostic filename message range)) (set should-err? true)
(if (. file.diagnostics 1) (print-diagnostic filename message range))))
(set should-err? true)))) (os.exit (not should-err?))))
(if should-err?
(os.exit 1))))
(λ main-loop [in out] (λ main-loop [in out]
(local send (partial json-rpc.write out)) (local send (partial json-rpc.write out))