Revise lua layer

Improve documentation to be clear about the choices of backends.
Remove obsolete functions.
Make sure that company-lua is only loaded when the right backend is used.
Make keybindings conform to conventions.
This commit is contained in:
Maximilian Wolff 2019-12-01 00:39:37 +01:00
parent cf376520ad
commit 39c42da792
No known key found for this signature in database
GPG key ID: 2DD07025BFDBD89A
5 changed files with 48 additions and 75 deletions

View file

@ -11,7 +11,7 @@
- [[#layer][Layer]]
- [[#backends][Backends]]
- [[#lsp][LSP]]
- [[#features-1][Features]]
- [[#lua-mode][Lua mode]]
- [[#key-bindings][Key bindings]]
- [[#lsp-1][LSP]]
- [[#commands][Commands]]
@ -20,10 +20,11 @@
This layer adds support for editing Lua.
** Features:
- LSP with EmmyLua-LS-all.
- Editing lua files using [[https://github.com/immerrr/lua-mode][lua-mode]]
- Code-Completion with =company-lsp= or =company-lua=
- Sending code to a lua REPL
- Code linting using [[https://github.com/mpeterv/luacheck][Luacheck]]
- Cross references (definitions, references, rename...)
* Install
** Layer
@ -36,32 +37,27 @@ In order to enable code linting, install [[https://github.com/mpeterv/luacheck][
** Backends
Supported backends are:
- =lsp-emmy= using clangd LSP server
- =lua-mode= using emacs package
*** LSP
LSP support is provided via the [[file:../../+tools/lsp/README.org][LSP layer]], currently only EmmyLua is supported,
- [[https://github.com/EmmyLua/EmmyLua-LanguageServer][lsp-emmy]]
LSP support is provided via the [[file:../../+tools/lsp/README.org][LSP layer]], currently only [[https://github.com/EmmyLua/EmmyLua-LanguageServer][EmmyLua]] is supported.
To use the =lsp-emmy= backend, please download the [[https://github.com/EmmyLua/EmmyLua-LanguageServer][EmmyLua-LS-all.jar]] and put
into =~/.emacs.d/=, then set the layer variables lsp-backend for =lua=:
#+begin_src elisp
(lua :variables lsp-backend 'lsp-emmy)
#+end_src
To use the =lsp-emmy= backend, 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-backend to =lsp-emmy=.
The complete layer variables are:
=Lsp-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
lsp-backend 'lsp-emmy
lua-backend 'lsp-emmy
lua-lsp-emmy-jar-path "~/.emacs.d/EmmyLua-LS-all.jar" ; default path
lua-lsp-emmy-java-path "java" ; default path
lua-lsp-emmy-enable-file-watchers t) ; enabled default
#+end_src
**** Features
- Cross references (definitions, references, rename...)
- Completion with =company-lsp=
- Syntax checking via flycheck (lsp-ui-flycheck)
- Cross-platform - functional on Windows, Linux and macOS.
- Refer https://github.com/EmmyLua/EmmyLua-LanguageServer for details.
*** 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=.
* Key bindings
** LSP
@ -77,3 +73,4 @@ the [[file:../../+tools/lsp/README.org][LSP layer]].
| ~SPC m s f~ | send current function to REPL |
| ~SPC m s l~ | send current line to REPL |
| ~SPC m s r~ | send current region to REPL |
| ~SPC m '~ | open repl buffer |

View file

@ -13,15 +13,14 @@
(spacemacs|define-jump-handlers lua-mode)
(defvar lua-backend nil
"If `lsp-emmy' then enables EmmyLua support")
(defvar lua-backend 'nil
"The backend to be used for lua must be `lsp-emmy' or nil if `lua-mode' should be used.")
;; lua-lsp-backend variables
(defvar lua-lsp-emmy-java-path nil
(defvar lua-lsp-emmy-java-path "java"
"Path to java which will be used for running emmy-lua language server.")
(defvar lua-lsp-emmy-jar-path nil
(defvar lua-lsp-emmy-jar-path "~/.emacs.d/EmmyLua-LS-all.jar"
"Path to jar which will be used for running EmmyLua language server.")
(defvar lua-lsp-emmy-enable-file-watchers t

View file

@ -9,60 +9,45 @@
;;
;;; License: GPLv3
(defun spacemacs//lua-backend ()
"Returns selected backend."
(or lua-backend
(cond
((configuration-layer/layer-used-p 'lsp) 'lsp-emmy))))
(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
"is" 'lua-show-process-buffer
"ih" 'lua-hide-process-buffer)
(pcase (spacemacs//lua-backend)
"'" 'lua-show-process-buffer)
(pcase lua-backend
(`lsp-emmy (spacemacs//lua-setup-lsp-emmy))))
(defun spacemacs//lua-setup-company ()
"Conditionally setup company based on backend."
(pcase (spacemacs//lua-backend)
(pcase lua-backend
(`lsp-emmy (spacemacs//lua-setup-lsp-company))
(_ (company-mode))))
(defun spacemacs//lua-setup-dap ()
"Conditionally setup elixir DAP integration."
;; currently DAP is only available using LSP
(pcase (spacemacs//lua-backend)
(`lsp-emmy (spacemacs//lua-setup-lsp-dap))))
(_ (spacemacs//lua-setup-company-lua))))
(defun spacemacs//lua-setup-flycheck ()
"Conditionally setup flycheck based on backend."
(pcase (spacemacs//lua-backend)
(pcase lua-backend
(`lsp-emmy (spacemacs//lua-setup-lsp-flycheck))))
;; LSP Lua
(defun spacemacs//lua-setup-lsp-emmy ()
"Setup LSP Lua."
(if (not (configuration-layer/layer-used-p 'lsp))
(message "`lsp' layer is not installed, please add `lsp' layer to your dotfile.")
(require 'lsp-clients)
(when lua-lsp-emmy-java-path
(setq lsp-clients-emmy-lua-java-path lua-lsp-emmy-java-path))
(when lua-lsp-emmy-jar-path
(setq lsp-clients-emmy-lua-jar-path (expand-file-name lua-lsp-emmy-jar-path)))
(setq lsp-enable-file-watchers lua-lsp-emmy-enable-file-watchers)
(lsp)))
(require 'lsp-clients)
(when lua-lsp-emmy-java-path
(setq lsp-clients-emmy-lua-java-path lua-lsp-emmy-java-path))
(when lua-lsp-emmy-jar-path
(setq lsp-clients-emmy-lua-jar-path (expand-file-name lua-lsp-emmy-jar-path)))
(setq lsp-enable-file-watchers lua-lsp-emmy-enable-file-watchers)
(lsp))
(defun spacemacs//lua-setup-lsp-company ()
"Setup lsp auto-completion."
@ -75,16 +60,16 @@
;; (add-to-list 'company-lsp-filter-candidates '(emmy-lua . t))
(company-mode))
(defun spacemacs//lua-setup-lsp-dap ()
"Setup DAP integration."
) ; TODO: Lua Debug Adapter Protocol
(defun spacemacs//lua-setup-lsp-flycheck ()
"Setup LSP Lua syntax checking."
(if (configuration-layer/layer-used-p 'lsp)
(when (spacemacs/enable-flycheck 'lua-mode)
(lsp-ui-flycheck-enable nil)
(flycheck-mode))
(message "`lsp' layer is not installed, please add `lsp' layer to your dotfile.")))
(when (spacemacs/enable-flycheck 'lua-mode)
(lsp-ui-flycheck-enable nil)
(flycheck-mode)))
;; Lua mode
(defun spacemacs//lua-setup-company-lua ()
(spacemacs|add-company-backends
:backends company-lua
:modes lua-mode)
(company-mode))

View file

@ -12,6 +12,3 @@
(when (and (boundp 'lua-backend)
(string-match-p "^lsp-.*" (symbol-name lua-backend)))
(configuration-layer/declare-layer-dependencies '(lsp)))

View file

@ -28,20 +28,16 @@
: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))))
(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
:defer t
:init (spacemacs|add-company-backends
:backends company-lua
:modes lua-mode)))
:defer t))
(defun lua/post-init-ggtags ()
(add-hook 'lua-mode-local-vars-hook #'spacemacs/ggtags-mode-enable))
@ -51,4 +47,3 @@
(defun lua/post-init-helm-gtags ()
(spacemacs/helm-gtags-define-keys-for-mode 'lua-mode))