[lua] Revise layer and fix LSP integration

The lsp integration was not standard at all in
this layer I have refactored it now to be working
like the rest of Spacemacs.
This commit is contained in:
Maximilian Wolff 2021-02-13 21:53:57 +00:00
parent ca0fe33c8d
commit fc2aa2601c
5 changed files with 127 additions and 77 deletions

View file

@ -10,8 +10,11 @@
- [[#install][Install]]
- [[#layer][Layer]]
- [[#backends][Backends]]
- [[#lsp][LSP]]
- [[#lua-mode][Lua mode]]
- [[#lsp][LSP]]
- [[#emmy-server-setup][Emmy server setup]]
- [[#lua-language-server-setup][Lua-language server setup]]
- [[#lua-lsp-server-setup][Lua-lsp server setup]]
- [[#key-bindings][Key bindings]]
- [[#lsp-1][LSP]]
- [[#commands][Commands]]
@ -36,39 +39,79 @@ In order to enable code linting, install [[https://github.com/mpeterv/luacheck][
** Backends
Supported backends are:
- =lsp-emmy-lua= using emmy LSP server
- =lsp-lua-language-server= using lua-language-server.
- =lsp-lua-lsp= using lua-lsp.
- =lua-mode= using emacs package
- =lua-mode= using emacs packages
- =lsp= using one of below LSP servers
- emmy LSP server (default)
- lua-language-server.
- lua-lsp.
*** Lua mode
This backend provides basic support via pure emacs packages. It requires less setup than a full blown LSP setup
but also provides less features. To enable it set the =lua-backend= to =lua-mode=.
#+BEGIN_SRC elisp
(lua :variables
lua-backend 'lua-mode)
#+END_SRC
*** LSP
LSP support is provided via the [[file:../../+tools/lsp/README.org][LSP layer]], currently [[https://github.com/EmmyLua/EmmyLua-LanguageServer][EmmyLua]], [[https://github.com/sumneko/lua-language-server][lua-language-server]] and [[https://github.com/Alloyed/lua-lsp][lua-lsp]] are supported.
This is the prefered backend choice. It relies on an external server implementing the LSP protocol.
It requires installing one of the supported server implementations below:
- [[https://github.com/EmmyLua/EmmyLua-LanguageServer][EmmyLua]]
- [[https://github.com/sumneko/lua-language-server][lua-language-server]]
- [[https://github.com/Alloyed/lua-lsp][lua-lsp]]
To use the =lsp-emmy-lua= backend, please download the [[https://github.com/EmmyLua/EmmyLua-LanguageServer][EmmyLua-LS-all.jar]] and put it into =~/.emacs.d/=,
and then set the layer variable lua-backend to =lsp-emmy-lua=.
Once it is installed the keybindings are provided by [[file:../../+tools/lsp/README.org][LSP layer]] allowing
consistent keybindings among different layers.
=lsp-emmy-lua= requires a working java installation in the path as well as the path to the LSP server jar.
To enable LSP set =lua-backend= to =lsp=. If =lua-backend= is =nil= and the =LSP layer= is loaded
=LSP= will be used automatically otherwise it will fallback to =lua-mode=.
#+BEGIN_SRC elisp
(lua :variables
lua-backend 'lsp)
#+END_SRC
**** Emmy server setup
To use the =emmy= lsp server, please download the [[https://github.com/EmmyLua/EmmyLua-LanguageServer][EmmyLua-LS-all.jar]] and put it into =~/.emacs.d/=.
Then set the layer variable =lua-lsp-server= to =emmy=. This is also the default choice if no specific
lsp server has been selected.
=emmy= requires a working java installation in the path as well as the path to the LSP server jar.
The java path and the path to the server executable can be configured as is shown below:
#+BEGIN_SRC elisp
(lua :variables
lua-backend 'lsp-emmy
lua-backend 'lsp
lua-lsp-server 'emmy
lsp-clients-emmy-lua-jar-path "~/.emacs.d/.cache/lsp/EmmyLua-LS-all.jar" ; default path
lsp-clients-emmy-lua-java-path "java") ; default path
#+END_SRC
To use the =lsp-lua-language-server= backend, please follow [[https://github.com/sumneko/lua-language-server][lua-language-server]] to compile the standalone binary,
and then set the layer variable =lua-backend= to =lsp-lua-language-server=, and the variables for binary and main entry such as the example below:
**** Lua-language server setup
To use the =lua-language-server= lsp server, please follow [[https://github.com/sumneko/lua-language-server][lua-language-server]] to compile the standalone binary.
Then set the layer variables as shown below:
#+BEGIN_SRC elisp
(lua :variables
lua-backend 'lsp-lua-language-server
lua-backend 'lsp
lua-lsp-server 'lua-language-server
lsp-clients-lua-language-server-bin "~/.emacs.d/.cache/lsp/lua-language-server/bin/Linux/lua-language-server" ; default path
lsp-clients-lua-language-server-main-location "~/.emacs.d/.cache/lsp/lua-language-server/main.lua") ; default path
#+END_SRC
*** Lua mode
This backend provides support via pure emacs packages. It requires less setup than a full blown LSP setup
but also provides less features. To enable it just set the =lua-backend= to =nil=.
**** Lua-lsp server setup
To use the =lua-lsp= lsp server, please follow [[https://github.com/Alloyed/lua-lsp][lua-lsp]] to install the server via
luarocks. Remember that the capabilities of this server are mainly dependent
on the installed luarocks packages, so make sure you have them all installed
for maximum benefit.
To instruct Spacemacs to use this server set your config as shown below:
#+BEGIN_SRC elisp
(lua :variables
lua-backend 'lsp
lua-lsp-server 'lua-lsp
lsp-clients-luarocks-bin-dir "~/.luarocks/bin/") ; default path
#+END_SRC
* Key bindings
** LSP

View file

@ -13,7 +13,11 @@
(spacemacs|define-jump-handlers lua-mode)
(defvar lua-backend nil
"The backend to be used for Lua.
When nil, use `lua-mode' as the backend.
When non-nil, use one of LSP Lua clients, i.e. `lsp-emmy', `lua-language-server', or `lsp-lua-lsp'.")
(defvar lua-backend 'nil
"The backend to use for IDE features.
Possible values are `lua-mode' and `lsp'.
If `nil' then `lua-mode' is the default backend unless `lsp' layer is used.")
(defvar lua-lsp-server 'emmy
"Language server to use for lsp backend.
Possible values are `emmy', `lua-language-server', or `lua-lsp'.")

View file

@ -9,55 +9,38 @@
;;
;;; License: GPLv3
(defun spacemacs//lua-backend ()
"Returns selected backend."
(if lua-backend
lua-backend
(cond
((configuration-layer/layer-used-p 'lsp) 'lsp)
(t 'lua-mode))))
(defun spacemacs//lua-setup-backend ()
"Conditionally setup lua backend."
(setq lua-indent-level 2
lua-indent-string-contents t)
(spacemacs/declare-prefix-for-mode 'lua-mode "mh" "help")
(spacemacs/declare-prefix-for-mode 'lua-mode "ms" "REPL")
(spacemacs/declare-prefix-for-mode 'lua-mode "mg" "goto")
(spacemacs/set-leader-keys-for-major-mode 'lua-mode
"hd" 'lua-search-documentation
"sb" 'lua-send-buffer
"sf" 'lua-send-defun
"sl" 'lua-send-current-line
"sr" 'lua-send-region
"'" 'lua-show-process-buffer)
(pcase lua-backend
('lsp-emmy-lua (spacemacs//lua-setup-lsp-emmy-lua))
('lsp-lua-language-server (spacemacs//lua-setup-lsp-lua-language-server))
('lsp-lua-lsp (spacemacs//lua-setup-lsp-lua-lsp))
(_ (if lua-backend
(user-error "Unexpected value of `lua-backend', %s" lua-backend)
(when (configuration-layer/layer-used-p 'lsp)
(lsp))))))
(pcase (spacemacs//lua-backend)
(`lsp (spacemacs//lua-setup-lsp))))
(defun spacemacs//lua-setup-company ()
"Conditionally setup company based on backend."
(pcase lua-backend
(_ (spacemacs//lua-setup-company-lua))))
(pcase (spacemacs//lua-backend)
(`lua-mode (spacemacs|add-company-backends
:backends company-lua
:modes lua-mode))
(`lsp (spacemacs|add-company-backends
:backends company-capf
:modes lua-mode))))
;; LSP Lua
(defun spacemacs//lua-setup-lsp-emmy-lua ()
"Setup emmy Lua."
(setq-local lsp-disabled-clients '(lua-language-server lsp-lua-lsp))
(lsp))
(defun spacemacs//lua-setup-lsp-lua-language-server ()
"Setup lua-language-server."
(setq-local lsp-disabled-clients '(emmy-lua lsp-lua-lsp))
(lsp))
(defun spacemacs//lua-setup-lsp-lua-lsp ()
"Setup lsp-lua-lsp."
(setq-local lsp-disabled-clients '(emmy-lua lsp-lua-language-server))
(lsp))
;; Lua mode
(defun spacemacs//lua-setup-company-lua ()
(spacemacs|add-company-backends
:backends company-lua
:modes lua-mode)
(company-mode))
(defun spacemacs//lua-setup-lsp ()
"Setup lsp backend."
(if (configuration-layer/layer-used-p 'lsp)
(progn
(cond ((eq lua-lsp-server 'emmy) (setq-local lsp-disabled-clients '(lua-language-server lsp-lua-lsp)))
((eq lua-lsp-server 'lua-language-server) (setq-local lsp-disabled-clients '(emmy-lua lsp-lua-lsp)))
((eq lua-lsp-server 'lua-lsp) (setq-local lsp-disabled-clients '(emmy-lua lsp-lua-language-server))))
(lsp))
(message "`lsp' layer is not installed, please add `lsp' layer to your dotfile.")))

View file

@ -10,5 +10,5 @@
;;; License: GPLv3
(when (and (boundp 'lua-backend)
(string-match-p "^lsp-.*" (symbol-name lua-backend)))
(eq lua-backend 'lsp))
(configuration-layer/declare-layer-dependencies '(lsp)))

View file

@ -9,16 +9,15 @@
;;
;;; License: GPLv3
(setq lua-packages
'(
company
(company-lua :requires company)
flycheck
ggtags
counsel-gtags
helm-gtags
lua-mode
))
(defconst lua-packages
'(
company
(company-lua :requires company)
flycheck
ggtags
counsel-gtags
helm-gtags
lua-mode))
(defun lua/post-init-flycheck ()
(spacemacs/enable-flycheck 'lua-mode))
@ -28,15 +27,36 @@
:defer t
:mode ("\\.lua\\'" . lua-mode)
:interpreter ("lua" . lua-mode)
:init (progn
(spacemacs/register-repl 'lua #'lua-show-process-buffer "lua")
(add-hook 'lua-mode-local-vars-hook #'spacemacs//lua-setup-backend))))
:init
(progn
(spacemacs/register-repl 'lua #'lua-show-process-buffer "lua")
(add-hook 'lua-mode-local-vars-hook #'spacemacs//lua-setup-backend)
;; Set global settings
(setq lua-indent-level 2
lua-indent-string-contents t)
;; Set general bindings
(spacemacs/declare-prefix-for-mode 'lua-mode "ms" "REPL")
(spacemacs/set-leader-keys-for-major-mode 'lua-mode
"hd" 'lua-search-documentation
"sb" 'lua-send-buffer
"sf" 'lua-send-defun
"sl" 'lua-send-current-line
"sr" 'lua-send-region
"'" 'lua-show-process-buffer)
;; Set lua-mode specific bindings
(when (eq (spacemacs/lua-backend) `lua-mode)
(spacemacs/declare-prefix-for-mode 'lua-mode "mh" "help")
(spacemacs/declare-prefix-for-mode 'lua-mode "mg" "goto")))))
(defun lua/post-init-company ()
(add-hook 'lua-mode-local-vars-hook #'spacemacs//lua-setup-company))
(defun lua/init-company-lua ()
(use-package company-lua
:if (eq (spacemacs//lua-backend) 'lua-mode)
:defer t))
(defun lua/post-init-ggtags ()