Skip to content

Instantly share code, notes, and snippets.

@dmj
Created October 14, 2012 18:59
Show Gist options
  • Save dmj/3889487 to your computer and use it in GitHub Desktop.
Save dmj/3889487 to your computer and use it in GitHub Desktop.
Highlight annotations in PHP
(defface php-annotations-annotation-face '((t . (:inherit 'font-lock-constant-face)))
"Face used to highlight annotations.")
(defconst php-annotations-re "\\(\\s-\\|{\\)\\(@[[:alpha:]]+\\)")
(defmacro php-annotations-inside-comment-p (pos)
"Return non-nil if POS is inside a comment."
`(eq (get-char-property ,pos 'face) 'font-lock-comment-face))
(defun php-annotations-font-lock-find-annotation (limit)
(let ((match
(catch 'match
(save-match-data
(while (re-search-forward php-annotations-re limit t)
(when (php-annotations-inside-comment-p (match-beginning 0))
(goto-char (match-end 0))
(throw 'match (match-data))))))))
(when match
(set-match-data match)
t)))
(eval-after-load 'php-mode
'(font-lock-add-keywords 'php-mode '((php-annotations-font-lock-find-annotation (2 'php-annotations-annotation-face t)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment