Skip to content

Commit

Permalink
Add support for the new Elixir v1.2 alias shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
tonini committed Jan 6, 2016
1 parent d5f12b2 commit 545a382
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Enhancements

* [Completion] New shortcut alias handling for Elixir v1.2 is now supported.
* [Info] Introduce Elixir v1.2 `IEx.Helpers.t` and `IEx.Helpers.i` functionality.
* [Eval] Display ansi escape sequences inside evaluation buffers correctly.
* [Test] Add a new key (`t`) to toggle the truncation of lines in the test report buffer.
Expand Down
30 changes: 28 additions & 2 deletions alchemist-scope.el
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@

(defconst alchemist-scope-alias-regex
"^\s+alias\s+\\([-:_A-Za-z0-9,\.\?!\]+\\)\\(\s*,\s*as:\s*\\)?\\([-_A-Za-z0-9,\.\?!\]+\\)?\n"
"The regex for matching Elixir alias definitions.")
"The regex for matching Elixir alias definitions.
Example:
alias Phoenix.Router.Resource, as: Special")

(defconst alchemist-scope-alias-regex-two
"^\s+alias\s+\\([-:_A-Za-z0-9,\.\?!\]+\\)\.{\\([-:_A-Za-z0-9\s,\.\?!\]+\\)}\n"
"The regex for matching Elixir alias definitions.
Example:
alias List.Chars.{Atom, Float}")

(defconst alchemist-scope-use-regex
"^\s+use\s+\\([A-Za-z0-9\.]+\\)"
Expand Down Expand Up @@ -91,6 +99,9 @@
(save-excursion
(when (alchemist-scope-inside-module-p)
(end-of-line)
;; alias definition like:
;;
;; alias Phoenix.Router.Resource, as: Special
(while (re-search-backward alchemist-scope-alias-regex nil t)
(when (and
(not (alchemist-scope-inside-string-p))
Expand All @@ -99,7 +110,22 @@
(as (if (match-string 3) (match-string 3) nil))
(as (if as as (car (last (split-string alias "\\."))))))
(setq aliases (append aliases (list (list (alchemist-utils-remove-dot-at-the-end alias)
(alchemist-utils-remove-dot-at-the-end as))))))))))
(alchemist-utils-remove-dot-at-the-end as))))))))
;; alias definition like:
;;
;; alias List.Chars.{Atom, Float}
(while (re-search-backward alchemist-scope-alias-regex-two nil t)
(when (and
(not (alchemist-scope-inside-string-p))
(equal context (alchemist-scope-module)))
(let* ((prefix (match-string 1))
(alias-collection (if (match-string 2) (split-string (match-string 2) ",") nil)))
(-map (lambda (alias)
(let* ((alias (replace-regexp-in-string "\s+" "" alias))
(namespace (format "%s.%s" prefix alias)))
(setq aliases (append aliases (list (list (alchemist-utils-remove-dot-at-the-end namespace)
(alchemist-utils-remove-dot-at-the-end alias)))))))
alias-collection))))))
aliases))

(defun alchemist-scope--modules (regex)
Expand Down
26 changes: 26 additions & 0 deletions test/alchemist-scope-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,32 @@ defmodule Phoenix.Router do
do_scope(options, context)
end
end")
(alchemist-scope-aliases))))
(should (equal (list '("Phoenix.Router.Resource" "Resource")
'("Phoenix.Router.Scope" "Scope"))
(with-temp-buffer
(alchemist-mode)
(insert "
defmodule Phoenix.Router do
alias Phoenix.Router.{Resource, Scope}
end")
(alchemist-scope-aliases))))
(should (equal (list '("Phoenix.Router.Scope" "Scope")
'("Phoenix.Router.Resource" "Special")
'("List.Chars.Atom" "Atom")
'("List.Chars.Float" "Float"))
(with-temp-buffer
(alchemist-mode)
(insert "
defmodule Phoenix.Router do
alias List.Chars.{Atom, Float}
alias Phoenix.Router.Resource, as: Special
alias Phoenix.Router.Scope
end")
(alchemist-scope-aliases)))))

Expand Down

0 comments on commit 545a382

Please sign in to comment.