Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add nix-mode #1587

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions extensions/nix-mode/indent.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(defpackage :lem-nix-mode/indent
(:use :cl
:lem
:lem/language-mode)
(:export :beginning-of-defun
:end-of-defun
:calc-indent))
(in-package :lem-nix-mode/indent)

(defun beginning-of-defun (point n)
(loop :repeat n :do (search-backward-regexp point "^\\{")))

(defun end-of-defun (point n)
(if (minusp n)
(beginning-of-defun point (- n))
(search-forward-regexp point "^\\}")))

(defun calc-indent (point)
(with-point ((point point))
(let ((tab-width (variable-value 'tab-width :default point))
(column (point-column point)))
(+ column (- tab-width (rem column tab-width))))))
5 changes: 5 additions & 0 deletions extensions/nix-mode/lem-nix-mode.asd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(defsystem "lem-nix-mode"
:depends-on ("lem")
:serial t
:components ((:file "indent")
(:file "nix-mode")))
59 changes: 59 additions & 0 deletions extensions/nix-mode/nix-mode.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
(defpackage :lem-nix-mode
(:use :cl
:lem
:lem/language-mode
:lem/language-mode-tools)
(:local-nicknames (:indent :lem-nix-mode/indent))
(:export :nix-mode))
(in-package :lem-nix-mode)

(defun tokens (boundary strings)
(let ((alternation
`(:alternation ,@(sort (copy-list strings) #'> :key #'length))))
(if boundary
`(:sequence ,boundary ,alternation ,boundary)
alternation)))

(defun make-tmlanguage-nix ()
(let ((patterns (make-tm-patterns
(make-tm-region '(:sequence "#") "$" :name 'syntax-comment-attribute)
(make-tm-region '(:sequence "''")
'(:sequence "''")
:name 'syntax-string-attribute)
(make-tm-match (tokens :word-boundary
'("true"
"false"
"null"))
:name 'syntax-keyword-attribute)
(make-tm-string-region "''")
(make-tm-string-region "\""))))
(make-tmlanguage :patterns patterns)))

(defvar *syntax-table*
(let ((table (make-syntax-table
:space-chars '(#\space #\tab #\newline)
:symbol-chars '(#\_)
:paren-pairs '((#\( . #\))
(#\{ . #\})
(#\[ . #\]))
:string-quote-chars '(#\")
:block-string-pairs '(("''" . "''"))
:line-comment-string "#")))
(set-syntax-parser table (make-tmlanguage-nix))
table))

(define-major-mode nix-mode language-mode
(:name "Nix"
:keymap *nix-mode-keymap*
:syntax-table *syntax-table*
:mode-hook *nix-mode-hook*)
(setf (variable-value 'enable-syntax-highlight) t
(variable-value 'indent-tabs-mode) nil
(variable-value 'tab-width) 2
(variable-value 'calc-indent-function) 'indent:calc-indent
(variable-value 'line-comment) "#"
(variable-value 'beginning-of-defun-function) 'indent:beginning-of-defun
(variable-value 'end-of-defun-function) 'indent:end-of-defun
))

(define-file-type ("nix") nix-mode)
1 change: 1 addition & 0 deletions lem.asd
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@
"lem-documentation-mode"
"lem-elisp-mode"
"lem-terraform-mode"
"lem-nix-mode"
"lem-markdown-mode"
"lem-color-preview"
"lem-lua-mode"
Expand Down
Loading