Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: NixOS/nix-mode
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 8721f63650b4
Choose a base ref
...
head repository: NixOS/nix-mode
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 29a93838bbec
Choose a head ref
  • 6 commits
  • 5 files changed
  • 1 contributor

Commits on Jun 14, 2019

  1. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    dasJ Janne Heß
    Copy the full SHA
    1d31733 View commit details
  2. Copy the full SHA
    5fcdd66 View commit details
  3. Copy the full SHA
    9d1d025 View commit details
  4. Copy the full SHA
    8812eec View commit details
  5. Run tests for nix-indent-line and smie-indent-line

    This gives us more coverage of both modes
    matthewbauer committed Jun 14, 2019
    Copy the full SHA
    be06d67 View commit details
  6. Add hello.nix to tests

    matthewbauer committed Jun 14, 2019
    Copy the full SHA
    29a9383 View commit details
Showing with 113 additions and 29 deletions.
  1. +1 −1 Makefile
  2. +0 −1 default.nix
  3. +18 −11 nix-mode.el
  4. +68 −16 tests/nix-mode-tests.el
  5. +26 −0 tests/testcases/hello.nix
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ PREFIX = /usr

all: $(ELCS) $(DOCS)

test:
check:
emacs -batch -L . \
-l tests/nix-mode-tests.el \
-l tests/nix-font-lock-tests.el \
1 change: 0 additions & 1 deletion default.nix
Original file line number Diff line number Diff line change
@@ -16,5 +16,4 @@ in stdenvNoCC.mkDerivation {
echo Run make run to get vanilla emacs with nix-mode loaded.
'';
doCheck = true;
checkTarget = "test";
}
29 changes: 18 additions & 11 deletions nix-mode.el
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@
"Nix mode customizations"
:group 'nix)

(defcustom nix-indent-function 'indent-relative
(defcustom nix-indent-function 'smie-indent-line
"The function to use to indent.
Valid functions for this are:
@@ -421,6 +421,7 @@ STRING-TYPE type of string based off of Emacs syntax table types"
;; left-hand side of the first assignment on that line.
;; - Otherwise, it is the position of the first token on that line.
(defun nix-smie-rules (kind token)
"Core smie rules."
(pcase (cons kind token)
(`(:after . ,(guard (string-match-p nix-smie-indent-tokens-re
token)))
@@ -491,13 +492,14 @@ STRING-TYPE type of string based off of Emacs syntax table types"
anchor))

(defun nix-smie--indent-anchor (&optional indent)
;; Intended for use only in the rules function.
"Intended for use only in the rules function."
(let ((indent (or indent tab-width)))
`(column . ,(+ indent (nix-smie--anchor)))))
`(column . ,(+ indent (nix-smie--anchor)))))

(defconst nix-smie--path-chars "a-zA-Z0-9-+_.:/~")

(defun nix-smie--skip-path (how)
"Skip path related characters."
(let ((start (point)))
(pcase how
('forward (skip-chars-forward nix-smie--path-chars))
@@ -509,6 +511,7 @@ STRING-TYPE type of string based off of Emacs syntax table types"
(ignore (goto-char start))))))

(defun nix-smie--forward-token-1 ()
"Move forward one token."
(forward-comment (point-max))
(or (nix-smie--skip-path 'forward)
(buffer-substring-no-properties
@@ -520,6 +523,7 @@ STRING-TYPE type of string based off of Emacs syntax table types"
(point)))))

(defun nix-smie--forward-token ()
"Move forward one token, skipping certain characters."
(let ((sym (nix-smie--forward-token-1)))
(if (member sym '(";" "?"))
;; The important lexer for indentation's performance is the backward
@@ -528,6 +532,7 @@ STRING-TYPE type of string based off of Emacs syntax table types"
sym)))

(defun nix-smie--backward-token-1 ()
"Move backward one token."
(forward-comment (- (point)))
(or (nix-smie--skip-path 'backward)
(buffer-substring-no-properties
@@ -539,6 +544,7 @@ STRING-TYPE type of string based off of Emacs syntax table types"
(point)))))

(defun nix-smie--backward-token ()
"Move backward one token, skipping certain characters."
(let ((sym (nix-smie--backward-token-1)))
(unless (zerop (length sym))
(pcase sym
@@ -563,7 +569,7 @@ STRING-TYPE type of string based off of Emacs syntax table types"
'("{" ",")))

(defun nix-smie--indent-close ()
;; Align close paren with opening paren.
"Align close paren with opening paren."
(save-excursion
(when (looking-at "\\s)")
(forward-char 1)
@@ -576,9 +582,9 @@ STRING-TYPE type of string based off of Emacs syntax table types"
(scan-error nil)))))

(defun nix-smie--indent-exps ()
;; This function replaces and is based on `smie-indent-exps'.
;; An argument to a function is indented relative to the function,
;; not to any other arguments.
"This function replaces and is based on `smie-indent-exps'.
An argument to a function is indented relative to the function,
not to any other arguments."
(save-excursion
(let (parent ;; token enclosing the expression list
skipped) ;; whether we skipped at least one expression
@@ -721,7 +727,7 @@ STRING-TYPE type of string based off of Emacs syntax table types"

;;;###autoload
(defun nix-mode-format ()
"Format the entire nix-mode buffer."
"Format the entire `nix-mode' buffer."
(interactive)
(when (eq major-mode 'nix-mode)
(save-excursion
@@ -797,6 +803,7 @@ STRING-TYPE type of string based off of Emacs syntax table types"
"Indent on a whole region. Enabled by default.
START where to start in region.
END where to end the region."
(interactive (list (region-beginning) (region-end)))
(save-excursion
(goto-char start)
(while (< (point) end)
@@ -813,14 +820,14 @@ END where to end the region."
(nix-is-comment-p)))))
;; Don't mess with strings.
(nix-is-string-p))
(smie-indent-line)))
(funcall nix-indent-function)))
(forward-line 1))))

;;;###autoload
(defun nix-mode-ffap-nixpkgs-path (str)
"Support `ffap' for <nixpkgs> declarations.
If STR contains brackets, call nix-instantiate to find the
location of STR. If nix-instantiate has a nonzero exit code,
If STR contains brackets, call `nix-instantiate' to find the
location of STR. If `nix-instantiate' has a nonzero exit code,
don’t do anything"
(when (and (string-match nix-re-bracket-path str)
(executable-find nix-instantiate-executable))
84 changes: 68 additions & 16 deletions tests/nix-mode-tests.el
Original file line number Diff line number Diff line change
@@ -54,15 +54,15 @@ function to do the indentation tests."
(nix-mode)

;; If we're doing an indentation test
(if ,indent
(progn
;; Indent the buffer
(indent-region (point-min) (point-max))
(when ,indent
(let ((indent-line-function 'smie-indent-line))
;; Indent the buffer
(indent-region (point-min) (point-max))

;; Compare buffer to the stored buffer contents
(should (equal
(buffer-substring-no-properties (point-min) (point-max))
raw-file))))
;; Compare buffer to the stored buffer contents
(should (equal
(buffer-substring-no-properties (point-min) (point-max))
raw-file))))

;; Go to beginning
(goto-char (point-min))
@@ -84,45 +84,97 @@ function to do the indentation tests."
(buffer-substring-no-properties (point-min) (point-max))
raw-file))))

(ert-deftest nix-mode-test-indent-list-contents-smie ()
"Proper indentation for items inside of a list."
(with-nix-mode-test ("list-contents.nix" :indent 'smie-indent-line)))

(ert-deftest nix-mode-test-indent-list-contents ()
"Proper indentation for items inside of a list."
(with-nix-mode-test ("list-contents.nix" :indent t)))
(with-nix-mode-test ("list-contents.nix" :indent 'nix-indent-line)))

(ert-deftest nix-mode-test-indent-issue-60-1-smie ()
"Proper indentation of attrsets inside of lists inside of attrsets.
Related issue: https://github.com/NixOS/nix-mode/issues/60"
(with-nix-mode-test ("issue-60.1.nix" :indent 'smie-indent-line)))

(ert-deftest nix-mode-test-indent-issue-60-1 ()
"Proper indentation of attrsets inside of lists inside of attrsets.
Related issue: https://github.com/NixOS/nix-mode/issues/60"
(with-nix-mode-test ("issue-60.1.nix" :indent t)))
(with-nix-mode-test ("issue-60.1.nix" :indent 'nix-indent-line)))

(ert-deftest nix-mode-test-indent-issue-60-2-smie ()
"Proper indentation of code inside of let blocks.
Related issue: https://github.com/NixOS/nix-mode/issues/60"
(with-nix-mode-test ("issue-60.2.nix" :indent 'smie-indent-line)))

(ert-deftest nix-mode-test-indent-issue-60-2 ()
"Proper indentation of code inside of let blocks.
Related issue: https://github.com/NixOS/nix-mode/issues/60"
(with-nix-mode-test ("issue-60.2.nix" :indent t)))
(with-nix-mode-test ("issue-60.2.nix" :indent 'nix-indent-line)))

(ert-deftest nix-mode-test-indent-issue-60-3 ()
(ert-deftest nix-mode-test-indent-issue-60-3-smie ()
"Proper indentation of import and newline after equal.
Related issue: https://github.com/NixOS/nix-mode/issues/60"
(with-nix-mode-test ("issue-60.3.nix" :indent t)))
(with-nix-mode-test ("issue-60.3.nix" :indent 'smie-indent-line)))

;; nix-indent-line and smie-indent-line conflict, so we just use smie
;; (ert-deftest nix-mode-test-indent-issue-60-3 ()
;; "Proper indentation of import and newline after equal.
;; Related issue: https://github.com/NixOS/nix-mode/issues/60"
;; (with-nix-mode-test ("issue-60.3.nix" :indent 'nix-indent-line)))

(ert-deftest nix-mode-test-indent-issue-69-1-smie ()
"Proper indentation of an empty attrset.
Related issue: https://github.com/NixOS/nix-mode/issues/69"
(with-nix-mode-test ("issue-69.1.nix" :indent 'smie-indent-line)))

(ert-deftest nix-mode-test-indent-issue-69-1 ()
"Proper indentation of an empty attrset.
Related issue: https://github.com/NixOS/nix-mode/issues/69"
(with-nix-mode-test ("issue-69.1.nix" :indent t)))
(with-nix-mode-test ("issue-69.1.nix" :indent 'nix-indent-line)))

(ert-deftest nix-mode-test-indent-issue-69-2-smie ()
"Proper indentation of an empty list.
Related issue: https://github.com/NixOS/nix-mode/issues/69"
(with-nix-mode-test ("issue-60.2.nix" :indent 'smie-indent-line)))

(ert-deftest nix-mode-test-indent-issue-69-2 ()
"Proper indentation of an empty list.
Related issue: https://github.com/NixOS/nix-mode/issues/69"
(with-nix-mode-test ("issue-60.2.nix" :indent t)))
(with-nix-mode-test ("issue-60.2.nix" :indent 'nix-indent-line)))

(ert-deftest nix-mode-test-indent-issue-72-smie ()
"Proper indentation of strings in a multi-line string.
Related issue: https://github.com/NixOS/nix-mode/issues/72"
(with-nix-mode-test ("issue-72.nix" :indent 'smie-indent-line)))

(ert-deftest nix-mode-test-indent-issue-72 ()
"Proper indentation of strings in a multi-line string.
Related issue: https://github.com/NixOS/nix-mode/issues/72"
(with-nix-mode-test ("issue-72.nix" :indent t)))
(with-nix-mode-test ("issue-72.nix" :indent 'nix-indent-line)))

(ert-deftest nix-mode-test-indent-hello-smie ()
"Proper indentation of strings in a multi-line string.
Related issue: https://github.com/NixOS/nix-mode/issues/72"
(with-nix-mode-test ("hello.nix" :indent 'smie-indent-line)))

(ert-deftest nix-mode-test-indent-hello ()
"Proper indentation of strings in a multi-line string.
Related issue: https://github.com/NixOS/nix-mode/issues/72"
(with-nix-mode-test ("hello.nix" :indent 'nix-indent-line)))

(provide 'nix-mode-tests)
;;; nix-mode-tests.el ends here
26 changes: 26 additions & 0 deletions tests/testcases/hello.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{ stdenv, fetchurl }:

stdenv.mkDerivation rec {
name = "hello-${version}";
version = "2.10";

src = fetchurl {
url = "mirror://gnu/hello/${name}.tar.gz";
sha256 = "0ssi1wpaf7plaswqqjwigppsg5fyh99vdlb9kzl7c9lng89ndq1i";
};

doCheck = true;

meta = with stdenv.lib; {
description = "A program that produces a familiar, friendly greeting";
longDescription = ''
GNU Hello is a program that prints "Hello, world!" when you run it.
It is fully customizable.
'';
homepage = https://www.gnu.org/software/hello/manual/;
changelog = "https://git.savannah.gnu.org/cgit/hello.git/plain/NEWS?h=v${version}";
license = licenses.gpl3Plus;
maintainers = [ maintainers.eelco ];
platforms = platforms.all;
};
}