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?
: view
: table?} (require :fennel))
(local message (require :fennel-ls.message))
(λ code-block [str]
(.. "```fnl\n" str "\n```"))
@ -38,7 +37,7 @@ user code. Fennel-ls doesn't support user-code formatting as of now."
(.. ": " $2 $3)))
(tostring arg)))
(fn fn-signature-format [special name args]
(fn fn-signature-format [special ?name args]
"Returns the LSP-formatted signature and parameters objects"
(fn render-arglist [arglist 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)))
rendered)))
(let [name (tostring (or name special))
(let [name (tostring (or ?name special))
args (case (type (?. args 1))
:table (icollect [_ v (ipairs args)]
(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))))
(fn fn-format [special name args docstring]
(.. (code-block (fn-signature-format special name args))
(if docstring (.. "\n---\n" docstring) "")))
(fn fn-format [special ?name args ?docstring]
(.. (code-block (fn-signature-format special ?name args))
(if ?docstring (.. "\n---\n" ?docstring) "")))
(fn metadata-format [{: binding : metadata}]
"formats a special using its builtin metadata magic"
@ -123,11 +122,11 @@ fntype is one of fn or λ or lambda"
(= (type arglist) :table))
{: fntype : arglist}))
(λ signature-help-format [symbol]
"Return a signatureHelp lsp object
(λ signature-help-format [doc-or-definition]
"Return a SignatureInformation lsp object
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}
(fn-signature-format ?fntype ?name ?arglist)
(signature parameters)
@ -135,7 +134,7 @@ fntype is one of fn or λ or lambda"
:documentation ?docstring
:parameters parameters}
;; 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
:fnl/docstring docstring}}
(fn-signature-format "" binding arglist)
@ -143,20 +142,20 @@ fntype is one of fn or λ or lambda"
{:label signature
:documentation docstring
:parameters parameters}
(catch _ {:parameters (message.array)
(catch _ {:parameters []
: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
(view symbol {:depth 3}))})))))
(view doc-or-definition {:depth 3}))})))))
(λ hover-format [result]
"Format code that will appear when the user hovers over a symbol"
{:kind "markdown"
:value
(case (analyze-fn result.definition)
{:fntype ?fntype :name ?name :arglist ?arglist :docstring ?docstring}
(fn-format ?fntype ?name ?arglist ?docstring)
_ (if (-?>> result.keys length (< 0))
{:fntype fntype :name ?name :arglist arglist :docstring ?docstring}
(fn-format fntype ?name arglist ?docstring)
_ (if (-?>> result.keys length (not= 0))
(code-block
(.. "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
(analyzer.find-nearest-definition server this-file symbol byte)
{: 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)
:kind documentHighlightKind.Read})]
(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}}]
(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)
(message.diagnostic->code-action server file diagnostic :quickfix)))))

View File

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