Skip to content

Added: elixir-quoted-minor-mode #103

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

Merged
merged 2 commits into from
Aug 27, 2014
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
1 change: 1 addition & 0 deletions elixir-mode-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

(load "test/elixir-mode-indentation-tests.el")
(load "test/elixir-mode-font-tests.el")
(load "test/elixir-quoted-minor-mode-tests.el")

(provide 'elixir-mode-tests)
;;; elixir-mode-tests.el ends here
24 changes: 22 additions & 2 deletions elixir-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@

(defvar elixir-mode--eval-filename "elixir-mode-tmp-eval-file.exs")

(defvar elixir-quoted--buffer-name "*elixir-quoted*")

(defvar elixir-basic-offset 2)
(defvar elixir-key-label-offset 0)
(defvar elixir-match-label-offset 2)
Expand Down Expand Up @@ -492,6 +494,18 @@ Argument FILE-NAME ."
(when (string= compiler-output "")
(message "Compiled and saved as %s" (elixir-mode-compiled-file-name)))))

(defun elixir-quoted--initialize-buffer (quoted)
(pop-to-buffer elixir-quoted--buffer-name)
(setq buffer-undo-list nil) ; Get rid of undo information from
; previous expansions
(let ((inhibit-read-only t)
(buffer-undo-list t)) ; Ignore undo information
(erase-buffer)
(insert quoted)
(goto-char (point-min))
(elixir-mode)
(elixir-quoted-minor-mode 1)))

;;;###autoload
(defun elixir-mode-iex (&optional args-p)
"Elixir mode interactive REPL.
Expand Down Expand Up @@ -542,7 +556,7 @@ Optional argument ARGS-P ."
file))

(defun elixir-mode--code-string-to-quoted-command (file)
(format "%s -e 'IO.puts inspect(elem(Code.string_to_quoted(File.read!(\"%s\")), 1))'"
(format "%s -e 'IO.puts inspect(elem(Code.string_to_quoted(File.read!(\"%s\")), 1), pretty: true)'"
elixir-mode-command
file))

Expand All @@ -566,7 +580,7 @@ Optional argument ARGS-P ."

(defun elixir-mode--string-to-quoted (string)
(let* ((output (elixir-mode--execute-elixir-with-code-string-to-quoted string)))
(message output)))
(elixir-quoted--initialize-buffer output)))

(defun elixir-mode-eval-on-region (beg end)
"Evaluate the Elixir code on the marked region.
Expand Down Expand Up @@ -646,6 +660,12 @@ Argument END End of the region."
(t
(remove-hook 'after-save-hook 'elixir-mode-compile-file t))))

(define-minor-mode elixir-quoted-minor-mode
"Minor mode for displaying elixir quoted expressions"
:group 'elixir-quoted :lighter " quoted"
:keymap '(("q" . quit-window))
(setq buffer-read-only t))

;;;###autoload
(defun elixir-mode-run-tests ()
"Run ERT test for `elixir-mode'."
Expand Down
52 changes: 52 additions & 0 deletions test/elixir-quoted-minor-mode-tests.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

(ert-deftest elixir-quoted-minor-mode/base ()
(elixir-test-with-temp-buffer
"sum(1, 2)"
(elixir-mode-string-to-quoted-on-current-line)
(should (string= (buffer-name) elixir-quoted--buffer-name))
(should (search-forward "{:sum, [line: 1], [1, 2]}" nil t))
(should (eq 'quit-window (key-binding "q")))
(call-interactively (key-binding "q"))
(should-not (string= (buffer-name) elixir-quoted--buffer-name))))

(ert-deftest elixir-quoted-minor-mode/read-only ()
(elixir-test-with-temp-buffer
"sum(1,2)"
(elixir-mode-string-to-quoted-on-current-line)
(should buffer-read-only)
(should-error (call-interactively (key-binding "w")))))

(ert-deftest elixir-quoted-minor-mode/multiple-invocation ()
(elixir-test-with-temp-buffer
"sum(1,2)"
(elixir-mode-string-to-quoted-on-current-line)
(should (search-forward "{:sum, [line: 1], [1, 2]}" nil t))
(quit-window)
(erase-buffer)
(insert "sum(3, 2)")
(elixir-mode-string-to-quoted-on-current-line)
(should-not (search-forward "{:sum, [line: 1], [1, 2]}" nil t))
(should (search-forward "{:sum, [line: 1], [3, 2]}" nil t))))

(ert-deftest elixir-quoted-minor-mode/indentation ()
(elixir-test-with-temp-buffer
""
(insert "if a do\n")
(insert " b\n")
(insert "else\n")
(insert " c\n")
(insert "end")
(elixir-mode-string-to-quoted-on-region (point-min) (point-max))
(should (search-forward "{:if, [line: 1],\n" nil t))
(should (search-forward " [{:a, [line: 1], nil}" nil t))))

(ert-deftest elixir-quoted-minor-mode/undo ()
(elixir-test-with-temp-buffer
"sum(1, 2)"
(elixir-mode-string-to-quoted-on-current-line)
(should-error (undo))
(quit-window)
(erase-buffer)
(insert "sum(3, 2)")
(elixir-mode-string-to-quoted-on-current-line)
(should-error (undo))))