From cf376520ada3c87317aeec427964cb220bfb88c7 Mon Sep 17 00:00:00 2001 From: "lin.sun" Date: Tue, 12 Nov 2019 11:29:50 +0800 Subject: [PATCH] [lua] Add LSP support --- CHANGELOG.develop | 1 + layers/+lang/lua/README.org | 41 ++++++++++++++++ layers/+lang/lua/config.el | 14 ++++++ layers/+lang/lua/funcs.el | 90 ++++++++++++++++++++++++++++++++++++ layers/+lang/lua/layers.el | 17 +++++++ layers/+lang/lua/packages.el | 13 ++---- layers/LAYERS.org | 4 ++ 7 files changed, 171 insertions(+), 9 deletions(-) create mode 100644 layers/+lang/lua/funcs.el create mode 100644 layers/+lang/lua/layers.el diff --git a/CHANGELOG.develop b/CHANGELOG.develop index 51d971c3c..756e99482 100644 --- a/CHANGELOG.develop +++ b/CHANGELOG.develop @@ -2157,6 +2157,7 @@ Other: (thanks to Olivier Verdier) **** Lua - Added support for auto-completion with =company= (thanks to halfcrazy) +- Added support for =LSP= (EmmyLua-LS-all) (thanks to Lin.Sun) **** Language Server Protocol (LSP) - Added core keybindings and prefix declarations for all LSP-based language layers, and helper functions to bind server-specific extensions diff --git a/layers/+lang/lua/README.org b/layers/+lang/lua/README.org index f58d0bd81..51b0b8f8e 100644 --- a/layers/+lang/lua/README.org +++ b/layers/+lang/lua/README.org @@ -8,25 +8,66 @@ - [[#description][Description]] - [[#features][Features:]] - [[#install][Install]] + - [[#layer][Layer]] + - [[#backends][Backends]] + - [[#lsp][LSP]] + - [[#features-1][Features]] - [[#key-bindings][Key bindings]] + - [[#lsp-1][LSP]] - [[#commands][Commands]] * Description 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]] - Sending code to a lua REPL - Code linting using [[https://github.com/mpeterv/luacheck][Luacheck]] * Install +** Layer To use this configuration layer, add it to your =~/.spacemacs=. You will need to add =lua= to the existing =dotspacemacs-configuration-layers= list in this file. In order to enable code linting, install [[https://github.com/mpeterv/luacheck][Luacheck]]. +** Backends +Supported backends are: +- =lsp-emmy= using clangd LSP server + +*** 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]] + +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 + +The complete layer variables are: +#+begin_src elisp + (lua :variables + lsp-backend 'lsp-emmy + lua-lsp-emmy-jar-path "~/.emacs.d/EmmyLua-LS-all.jar" ; 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. + + * Key bindings +** LSP +The default key bindings for the LSP implementations are defined and documented in +the [[file:../../+tools/lsp/README.org][LSP layer]]. + ** Commands | Key binding | Description | diff --git a/layers/+lang/lua/config.el b/layers/+lang/lua/config.el index c93414c8a..47010af3d 100644 --- a/layers/+lang/lua/config.el +++ b/layers/+lang/lua/config.el @@ -12,3 +12,17 @@ ;; variables (spacemacs|define-jump-handlers lua-mode) + +(defvar lua-backend nil + "If `lsp-emmy' then enables EmmyLua support") + +;; lua-lsp-backend variables + +(defvar lua-lsp-emmy-java-path nil + "Path to java which will be used for running emmy-lua language server.") + +(defvar lua-lsp-emmy-jar-path nil + "Path to jar which will be used for running EmmyLua language server.") + +(defvar lua-lsp-emmy-enable-file-watchers t + "Enabled the EmmyLua file watchers.") diff --git a/layers/+lang/lua/funcs.el b/layers/+lang/lua/funcs.el new file mode 100644 index 000000000..b8d37ce7e --- /dev/null +++ b/layers/+lang/lua/funcs.el @@ -0,0 +1,90 @@ +;;; funcs.el --- Lua Layer functions File for Spacemacs +;; +;; Copyright (c) 2012-2019 Sylvain Benner & Contributors +;; +;; Author: Lin Sun +;; URL: https://github.com/syl20bnr/spacemacs +;; +;; This file is not part of GNU Emacs. +;; +;;; 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/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) + (`lsp-emmy (spacemacs//lua-setup-lsp-emmy)))) + +(defun spacemacs//lua-setup-company () + "Conditionally setup company based on backend." + (pcase (spacemacs//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)))) + +(defun spacemacs//lua-setup-flycheck () + "Conditionally setup flycheck based on backend." + (pcase (spacemacs//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))) + +(defun spacemacs//lua-setup-lsp-company () + "Setup lsp auto-completion." + (spacemacs|add-company-backends + :backends company-lsp + :modes lua-mode + :append-hooks nil + :call-hooks t) + ;; TODO: disable the cache + ;; (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."))) + + diff --git a/layers/+lang/lua/layers.el b/layers/+lang/lua/layers.el new file mode 100644 index 000000000..9905ac5fc --- /dev/null +++ b/layers/+lang/lua/layers.el @@ -0,0 +1,17 @@ +;;; layers.el --- Lua Layer declarations File for Spacemacs +;; +;; Copyright (c) 2012-2019 Sylvain Benner & Contributors +;; +;; Author: Lin Sun +;; URL: https://github.com/syl20bnr/spacemacs +;; +;; This file is not part of GNU Emacs. +;; +;;; License: GPLv3 + +(when (and (boundp 'lua-backend) + (string-match-p "^lsp-.*" (symbol-name lua-backend))) + (configuration-layer/declare-layer-dependencies '(lsp))) + + + diff --git a/layers/+lang/lua/packages.el b/layers/+lang/lua/packages.el index fa364d023..b9ca8d82e 100644 --- a/layers/+lang/lua/packages.el +++ b/layers/+lang/lua/packages.el @@ -30,17 +30,11 @@ :interpreter ("lua" . lua-mode) :init (progn - (setq lua-indent-level 2 - lua-indent-string-contents t) - (spacemacs/set-leader-keys-for-major-mode 'lua-mode - "d" 'lua-search-documentation - "sb" 'lua-send-buffer - "sf" 'lua-send-defun - "sl" 'lua-send-current-line - "sr" 'lua-send-region)))) + (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-hook 'company-mode)) + (add-hook 'lua-mode-local-vars-hook #'spacemacs//lua-setup-company)) (defun lua/init-company-lua () (use-package company-lua @@ -57,3 +51,4 @@ (defun lua/post-init-helm-gtags () (spacemacs/helm-gtags-define-keys-for-mode 'lua-mode)) + diff --git a/layers/LAYERS.org b/layers/LAYERS.org index 91b1328e8..4908e8833 100644 --- a/layers/LAYERS.org +++ b/layers/LAYERS.org @@ -1806,6 +1806,10 @@ Features: - Editing lua files using [[https://github.com/immerrr/lua-mode][lua-mode]] - Sending code to a lua REPL - Code linting using [[https://github.com/mpeterv/luacheck][Luacheck]] +- LSP backend support + - Cross references (definitions, references, rename...) + - Completion with =company-lsp= + - Syntax checking via flycheck (lsp-ui-flycheck) **** Nim [[file:+lang/nim/README.org][+lang/nim/README.org]]