Skip to content

Add the ability to run elixir tests from test lenses #2407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.org
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Automatically download [[https://github.com/eclipse/lemminx][XML language server Lemminx]]
* Add Vala support.
* Add [[https://github.com/sorbet/sorbet][Sorbet Language Server]] for typechecking Ruby code.
* Add Elixir test lenses support.
** Release 7.0.1
* Introduced ~lsp-diagnostics-mode~.
* Safe renamed ~lsp-flycheck-default-level~ -> ~lsp-diagnostics-flycheck-default-level~
Expand Down
37 changes: 36 additions & 1 deletion clients/lsp-elixir.el
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,39 @@ finding the executable with `exec-path'."
:group 'lsp-elixir
:type 'file)

(defcustom lsp-elixir-enable-test-lenses t
"Suggest Tests."
:type 'boolean
:group 'lsp-elixir
:package-version '(lsp-mode . "7.1"))

(defun lsp-elixir--build-test-command (argument)
"Builds the test command from the ARGUMENT."
(let ((test-name (gethash "testName" argument))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using gethash is not allowed. All the access to the data structures that come from the language server should happen either by defined lsp-interface or by using lsp-get. This will allow us to migrate to using plists instead of hashtables. You may refer to rust-analyzer for example: https://github.com/yyoncho/lsp-mode/blob/headerline-errors/lsp-protocol.el#L278

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, will have a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yyoncho I changed to lsp-get, but will figure out how lsp-interface works tomorrow if it might be a better fit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lsp-get is good enough for non-core components given the fact that this structure will be accessed only in a few places.

As a side note, most likely vscode supports running the tests in the debugger, right? We can implement that as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean dap-mode? I have not really worked with it before, but can have a look. Ideally i would not want to click to run tests, but this is the baseline work for future capabilities it seems.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean dap-mode?

yes.

Ideally i would not want to click to run tests, but this is the baseline work for future capabilities it seems.

There is lsp-avy-lens. And then you will be able to re-run with M-x recompile.

(module (gethash "module" argument))
(describe (gethash "describe" argument)))
(cond (module (concat "\"" "module:" module "\""))
((not test-name) (concat "\"" "describe:" describe "\""))
(describe (concat "\"" "test:test " describe " " test-name "\"" ))
(t (concat "\"" "test:test " test-name "\"" )))))

(lsp-defun lsp-elixir--run-test ((&Command :arguments?))
"Runs tests."
(let* ((argument (lsp-seq-first arguments?))
(file-path (gethash "filePath" argument))
(test-command (lsp-elixir--build-test-command argument)))
(compile
(concat
"cd "
(lsp-workspace-root file-path)
" && "
"mix test --exclude test --include "
test-command
" "
file-path
" --no-color"))
file-path))

(lsp-register-custom-settings
'(("elixirLS.dialyzerEnabled" lsp-elixir-dialyzer-enabled t)
("elixirLS.dialyzerWarnOpts" lsp-elixir-dialyzer-warn-opts)
Expand All @@ -109,13 +142,15 @@ finding the executable with `exec-path'."
("elixirLS.projectDir" lsp-elixir-project-dir)
("elixirLS.fetchDeps" lsp-elixir-fetch-deps t)
("elixirLS.suggestSpecs" lsp-elixir-suggest-specs t)
("elixirLS.signatureAfterComplete" lsp-elixir-signature-after-complete t)))
("elixirLS.signatureAfterComplete" lsp-elixir-signature-after-complete t)
("elixirLS.enableTestLenses" lsp-elixir-enable-test-lenses t)))

(lsp-register-client
(make-lsp-client :new-connection (lsp-stdio-connection (lambda () `(,lsp-clients-elixir-server-executable)))
:major-modes '(elixir-mode)
:priority -1
:server-id 'elixir-ls
:action-handlers (ht ("elixir.lens.test.run" 'lsp-elixir--run-test))
:initialized-fn (lambda (workspace)
(with-lsp-workspace workspace
(lsp--set-configuration
Expand Down