These strings will be displayed in a menu for the user to select.
The error codes, in addition to not being human readable sentences,
describe the issue being reported, not the solution that will be
applied.
The code action response only includes the human readable string, so the
tests need to check for that. I considered adding a custom field but
decided against going out of spec.
---
changelog.md | 1 +
src/fennel-ls/handlers.fnl | 3 +--
src/fennel-ls/message.fnl | 19 +++++++++++++++++++
test/code-action.fnl | 5 +++--
4 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/changelog.md b/changelog.md
index f2279f5..7dd55cd 100644
--- a/changelog.md
+++ b/changelog.md
@@ -4,6 +4,7 @@
### Features
+* Provide human readable code actions titles.
* Add --help and --version command line flags.
* Support providing improved completion kinds to clients.
* Support highlighting references to the symbol under the cursor in the current file.
diff --git a/src/fennel-ls/handlers.fnl b/src/fennel-ls/handlers.fnl
index b9f7ca9..3ea6d10 100644
--- a/src/fennel-ls/handlers.fnl
+++ b/src/fennel-ls/handlers.fnl
@@ -294,8 +294,7 @@ Every time the client sends a message, it gets handled by a function in the corr
(icollect [_ diagnostic (ipairs file.diagnostics)]
(if (and (overlap? diagnostic.range range)
diagnostic.quickfix)
- {:title diagnostic.codeDescription
- :edit {:changes {uri (diagnostic.quickfix)}}}))))
+ (message.diagnostic->code-action server file diagnostic :quickfix)))))
(λ notifications.textDocument/didChange [server send {: contentChanges :textDocument {: uri}}]
(local file (files.get-by-uri server uri))
diff --git a/src/fennel-ls/message.fnl b/src/fennel-ls/message.fnl
index 72209e7..9b3205a 100644
--- a/src/fennel-ls/message.fnl
+++ b/src/fennel-ls/message.fnl
@@ -73,6 +73,24 @@ LSP json objects."
:end (utils.byte->position file.text (+ byteend 1)
server.position-encoding)}))
+(λ code-action-title [diag]
+ (let [titles {:match-should-case "Replace match with case"
+ :unused-definition "Prefix with _ to silence warning"
+ :op-with-no-arguments "Replace with the corresponding literal"
+ :no-decreasing-comparison "Reverse comparison"
+ :unnecessary-tset "Replace with set"
+ :unnecessary-do-values "Remove unnecessary do or values"
+ :redundant-do "Remove redundant do"
+ :bad-unpack "Replace with table.concat call"}]
+ (or (. titles diag.codeDescription)
+ (.. "Action title missing - " diag.codeDescription))))
+
+(λ diagnostic->code-action [_server file diagnostic ?kind]
+ (let [{: uri} file]
+ {:title (code-action-title diagnostic)
+ :kind ?kind
+ :edit {:changes {uri (diagnostic.quickfix)}}}))
+
(λ multisym->range [server file ast n]
(let [spl (utils.multi-sym-split ast)
n (if (< n 0) (+ n 1 (length spl)) n)]
@@ -110,6 +128,7 @@ LSP json objects."
: create-response
: create-error
: ast->range
+ : diagnostic->code-action
: multisym->range
: range-and-uri
: diagnostics
diff --git a/test/code-action.fnl b/test/code-action.fnl
index eb13bd0..c376f9b 100644
--- a/test/code-action.fnl
+++ b/test/code-action.fnl
@@ -20,6 +20,7 @@
(let [edits (?. action :edit :changes uri)
edited-text (apply-edits text edits encoding)]
(faith.= desired-file-contents edited-text))))
+
(fn check-negative [file-contents action-not-suggested]
(let [{: client : uri :locations [range]} (create-client file-contents)
[{:result responses}] (client:code-action uri range.range)]
@@ -32,13 +33,13 @@
(fn test-fix-op-no-arguments []
(check "(let [x (+====)]
(print x))"
- "op-with-no-arguments"
+ "Replace with the corresponding literal"
"(let [x 0]
(print x))"))
(fn test-fix-unused-definition []
(check "(local x==== 10)"
- "unused-definition"
+ "Prefix with _ to silence warning"
"(local _x 10)"))
; (fn test-fix-method-function []
--
2.39.5 (Apple Git-154)
Michele Campeotto <micampe@micampe.it> writes:
> These strings will be displayed in a menu for the user to select.
>
> The error codes, in addition to not being human readable sentences,
> describe the issue being reported, not the solution that will be
> applied.
>
> The code action response only includes the human readable string, so the
> tests need to check for that. I considered adding a custom field but
> decided against going out of spec.
Thanks; looks great!
-Phil