2023-09-11 10:03:13 -05:00
|
|
|
;;; lua-ts-mode.el --- Major mode for editing Lua files -*- lexical-binding: t -*-
|
|
|
|
|
2025-01-01 07:39:17 +00:00
|
|
|
;; Copyright (C) 2023-2025 Free Software Foundation, Inc.
|
2023-09-11 10:03:13 -05:00
|
|
|
|
|
|
|
;; Author: John Muhl <jm@pub.pink>
|
|
|
|
;; Created: June 27, 2023
|
|
|
|
;; Keywords: lua languages tree-sitter
|
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
;;
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
;;
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2024-12-30 00:21:46 -08:00
|
|
|
;;; Tree-sitter language versions
|
|
|
|
;;
|
2025-03-21 14:10:36 -05:00
|
|
|
;; lua-ts-mode has been tested with the following grammars and version:
|
|
|
|
;; - tree-sitter-lua: v0.3.0-1-gdb16e76
|
2024-12-30 00:21:46 -08:00
|
|
|
;;
|
|
|
|
;; We try our best to make builtin modes work with latest grammar
|
2025-03-21 14:10:36 -05:00
|
|
|
;; versions, so a more recent grammar has a good chance to work too.
|
2024-12-30 00:21:46 -08:00
|
|
|
;; Send us a bug report if it doesn't.
|
|
|
|
|
2023-09-11 10:03:13 -05:00
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; This package provides `lua-ts-mode' which is a major mode for Lua
|
|
|
|
;; files that uses Tree Sitter to parse the language.
|
|
|
|
;;
|
2024-02-21 10:14:05 -06:00
|
|
|
;; This package is compatible with and tested against the grammar for
|
|
|
|
;; Lua found at https://github.com/tree-sitter-grammars/tree-sitter-lua
|
2023-09-11 10:03:13 -05:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2025-03-09 20:40:58 -05:00
|
|
|
(require 'c-ts-common)
|
2023-09-11 10:03:13 -05:00
|
|
|
(require 'comint)
|
|
|
|
(require 'treesit)
|
2024-12-07 22:13:07 -08:00
|
|
|
(treesit-declare-unavailable-functions)
|
2023-09-11 10:03:13 -05:00
|
|
|
|
|
|
|
(eval-when-compile
|
|
|
|
(require 'rx))
|
|
|
|
|
2025-04-18 19:22:50 +03:00
|
|
|
(add-to-list
|
|
|
|
'treesit-language-source-alist
|
2025-06-20 19:56:01 +03:00
|
|
|
'(lua "https://github.com/tree-sitter-grammars/tree-sitter-lua"
|
|
|
|
:commit "db16e76558122e834ee214c8dc755b4a3edc82a9")
|
2025-04-18 19:22:50 +03:00
|
|
|
t)
|
|
|
|
|
2023-09-11 10:03:13 -05:00
|
|
|
(defgroup lua-ts nil
|
|
|
|
"Major mode for editing Lua files."
|
|
|
|
:prefix "lua-ts-"
|
|
|
|
:group 'languages)
|
|
|
|
|
2023-10-04 20:46:15 -05:00
|
|
|
(defcustom lua-ts-mode-hook nil
|
|
|
|
"Hook run after entering `lua-ts-mode'."
|
|
|
|
:type 'hook
|
2023-11-15 12:45:18 -06:00
|
|
|
:options '(eglot-ensure
|
|
|
|
flymake-mode
|
2023-10-04 20:46:15 -05:00
|
|
|
hs-minor-mode
|
|
|
|
outline-minor-mode)
|
|
|
|
:version "30.1")
|
|
|
|
|
2023-09-11 10:03:13 -05:00
|
|
|
(defcustom lua-ts-indent-offset 4
|
|
|
|
"Number of spaces for each indentation step in `lua-ts-mode'."
|
|
|
|
:type 'natnum
|
|
|
|
:safe 'natnump
|
|
|
|
:version "30.1")
|
|
|
|
|
|
|
|
(defcustom lua-ts-luacheck-program "luacheck"
|
|
|
|
"Location of the Luacheck program."
|
2024-11-09 11:01:45 -06:00
|
|
|
:type 'file
|
2023-09-11 10:03:13 -05:00
|
|
|
:version "30.1")
|
|
|
|
|
|
|
|
(defcustom lua-ts-inferior-buffer "*Lua*"
|
|
|
|
"Name of the inferior Lua buffer."
|
|
|
|
:type 'string
|
|
|
|
:safe 'stringp
|
|
|
|
:version "30.1")
|
|
|
|
|
|
|
|
(defcustom lua-ts-inferior-program "lua"
|
|
|
|
"Program to run in the inferior Lua process."
|
2024-11-09 11:01:45 -06:00
|
|
|
:type 'file
|
2023-09-11 10:03:13 -05:00
|
|
|
:version "30.1")
|
|
|
|
|
|
|
|
(defcustom lua-ts-inferior-options '("-i")
|
|
|
|
"Command line options for the inferior Lua process."
|
|
|
|
:type '(repeat string)
|
|
|
|
:version "30.1")
|
|
|
|
|
|
|
|
(defcustom lua-ts-inferior-startfile nil
|
|
|
|
"File to load into the inferior Lua process at startup."
|
2023-10-04 20:46:15 -05:00
|
|
|
:type '(choice (const :tag "None" nil) (file :must-match t))
|
|
|
|
:version "30.1")
|
|
|
|
|
|
|
|
(defcustom lua-ts-inferior-prompt ">"
|
|
|
|
"Prompt used by the inferior Lua process."
|
|
|
|
:type 'string
|
|
|
|
:safe 'stringp
|
|
|
|
:version "30.1")
|
|
|
|
|
|
|
|
(defcustom lua-ts-inferior-prompt-continue ">>"
|
|
|
|
"Continuation prompt used by the inferior Lua process."
|
|
|
|
:type 'string
|
|
|
|
:safe 'stringp
|
2023-09-11 10:03:13 -05:00
|
|
|
:version "30.1")
|
|
|
|
|
2023-10-04 20:46:15 -05:00
|
|
|
(defcustom lua-ts-inferior-history nil
|
|
|
|
"File used to save command history of the inferior Lua process."
|
|
|
|
:type '(choice (const :tag "None" nil) file)
|
|
|
|
:safe 'string-or-null-p
|
2023-09-11 10:03:13 -05:00
|
|
|
:version "30.1")
|
|
|
|
|
2024-01-06 09:36:33 -06:00
|
|
|
(defcustom lua-ts-indent-continuation-lines t
|
|
|
|
"Controls how multi-line if/else statements are aligned.
|
|
|
|
|
2024-06-12 10:41:03 -05:00
|
|
|
If non-nil, then continuation lines are indented by `lua-ts-indent-offset':
|
2024-01-06 09:36:33 -06:00
|
|
|
|
|
|
|
if a
|
|
|
|
and b then
|
|
|
|
print(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
If nil, then continuation lines are aligned with the beginning of
|
|
|
|
the statement:
|
|
|
|
|
|
|
|
if a
|
|
|
|
and b then
|
|
|
|
print(1)
|
|
|
|
end"
|
|
|
|
:type 'boolean
|
|
|
|
:safe 'booleanp
|
|
|
|
:version "30.1")
|
|
|
|
|
2023-09-11 10:03:13 -05:00
|
|
|
(defvar lua-ts--builtins
|
|
|
|
'("assert" "bit32" "collectgarbage" "coroutine" "debug" "dofile"
|
|
|
|
"error" "getmetatable" "io" "ipairs" "load" "loadfile"
|
|
|
|
"math" "next" "os" "package" "pairs" "pcall" "print"
|
|
|
|
"rawequal" "rawget" "rawlen" "rawset" "require" "select"
|
|
|
|
"setmetatable" "string" "table" "tonumber" "tostring"
|
|
|
|
"type" "utf8" "warn" "xpcall" "_G" "_VERSION"
|
|
|
|
;; methods for file handlers
|
|
|
|
"close" "flush" "lines" "read" "seek" "setvbuf" "write")
|
|
|
|
"Lua built-in functions for tree-sitter font-locking.")
|
|
|
|
|
2023-10-04 20:46:15 -05:00
|
|
|
(defvar lua-ts--keywords
|
2023-11-14 16:25:43 -06:00
|
|
|
'("and" "do" "else" "elseif" "end" "for" "function" "goto" "if"
|
|
|
|
"in" "local" "not" "or" "repeat" "return" "then" "until" "while")
|
2023-10-04 20:46:15 -05:00
|
|
|
"Lua keywords for tree-sitter font-locking and navigation.")
|
|
|
|
|
2023-11-14 16:25:43 -06:00
|
|
|
(defun lua-ts--comment-font-lock (node override start end &rest _)
|
|
|
|
"Apply font lock to comment NODE within START and END.
|
|
|
|
Applies `font-lock-comment-delimiter-face' and
|
2023-12-02 15:50:45 +02:00
|
|
|
`font-lock-comment-face'. See `treesit-fontify-with-override' for
|
2023-11-14 16:25:43 -06:00
|
|
|
values of OVERRIDE."
|
|
|
|
(let* ((node-start (treesit-node-start node))
|
|
|
|
(node-end (treesit-node-end node))
|
|
|
|
(node-text (treesit-node-text node t))
|
|
|
|
(delimiter-end (+ 2 node-start)))
|
|
|
|
(when (and (>= node-start start)
|
|
|
|
(<= delimiter-end end)
|
|
|
|
(string-match "\\`--" node-text))
|
|
|
|
(treesit-fontify-with-override node-start
|
|
|
|
delimiter-end
|
Make font-lock face variables obsolete
Using these variables as "another mechanism for changing face
appearance" never really worked, since mode authors were free to use the
face instead of the variable (i.e. quoting the symbol). This means that
when users customized the variables, it would change its appearance only
in some cases, which is a rather surprising interface.
Indeed, it turns out that some modes have not always been consistent,
and mixed using the variable and the face, increasing the confusion even
further. Thus, the only thing users could rely on to always work was to
leave the variables at their default value, and to customize the face.
We now recommend customizing the face, and are obsoleting these
variables. This will lead to some minor code-churn, but it is believed
that it will be ultimately worth it. To fix existing Lisp code, you
should typically only need to quote the symbol, as this commit shows.
It might be the case that we will have to keep the obsolete variables,
that have been with us since Emacs 19, around for a longer time-period
than normal, to allow package authors to adapt.
* lisp/font-lock.el (font-lock-comment-face)
(font-lock-comment-delimiter-face, font-lock-string-face)
(font-lock-doc-face, font-lock-doc-markup-face, font-lock-keyword-face)
(font-lock-builtin-face, font-lock-function-name-face)
(font-lock-variable-name-face, font-lock-type-face)
(font-lock-constant-face, font-lock-warning-face)
(font-lock-negation-char-face, font-lock-preprocessor-face): Make face
variables obsolete in favor of customizing the faces directly
instead. (Bug#71469)
* doc/lispref/modes.texi (Faces for Font Lock): Don't document above
obsolete variables.
* lisp/cedet/data-debug.el (data-debug-insert-hash-table)
(data-debug-insert-widget-properties, data-debug-insert-string)
(data-debug-insert-number):
* lisp/emacs-lisp/lisp-mode.el (elisp--font-lock-backslash)
(lisp-font-lock-syntactic-face-function):
* lisp/emacs-lisp/package.el (describe-package-1):
* lisp/generic-x.el (rc-generic-mode, rul-generic-mode):
* lisp/org/org-table.el (org-table-edit-formulas):
* lisp/progmodes/cc-fonts.el (c-font-lock-invalid-single-quotes)
(c-maybe-font-lock-wrong-style-comments, c-font-lock-declarators)
(c-font-lock-c++-lambda-captures, c-font-lock-c++-modules):
* lisp/progmodes/cperl-mode.el (cperl-find-sub-attrs)
(cperl-look-at-leading-count, cperl-process-here-doc)
(cperl-find-pods-heres):
* lisp/progmodes/gdb-mi.el (gdb-init-1, gdb-update, gdb-running)
(gdb-starting, gdb-stopped, gdb-internals)
(gdb-breakpoints--add-breakpoint-row, gdb-locals-handler-custom)
(gdb-registers-handler-custom, gdb-get-source-file):
* lisp/progmodes/js.el (js--font-lock-keywords-1)
(js--font-lock-keywords-2, js-font-lock-syntactic-face-function):
* lisp/progmodes/lua-ts-mode.el (lua-ts--comment-font-lock):
* lisp/progmodes/meta-mode.el (meta-indent-in-string-p):
* lisp/progmodes/prolog.el (prolog-font-lock-keywords):
* lisp/progmodes/python.el (python-font-lock-syntactic-face-function):
* lisp/progmodes/ruby-ts-mode.el (ruby-ts--comment-font-lock):
* lisp/progmodes/sh-script.el (sh-font-lock-syntactic-face-function):
* lisp/textmodes/tex-mode.el (doctex-font-lock-syntactic-face-function):
* test/lisp/faces-tests.el (faces--test-color-at-point):
* test/lisp/progmodes/cperl-mode-tests.el (cperl-test-extra-delimiters)
(cperl-test-bug-47112, cperl-test-bug-65834, cperl-test-bug-66145)
(cperl-test-bug-66161, cperl-test-bug-69604):
* test/lisp/progmodes/python-tests.el
(python-ts-mode-compound-keywords-face)
(python-ts-mode-named-assignment-face-1)
(python-ts-mode-assignment-face-2, python-ts-mode-nested-types-face-1)
(python-ts-mode-union-types-face-1, python-ts-mode-union-types-face-2)
(python-ts-mode-types-face-1, python-ts-mode-types-face-2)
(python-ts-mode-types-face-3, python-ts-mode-isinstance-type-face-1)
(python-ts-mode-isinstance-type-face-2)
(python-ts-mode-isinstance-type-face-3)
(python-ts-mode-superclass-type-face, python-ts-mode-class-patterns-face)
(python-ts-mode-dotted-decorator-face-1)
(python-ts-mode-dotted-decorator-face-2)
(python-ts-mode-builtin-call-face)
(python-ts-mode-interpolation-nested-string)
(python-ts-mode-level-fontification-wo-interpolation)
(python-ts-mode-disabled-string-interpolation)
(python-ts-mode-interpolation-doc-string):
* test/lisp/progmodes/ruby-mode-tests.el
(ruby-heredoc-font-lock, ruby-heredoc-highlights-interpolations)
(ruby-exit!-font-lock, ruby-regexp-interpolation-is-highlighted)
(ruby-recognize-symbols-starting-with-at-character)
(ruby-hash-character-not-interpolation)
(ruby-interpolation-suppresses-quotes-inside)
(ruby-interpolation-suppresses-one-double-quote)
(ruby-interpolation-inside-percent-literal)
(ruby-interpolation-inside-percent-literal-with-paren)
(ruby-interpolation-inside-another-interpolation)
(ruby-interpolation-inside-double-quoted-percent-literals)
(ruby-no-interpolation-in-single-quoted-literals):
Don't use above obsolete variables.
* lisp/progmodes/cc-fonts.el (font-lock-constant-face): Don't
declare now-unused variable.
2024-12-18 03:46:04 +01:00
|
|
|
'font-lock-comment-delimiter-face
|
2023-11-14 16:25:43 -06:00
|
|
|
override))
|
|
|
|
(treesit-fontify-with-override (max delimiter-end start)
|
|
|
|
(min node-end end)
|
Make font-lock face variables obsolete
Using these variables as "another mechanism for changing face
appearance" never really worked, since mode authors were free to use the
face instead of the variable (i.e. quoting the symbol). This means that
when users customized the variables, it would change its appearance only
in some cases, which is a rather surprising interface.
Indeed, it turns out that some modes have not always been consistent,
and mixed using the variable and the face, increasing the confusion even
further. Thus, the only thing users could rely on to always work was to
leave the variables at their default value, and to customize the face.
We now recommend customizing the face, and are obsoleting these
variables. This will lead to some minor code-churn, but it is believed
that it will be ultimately worth it. To fix existing Lisp code, you
should typically only need to quote the symbol, as this commit shows.
It might be the case that we will have to keep the obsolete variables,
that have been with us since Emacs 19, around for a longer time-period
than normal, to allow package authors to adapt.
* lisp/font-lock.el (font-lock-comment-face)
(font-lock-comment-delimiter-face, font-lock-string-face)
(font-lock-doc-face, font-lock-doc-markup-face, font-lock-keyword-face)
(font-lock-builtin-face, font-lock-function-name-face)
(font-lock-variable-name-face, font-lock-type-face)
(font-lock-constant-face, font-lock-warning-face)
(font-lock-negation-char-face, font-lock-preprocessor-face): Make face
variables obsolete in favor of customizing the faces directly
instead. (Bug#71469)
* doc/lispref/modes.texi (Faces for Font Lock): Don't document above
obsolete variables.
* lisp/cedet/data-debug.el (data-debug-insert-hash-table)
(data-debug-insert-widget-properties, data-debug-insert-string)
(data-debug-insert-number):
* lisp/emacs-lisp/lisp-mode.el (elisp--font-lock-backslash)
(lisp-font-lock-syntactic-face-function):
* lisp/emacs-lisp/package.el (describe-package-1):
* lisp/generic-x.el (rc-generic-mode, rul-generic-mode):
* lisp/org/org-table.el (org-table-edit-formulas):
* lisp/progmodes/cc-fonts.el (c-font-lock-invalid-single-quotes)
(c-maybe-font-lock-wrong-style-comments, c-font-lock-declarators)
(c-font-lock-c++-lambda-captures, c-font-lock-c++-modules):
* lisp/progmodes/cperl-mode.el (cperl-find-sub-attrs)
(cperl-look-at-leading-count, cperl-process-here-doc)
(cperl-find-pods-heres):
* lisp/progmodes/gdb-mi.el (gdb-init-1, gdb-update, gdb-running)
(gdb-starting, gdb-stopped, gdb-internals)
(gdb-breakpoints--add-breakpoint-row, gdb-locals-handler-custom)
(gdb-registers-handler-custom, gdb-get-source-file):
* lisp/progmodes/js.el (js--font-lock-keywords-1)
(js--font-lock-keywords-2, js-font-lock-syntactic-face-function):
* lisp/progmodes/lua-ts-mode.el (lua-ts--comment-font-lock):
* lisp/progmodes/meta-mode.el (meta-indent-in-string-p):
* lisp/progmodes/prolog.el (prolog-font-lock-keywords):
* lisp/progmodes/python.el (python-font-lock-syntactic-face-function):
* lisp/progmodes/ruby-ts-mode.el (ruby-ts--comment-font-lock):
* lisp/progmodes/sh-script.el (sh-font-lock-syntactic-face-function):
* lisp/textmodes/tex-mode.el (doctex-font-lock-syntactic-face-function):
* test/lisp/faces-tests.el (faces--test-color-at-point):
* test/lisp/progmodes/cperl-mode-tests.el (cperl-test-extra-delimiters)
(cperl-test-bug-47112, cperl-test-bug-65834, cperl-test-bug-66145)
(cperl-test-bug-66161, cperl-test-bug-69604):
* test/lisp/progmodes/python-tests.el
(python-ts-mode-compound-keywords-face)
(python-ts-mode-named-assignment-face-1)
(python-ts-mode-assignment-face-2, python-ts-mode-nested-types-face-1)
(python-ts-mode-union-types-face-1, python-ts-mode-union-types-face-2)
(python-ts-mode-types-face-1, python-ts-mode-types-face-2)
(python-ts-mode-types-face-3, python-ts-mode-isinstance-type-face-1)
(python-ts-mode-isinstance-type-face-2)
(python-ts-mode-isinstance-type-face-3)
(python-ts-mode-superclass-type-face, python-ts-mode-class-patterns-face)
(python-ts-mode-dotted-decorator-face-1)
(python-ts-mode-dotted-decorator-face-2)
(python-ts-mode-builtin-call-face)
(python-ts-mode-interpolation-nested-string)
(python-ts-mode-level-fontification-wo-interpolation)
(python-ts-mode-disabled-string-interpolation)
(python-ts-mode-interpolation-doc-string):
* test/lisp/progmodes/ruby-mode-tests.el
(ruby-heredoc-font-lock, ruby-heredoc-highlights-interpolations)
(ruby-exit!-font-lock, ruby-regexp-interpolation-is-highlighted)
(ruby-recognize-symbols-starting-with-at-character)
(ruby-hash-character-not-interpolation)
(ruby-interpolation-suppresses-quotes-inside)
(ruby-interpolation-suppresses-one-double-quote)
(ruby-interpolation-inside-percent-literal)
(ruby-interpolation-inside-percent-literal-with-paren)
(ruby-interpolation-inside-another-interpolation)
(ruby-interpolation-inside-double-quoted-percent-literals)
(ruby-no-interpolation-in-single-quoted-literals):
Don't use above obsolete variables.
* lisp/progmodes/cc-fonts.el (font-lock-constant-face): Don't
declare now-unused variable.
2024-12-18 03:46:04 +01:00
|
|
|
'font-lock-comment-face
|
2023-11-14 16:25:43 -06:00
|
|
|
override)))
|
|
|
|
|
2023-09-11 10:03:13 -05:00
|
|
|
(defvar lua-ts--font-lock-settings
|
|
|
|
(treesit-font-lock-rules
|
2023-11-14 16:25:43 -06:00
|
|
|
:default-language 'lua
|
2023-09-11 10:03:13 -05:00
|
|
|
:feature 'bracket
|
|
|
|
'(["(" ")" "[" "]" "{" "}"] @font-lock-bracket-face)
|
|
|
|
|
|
|
|
:feature 'delimiter
|
|
|
|
'(["," ";"] @font-lock-delimiter-face)
|
|
|
|
|
|
|
|
:feature 'constant
|
2023-11-14 16:25:43 -06:00
|
|
|
'([(variable_list
|
|
|
|
attribute: (attribute (["<" ">"] (identifier))))
|
|
|
|
(label_statement)
|
|
|
|
(true) (false) (nil)]
|
|
|
|
@font-lock-constant-face)
|
2023-09-11 10:03:13 -05:00
|
|
|
|
|
|
|
:feature 'operator
|
2023-11-14 16:25:43 -06:00
|
|
|
'(["+" "-" "*" "/" "%" "^" "#" "==" "~=" "<=" ">="
|
|
|
|
"<" ">" "=" "&" "~" "|" "<<" ">>" "//" ".."
|
|
|
|
(vararg_expression)]
|
|
|
|
@font-lock-operator-face)
|
2023-09-11 10:03:13 -05:00
|
|
|
|
|
|
|
:feature 'builtin
|
|
|
|
`(((identifier) @font-lock-builtin-face
|
|
|
|
(:match ,(regexp-opt lua-ts--builtins 'symbols)
|
|
|
|
@font-lock-builtin-face)))
|
|
|
|
|
|
|
|
:feature 'function
|
|
|
|
'((function_call name: (identifier) @font-lock-function-call-face)
|
|
|
|
(function_call
|
2023-11-14 16:25:43 -06:00
|
|
|
(method_index_expression
|
|
|
|
method: (identifier) @font-lock-function-call-face))
|
2023-09-11 10:03:13 -05:00
|
|
|
(function_call
|
2023-11-14 16:25:43 -06:00
|
|
|
(dot_index_expression
|
|
|
|
field: (identifier) @font-lock-function-call-face)))
|
2023-09-11 10:03:13 -05:00
|
|
|
|
|
|
|
:feature 'punctuation
|
|
|
|
'(["." ":"] @font-lock-punctuation-face)
|
|
|
|
|
|
|
|
:feature 'variable
|
|
|
|
'((function_call
|
2023-11-14 16:25:43 -06:00
|
|
|
(arguments (identifier) @font-lock-variable-use-face))
|
2023-09-11 10:03:13 -05:00
|
|
|
(function_call
|
2023-11-14 16:25:43 -06:00
|
|
|
(arguments
|
|
|
|
(binary_expression (identifier) @font-lock-variable-use-face)))
|
|
|
|
(function_call
|
|
|
|
(arguments
|
|
|
|
(bracket_index_expression (identifier) @font-lock-variable-use-face)))
|
|
|
|
(function_declaration
|
|
|
|
(parameters name: (identifier) @font-lock-variable-name-face)))
|
2023-09-11 10:03:13 -05:00
|
|
|
|
|
|
|
:feature 'number
|
|
|
|
'((number) @font-lock-number-face)
|
|
|
|
|
|
|
|
:feature 'keyword
|
2023-11-14 16:25:43 -06:00
|
|
|
`([(break_statement)
|
|
|
|
,(vconcat lua-ts--keywords)]
|
|
|
|
@font-lock-keyword-face
|
|
|
|
(goto_statement ((identifier) @font-lock-constant-face)))
|
|
|
|
|
2023-09-11 10:03:13 -05:00
|
|
|
:feature 'string
|
|
|
|
'((string) @font-lock-string-face)
|
|
|
|
|
2023-11-12 11:36:09 -08:00
|
|
|
:feature 'escape
|
|
|
|
:override t
|
|
|
|
'((escape_sequence) @font-lock-escape-face)
|
|
|
|
|
2023-09-11 10:03:13 -05:00
|
|
|
:feature 'comment
|
2023-11-14 16:25:43 -06:00
|
|
|
'((comment) @lua-ts--comment-font-lock
|
2023-09-11 10:03:13 -05:00
|
|
|
(hash_bang_line) @font-lock-comment-face)
|
|
|
|
|
|
|
|
:feature 'definition
|
|
|
|
'((function_declaration
|
2023-11-14 16:25:43 -06:00
|
|
|
(identifier) @font-lock-function-name-face)
|
2023-09-22 18:36:18 -05:00
|
|
|
(function_declaration
|
2023-11-14 16:25:43 -06:00
|
|
|
(dot_index_expression
|
|
|
|
field: (identifier) @font-lock-function-name-face))
|
2023-09-22 18:36:18 -05:00
|
|
|
(function_declaration
|
|
|
|
(method_index_expression
|
2023-11-14 16:25:43 -06:00
|
|
|
method: (identifier) @font-lock-function-name-face))
|
|
|
|
(assignment_statement
|
|
|
|
(variable_list
|
|
|
|
(identifier) @font-lock-function-name-face)
|
|
|
|
(expression_list value: (function_definition)))
|
|
|
|
(field
|
|
|
|
name: (identifier) @font-lock-function-name-face
|
|
|
|
value: (function_definition))
|
|
|
|
(assignment_statement
|
|
|
|
(variable_list
|
2023-09-22 18:36:18 -05:00
|
|
|
(dot_index_expression
|
2023-11-14 16:25:43 -06:00
|
|
|
field: (identifier) @font-lock-function-name-face))
|
|
|
|
(expression_list
|
|
|
|
value:
|
|
|
|
(function_definition))))
|
|
|
|
|
|
|
|
:feature 'assignment
|
|
|
|
'((variable_list (identifier) @font-lock-variable-name-face)
|
|
|
|
(variable_list
|
|
|
|
(bracket_index_expression
|
|
|
|
field: (identifier) @font-lock-variable-name-face))
|
|
|
|
(variable_list
|
|
|
|
(dot_index_expression
|
|
|
|
field: (identifier) @font-lock-variable-name-face))
|
2023-09-22 18:36:18 -05:00
|
|
|
(for_numeric_clause name: (identifier) @font-lock-variable-name-face))
|
|
|
|
|
|
|
|
:feature 'property
|
|
|
|
'((field name: (identifier) @font-lock-property-name-face)
|
|
|
|
(dot_index_expression
|
|
|
|
field: (identifier) @font-lock-property-use-face))
|
|
|
|
|
2023-09-11 10:03:13 -05:00
|
|
|
:feature 'error
|
|
|
|
:override t
|
|
|
|
'((ERROR) @font-lock-warning-face))
|
|
|
|
"Tree-sitter font-lock settings for `lua-ts-mode'.")
|
|
|
|
|
|
|
|
(defvar lua-ts--simple-indent-rules
|
|
|
|
`((lua
|
2025-03-09 20:40:58 -05:00
|
|
|
;; Handle multi-line strings and comments.
|
2024-11-10 11:26:33 -06:00
|
|
|
((or (and (node-is "comment") (parent-is "chunk"))
|
|
|
|
lua-ts--multi-line-comment-start
|
2023-10-04 20:46:15 -05:00
|
|
|
(parent-is "comment_content")
|
|
|
|
(parent-is "string_content")
|
2024-12-25 18:03:50 -06:00
|
|
|
(or (node-is "]]") (node-is "comment_end")))
|
2023-10-04 20:46:15 -05:00
|
|
|
no-indent 0)
|
2025-03-09 20:40:58 -05:00
|
|
|
|
|
|
|
;; Handle multiple "end" statements on a single line.
|
|
|
|
((and (node-is "end") lua-ts--end-line-matcher)
|
|
|
|
standalone-parent lua-ts--end-indent-offset)
|
|
|
|
|
|
|
|
;; Handle tables in the arguments of a function call.
|
2024-05-03 15:51:01 -05:00
|
|
|
((and (n-p-gp "field" "table_constructor" "arguments")
|
|
|
|
lua-ts--multi-arg-function-call-matcher
|
|
|
|
lua-ts--last-arg-function-call-matcher)
|
|
|
|
standalone-parent lua-ts-indent-offset)
|
|
|
|
((and (n-p-gp "}" "table_constructor" "arguments")
|
|
|
|
lua-ts--multi-arg-function-call-matcher
|
|
|
|
lua-ts--last-arg-function-call-matcher)
|
|
|
|
standalone-parent 0)
|
2025-03-09 20:40:58 -05:00
|
|
|
((and (match "field" "table_constructor" nil 1 1)
|
2023-10-04 20:46:15 -05:00
|
|
|
lua-ts--multi-arg-function-call-matcher)
|
|
|
|
parent lua-ts-indent-offset)
|
|
|
|
((and (n-p-gp "}" "table_constructor" "arguments")
|
|
|
|
lua-ts--multi-arg-function-call-matcher)
|
|
|
|
parent 0)
|
2025-03-09 20:40:58 -05:00
|
|
|
|
|
|
|
;; Handle multi-line concatenation and continuation.
|
|
|
|
((or (n-p-gp "expression_list" "assignment_statement" "variable_declaration")
|
|
|
|
(and (parent-is "binary_expression")
|
|
|
|
lua-ts--variable-declaration-continuation))
|
2024-01-06 09:36:33 -06:00
|
|
|
lua-ts--variable-declaration-continuation-anchor
|
|
|
|
lua-ts-indent-offset)
|
2025-03-09 20:40:58 -05:00
|
|
|
;; `lua-ts-indent-continuation-lines' is non-nil.
|
2024-01-06 09:36:33 -06:00
|
|
|
((and (lambda (&rest _) lua-ts-indent-continuation-lines)
|
|
|
|
(parent-is "binary_expression"))
|
|
|
|
standalone-parent lua-ts-indent-offset)
|
2025-03-09 20:40:58 -05:00
|
|
|
;; `lua-ts-indent-continuation-lines' is nil.
|
2024-01-06 09:36:33 -06:00
|
|
|
((parent-is "binary_expression") standalone-parent 0)
|
2025-03-09 20:40:58 -05:00
|
|
|
|
|
|
|
;; Handle immediately invoked function expressions.
|
|
|
|
((or (n-p-gp "block" "function_definition" "parenthesized_expression")
|
|
|
|
(n-p-gp "block" "function_definition" "arguments"))
|
|
|
|
parent lua-ts-indent-offset)
|
|
|
|
((or (n-p-gp "end" "function_definition" "parenthesized_expression")
|
|
|
|
(n-p-gp "end" "function_definition" "arguments"))
|
|
|
|
parent 0)
|
|
|
|
|
|
|
|
;; Handle basic indentation.
|
|
|
|
((or (node-is "do")
|
|
|
|
(node-is "then")
|
|
|
|
(node-is "elseif_statement")
|
|
|
|
(node-is "else_statement")
|
|
|
|
(node-is "until")
|
|
|
|
(node-is "end")
|
|
|
|
(node-is ")"))
|
|
|
|
standalone-parent 0)
|
|
|
|
|
2023-10-04 20:46:15 -05:00
|
|
|
((or (parent-is "function_declaration")
|
|
|
|
(parent-is "function_definition")
|
|
|
|
(parent-is "do_statement")
|
|
|
|
(parent-is "for_statement")
|
|
|
|
(parent-is "repeat_statement")
|
|
|
|
(parent-is "while_statement")
|
|
|
|
(parent-is "if_statement")
|
|
|
|
(parent-is "else_statement")
|
2025-03-09 20:40:58 -05:00
|
|
|
(parent-is "elseif_statement")
|
|
|
|
;; `c-ts-common-baseline-indent-rule' will handle further
|
|
|
|
;; siblings after the first one has been properly indented.
|
|
|
|
;; The opening bracket occupies index 0.
|
|
|
|
(match nil "arguments" nil 1 1)
|
|
|
|
(match nil "parameters" nil 1 1)
|
|
|
|
(match "field" "table_constructor" nil 1 1))
|
2023-10-04 20:46:15 -05:00
|
|
|
standalone-parent lua-ts-indent-offset)
|
2025-03-09 20:40:58 -05:00
|
|
|
|
|
|
|
((parent-is "block") parent 0)
|
2023-09-11 10:03:13 -05:00
|
|
|
((parent-is "chunk") column-0 0)
|
2025-03-09 20:40:58 -05:00
|
|
|
((parent-is "ERROR") no-indent 0)
|
|
|
|
c-ts-common-baseline-indent-rule)))
|
2023-09-11 10:03:13 -05:00
|
|
|
|
2023-10-04 20:46:15 -05:00
|
|
|
(defun lua-ts--end-line-matcher (&rest _)
|
|
|
|
"Matches if there is more than one `end' on the current line."
|
|
|
|
(> (lua-ts--end-count) 1))
|
|
|
|
|
|
|
|
(defun lua-ts--end-indent-offset (&rest _)
|
|
|
|
"Calculate indent offset based on `end' count."
|
|
|
|
(- (* (1- (lua-ts--end-count)) lua-ts-indent-offset)))
|
|
|
|
|
|
|
|
(defun lua-ts--end-count ()
|
|
|
|
"Count the number of `end's on the current line."
|
|
|
|
(count-matches "end" (line-beginning-position) (line-end-position)))
|
|
|
|
|
2024-05-03 15:51:01 -05:00
|
|
|
(defun lua-ts--g-parent (node)
|
|
|
|
"Return the grand-parent of NODE."
|
|
|
|
(let ((parent (treesit-node-parent node)))
|
|
|
|
(treesit-node-parent parent)))
|
|
|
|
|
2023-10-04 20:46:15 -05:00
|
|
|
(defun lua-ts--multi-arg-function-call-matcher (_n parent &rest _)
|
|
|
|
"Matches if PARENT has multiple arguments."
|
|
|
|
(> (treesit-node-child-count (treesit-node-parent parent)) 3))
|
|
|
|
|
2024-05-03 15:51:01 -05:00
|
|
|
(defun lua-ts--last-arg-function-call-matcher (node parent &rest _)
|
|
|
|
"Matches if NODE's PARENT is the last argument in a function call."
|
|
|
|
(let* ((g-parent (lua-ts--g-parent node))
|
|
|
|
(last (1- (treesit-node-child-count g-parent t))))
|
|
|
|
(treesit-node-eq parent (seq-elt (treesit-node-children g-parent t) last))))
|
|
|
|
|
2024-01-06 09:36:33 -06:00
|
|
|
(defun lua-ts--variable-declaration-continuation (node &rest _)
|
|
|
|
"Matches if NODE is part of a multi-line variable declaration."
|
2025-03-09 20:40:58 -05:00
|
|
|
(treesit-parent-until node (lambda (p)
|
|
|
|
(equal "variable_declaration"
|
|
|
|
(treesit-node-type p)))))
|
2024-01-06 09:36:33 -06:00
|
|
|
|
|
|
|
(defun lua-ts--variable-declaration-continuation-anchor (node &rest _)
|
|
|
|
"Return the start position of the variable declaration for NODE."
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (treesit-node-start
|
|
|
|
(lua-ts--variable-declaration-continuation node)))
|
2025-03-09 20:40:58 -05:00
|
|
|
(when (looking-back (rx bol (* whitespace)) (line-beginning-position))
|
2024-01-06 09:36:33 -06:00
|
|
|
(point))))
|
|
|
|
|
2024-11-10 11:26:33 -06:00
|
|
|
(defun lua-ts--multi-line-comment-start (node &rest _)
|
|
|
|
"Matches if NODE is the beginning of a multi-line comment."
|
|
|
|
(and node
|
|
|
|
(equal "comment" (treesit-node-type node))
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (treesit-node-start node))
|
|
|
|
(forward-char 2) ; Skip the -- part.
|
|
|
|
(looking-at "\\[\\["))))
|
|
|
|
|
2023-09-11 10:03:13 -05:00
|
|
|
(defvar lua-ts--syntax-table
|
|
|
|
(let ((table (make-syntax-table)))
|
|
|
|
(modify-syntax-entry ?+ "." table)
|
|
|
|
(modify-syntax-entry ?- ". 12" table)
|
|
|
|
(modify-syntax-entry ?= "." table)
|
|
|
|
(modify-syntax-entry ?% "." table)
|
|
|
|
(modify-syntax-entry ?^ "." table)
|
|
|
|
(modify-syntax-entry ?~ "." table)
|
|
|
|
(modify-syntax-entry ?< "." table)
|
|
|
|
(modify-syntax-entry ?> "." table)
|
|
|
|
(modify-syntax-entry ?/ "." table)
|
|
|
|
(modify-syntax-entry ?* "." table)
|
|
|
|
(modify-syntax-entry ?\n ">" table)
|
|
|
|
(modify-syntax-entry ?\' "\"" table)
|
|
|
|
(modify-syntax-entry ?\" "\"" table)
|
|
|
|
table)
|
|
|
|
"Syntax table for `lua-ts-mode'.")
|
|
|
|
|
|
|
|
(defun lua-ts--defun-name-function (node)
|
|
|
|
"Return the defun name of NODE.
|
|
|
|
Return nil if there is no name or if NODE is not a defun node."
|
|
|
|
(let ((child (treesit-node-child-by-field-name node "name")))
|
|
|
|
(pcase (treesit-node-type node)
|
|
|
|
((or "function_declaration" "function_definition")
|
|
|
|
(treesit-node-text child t))
|
|
|
|
("variable_declaration"
|
|
|
|
(if child
|
|
|
|
(treesit-node-text child t)
|
|
|
|
(treesit-node-text
|
|
|
|
(treesit-node-child-by-field-name
|
|
|
|
(treesit-search-subtree node "assignment_statement" nil nil 1)
|
|
|
|
"name"))))
|
|
|
|
("field"
|
|
|
|
(and (treesit-search-subtree node "function_definition" nil nil 1)
|
|
|
|
(treesit-node-text child t))))))
|
|
|
|
|
2023-10-10 14:39:30 -05:00
|
|
|
(defun lua-ts--named-function-p (node)
|
|
|
|
"Matches if NODE is a named function."
|
|
|
|
(let ((type (treesit-node-type node)))
|
|
|
|
(or (equal "function_declaration" type)
|
|
|
|
(and (equal "field" type)
|
|
|
|
(equal "function_definition"
|
|
|
|
(treesit-node-type
|
|
|
|
(treesit-node-child-by-field-name
|
|
|
|
node "value")))
|
|
|
|
(treesit-node-child-by-field-name node "name")))))
|
|
|
|
|
|
|
|
(defun lua-ts--require-name-function (node)
|
|
|
|
"Return name of NODE to use for requires in imenu."
|
|
|
|
(when-let* (((lua-ts--require-p node))
|
|
|
|
(parent (treesit-node-parent node))
|
|
|
|
(parent-type (treesit-node-type parent)))
|
|
|
|
(if (equal "expression_list" parent-type)
|
|
|
|
(let* ((g-parent (treesit-node-parent parent))
|
|
|
|
(name (treesit-node-child-by-field-name g-parent "name")))
|
|
|
|
(treesit-node-text name t))
|
|
|
|
(treesit-node-text (treesit-search-subtree node "string_content") t))))
|
|
|
|
|
|
|
|
(defun lua-ts--require-p (node)
|
|
|
|
"Matches if NODE is a require statement."
|
|
|
|
(let ((name (treesit-node-child-by-field-name node "name")))
|
|
|
|
(equal "require" (treesit-node-text name t))))
|
|
|
|
|
2023-09-11 10:03:13 -05:00
|
|
|
(defvar-local lua-ts--flymake-process nil)
|
|
|
|
|
|
|
|
(defun lua-ts-flymake-luacheck (report-fn &rest _args)
|
|
|
|
"Luacheck backend for Flymake.
|
|
|
|
Calls REPORT-FN directly."
|
|
|
|
(when (process-live-p lua-ts--flymake-process)
|
|
|
|
(kill-process lua-ts--flymake-process))
|
|
|
|
(let ((source (current-buffer)))
|
|
|
|
(save-restriction
|
|
|
|
(widen)
|
|
|
|
(setq lua-ts--flymake-process
|
|
|
|
(make-process
|
|
|
|
:name "lua-ts-flymake-luacheck"
|
|
|
|
:noquery t
|
|
|
|
:connection-type 'pipe
|
|
|
|
:buffer (generate-new-buffer " *lua-ts-flymake-luacheck*")
|
|
|
|
:command `(,lua-ts-luacheck-program
|
|
|
|
"--codes" "--ranges" "--formatter" "plain" "-")
|
|
|
|
:sentinel
|
|
|
|
(lambda (proc _event)
|
|
|
|
(when (eq 'exit (process-status proc))
|
|
|
|
(unwind-protect
|
|
|
|
(if (with-current-buffer source
|
|
|
|
(eq proc lua-ts--flymake-process))
|
|
|
|
(with-current-buffer (process-buffer proc)
|
|
|
|
(goto-char (point-min))
|
2024-03-13 08:35:08 -05:00
|
|
|
(let (diags)
|
|
|
|
(while (search-forward-regexp
|
|
|
|
(rx bol (0+ alnum) ":"
|
|
|
|
(group (1+ digit)) ":"
|
|
|
|
(group (1+ digit)) "-"
|
|
|
|
(group (1+ digit)) ": "
|
|
|
|
(group (0+ nonl)) eol)
|
|
|
|
nil t)
|
|
|
|
(let* ((beg
|
|
|
|
(car (flymake-diag-region
|
|
|
|
source
|
|
|
|
(string-to-number (match-string 1))
|
|
|
|
(string-to-number (match-string 2)))))
|
|
|
|
(end
|
|
|
|
(cdr (flymake-diag-region
|
|
|
|
source
|
|
|
|
(string-to-number (match-string 1))
|
|
|
|
(string-to-number (match-string 3)))))
|
|
|
|
(msg (match-string 4))
|
|
|
|
(type (if (string-prefix-p "(W" msg)
|
|
|
|
:warning
|
|
|
|
:error)))
|
|
|
|
(push (flymake-make-diagnostic
|
|
|
|
source beg end type msg)
|
|
|
|
diags)))
|
|
|
|
(funcall report-fn diags)))
|
2023-09-11 10:03:13 -05:00
|
|
|
(flymake-log :warning "Canceling obsolete check %s" proc))
|
|
|
|
(kill-buffer (process-buffer proc)))))))
|
|
|
|
(process-send-region lua-ts--flymake-process (point-min) (point-max))
|
|
|
|
(process-send-eof lua-ts--flymake-process))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun lua-ts-inferior-lua ()
|
|
|
|
"Run a Lua interpreter in an inferior process."
|
|
|
|
(interactive)
|
2024-11-09 11:01:45 -06:00
|
|
|
(if (not lua-ts-inferior-program)
|
|
|
|
(user-error "You must set `lua-ts-inferior-program' to use this command")
|
|
|
|
(unless (comint-check-proc lua-ts-inferior-buffer)
|
|
|
|
(apply #'make-comint-in-buffer
|
|
|
|
(string-replace "*" "" lua-ts-inferior-buffer)
|
|
|
|
lua-ts-inferior-buffer
|
|
|
|
lua-ts-inferior-program
|
|
|
|
lua-ts-inferior-startfile
|
|
|
|
lua-ts-inferior-options)
|
|
|
|
(when lua-ts-inferior-history
|
2023-10-04 20:46:15 -05:00
|
|
|
(set-process-sentinel (get-buffer-process lua-ts-inferior-buffer)
|
|
|
|
'lua-ts-inferior--write-history))
|
2024-11-09 11:01:45 -06:00
|
|
|
(with-current-buffer lua-ts-inferior-buffer
|
|
|
|
(setq-local comint-input-ignoredups t
|
|
|
|
comint-input-ring-file-name lua-ts-inferior-history
|
|
|
|
comint-prompt-read-only t
|
2024-11-23 14:08:21 -06:00
|
|
|
comint-prompt-regexp (rx bol
|
|
|
|
(literal lua-ts-inferior-prompt)
|
|
|
|
(1+ space)))
|
2024-11-09 11:01:45 -06:00
|
|
|
(comint-read-input-ring t)
|
|
|
|
(add-hook 'comint-preoutput-filter-functions
|
|
|
|
(lambda (string)
|
|
|
|
(if (equal string (concat lua-ts-inferior-prompt-continue " "))
|
|
|
|
string
|
|
|
|
(concat
|
|
|
|
;; Filter out the extra prompt characters that
|
|
|
|
;; accumulate in the output when sending regions
|
|
|
|
;; to the inferior process.
|
|
|
|
(replace-regexp-in-string
|
2024-11-23 14:08:21 -06:00
|
|
|
(rx bol
|
|
|
|
(* (literal lua-ts-inferior-prompt)
|
|
|
|
(? (literal lua-ts-inferior-prompt))
|
|
|
|
(1+ space))
|
|
|
|
(group (* nonl)))
|
2024-11-09 11:01:45 -06:00
|
|
|
"\\1" string)
|
|
|
|
;; Re-add the prompt for the next line.
|
|
|
|
lua-ts-inferior-prompt " ")))
|
|
|
|
nil t)))
|
|
|
|
(select-window (display-buffer lua-ts-inferior-buffer
|
|
|
|
'((display-buffer-reuse-window
|
|
|
|
display-buffer-pop-up-window)
|
|
|
|
(reusable-frames . t))))
|
|
|
|
(get-buffer-process (current-buffer))))
|
2023-10-04 20:46:15 -05:00
|
|
|
|
|
|
|
(defun lua-ts-send-buffer ()
|
|
|
|
"Send current buffer to the inferior Lua process."
|
2024-07-13 18:36:38 -05:00
|
|
|
(interactive nil lua-ts-mode)
|
2023-10-04 20:46:15 -05:00
|
|
|
(lua-ts-send-region (point-min) (point-max)))
|
|
|
|
|
|
|
|
(defun lua-ts-send-file (file)
|
|
|
|
"Send contents of FILE to the inferior Lua process."
|
2024-12-02 09:20:01 -06:00
|
|
|
(interactive "f")
|
2023-10-04 20:46:15 -05:00
|
|
|
(with-temp-buffer
|
|
|
|
(insert-file-contents-literally file)
|
|
|
|
(lua-ts-send-region (point-min) (point-max))))
|
|
|
|
|
|
|
|
(defun lua-ts-send-region (beg end)
|
|
|
|
"Send region between BEG and END to the inferior Lua process."
|
2024-07-13 18:36:38 -05:00
|
|
|
(interactive "r" lua-ts-mode)
|
2023-10-04 20:46:15 -05:00
|
|
|
(let ((string (buffer-substring-no-properties beg end))
|
|
|
|
(proc-buffer (lua-ts-inferior-lua)))
|
|
|
|
(comint-send-string proc-buffer "print()") ; Prevent output from
|
|
|
|
(comint-send-string proc-buffer "\n") ; appearing at prompt.
|
|
|
|
(comint-send-string proc-buffer string)
|
|
|
|
(comint-send-string proc-buffer "\n")))
|
|
|
|
|
|
|
|
(defun lua-ts-show-process-buffer ()
|
|
|
|
"Show the inferior Lua process buffer."
|
|
|
|
(interactive)
|
|
|
|
(display-buffer lua-ts-inferior-buffer))
|
|
|
|
|
|
|
|
(defun lua-ts-hide-process-buffer ()
|
|
|
|
"Hide the inferior Lua process buffer."
|
|
|
|
(interactive)
|
|
|
|
(delete-windows-on lua-ts-inferior-buffer))
|
|
|
|
|
|
|
|
(defun lua-ts-kill-process ()
|
|
|
|
"Kill the inferior Lua process."
|
|
|
|
(interactive)
|
|
|
|
(with-current-buffer lua-ts-inferior-buffer
|
|
|
|
(kill-buffer-and-window)))
|
|
|
|
|
|
|
|
(defun lua-ts-inferior--write-history (process _)
|
|
|
|
"Write history file for inferior Lua PROCESS."
|
|
|
|
;; Depending on how the process is killed the buffer may not be
|
|
|
|
;; around anymore; e.g. `kill-buffer'.
|
|
|
|
(when-let* ((buffer (process-buffer process))
|
|
|
|
((buffer-live-p (process-buffer process))))
|
|
|
|
(with-current-buffer buffer (comint-write-input-ring))))
|
|
|
|
|
2024-07-08 15:54:02 -05:00
|
|
|
(defvar-keymap lua-ts-mode-map
|
|
|
|
:doc "Keymap for `lua-ts-mode' buffers."
|
|
|
|
"C-c C-n" #'lua-ts-inferior-lua
|
|
|
|
"C-c C-c" #'lua-ts-send-buffer
|
|
|
|
"C-c C-l" #'lua-ts-send-file
|
|
|
|
"C-c C-r" #'lua-ts-send-region)
|
2023-10-04 20:46:15 -05:00
|
|
|
|
|
|
|
(easy-menu-define lua-ts-mode-menu lua-ts-mode-map
|
|
|
|
"Menu bar entry for `lua-ts-mode'."
|
|
|
|
`("Lua"
|
|
|
|
["Evaluate Buffer" lua-ts-send-buffer]
|
|
|
|
["Evaluate File" lua-ts-send-file]
|
|
|
|
["Evaluate Region" lua-ts-send-region]
|
|
|
|
"--"
|
|
|
|
["Start Process" lua-ts-inferior-lua]
|
|
|
|
["Show Process Buffer" lua-ts-show-process-buffer]
|
|
|
|
["Hide Process Buffer" lua-ts-hide-process-buffer]
|
|
|
|
["Kill Process" lua-ts-kill-process]
|
|
|
|
"--"
|
|
|
|
["Customize" (lambda () (interactive) (customize-group "lua-ts"))]))
|
2023-09-11 10:03:13 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(define-derived-mode lua-ts-mode prog-mode "Lua"
|
2023-10-04 20:46:15 -05:00
|
|
|
"Major mode for editing Lua files, powered by tree-sitter.
|
|
|
|
|
|
|
|
\\{lua-ts-mode-map}"
|
2023-09-11 10:03:13 -05:00
|
|
|
:syntax-table lua-ts--syntax-table
|
2023-10-04 20:46:15 -05:00
|
|
|
(use-local-map lua-ts-mode-map)
|
2023-09-11 10:03:13 -05:00
|
|
|
|
2025-04-18 19:22:50 +03:00
|
|
|
(when (treesit-ensure-installed 'lua)
|
2024-10-07 17:24:32 -07:00
|
|
|
(setq treesit-primary-parser (treesit-parser-create 'lua))
|
2023-09-11 10:03:13 -05:00
|
|
|
|
|
|
|
;; Comments.
|
|
|
|
(setq-local comment-start "--")
|
|
|
|
(setq-local comment-start-skip "--\\s-*")
|
|
|
|
(setq-local comment-end "")
|
|
|
|
|
|
|
|
;; Font-lock.
|
|
|
|
(setq-local treesit-font-lock-settings lua-ts--font-lock-settings)
|
|
|
|
(setq-local treesit-font-lock-feature-list
|
|
|
|
'((comment definition)
|
2023-11-14 16:25:43 -06:00
|
|
|
(keyword string)
|
2023-09-11 10:03:13 -05:00
|
|
|
(assignment builtin constant number)
|
|
|
|
(bracket
|
|
|
|
delimiter
|
|
|
|
escape
|
|
|
|
function
|
|
|
|
operator
|
2023-11-14 16:25:43 -06:00
|
|
|
property
|
2023-09-11 10:03:13 -05:00
|
|
|
punctuation
|
|
|
|
variable)))
|
|
|
|
|
|
|
|
;; Indent.
|
2025-03-09 20:40:58 -05:00
|
|
|
(setq-local c-ts-common-indent-offset 'lua-ts-indent-offset)
|
|
|
|
(setq-local c-ts-common-list-indent-style 'simple)
|
2023-09-11 10:03:13 -05:00
|
|
|
(setq-local treesit-simple-indent-rules lua-ts--simple-indent-rules)
|
|
|
|
|
|
|
|
;; Navigation.
|
|
|
|
(setq-local treesit-defun-name-function #'lua-ts--defun-name-function)
|
|
|
|
(setq-local treesit-defun-type-regexp
|
|
|
|
(rx (or "function_declaration" "function_definition")))
|
|
|
|
(setq-local treesit-thing-settings
|
|
|
|
`((lua
|
2025-02-26 10:31:24 -06:00
|
|
|
(function (or "function_declaration"
|
|
|
|
"function_definition"))
|
2025-02-24 15:21:38 -06:00
|
|
|
(keyword ,(regexp-opt lua-ts--keywords 'symbols))
|
2025-02-26 10:31:24 -06:00
|
|
|
(loop-statement (or "do_statement"
|
|
|
|
"for_statement"
|
|
|
|
"repeat_statement"
|
|
|
|
"while_statement"))
|
2023-10-04 20:46:15 -05:00
|
|
|
(sentence (or function
|
|
|
|
loop-statement
|
2025-02-26 10:31:24 -06:00
|
|
|
comment
|
|
|
|
"assignment_statement"
|
|
|
|
"field"
|
|
|
|
"function_call"
|
|
|
|
"if_statement"
|
|
|
|
"return_statement"
|
|
|
|
"variable_declaration"))
|
2023-10-04 20:46:15 -05:00
|
|
|
(sexp (or function
|
|
|
|
keyword
|
|
|
|
loop-statement
|
2025-02-26 10:31:24 -06:00
|
|
|
"arguments"
|
|
|
|
"parameters"
|
|
|
|
"parenthesized_expression"
|
|
|
|
"string"
|
|
|
|
"table_constructor"))
|
|
|
|
(list (or function
|
|
|
|
loop-statement
|
|
|
|
"arguments"
|
|
|
|
"parameters"
|
|
|
|
"table_constructor"
|
|
|
|
"parenthesized_expression"
|
|
|
|
,(rx bos "if_statement" eos)))
|
|
|
|
(text (or comment "string"))
|
|
|
|
(comment ,(rx bos "comment" eos)))))
|
2023-09-11 10:03:13 -05:00
|
|
|
|
2024-04-20 21:19:27 -05:00
|
|
|
;; Imenu/Outline/Which-function.
|
2023-09-11 10:03:13 -05:00
|
|
|
(setq-local treesit-simple-imenu-settings
|
2023-10-10 14:39:30 -05:00
|
|
|
`(("Requires"
|
|
|
|
"\\`function_call\\'"
|
|
|
|
lua-ts--require-p
|
|
|
|
lua-ts--require-name-function)
|
|
|
|
("Variables" "\\`variable_declaration\\'" nil nil)
|
|
|
|
(nil
|
|
|
|
"\\`\\(?:f\\(?:ield\\|unction_declaration\\)\\)\\'"
|
|
|
|
lua-ts--named-function-p
|
|
|
|
nil)))
|
2023-09-11 10:03:13 -05:00
|
|
|
|
2023-10-10 09:18:10 -05:00
|
|
|
;; Align.
|
|
|
|
(setq-local align-indent-before-aligning t)
|
|
|
|
|
2023-09-11 10:03:13 -05:00
|
|
|
(treesit-major-mode-setup))
|
|
|
|
|
|
|
|
(add-hook 'flymake-diagnostic-functions #'lua-ts-flymake-luacheck nil 'local))
|
|
|
|
|
2024-03-08 12:58:11 -05:00
|
|
|
(derived-mode-add-parents 'lua-ts-mode '(lua-mode))
|
|
|
|
|
2023-09-11 10:03:13 -05:00
|
|
|
(when (treesit-ready-p 'lua)
|
2024-12-18 10:59:19 -06:00
|
|
|
(add-to-list 'auto-mode-alist '("\\.lua\\'" . lua-ts-mode))
|
|
|
|
(add-to-list 'interpreter-mode-alist '("\\<lua\\(?:jit\\)?" . lua-ts-mode)))
|
2023-09-11 10:03:13 -05:00
|
|
|
|
|
|
|
(provide 'lua-ts-mode)
|
|
|
|
|
|
|
|
;;; lua-ts-mode.el ends here
|