Skip to content

Customizable face for numbers #459

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 8 commits into from
Nov 28, 2020
2 changes: 2 additions & 0 deletions elixir-format.el
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
(defcustom elixir-format-arguments nil
"Additional arguments to 'mix format'"
:type '(repeat string)
:group 'elixir
:group 'elixir-format)

(defcustom elixir-format-hook nil
"Hook called by `elixir-format'."
:type 'hook
:group 'elixir
:group 'elixir-format)


Expand Down
56 changes: 42 additions & 14 deletions elixir-mode.el
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;;; elixir-mode.el --- Major mode for editing Elixir files
;;; elixir-mode.el --- Major mode for editing Elixir files -*- lexical-binding: t -*-

;; Copyright 2011-2015 secondplanet
;; 2013-2015 Samuel Tonini, Matt DeBoard, Andreas Fuchs
Expand All @@ -10,7 +10,7 @@
;; URL: https://github.com/elixir-editors/emacs-elixir
;; Created: Mon Nov 7 2011
;; Keywords: languages elixir
;; Version: 2.3.1
;; Version: 2.4.0
;; Package-Requires: ((emacs "24") (pkg-info "0.4"))

;; This file is not a part of GNU Emacs.
Expand Down Expand Up @@ -48,13 +48,17 @@
:link '(url-link :tag "Github" "https://github.com/elixir-editors/emacs-elixir")
:link '(emacs-commentary-link :tag "Commentary" "elixir-mode"))

(defvar elixir-mode-website-url "http://elixir-lang.org"
"Official url of Elixir programming website.")
(defcustom elixir-mode-website-url "http://elixir-lang.org"
"Official url of Elixir programming website."
:type 'string)

(defvar elixir-mode-doc-url "https://hexdocs.pm/elixir"
"Official documentation for the Elixir programming language.")
(defcustom elixir-mode-doc-url "https://hexdocs.pm/elixir"
"Official documentation for the Elixir programming language."
:type 'string)

(defvar elixir-mode-hook nil)
(defcustom elixir-mode-hook nil
"Hook that runs when switching to major mode"
:type 'hook)

(defvar elixir-mode-map
(let ((map (make-sparse-keymap)))
Expand All @@ -72,21 +76,36 @@
("Tests" "^\\s-*test[ \t\n]+\"?\\(:?[a-z0-9_@+() \t-]+\\)\"?[ \t\n]+.*" 1))
"Imenu pattern for `elixir-mode'.")

(defvar elixir-basic-offset 2)
(defvar elixir-key-label-offset 0)
(defvar elixir-match-label-offset 2)
(defcustom elixir-basic-offset 2
"Basic offset."
:type 'integer)
(defcustom elixir-key-label-offset 0
"Offset used for key label."
:type 'integer)
(defcustom elixir-match-label-offset 2
"Offset for a match label."
:type 'integer)

(defgroup elixir-faces nil
"Font-lock faces for `elixir'."
:group 'elixir
:group 'faces)

(defvar elixir-attribute-face 'elixir-attribute-face)
(defface elixir-attribute-face
'((t (:inherit font-lock-preprocessor-face)))
"For use with module attribute tokens."
:group 'font-lock-faces)
"For use with module attribute tokens.")

(defvar elixir-atom-face 'elixir-atom-face)
(defface elixir-atom-face
'((t (:inherit font-lock-builtin-face)))
"For use with atoms & map keys."
:group 'font-lock-faces)
"For use with atoms & map keys.")

(defvar elixir-number-face 'elixir-number-face)
(defface elixir-number-face
'((t (:inherit default)))
"For use with numbers.")


(eval-when-compile
(defconst elixir-rx-constituents
Expand All @@ -107,6 +126,11 @@
(zero-or-more (any "a-z" "A-Z" "0-9" "_" "\"" "'" "!" "@" "?")))
(and "\"" (one-or-more (not (any "\""))) "\"")
(and "'" (one-or-more (not (any "'"))) "'"))))
(numbers . ,(rx (and symbol-start
(? "-")
(+ digit)
(0+ (and "_" (= 3 digit)))
symbol-end)))
Comment on lines +129 to +133
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is too naive for parsing numbers in Elixir. I propose the following:

Suggested change
(numbers . ,(rx (and symbol-start
(? "-")
(+ digit)
(0+ (and "_" (= 3 digit)))
symbol-end)))
(numbers . ,(rx (and symbol-start
(zero-or-one "-")
(one-or-more digit)
(zero-or-more (or digit (seq ?_ digit)))
(zero-or-one ".")
(zero-or-more (or digit (seq ?_ digit)))
symbol-end)))

This will match things like:

0
-0
0_0
-0_0.0_0

I know they are not what the formatter will typically do but I see no reason why we should bind this face to only what the formatter does.

Not sure how this will behave inside comments, heredocs and so on though.

Also I am trying to follow the use of zero-or-more and friends because of what the rest of the file is using.

Tests would be good here too if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you prefer that, that's fine. I'll leave it your capable hands to get this across the finish line 🙏

(builtin . ,(rx symbol-start
(or "case" "cond" "for" "if" "quote" "raise" "receive" "send"
"super" "throw" "try" "unless" "unquote" "unquote_splicing"
Expand Down Expand Up @@ -382,6 +406,10 @@ is used to limit the scan."
(optional "="))
1 elixir-atom-face)

;; Numbers
(,(elixir-rx (group numbers))
1 elixir-number-face)

;; Gray out variables starting with "_"
(,(elixir-rx symbol-start
(group (and "_"
Expand Down