branch: elpa/dart-mode
commit e41f41e09cfeb35f3733f07c06ee8037825fb5ab
Author: Brady Trainor <[email protected]>
Commit: Brady Trainor <[email protected]>
Fontify untyped parameters in anonymous functions
---
dart-mode.el | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 72 insertions(+)
diff --git a/dart-mode.el b/dart-mode.el
index 596d2d2..ade34ad 100644
--- a/dart-mode.el
+++ b/dart-mode.el
@@ -671,6 +671,73 @@ fontify as declared variables. From ECMA-408,
;; Otherwise, return nil.
(t (throw 'result nil)))))))
+(defun dart--anonymous-function-matcher (limit)
+ "Font-lock matcher for start of anonymous functions.
+
+Looks for opening parenthesis, tries to jump to opening
+parenthesis, ensure it is not preceded by for, while, etc. Then
+tries to jump to closing parenthesis and check if followed by \"
+{\" or \" =>\".
+
+Used with `dart--untyped-parameter-anchored-matcher' to fontify
+untyped parameters. For example, in
+
+ (untypedParameter) => untypedParameter.length"
+ (catch 'result
+ (let (beg end)
+ (while (search-forward "(" limit t)
+ (setq beg (match-beginning 0))
+ (setq end (match-end 0))
+ (unless (looking-back (rx (or (and (or "do" "for" "if" "switch"
"while")
+ space)
+ "super")
+ ?\())
+ (condition-case nil
+ (up-list)
+ (scan-error (throw 'result nil)))
+ (when (looking-at (rx space (or ?\{ "=>")))
+ (set-match-data (list beg end))
+ (goto-char end)
+ (throw 'result t))
+ (goto-char end)))
+ (throw 'result nil))))
+
+(defun dart--untyped-parameter-anchored-matcher (limit)
+ "Font-lock anchored-matcher for untyped parameters.
+
+Searches forward for for lowercase idenitifer and ensures depth
+is still same.
+
+Used with `dart--anonymous-function-matcher' to fontify
+untyped parameters. For example, in
+
+ (untypedParameter) => untypedParameter.length"
+ (let (beg end)
+ (catch 'result
+ (if (equal (char-after) ?\))
+ (throw 'result nil))
+ (let ((depth (car (syntax-ppss))))
+ (while (re-search-forward
+ (rx (and (group (or (one-or-more (char ?_))
+ (eval (dart--identifier 'lower))))
+ (or ?, ?\)))))
+ (setq beg (match-beginning 1))
+ (setq end (match-end 1))
+ (goto-char end)
+ (if (or (> (point) limit)
+ (< (car (syntax-ppss)) depth))
+ (throw 'result nil)
+ (set-match-data (list beg end))
+ (throw 'result t))))
+ (throw 'result nil))))
+
+(defun dart--get-point-at-end-of-list ()
+ (let (pt)
+ (save-excursion
+ (up-list)
+ (setq pt (point)))
+ pt))
+
(defvar dart-font-lock-defaults
'((dart-font-lock-keywords-1 dart-font-lock-keywords-1
dart-font-lock-keywords-2
@@ -702,6 +769,11 @@ fontify as declared variables. From ECMA-408,
nil
nil
(0 font-lock-variable-name-face)))
+ (dart--anonymous-function-matcher
+ . (dart--untyped-parameter-anchored-matcher
+ (dart--get-point-at-end-of-list)
+ nil
+ (0 font-lock-variable-name-face)))
(dart--string-interpolation-id-func (0 font-lock-variable-name-face t))
(dart--string-interpolation-exp-func (0 font-lock-variable-name-face
t)))))