Skip to content

Commit

Permalink
WIP: font-lock-variable-face on assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
non-Jedi committed Mar 31, 2020
1 parent e56e9d5 commit 0c802f9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
39 changes: 39 additions & 0 deletions julia-mode-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ function( i=1:2 )
end
end")

;;; font-lock tests

(ert-deftest julia--test-symbol-font-locking-at-bol ()
"Symbols get font-locked at beginning or line."
(julia--should-font-lock
Expand Down Expand Up @@ -513,6 +515,43 @@ end")
(julia--should-font-lock string 74 font-lock-type-face) ; B
))

(ert-deftest julia--test-variable-font-lock ()
(julia--should-font-lock "x = 5" 1 font-lock-variable-name-face)
(julia--should-font-lock " x = 5" 3 font-lock-variable-name-face)
(julia--should-font-lock "x[] = 5" 1 font-lock-variable-name-face)
(julia--should-font-lock "x[3][2] = 5" 1 font-lock-variable-name-face)
(julia--should-font-lock "x[3,2] = 5" 1 font-lock-variable-name-face)
(julia--should-font-lock "x[1:2] = 5" 1 font-lock-variable-name-face)
(julia--should-font-lock "x += 5" 1 font-lock-variable-name-face)
(julia--should-font-lock "x .= 5" 1 font-lock-variable-name-face)
(julia--should-font-lock "x .+= 5" 1 font-lock-variable-name-face)
(let ((str "foo.bar = 5" ))
(julia--should-font-lock str 1 font-lock-variable-name-face)
(julia--should-font-lock str 4 font-lock-variable-name-face)
(julia--should-font-lock str 5 font-lock-variable-name-face)
(julia--should-font-lock str 7 font-lock-variable-name-face))
(let ((str "foo.baz._bar = 5" ))
(julia--should-font-lock str 1 font-lock-variable-name-face)
(julia--should-font-lock str 4 font-lock-variable-name-face)
(julia--should-font-lock str 5 font-lock-variable-name-face)
(julia--should-font-lock str 8 font-lock-variable-name-face)
(julia--should-font-lock str 9 font-lock-variable-name-face)
(julia--should-font-lock str 12 font-lock-variable-name-face)))

(ert-deftest julia--test-variable-font-lock-named-tuple ()
"Test named tuples aren't font-locked as variable declarations."
:expected-result :failed
(julia--should-font-lock "(x=1, y=2)" 2 nil)
(julia--should-font-lock "(x=1, y=2)" 7 nil))

(ert-deftest julia--test-variable-font-lock-kwarg ()
"Test function keyword arguments aren't font-locked as variable declarations."
:expected-result :failed
(julia--should-font-lock "f(x=1, y=2)" 3 nil)
(julia--should-font-lock "f(x=1, y=2)" 8 nil)
(julia--should-font-lock "f(; x=1, y=2)" 5 nil)
(julia--should-font-lock "f(; x=1, y=2)" 10 nil))

;;; Movement
(ert-deftest julia--test-beginning-of-defun-assn-1 ()
"Point moves to beginning of single-line assignment function."
Expand Down
28 changes: 28 additions & 0 deletions julia-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,33 @@ a LaTeX string with a unicode symbol."
(rx (or bol whitespace "(" "[" "," "=")
(group ":" (or letter (syntax symbol)) (0+ (or word (syntax symbol))))))

;; TODO: multiple assignment "a, b, c = x"
;; TODO: this highlights named tuple symbols (e.g. "a" in "(a=1,
;; b=2)") and keyword args after a semi-colon
(defconst julia--variable-assignment-regex
(rx
;; assignment can only happen in certain contexts
(or bol ?\;
;; don't count f(a=5) as assignment
(and (or buffer-start (not (any word ?_))) ?\()
(and (or "begin" "let") (0+ blank)))
(0+ blank)
(group
;; Namespace qualifier e.g. "Base" in "Base.Threads"
(optional (any word ?_) (0+ (any word ?_ ?.)) ?.)
;; actual variable name
(1+ (any word ?_)))
;; setindex! expression
(* ?\[ (* (not (any ?\]))) ?\])
(0+ blank)
;; assignment can be broadcasted
(optional ?.)
;; modify and set via e.g. +=
(optional (any ?+ ?- ?* ?/ "//" ?\\ ?^ ?% "<<" ">>" ">>>" ?| ?& ?⊻))
;; make sure assignment expression isn't some other operator that
;; starts with =
?= (not (any ?> ?=))))

(defconst julia-font-lock-keywords
;; font-lock-builtin-face intentionally unused since any name from
;; names(Base) can be aliased in a baremodule.
Expand All @@ -283,6 +310,7 @@ a LaTeX string with a unicode symbol."
'("true" "false" "C_NULL" "Inf" "NaN" "Inf32" "NaN32" "nothing" "undef" "missing")
'symbols)
'font-lock-constant-face)
(list julia--variable-assignment-regex 1 'font-lock-variable-name-face)
(list julia-unquote-regex 2 'font-lock-constant-face)
(list julia-forloop-in-regex 1 'font-lock-keyword-face)
(list julia--forloop-=-regex 1 'font-lock-keyword-face)
Expand Down

0 comments on commit 0c802f9

Please sign in to comment.