1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
|
;;; scala-mode-fontlock.el - Major mode for editing scala, font-lock
;;; Copyright (c) 2012 Heikki Vesalainen
;;; For information on the License, see the LICENSE file
(require 'scala-mode-syntax)
(defcustom scala-font-lock:constant-list '()
"A list of strigs that should be fontified in constant
face. This customization property takes effect only after the
scala-mode has been reloaded."
:type '(repeat string)
:group 'scala)
(defun scala-font-lock:create-user-constant-re ()
(regexp-opt scala-font-lock:constant-list 'words))
(defun scala-font-lock:mark-reserved-symbols (limit)
(when (re-search-forward scala-syntax:reserved-symbols-re limit t)
(goto-char (match-end 2)))) ;; step back to the match (re matches futher)
(defun scala-font-lock:mark-underscore (limit)
(when (re-search-forward scala-syntax:reserved-symbol-underscore-re limit t)
(goto-char (match-end 2)))) ;; step back to the match (re matches futher)
;(defun scala-font-lock:extend-region-function ()
(defun scala-font-lock:limit-pattern2 (&optional start)
(save-excursion
(when start (goto-char start))
(scala-syntax:skip-forward-ignorable)
(ignore-errors
(while (and (not (or (eobp)
(looking-at scala-syntax:other-keywords-unsafe-re)
(scala-syntax:looking-at-reserved-symbol nil)))
(scala-syntax:looking-at-simplePattern-beginning))
; (message "- now at %d" (point))
(if (= (char-after) ?\()
(forward-list)
;; else
(goto-char (match-end 0))
(scala-syntax:skip-forward-ignorable)
; (message "+ now at %d" (point))
(cond ((looking-at "(")
(forward-list))
((looking-at "@")
(goto-char (match-end 0)))
((or (scala-syntax:looking-at-reserved-symbol nil)
(looking-at scala-syntax:other-keywords-unsafe-re))
; (messssage "saw reserved symbol or keyword")
nil)
((looking-at scala-syntax:id-re)
; (message "saw id-re %d" (match-beginning 0))
(goto-char (match-end 0)))
; (t
; (message "nothing special here %s" (point)))
))
(scala-syntax:skip-forward-ignorable)))
; (message "limit at %s" (point))
(point)))
(defun scala-font-lock:limit-pattern2-list (&optional start)
(let ((limit (scala-font-lock:limit-pattern2 start)))
(while (= (char-after limit) ?,)
(setq limit (scala-font-lock:limit-pattern2 (1+ limit))))
; (message "list limit at %s" limit)
limit))
(defun scala-font-lock:mark-pattern1-part (&optional limit pattern-p)
"Parses a part of val, var and case pattern (or id). Always
parses a variable or constant name first and then type, leaving
the pointer at the next variablename, constant name, list or
Pattern3, if any, and setting up match data 1 (variable),
2 (constant) and 3 (type) accordingly. If there is no variable
name before the first type, then the match data for the variable
name is nil. Returns t if something was matched or nil if nothing
was found.
If pattern-p is defined, then only varid is matched as variable
and everything else is constant.
Does not continue past limit.
"
; (message "will stop at %d" limit)
(cond
;; quit if we are past limit
((or (and limit (>= (point) limit))
(eobp))
; (message "at limit %s" (point))
nil)
;; Type pattern, just skip the whole thing. It will end at ',' or ')'.
;; Note: forms starting with ':' are handled by a completely separete
;; font-lock matcher.
((scala-syntax:looking-at-reserved-symbol ":")
; (message ":")
(while (not (or (eobp)
(scala-syntax:looking-at "[,);]")
(scala-syntax:looking-at-reserved-symbol "|")
(scala-syntax:looking-at-reserved-symbol
scala-syntax:double-arrow-unsafe-re)
(scala-syntax:looking-at-empty-line-p)))
(scala-syntax:forward-sexp)
(scala-syntax:skip-forward-ignorable))
(set-match-data nil)
t)
;; Binding part cannot start with reserved symbols. If they
;; are seen, we must quit.
((scala-syntax:looking-at-reserved-symbol nil)
; (message "symbol")
nil)
((scala-syntax:looking-at-stableIdOrPath)
; (message "stableId")
(let ((beg (match-beginning 0))
(end (match-end 0))
(varid (scala-syntax:looking-at-varid-p)))
(goto-char end)
(let ((new-match-data
(cond
((= (char-after end) ?\()
;; matched type
; (message "it's a type")
`(,beg ,end nil nil nil nil ,beg ,end))
((progn (scala-syntax:backward-sexp)
(= (char-before) ?.))
;; matched constant
`(,beg ,end nil nil ,(point) ,end nil nil))
((or varid (not pattern-p))
;; matched variable name or we can't be sure
`(,beg ,end ,beg ,end nil nil nil nil))
(t
;; matched constant
`(,beg ,end nil nil ,beg ,end nil nil)))))
(goto-char end)
(scala-syntax:skip-forward-ignorable)
(cond
((and (not (or (scala-syntax:looking-at-reserved-symbol nil)
(scala-syntax:looking-at-reserved-symbol "|")))
(scala-syntax:looking-at-stableIdOrPath))
(setq new-match-data
(append (butlast new-match-data 2)
`(,(match-beginning 0)
,(match-end 0))))
(goto-char (match-end 0))
(scala-syntax:skip-forward-ignorable))
((= (char-after) ?@)
(forward-char)
(scala-syntax:skip-forward-ignorable)))
(set-match-data new-match-data)))
t)
;; Pattern3 can be a literal. Just skip them.
((looking-at scala-syntax:literal-re)
; (message "literal")
(goto-char (match-end 0))
(scala-syntax:skip-forward-ignorable)
(set-match-data nil)
t)
;; Start of a patterns list or alternatives. Skip if alternatives or
;; else leave point at start of first element.
((= (char-after) ?\()
; (message "(")
(let ((alternatives-p
(save-excursion
(forward-char)
(ignore-errors
;; forward-sexp will terminate the loop with error
;; if '|' is not found before end of list ')'
(while (not (or (eobp)
(= (char-before) ?|)
(scala-syntax:looking-at-empty-line-p)))
(scala-syntax:forward-sexp))
t))))
(if alternatives-p
(forward-list)
(forward-char)))
(scala-syntax:skip-forward-ignorable)
(set-match-data nil)
t)
;; continuation or end of list, just skip and position at the
;; next element
((or (= (char-after) ?,)
(= (char-after) ?\)))
; (message ", or )")
(forward-char)
(scala-syntax:skip-forward-ignorable)
(set-match-data nil)
t)
;; none of the above, just stop
(t
; (message "Cannot continue Pattern1 at %d" (point))
nil)
))
(defun scala-font-lock:limit-pattern (&optional start)
(save-excursion
(goto-char (scala-font-lock:limit-pattern2 start))
; (message "now at %d" (point))
(when (scala-syntax:looking-at-reserved-symbol ":")
(while (not (or (eobp)
(scala-syntax:looking-at-reserved-symbol "|")
(scala-syntax:looking-at-reserved-symbol
scala-syntax:double-arrow-unsafe-re)
(scala-syntax:looking-at-empty-line-p)))
(scala-syntax:forward-sexp)
(scala-syntax:skip-forward-ignorable)))
(if (or (/= (char-after) ?|)
(scala-syntax:looking-at-reserved-symbol
scala-syntax:double-arrow-unsafe-re))
(point)
(forward-char)
(scala-font-lock:limit-pattern))))
(defun scala-font-lock:mark-pattern-part (&optional limit)
(when (scala-syntax:looking-at-reserved-symbol "|")
; (message "skipping |")
(forward-char)
(scala-syntax:skip-forward-ignorable))
(scala-font-lock:mark-pattern1-part limit t))
(defun scala-font-lock:limit-type (&optional start)
start)
(defun scala-font-lock:limit-simpleType (&optional start)
(when start (goto-char start))
(scala-syntax:skip-forward-ignorable)
(setq start (point))
(if (= (char-after) ?\()
(ignore-errors (forward-list))
(scala-font-lock:mark-simpleType))
(when (and (not (eobp)) (= (char-after) ?#))
(scala-font-lock:mark-simpleType))
(when (and (not (eobp)) (= (char-after) ?\[))
(ignore-errors (forward-list))
(scala-syntax:skip-forward-ignorable))
(let ((limit (point)))
(goto-char start)
; (message "simpeType limit at %d" limit)
limit))
(defun scala-font-lock:mark-simpleType (&optional limit)
; (message "looking for simpleType at %d" (point))
(cond
;; stop at limit
((and limit (>= (point) limit))
nil)
;; just dive into lists
((> (skip-chars-forward "[(,)]") 0)
; (message "skipping list-marks")
(scala-syntax:skip-forward-ignorable)
(set-match-data nil)
t)
;; jump over blocks
((= (char-after) ?\{)
(ignore-errors
(forward-list)
(set-match-data nil)
t))
;; ignore arrows and reserved words and symbols
((or (scala-syntax:looking-at-reserved-symbol
scala-syntax:double-arrow-unsafe-re)
(scala-syntax:looking-at-reserved-symbol
"<[:%]\\|>?:")
(looking-at "\\<forSome\\>"))
; (message "skipping reserved")
(goto-char (match-end 0))
(scala-syntax:skip-forward-ignorable)
(set-match-data nil)
t)
;; color id after '#'
((= (char-after) ?#)
; (message "at #")
(forward-char)
(if (and (not (or (looking-at scala-syntax:keywords-unsafe-re)
(scala-syntax:looking-at-reserved-symbol nil)))
(looking-at scala-syntax:id-re))
(goto-char (match-end 0)) nil))
;; color paths (including stableid)
((scala-syntax:looking-at-stableIdOrPath t)
; (message "at path")
(let ((end (match-end 0)))
(goto-char end)
(while (scala-syntax:looking-back-token "this\\|type")
(goto-char (match-beginning 0))
(skip-chars-backward "."))
(unless (scala-syntax:looking-back-token scala-syntax:id-re)
(set-match-data nil))
(goto-char end))
(scala-syntax:skip-forward-ignorable)
t)
(t
; (message "Cannot continue simpleType at %d" (point))
nil)))
(defun scala-font-lock:mark-string-escapes (limit)
(when (re-search-forward scala-syntax:string-escape-re limit t)
(goto-char (match-end 0))
(or (eq (nth 3 (save-excursion (syntax-ppss (match-beginning 0)))) ?\")
(scala-font-lock:mark-string-escapes limit))))
(defun scala-font-lock:mark-numberLiteral (re limit)
(when (re-search-forward re limit t)
(if (string-match-p scala-syntax:number-safe-start-re
;; get char-before match or a magic ',', which is safe
(string (or (char-before (match-beginning 0)) ?,)))
t
(scala-font-lock:mark-numberLiteral re limit))))
(defun scala-font-lock:mark-floatingPointLiteral (limit)
(scala-font-lock:mark-numberLiteral
scala-syntax:floatingPointLiteral-re
limit))
(defun scala-font-lock:mark-integerLiteral (limit)
(scala-font-lock:mark-numberLiteral
scala-syntax:integerLiteral-re
limit))
(defun scala-font-lock:keywords ()
;; chars, string, comments are handled acording to syntax and
;; syntax propertize
`(;; keywords
(,scala-syntax:override-re 2 scala-font-lock:override-face)
(,scala-syntax:abstract-re 2 scala-font-lock:abstract-face)
(,scala-syntax:final-re 2 scala-font-lock:final-face)
(,scala-syntax:sealed-re 2 scala-font-lock:sealed-face)
(,scala-syntax:implicit-re 2 scala-font-lock:implicit-face)
(,scala-syntax:lazy-re 2 scala-font-lock:lazy-face)
(,scala-syntax:private-re 2 scala-font-lock:private-face)
(,scala-syntax:protected-re 2 scala-font-lock:protected-face)
(,scala-syntax:other-keywords-re 2 font-lock-keyword-face)
(,scala-syntax:value-keywords-re 2 font-lock-constant-face)
(,scala-syntax:path-keywords-re 2 font-lock-keyword-face)
;; User defined constants
(,(scala-font-lock:create-user-constant-re) 0 font-lock-constant-face)
;; Annotations
(, (rx (and "@" (in "a-zA-Z_.") (0+ (in "a-zA-Z0-9_."))))
. font-lock-preprocessor-face)
;; reserved symbols
(scala-font-lock:mark-reserved-symbols 2 font-lock-keyword-face)
;; 'Symbols
(,scala-syntax:symbolLiteral-re 1 font-lock-string-face)
;; underscore
(scala-font-lock:mark-underscore 2 font-lock-keyword-face)
;; escapes inside strings
(scala-font-lock:mark-string-escapes (0 font-lock-constant-face prepend nil))
;; object
(,(concat "\\<object[ \t]+\\("
scala-syntax:id-re
"\\)")
1 font-lock-constant-face)
;; class, trait, object
(,(concat "\\<\\(class\\|trait\\)[ \t]+\\("
scala-syntax:id-re
"\\)")
2 font-lock-type-face)
;; ;; extends, with, new
;; (,(concat "\\<\\(extends\\|with\\|new\\)[ \t]+\\([("
;; scala-syntax:id-first-char-group "]\\)")
;; (scala-font-lock:mark-simpleType (scala-font-lock:limit-simpleType
;; (goto-char (match-beginning 2)))
;; nil
;; (0 font-lock-type-face nil t)))
;; ;; ':'
;; (,scala-syntax:colon-re
;; (scala-font-lock:mark-simpleType (scala-font-lock:limit-simpleType
;; (goto-char (match-end 2)))
;; nil
;; (0 font-lock-type-face nil t)))
;; def
(,(concat "\\<def[ \t]+\\(" scala-syntax:id-re "\\)") 1 font-lock-function-name-face)
;; VarDcl
("\\<val[ \t]+\\([^:]\\)"
(scala-font-lock:mark-pattern1-part (scala-font-lock:limit-pattern2-list
(goto-char (match-beginning 1)))
nil
(1 font-lock-variable-name-face nil t)
(2 font-lock-constant-face nil t)
(3 font-lock-type-face nil t)))
("\\<var[ \t]+\\([^:]\\)"
(scala-font-lock:mark-pattern1-part (scala-font-lock:limit-pattern2-list
(goto-char (match-beginning 1)))
nil
(1 scala-font-lock:var-face nil t)
(2 font-lock-constant-face nil t)
(3 font-lock-type-face nil t)
))
;; case (but not case class|object)
("\\<case[ \t]+\\([^:]\\)"
(scala-font-lock:mark-pattern-part (scala-font-lock:limit-pattern
(goto-char (match-beginning 1)))
nil
(1 font-lock-variable-name-face nil t)
(2 font-lock-constant-face nil t)
(3 font-lock-type-face nil t)))
;; type ascription (: followed by alpha type name)
(,(rx
(or (not (in "!#%&*+-/:<=>?@\\^|~")) line-start)
(group ":")
(0+ space)
(group (in "a-zA-Z_")
(0+ (in "a-zA-Z0-9_"))
(\? (and "_" (1+ (in "!#%&*+-/:<=>?@\\^|~"))))))
(1 font-lock-keyword-face) (2 font-lock-type-face))
;; type ascription (: followed by punctuation type name)
(,(rx
(or (not (in "!#%&*+-/:<=>?@\\^|~")) line-start)
(group ":")
(1+ space)
(group (1+ (in "-!#%&*+/:<=>?@\\^|~"))))
(1 font-lock-keyword-face) (2 font-lock-type-face))
;; extends followed by type
(,(rx symbol-start
(group "extends")
(1+ space)
(group (or
(and (in "a-zA-Z_")
(0+ (in "a-zA-Z0-9_"))
(\? (and "_" (1+ (in "!#%&*+-/:<=>?@\\^|~")))))
(1+ (in "!#%&*+-/:<=>?@\\^|~")))))
(1 font-lock-keyword-face) (2 font-lock-type-face))
;; with followed by type
(,(rx symbol-start
(group "with")
(1+ space)
(group (or
(and (in "a-zA-Z_")
(0+ (in "a-zA-Z0-9_"))
(\? (and "_" (1+ (in "!#%&*+-/:<=>?@\\^|~")))))
(1+ (in "!#%&*+-/:<=>?@\\^|~")))))
(1 font-lock-keyword-face) (2 font-lock-type-face))
;; new followed by type
(,(rx symbol-start
(group "new")
(1+ space)
(group (or
(and (in "a-zA-Z_")
(0+ (in "a-zA-Z0-9_"))
(\? (and "_" (1+ (in "!#%&*+-/:<=>?@\\^|~")))))
(1+ (in "!#%&*+-/:<=>?@\\^|~")))))
(1 font-lock-keyword-face) (2 font-lock-type-face))
;; uppercase means a type or object
(,(rx symbol-start
(and (in "A-Z")
(0+ (in "a-zA-Z0-9_"))
(\? (and "_" (1+ (in "!#%&*+-/:<=>?@\\^|~"))))))
. font-lock-constant-face)
;; . font-lock-type-face)
; uncomment this to go back to highlighting objects as types
;; uppercase
(,(rx symbol-start
(group
(and (in "A-Z")
(0+ (in "a-zA-Z0-9_"))
(\? (and "_" (1+ (in "!#%&*+-/:<=>?@\\^|~")))))))
. font-lock-constant-face)
;; package name
(,(rx symbol-start
(group "package")
(1+ space)
(group (and (in "a-zA-Z_.") (0+ (in "a-zA-Z0-9_.")))))
(1 font-lock-keyword-face) (2 font-lock-string-face))
;; number literals (have to be here so that other rules take precedence)
(scala-font-lock:mark-floatingPointLiteral . font-lock-constant-face)
(scala-font-lock:mark-integerLiteral . font-lock-constant-face)
(scala-syntax:interpolation-matcher 0 font-lock-variable-name-face t)
))
(defun scala-font-lock:syntactic-face-function (state)
"Return correct face for string or comment"
(if (and (integerp (nth 4 state))
(save-excursion
(goto-char (nth 8 state))
(looking-at "/\\*\\*\\($\\|[^*]\\)")))
;; scaladoc (starts with /** only)
font-lock-doc-face
(if (nth 3 state) font-lock-string-face font-lock-comment-face)))
(defface scala-font-lock:var-face
'((t (:inherit font-lock-warning-face)))
"Font Lock mode face used to highlight scala variable names."
:group 'scala)
(defvar scala-font-lock:var-face 'scala-font-lock:var-face
"Face for scala variable names.")
(defface scala-font-lock:private-face
'((t (:inherit font-lock-builtin-face)))
"Font Lock mode face used for the private keyword."
:group 'scala)
(defvar scala-font-lock:private-face 'scala-font-lock:private-face
"Face for the scala private keyword.")
(defface scala-font-lock:protected-face
'((t (:inherit font-lock-builtin-face)))
"Font Lock mode face used for the protected keyword."
:group 'scala)
(defvar scala-font-lock:protected-face 'scala-font-lock:protected-face
"Face for the scala protected keyword.")
(defface scala-font-lock:override-face
'((t (:inherit font-lock-builtin-face)))
"Font Lock mode face used for the override keyword."
:group 'scala)
(defvar scala-font-lock:override-face 'scala-font-lock:override-face
"Face for the scala override keyword.")
(defface scala-font-lock:sealed-face
'((t (:inherit font-lock-builtin-face)))
"Font Lock mode face used for the sealed keyword."
:group 'scala)
(defvar scala-font-lock:sealed-face 'scala-font-lock:sealed-face
"Face for the scala sealed keyword.")
(defface scala-font-lock:abstract-face
'((t (:inherit font-lock-builtin-face)))
"Font Lock mode face used for the abstract keyword."
:group 'scala)
(defvar scala-font-lock:abstract-face 'scala-font-lock:abstract-face
"Face for the scala abstract keyword.")
(defface scala-font-lock:final-face
'((t (:inherit font-lock-builtin-face)))
"Font Lock mode face used for the final keyword."
:group 'scala)
(defvar scala-font-lock:final-face 'scala-font-lock:final-face
"Face for the scala final keyword.")
(defface scala-font-lock:implicit-face
'((t (:inherit font-lock-builtin-face)))
"Font Lock mode face used for the implicit keyword."
:group 'scala)
(defvar scala-font-lock:implicit-face 'scala-font-lock:implicit-face
"Face for the scala implicit keyword.")
(defface scala-font-lock:lazy-face
'((t (:inherit font-lock-builtin-face)))
"Font Lock mode face used for the lazy keyword."
:group 'scala)
(defvar scala-font-lock:lazy-face 'scala-font-lock:lazy-face
"Face for the scala lazy keyword.")
(defface scala-font-lock:var-keyword-face
'((t (:inherit font-lock-keyword-face)))
"Font Lock mode face used for the var keyword."
:group 'scala)
(defvar scala-font-lock:var-keyword-face 'scala-font-lock:var-keyword-face
"Face for the scala var keyword.")
(provide 'scala-mode-fontlock)
|