Emacs - Elixir Setup Configuration Wiki

Just installed spacemacs from develop branch and when I opened a project alchemist was in the mode line, so it seems like it’s still the default.

Also the elixir layer readme:

Hey guys!

Two days ago I removed obsolete flycheck-mix-compile from spacemacs Elixir layer. Now it uses lsp (or alchemist) to display errors. If you have the time, please update Spacemacs to most recent develop and check it out :slight_smile:

5 Likes

This is intentional, as lsp requires additional setup step of installing elixir-ls

1 Like

I mentioned because the parent includes:

For posterity. This is answered here: Emacs - Elixir Setup Configuration Wiki

Is this still up tp date? I’ve spent day’s trying to get this to work per these instructions, I have updated layers and additional packaged and installed the release from the zip:

dotspacemacs-configuration-layers
'((elixir :variables slixir-backend 'lsp)
auto-completion
lsp
elixir)

dotspacemacs-additional-packaged
'(use-package lsp-mode
:commands lsp
:ensure t
:diminish lsp-mode
:hook
(elixir-mode . lsp)
(add-to-list 'exec-path “~/elixir-ls/release/language_server.sh”)
)

It is up to date.

Looks like you have a spelling error in your layer variables

1 Like

Hi @Mpanarin,
I’m not sure if you solved your problem. I have one repo that is shared between electronjs and elixir/phoenix projects. The mix.exs is in subfolder of that repo, so elixir-ls has problem locating it. I was trying to apply suggestions from the wiki but it didn’t work for me. So I looked at lsp repo and elixir support there. I found that there are custom variables defined that you can tune. One specific to project location is called lsp-elixir-project-dir. So you have an option just to set it whenever you work with the project or you can automate it.
Looking at lsp repo I found that the function it is using to change elixir-ls options is lsp-register-custom-settings, so I made the following changes:

In init.el (or .emacs) I put the following:

(add-hook 'lsp-after-initialize-hook
          (lambda ()
            (lsp-register-custom-settings '(("elixirLS.projectDir" lsp-elixir-project-dir)))))

I also created a file called .dir-locals.el in the root folder of my repo with the following content:

((nil . ((lsp-elixir-project-dir . "/<path name of my repo with mix.exs in>"))))

Having these two pieces worked for me. The file local custom variable lsp-elixir-project-dir is set (or default) depending on the repo I’m working with.

If you would like to change some other options It would probably require to add more options to hook function above. Here is a complete list of valiables that lsp-mode sets for elixir when the elixir mode is initialized:

(lsp-register-custom-settings
 '(("elixirLS.dialyzerEnabled" lsp-elixir-dialyzer-enabled t)
   ("elixirLS.dialyzerWarnOpts" lsp-elixir-dialyzer-warn-opts)
   ("elixirLS.dialyzerFormat" lsp-elixir-dialyzer-format)
   ("elixirLS.mixEnv" lsp-elixir-mix-env)
   ("elixirLS.mixTarget" lsp-elixir-mix-target)
   ("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.enableTestLenses" lsp-elixir-enable-test-lenses t)))

You can just copy/paste lines you are interested in tuning then use the custom variables to make changes.

I hope it helps,

Pawel

2 Likes

I just updated this wiki to update some outdated information and added a section on configuring Doom Emacs

2 Likes

Can’t wait to share with folks : GitHub - Matsa59/web-mode: web template editing mode for emacs

Basically I fix <%= f = form ... %> pattern (only support f = ... for now).
Also fix multilines for <%= ... %>.

I would like to have some feedbacks to improve web-mode mode for (l)eex.

For people using doom-emacs, in packages.el use

(package! web-mode
  :recipe (:host github :repo "Matsa59/web-mode"))

Disable web from init.el and install using doom sync -u

All your configuration should continue to works, I just change elixir language.

Too many times waiting for someone to fix this x)

Spacemacs users : don’t know the configuration but it should works too if you use web-mode.

Edit : for people who wants to wait for official release, you can track this PR : Improve elixir language by Matsa59 · Pull Request #1191 · fxbois/web-mode · GitHub

1 Like

I has just update Emacs and all relevant packages for Elixir. So that I share some short steps to whom it may help.

  1. Install packages by config init.el
 (require 'package)
 (dolist (source '(("org" . "https://orgmode.org/elpa/")
                       ("melpa-stable" . "https://stable.melpa.org/packages/")
                       ("melpa" . "https://melpa.org/packages/")
                       ))
       (add-to-list 'package-archives source t))
     ;; Prefer stable packages.
     (setq package-archive-priorities '(("melpa-stable" . 1)
					("melpa" . 2)))
(package-initialize)
(when (not package-archive-contents)
  (package-refresh-contents))

;;Package list
(defvar anyname/packages
  '(helm helm-ag rg paredit use-package erlang elixir-mode flycheck lsp-mode lsp-ui company)

;;Run the defined packages if not install will be install
(dolist (p anyname/packages)
  (unless (package-installed-p p)
    (package-install p)))


  1. Install elixir-ls and config as this guide
    GitHub - elixir-lsp/elixir-ls: A frontend-independent IDE "smartness" server for Elixir. Implements the "Language Server Protocol" standard and provides debugger support via the "Debug Adapter Protocol"
Download the latest release: https://github.com/elixir-lsp/elixir-ls/releases/latest and unzip it into a directory (this is the directory referred to as the "path-to-elixir-ls/release" below)

If using lsp-mode add this configuration:

  (use-package lsp-mode
    :commands lsp
    :ensure t
    :diminish lsp-mode
    :hook
    (elixir-mode . lsp)
    :init
    (add-to-list 'exec-path "path-to-elixir-ls/release"))
1 Like

I’ve successfully set up elixir-ls within Emacs and lsp-mode, but I’m missing one feature and I’m not sure if this is

  • because of my emacs / company / lsp configuration
  • elixir-ls simply does not support it

I’d like to see function auto completes after typing Module., i.e. Regex..

(use-package lsp-mode
  :commands lsp
  :ensure t
  :diminish lsp-mode
  :hook
  (elixir-mode . lsp)
  :init
  (setq lsp-file-watch-threshold 10000
        lsp-enable-xref t
        lsp-prefer-flymake nil)
  (add-to-list 'exec-path "/home/ntraum/bin/elixir-ls/4c512a2b4e8fc5db1d4700b4b3922f0dc763506f_1.12.1"))

(use-package company
  :ensure t
  :diminish company-mode
  :init (add-hook 'after-init-hook 'global-company-mode)
  (setq company-idle-delay 0)
  (setq company-minimum-prefix-length 0)
  (setq company-selection-wrap-around t)
  (company-tng-mode)
  (push '(company-web-html
          company-css
          company-dabbrev-code
          company-dabbrev
          company-yasnippet
          company-files) company-backends))

What am I missing here? The company popup does open, but it does not show me all the function defined under this module.

PS: I’m pretty sure the language server itself works as the lsp-log buffer reports no errors and when hovering over a function like Regex.replace I do see the documentation and typespec in a popup.

The authors of this Wiki assume people want to use even more files (.dir-locals.el in this case) which I find to be a very strange assumption. Emacs and Spacemacs are complex enough already. We could use much more self-sufficient (single-file / single-function) examples and they’ll be far more useful.

Complete working example on disabling elixir-ls running Dialyzer, with context:

(defun dotspacemacs/user-config ()
  ;; Use LSP.
  (use-package lsp-mode
    :commands lsp
    :ensure t
    :diminish lsp-mode
    :hook
    (elixir-mode . lsp)
    :init
    (add-to-list 'exec-path "~/bin/elixir-ls/release")
    )

  ;; Set Elixir LSP hooks (in this case, format on save).
  (add-hook 'elixir-mode-hook
            (lambda ()
              (add-hook 'before-save-hook #'lsp-format-buffer nil t)))

  ;; Disable Dialyzer in `elixir-ls`.
  (defvar lsp-elixir--config-options (make-hash-table))
  (puthash "dialyzerEnabled" :json-false lsp-elixir--config-options)
  (add-hook 'lsp-after-initialize-hook
            (lambda ()
              (lsp--set-configuration `(:elixirLS, lsp-elixir--config-options))))

  ;; ...all your other user initialization code/config goes here...
)

This worked for me and Spacemacs stopped thrashing my server-grade workstation to a halt on literally every save. Spacemacs feels more snappy now.

2 Likes

Die locals is the blessed way to set variables within a project, this is how Emacs works. I’m not sure what your problem is with that.

That the solution offered isn’t self-contained. You just mention "use .dir-locals.el" and stop there. I never used it even if I use Emacs for ~19 years now so I appreciate complete and isolated advice on “how do I do X?”.

But it might be me burning out on Emacs lately. :slight_smile: Wish it was simpler and didn’t require me to always learn more and more and more just to have basic functionality in place.

Does anyone have any advice on how to get typespec suggestions working? I’m using Doom and elixir-lsp/lsp-mode. I note that the setup for elixir-lsp + emacs notes that Dialyzer is required for typespec suggestion. Do I need to do something additional to get Dialyzer working within the lsp/emacs world? Everything is enabled in my lsp-config.

2 Likes

In doom they should work out of the box. Just did a clean install and can confirm that without the need for any changes you’ll get working typespec completion

Interesting! Thanks for the info. Do you have to do anything to get them to appear? I’m still not getting them and haven’t been able to figure out why. I’m not really sure how best to debug it.

Maybe you need to update elixir-ls? And make a new release ofcourse

Hmm, thanks for the idea. I pulled elixir-ls and built a fresh version. Also tried a fresh install of Doom with (elixir +lsp) in my init.el and still am not seeing type suggestions… Would you mind screenshotting what it looks like? And do you have to run any command to get them to appear? Sorry, I’m just a bit befuddled at this point!