print severity levels in the output when linting with --lint

This commit is contained in:
Andrey Listopadov 2025-03-04 22:43:36 +03:00 committed by Phil Hagelberg
parent e48aa8af89
commit db37838272
3 changed files with 19 additions and 6 deletions

View File

@ -2,13 +2,17 @@
(local dispatch (require :fennel-ls.dispatch))
(local json-rpc (require :fennel-ls.json-rpc))
(local files (require :fennel-ls.files))
(local {: severity->string} (require :fennel-ls.message))
(fn print-diagnostic [filename message ?range]
(print (: "%s:%s:%s: %s" :format filename
(fn print-diagnostic [filename message ?range ?severity]
(print (: "%s:%s:%s: %s: %s" :format
filename
;; LSP line numbers are zero-indexed, but Emacs and Vim both use
;; 1-indexing for this.
(+ (or (?. ?range :start :line) 0) 1)
(or (?. ?range :start :character) "?") message)))
(or (?. ?range :start :character) "?")
(or (. severity->string ?severity) "?")
message)))
(fn initialize [server]
(let [params {:id 1
@ -31,9 +35,9 @@
(each [_ filename (ipairs filenames)]
(let [file (files.get-by-uri server (.. "file://" filename))]
(lint.add-lint-diagnostics server file)
(each [_ {: message : range} (ipairs file.diagnostics)]
(each [_ {: message : range : severity} (ipairs file.diagnostics)]
(set should-err? true)
(print-diagnostic filename message range))))
(print-diagnostic filename message range severity))))
(when should-err?
(os.exit 1))))

View File

@ -35,6 +35,14 @@ LSP json objects."
:INFO 3
:HINT 4})
(local severity->string
;; Transforming the `severity` table into
;; `{severity-code printable-string ...}`
(collect [k v (pairs severity)]
v (case k
:WARN :warning
_ (string.lower k))))
(λ create-error [code message ?id ?data]
{:jsonrpc "2.0"
:id ?id
@ -106,4 +114,5 @@ LSP json objects."
: range-and-uri
: diagnostics
: severity
: severity->string
: show-message}

View File

@ -10,7 +10,7 @@
(let [output-file (io.popen (.. "./fennel-ls --lint "
input-file-name)
:r)]
(faith.= (.. input-file-name ":1:7: unused definition: x\n")
(faith.= (.. input-file-name ":1:7: warning: unused definition: x\n")
(output-file:read :*a))
(os.remove input-file-name))
nil))