Skip to content
Adam B edited this page Nov 6, 2019 · 14 revisions

It is possible to use the Julia LanguageServer in Emacs using the lsp-mode and the lsp-julia packages. See the README of lsp-julia to get it working. It also might be helpful to look at this discourse post from @ffevotte to get an idea of the capabilities available.

Alternatives

The following is my (@non-Jedi) config for using LanguageServer.jl with eglot. I haven't tested it much, and there may be weirdness/dragons here I'm not aware of, but you can base your own config on it if interested. Let me know in the discourse thread if you have trouble getting this working.

(require 'cl-generic)

(defcustom julia-default-depot ""
  "The default depot path, used if `JULIA_DEPOT_PATH' is unset"
  :type 'string
  :group 'julia-config)

(defcustom julia-default-environment "~/.julia/environment/v1.1"
  "The default julia environment"
  :type 'string
  :group 'julia-config)

(defun julia/get-depot-path ()
  (if-let (env-depot (getenv "JULIA_DEPOT_PATH"))
      (expand-file-name env-depot)
    (if (equal julia-default-depot "")
        julia-default-depot
      (expand-file-name julia-default-depot))))

(defun julia/get-environment (dir)
  (expand-file-name (if dir (or (locate-dominating-file dir "JuliaProject.toml")
                                (locate-dominating-file dir "Project.toml")
                                julia-default-environment)
                      julia-default-environment)))

;; Make project.el aware of Julia projects
(defun julia/project-try (dir)
  (let ((root (or (locate-dominating-file dir "JuliaProject.toml")
                 (locate-dominating-file dir "Project.toml"))))
    (and root (cons 'julia root))))
(add-hook 'project-find-functions 'julia/project-try)

(cl-defmethod project-roots ((project (head julia)))
  (list (cdr project)))

(defun julia/get-language-server-invocation (interactive)
  `("julia"
    ,(expand-file-name "~/.emacs.d/lisp/eglot-julia/eglot.jl")
    ,(julia/get-environment (buffer-file-name))
    ,(julia/get-depot-path)))

;; Setup eglot with julia
(with-eval-after-load 'eglot
  (setq eglot-connect-timeout 100)
  (add-to-list 'eglot-server-programs
               ;; function instead of strings to find project dir at runtime
			   '(julia-mode . julia/get-language-server-invocation))
  (add-hook 'julia-mode-hook 'eglot-ensure))

~/.emacs.d/lisp/eglot-julia/eglot.jl is just:

import Pkg
Pkg.activate(@__DIR__)

using LanguageServer

server = LanguageServer.LanguageServerInstance(stdin, stdout, false,
                                               ARGS[1], ARGS[2], Dict())
run(server)

with the following ~/.emacs.d/lisp/eglot-julia/Project.toml:

[deps]
LanguageServer = "2b0e0bc5-e4fd-59b4-8912-456d1b03d8d7"
Clone this wiki locally