This commit is contained in:
ry
2022-01-11 18:05:13 -08:00
parent 2046befee2
commit 8e7b654716
846 changed files with 71287 additions and 4 deletions

View File

@@ -0,0 +1,78 @@
#+TITLE: lang/lua
#+DATE: Jun 3, 2020
#+SINCE: v3.0
#+STARTUP: inlineimages nofold
* Table of Contents :TOC_3:noexport:
- [[#description][Description]]
- [[#maintainers][Maintainers]]
- [[#module-flags][Module Flags]]
- [[#plugins][Plugins]]
- [[#prerequisites][Prerequisites]]
- [[#language-server-protocol-servers][Language Server Protocol servers]]
- [[#lsp-mode][LSP-mode]]
- [[#eglot][Eglot]]
- [[#features][Features]]
- [[#configuration][Configuration]]
- [[#troubleshooting][Troubleshooting]]
- [[#install-lua-language-server][Install lua-language-server]]
* Description
Adds Lua support to Doom Emacs
# A summary of what this module does.
+ REPL
+ Love2D specific functions
+ Moonscript support
** Maintainers
This module has no dedicated maintainers.
** Module Flags
+ =+moonscript= Moonscript language support
+ =+fennel= Fennel language support
+ =+lsp= Language Server Protocol support
** Plugins
# A list of linked plugins
+ [[https://github.com/immerrr/lua-mode][lua-mode]]
+ [[https://github.com/k2052/moonscript-mode][moonscript-mode]] (=+moonscript=)
+ [[https://gitlab.com/technomancy/fennel-mode][fennel-mode]] (=+fennel=)
* Prerequisites
** Language Server Protocol servers
LSP server support depends on which flavor of the =:tools lsp= module you have
installed (Eglot or LSP-mode).
*** LSP-mode
Three servers are supported, ordered from highest to lowest priority:
+ [[https://github.com/EmmyLua/EmmyLua-LanguageServer][EmmyLua-LanguageServer]] :: Must be in =~/.emacs.d/.local/etc/lsp/EmmyLua-LS-all.jar=. See ~lsp-clients-emmy-lua-jar-path~ variable to change this.
+ [[https://github.com/sumneko/lua-language-server][Sumneko language server]] (lua-language-server) :: Must be in
=~/.config/emacs/.local/etc/lsp/lua-language-server/=. See
~lsp-clients-lua-language-server-bin~ variable to change this.
+ [[https://github.com/Alloyed/lua-lsp][lua-lsp]] :: Must be available in =~/.luarocks/bin/lua-lsp=. See
~lsp-clients-lua-lsp-server-install-dir~ variable to change this.
[[https://emacs-lsp.github.io/lsp-mode/page/lsp-emmy-lua/][LSP-mode documentation]] has more information about setting up the server and the
configuration variables correctly.
*** Eglot
Eglot currently only supports one of the above servers out of the box:
+ [[https://github.com/sumneko/lua-language-server][Sumneko language server]] (lua-language-server) :: Must be in
=~/.config/emacs/.local/etc/lsp/lua-language-server/=. See
~+lua-lsp-dir~ variable to change this.
* TODO Features
# An in-depth list of features, how to use them, and their dependencies.
* Configuration
- lua-lsp-dir :: This must be set when using =+lsp= and using
[[https://github.com/sumneko/lua-language-server][lua-language-server]]. This controls where the repository has been cloned and
built to finish the configuration of the server.
* Troubleshooting
** Install lua-language-server
A [[https://github.com/sumneko/lua-language-server/issues/60][catch-all issue]] has been created to gather help for installing
lua-language-server on non-VSCode platforms.

View File

@@ -0,0 +1,51 @@
;;; lang/lua/autoload/lua.el -*- lexical-binding: t; -*-
(defun +lua-love-build-command ()
(when-let (root (+lua-love-project-root))
(format "%s %s"
(if (executable-find "love")
"love"
(if IS-MAC "open -a love.app"))
(shell-quote-argument root))))
;;;###autoload
(defun +lua-love-project-root ()
"Returns the directory where a main.lua or main.moon exists.
Returns nil if 'love' executable can't be found."
(when (executable-find "love")
(if (doom-project-p)
(file-name-directory
(or (project-file-exists-p! (or "main.lua" "src/main.lua"))
(and (featurep! +moonscript)
(project-file-exists-p! (or "main.moon" "src/main.moon")))
""))
;; Since Love2D games are likely to be prototypes, they may not be in a
;; well-formed project as far as projecitle is concerned, so we search for
;; main.lua/main.moon up the file tree as a backup.
(or (projectile-locate-dominating-file default-directory "main.lua")
(when-let (root (projectile-locate-dominating-file default-directory "src/main.lua"))
(expand-file-name "src" root))
(and (featurep! +moonscript)
(or (projectile-locate-dominating-file default-directory "main.moon")
(when-let (root (projectile-locate-dominating-file default-directory "src/main.moon"))
(expand-file-name "src" root))))))))
;;
;;; Commands
;;;###autoload
(defun +lua/open-repl ()
"Open Lua REPL."
(interactive)
(lua-start-process "lua" "lua")
(pop-to-buffer lua-process-buffer))
;;;###autoload
(defun +lua/run-love-game ()
"Run the current project with Love2D."
(interactive)
(if-let (cmd (+lua-love-build-command))
(async-shell-command cmd)
(user-error "Couldn't find love project")))

View File

@@ -0,0 +1,16 @@
;;; lang/lua/autoload/moonscript.el -*- lexical-binding: t; -*-
;;;###if (featurep! +moonscript)
;;;###autoload
(defun +lua-moonscript-fix-single-quotes-h ()
"Single-quoted strings aren't treated as strings."
;; (modify-syntax-entry ?\" "\"" moonscript-mode-syntax-table)
(modify-syntax-entry ?\' "\"" moonscript-mode-syntax-table))
;;;###autoload
(defun +lua-moonscript-fontify-interpolation-h ()
"Highlight interpolated expressions in moonscript strings."
(font-lock-add-keywords
nil '(("#{\\([^}]+\\)}"
(0 font-lock-preprocessor-face t)
(1 nil t)))))

View File

@@ -0,0 +1,90 @@
;;; lang/lua/config.el -*- lexical-binding: t; -*-
;; sp's default rules are obnoxious, so disable them
(provide 'smartparens-lua)
;;
;;; Major modes
(use-package! lua-mode
:defer t
:init
;; lua-indent-level defaults to 3 otherwise. Madness.
(setq lua-indent-level 2)
:config
(set-lookup-handlers! 'lua-mode :documentation 'lua-search-documentation)
(set-electric! 'lua-mode :words '("else" "end"))
(set-repl-handler! 'lua-mode #'+lua/open-repl)
(set-company-backend! 'lua-mode '(company-lua company-yasnippet))
(when (featurep! +lsp)
(add-hook 'lua-mode-local-vars-hook #'lsp!)
(when (featurep! :tools lsp +eglot)
(defvar +lua-lsp-dir (concat doom-etc-dir "lsp/lua-language-server/")
"Absolute path to the directory of sumneko's lua-language-server.
This directory MUST contain the 'main.lua' file and be the in-source build of
lua-language-server.")
(defun +lua-generate-lsp-server-command ()
;; The absolute path to lua-language-server binary is necessary because
;; the bundled dependencies aren't found otherwise. The only reason this
;; is a function is to dynamically change when/if `+lua-lsp-dir' does
(list (or (executable-find "lua-language-server")
(doom-path +lua-lsp-dir
(cond (IS-MAC "bin/macOS")
(IS-LINUX "bin/Linux")
(IS-WINDOWS "bin/Windows"))
"lua-language-server"))
"-E" "-e" "LANG=en"
(doom-path +lua-lsp-dir "main.lua")))
(set-eglot-client! 'lua-mode (+lua-generate-lsp-server-command)))))
(use-package! moonscript
:when (featurep! +moonscript)
:defer t
:config
(setq-hook! 'moonscript-mode-hook
moonscript-indent-offset tab-width)
(add-hook! 'moonscript-mode-hook
#'+lua-moonscript-fix-single-quotes-h
#'+lua-moonscript-fontify-interpolation-h)
(when (featurep! :checkers syntax)
(require 'flycheck-moonscript nil t)))
(use-package! fennel-mode
:when (featurep! +fennel)
:defer t
:config
(set-lookup-handlers! 'fennel-mode
:definition #'fennel-find-definition
:documentation #'fennel-show-documentation)
(set-repl-handler! 'fennel-mode #'fennel-repl)
(setq-hook! 'fennel-mode-hook
;; To match the `tab-width' default for other lisp modes
tab-width 2
;; Don't treat autoloads or sexp openers as outline headers, we have
;; hideshow for that.
outline-regexp "[ \t]*;;;;* [^ \t\n]"))
;;
;;; Frameworks
(def-project-mode! +lua-love-mode
:modes '(moonscript-mode lua-mode markdown-mode json-mode)
:when #'+lua-love-project-root
:on-load
(progn
(set-project-type! 'love2d
:predicate #'+lua-love-project-root
:run #'+lua-love-build-command)
(map! :localleader
:map +lua-love-mode-map
"b" #'+lua/run-love-game)))

View File

@@ -0,0 +1,17 @@
;; -*- no-byte-compile: t; -*-
;;; lang/lua/packages.el
(package! lua-mode :pin "5a9bee8d5fc978dc64fcb677167417010321ba65")
(when (featurep! +moonscript)
(package! moonscript :pin "56f90471e2ced2b0a177aed4d8c2f854797e9cc7")
(when (featurep! :checkers syntax)
(package! flycheck-moonscript
:recipe (:host github :repo "hlissner/emacs-flycheck-moonscript")
:pin "fcb99e5efcf31db05f236f02eaa575986a57172d")))
(when (featurep! +fennel)
(package! fennel-mode :pin "47152970a98734723b5086b5c774f50da34c0488"))
(when (featurep! :completion company)
(package! company-lua :pin "29f6819de4d691e5fd0b62893a9f4fbc1c6fcb52"))