clean up formatter code

? only on optional fields, instead of randomly.
also, don't use the variable name `symbol` for things that aren't
fennel.sym?

remove message.array entirely: dkjson defaults to [] not {} on empty
tables.
This commit is contained in:
XeroOl 2025-06-25 01:12:00 -05:00
parent 01b03de17f
commit dca991eb2f
3 changed files with 19 additions and 26 deletions

View File

@ -6,7 +6,6 @@ user code. Fennel-ls doesn't support user-code formatting as of now."
(local {: sym? (local {: sym?
: view : view
: table?} (require :fennel)) : table?} (require :fennel))
(local message (require :fennel-ls.message))
(λ code-block [str] (λ code-block [str]
(.. "```fnl\n" str "\n```")) (.. "```fnl\n" str "\n```"))
@ -38,7 +37,7 @@ user code. Fennel-ls doesn't support user-code formatting as of now."
(.. ": " $2 $3))) (.. ": " $2 $3)))
(tostring arg))) (tostring arg)))
(fn fn-signature-format [special name args] (fn fn-signature-format [special ?name args]
"Returns the LSP-formatted signature and parameters objects" "Returns the LSP-formatted signature and parameters objects"
(fn render-arglist [arglist offset] (fn render-arglist [arglist offset]
(var offset offset) (var offset offset)
@ -47,7 +46,7 @@ user code. Fennel-ls doesn't support user-code formatting as of now."
(set offset (+ 1 (. rendered :label 2))) (set offset (+ 1 (. rendered :label 2)))
rendered))) rendered)))
(let [name (tostring (or name special)) (let [name (tostring (or ?name special))
args (case (type (?. args 1)) args (case (type (?. args 1))
:table (icollect [_ v (ipairs args)] :table (icollect [_ v (ipairs args)]
(render-arg v)) (render-arg v))
@ -60,9 +59,9 @@ user code. Fennel-ls doesn't support user-code formatting as of now."
")") ")")
(render-arglist args args-offset)))) (render-arglist args args-offset))))
(fn fn-format [special name args docstring] (fn fn-format [special ?name args ?docstring]
(.. (code-block (fn-signature-format special name args)) (.. (code-block (fn-signature-format special ?name args))
(if docstring (.. "\n---\n" docstring) ""))) (if ?docstring (.. "\n---\n" ?docstring) "")))
(fn metadata-format [{: binding : metadata}] (fn metadata-format [{: binding : metadata}]
"formats a special using its builtin metadata magic" "formats a special using its builtin metadata magic"
@ -123,11 +122,11 @@ fntype is one of fn or λ or lambda"
(= (type arglist) :table)) (= (type arglist) :table))
{: fntype : arglist})) {: fntype : arglist}))
(λ signature-help-format [symbol] (λ signature-help-format [doc-or-definition]
"Return a signatureHelp lsp object "Return a SignatureInformation lsp object
symbol can be an actual ast symbol or a binding object from a docset" symbol can be an actual ast symbol or a binding object from a docset"
(case-try (analyze-fn symbol.definition) (case-try (analyze-fn doc-or-definition.definition)
{:fntype ?fntype :name ?name :arglist ?arglist :docstring ?docstring} {:fntype ?fntype :name ?name :arglist ?arglist :docstring ?docstring}
(fn-signature-format ?fntype ?name ?arglist) (fn-signature-format ?fntype ?name ?arglist)
(signature parameters) (signature parameters)
@ -135,7 +134,7 @@ fntype is one of fn or λ or lambda"
:documentation ?docstring :documentation ?docstring
:parameters parameters} :parameters parameters}
;; if we couldn't get the info from the ast, try the metadata ;; if we couldn't get the info from the ast, try the metadata
(catch _ (case-try symbol (catch _ (case-try doc-or-definition
{: binding :metadata {:fnl/arglist arglist {: binding :metadata {:fnl/arglist arglist
:fnl/docstring docstring}} :fnl/docstring docstring}}
(fn-signature-format "" binding arglist) (fn-signature-format "" binding arglist)
@ -143,20 +142,20 @@ fntype is one of fn or λ or lambda"
{:label signature {:label signature
:documentation docstring :documentation docstring
:parameters parameters} :parameters parameters}
(catch _ {:parameters (message.array) (catch _ {:parameters []
:label (.. "ERROR: don't know how to format " :label (.. "ERROR: don't know how to format "
(view symbol {:one-line? true :depth 3})) (view doc-or-definition {:one-line? true :depth 3}))
:documentation (code-block :documentation (code-block
(view symbol {:depth 3}))}))))) (view doc-or-definition {:depth 3}))})))))
(λ hover-format [result] (λ hover-format [result]
"Format code that will appear when the user hovers over a symbol" "Format code that will appear when the user hovers over a symbol"
{:kind "markdown" {:kind "markdown"
:value :value
(case (analyze-fn result.definition) (case (analyze-fn result.definition)
{:fntype ?fntype :name ?name :arglist ?arglist :docstring ?docstring} {:fntype fntype :name ?name :arglist arglist :docstring ?docstring}
(fn-format ?fntype ?name ?arglist ?docstring) (fn-format fntype ?name arglist ?docstring)
_ (if (-?>> result.keys length (< 0)) _ (if (-?>> result.keys length (not= 0))
(code-block (code-block
(.. "ERROR, I don't know how to show this " (.. "ERROR, I don't know how to show this "
"(. " "(. "

View File

@ -108,7 +108,7 @@ Every time the client sends a message, it gets handled by a function in the corr
symbol symbol
(analyzer.find-nearest-definition server this-file symbol byte) (analyzer.find-nearest-definition server this-file symbol byte)
{: referenced-by :file {:uri this-file.uri &as file} : binding} {: referenced-by :file {:uri this-file.uri &as file} : binding}
(let [result (icollect [_ {:symbol reference} (ipairs referenced-by) &into (message.array)] (let [result (icollect [_ {:symbol reference} (ipairs referenced-by)]
{:range (message.ast->range server file reference) {:range (message.ast->range server file reference)
:kind documentHighlightKind.Read})] :kind documentHighlightKind.Read})]
(table.insert result {:range (message.ast->range server file binding) (table.insert result {:range (message.ast->range server file binding)
@ -207,7 +207,7 @@ Every time the client sends a message, it gets handled by a function in the corr
(λ requests.textDocument/codeAction [server _send {: range :textDocument {: uri}}] (λ requests.textDocument/codeAction [server _send {: range :textDocument {: uri}}]
(let [file (files.get-by-uri server uri)] (let [file (files.get-by-uri server uri)]
(icollect [_ diagnostic (ipairs file.diagnostics) &into (message.array)] (icollect [_ diagnostic (ipairs file.diagnostics)]
(if (overlap? diagnostic.range range) (if (overlap? diagnostic.range range)
(message.diagnostic->code-action server file diagnostic :quickfix))))) (message.diagnostic->code-action server file diagnostic :quickfix)))))

View File

@ -9,8 +9,6 @@ LSP json objects."
(local utils (require :fennel-ls.utils)) (local utils (require :fennel-ls.utils))
(local json (require :dkjson)) (local json (require :dkjson))
(local json-array-mt {:__jsontype :array})
(λ nullify [?value] (λ nullify [?value]
(case ?value (case ?value
nil json.null nil json.null
@ -75,16 +73,13 @@ LSP json objects."
:end (utils.byte->position file.text (+ byteend 1) :end (utils.byte->position file.text (+ byteend 1)
server.position-encoding)})) server.position-encoding)}))
(λ array [?t]
(setmetatable (or ?t []) json-array-mt))
(λ diagnostic->code-action [_server {: uri} diagnostic ?kind] (λ diagnostic->code-action [_server {: uri} diagnostic ?kind]
(case-try diagnostic.fix (case-try diagnostic.fix
fix (fix) fix (fix)
{: title : changes} {: title {: title : changes} {: title
:kind ?kind :kind ?kind
:diagnostics [diagnostic] :diagnostics [diagnostic]
:edit {:changes {uri (array changes)}}})) :edit {:changes {uri changes}}}))
(λ call->signature-help [_server _file _call signature active-parameter] (λ call->signature-help [_server _file _call signature active-parameter]
(let [params-count (length signature.parameters)] (let [params-count (length signature.parameters)]
@ -138,5 +133,4 @@ LSP json objects."
: diagnostics : diagnostics
: severity : severity
: severity->string : severity->string
: show-message : show-message}
: array}