Skip to content

Commit 6eb17d9

Browse files
authored
Merge pull request #48 from Hi-Angel/font-lock-operator-fixes
font-lock: unduplicate operator-faces and test them
2 parents 8c47367 + d150f6d commit 6eb17d9

File tree

2 files changed

+195
-48
lines changed

2 files changed

+195
-48
lines changed

purescript-font-lock.el

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -204,54 +204,48 @@ Returns keywords suitable for `font-lock-keywords'."
204204
(concat line-prefix "\\(" varid "\\|" conid "\\)\\s-*`\\(" varid "\\)`"))
205205
(topdecl-sym
206206
(concat line-prefix "\\(" varid "\\|" conid "\\)\\s-*\\(" sym "\\)"))
207-
(topdecl-sym2 (concat line-prefix "(\\(" sym "\\))"))
208-
209-
keywords)
210-
211-
(setq keywords
212-
`(;; NOTICE the ordering below is significant
213-
;;
214-
(,toplevel-keywords 1 (symbol-value 'purescript-keyword-face))
215-
(,reservedid 1 (symbol-value 'purescript-keyword-face))
216-
(,reservedsym 1 (symbol-value 'purescript-operator-face))
217-
;; Special case for `as', `hiding', `safe' and `qualified', which are
218-
;; keywords in import statements but are not otherwise reserved.
219-
("\\<import[ \t]+\\(?:\\(safe\\>\\)[ \t]*\\)?\\(?:\\(qualified\\>\\)[ \t]*\\)?[^ \t\n()]+[ \t]*\\(?:\\(\\<as\\>\\)[ \t]*[^ \t\n()]+[ \t]*\\)?\\(\\<hiding\\>\\)?"
220-
(1 (symbol-value 'purescript-keyword-face) nil lax)
221-
(2 (symbol-value 'purescript-keyword-face) nil lax)
222-
(3 (symbol-value 'purescript-keyword-face) nil lax)
223-
(4 (symbol-value 'purescript-keyword-face) nil lax))
224-
225-
(,reservedsym 1 (symbol-value 'purescript-operator-face))
226-
;; Case for `foreign import'
227-
(,(rx line-start (0+ whitespace)
228-
(group "foreign") (1+ whitespace) (group "import") word-end)
229-
(1 (symbol-value 'purescript-keyword-face) nil lax)
230-
(2 (symbol-value 'purescript-keyword-face) nil lax))
231-
232-
(,reservedsym 1 (symbol-value 'purescript-operator-face))
233-
;; Toplevel Declarations.
234-
;; Place them *before* generic id-and-op highlighting.
235-
(,topdecl-var (1 (symbol-value 'purescript-definition-face)))
236-
(,topdecl-var2 (2 (symbol-value 'purescript-definition-face)))
237-
(,topdecl-sym (2 (symbol-value 'purescript-definition-face)))
238-
(,topdecl-sym2 (1 (symbol-value 'purescript-definition-face)))
239-
240-
;; These four are debatable...
241-
("(\\(,*\\|->\\))" 0 (symbol-value 'purescript-constructor-face))
242-
("\\[\\]" 0 (symbol-value 'purescript-constructor-face))
243-
;; Expensive.
244-
(,qvarid 0 (symbol-value 'purescript-default-face))
245-
(,qconid 0 (symbol-value 'purescript-constructor-face))
246-
(,(concat "\`" varid "\`") 0 (symbol-value 'purescript-operator-face))
247-
;; Expensive.
248-
(,conid 0 (symbol-value 'purescript-constructor-face))
249-
250-
;; Very expensive.
251-
(,sym 0 (if (eq (char-after (match-beginning 0)) ?:)
252-
purescript-constructor-face
253-
purescript-operator-face))))
254-
keywords))
207+
(topdecl-sym2 (concat line-prefix "(\\(" sym "\\))")))
208+
209+
`(;; NOTICE the ordering below is significant
210+
;;
211+
(,toplevel-keywords 1 (symbol-value 'purescript-keyword-face))
212+
(,reservedid 1 (symbol-value 'purescript-keyword-face))
213+
(,reservedsym 1 (symbol-value 'purescript-operator-face))
214+
;; Special case for `as', `hiding', `safe' and `qualified', which are
215+
;; keywords in import statements but are not otherwise reserved.
216+
("\\<import[ \t]+\\(?:\\(safe\\>\\)[ \t]*\\)?\\(?:\\(qualified\\>\\)[ \t]*\\)?[^ \t\n()]+[ \t]*\\(?:\\(\\<as\\>\\)[ \t]*[^ \t\n()]+[ \t]*\\)?\\(\\<hiding\\>\\)?"
217+
(1 (symbol-value 'purescript-keyword-face) nil lax)
218+
(2 (symbol-value 'purescript-keyword-face) nil lax)
219+
(3 (symbol-value 'purescript-keyword-face) nil lax)
220+
(4 (symbol-value 'purescript-keyword-face) nil lax))
221+
222+
;; Case for `foreign import'
223+
(,(rx line-start (0+ whitespace)
224+
(group "foreign") (1+ whitespace) (group "import") word-end)
225+
(1 (symbol-value 'purescript-keyword-face) nil lax)
226+
(2 (symbol-value 'purescript-keyword-face) nil lax))
227+
228+
;; Toplevel Declarations.
229+
;; Place them *before* generic id-and-op highlighting.
230+
(,topdecl-var (1 (symbol-value 'purescript-definition-face)))
231+
(,topdecl-var2 (2 (symbol-value 'purescript-definition-face)))
232+
(,topdecl-sym (2 (symbol-value 'purescript-definition-face)))
233+
(,topdecl-sym2 (1 (symbol-value 'purescript-definition-face)))
234+
235+
;; These four are debatable...
236+
("(\\(,*\\|->\\))" 0 (symbol-value 'purescript-constructor-face))
237+
("\\[\\]" 0 (symbol-value 'purescript-constructor-face))
238+
;; Expensive.
239+
(,qvarid 0 (symbol-value 'purescript-default-face))
240+
(,qconid 0 (symbol-value 'purescript-constructor-face))
241+
(,(concat "\`" varid "\`") 0 (symbol-value 'purescript-operator-face))
242+
;; Expensive.
243+
(,conid 0 (symbol-value 'purescript-constructor-face))
244+
245+
;; Very expensive.
246+
(,sym 0 (if (eq (char-after (match-beginning 0)) ?:)
247+
purescript-constructor-face
248+
purescript-operator-face)))))
255249

256250
;; The next three aren't used in Emacs 21.
257251

tests/purescript-font-lock-tests.el

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,156 @@ foreign importinvalid
355355
(208 229 nil)
356356
(230 236 font-lock-function-name-face)
357357
(237 251 nil))))
358+
359+
(ert-deftest operator-faces-simple ()
360+
"Tests operator faces unrelated to backticks"
361+
(purescript-test-ranges
362+
"-- ..
363+
import Data.Maybe (Maybe(..))
364+
365+
-- -> and ::
366+
id :: a -> a
367+
id = \\x -> x
368+
369+
-- <-
370+
main = do
371+
n <- pure 42
372+
pure n
373+
374+
-- => and =
375+
showVal :: Show a => a -> String
376+
showVal = show
377+
378+
-- @
379+
firstElem j@(Just _) = j
380+
381+
-- guard |
382+
abs' x | x < 0 = -x
383+
| otherwise = x
384+
385+
-- ~ and unicode ∷
386+
toUnfoldable ∷ forall f. Unfoldable f => List ~> f
387+
toUnfoldable = undefined
388+
389+
-- : as an operator
390+
arr :: Array Int
391+
arr = 1 : [2,3]
392+
"
393+
'((1 3 font-lock-comment-delimiter-face)
394+
(4 6 font-lock-comment-face)
395+
(7 12 font-lock-keyword-face)
396+
(13 13 nil)
397+
(14 23 font-lock-type-face)
398+
(24 25 nil)
399+
(26 30 font-lock-type-face)
400+
(31 31 nil)
401+
(32 33 font-lock-variable-name-face)
402+
(34 37 nil)
403+
(38 40 font-lock-comment-delimiter-face)
404+
(41 50 font-lock-comment-face)
405+
(51 52 font-lock-function-name-face)
406+
(53 53 nil)
407+
(54 55 font-lock-variable-name-face)
408+
(56 58 nil)
409+
(59 60 font-lock-variable-name-face)
410+
(61 63 nil)
411+
(64 65 font-lock-function-name-face)
412+
(66 66 nil)
413+
(67 67 font-lock-variable-name-face)
414+
(68 68 nil)
415+
(69 69 font-lock-variable-name-face)
416+
(70 71 nil)
417+
(72 73 font-lock-variable-name-face)
418+
(74 77 nil)
419+
(78 80 font-lock-comment-delimiter-face)
420+
(81 83 font-lock-comment-face)
421+
(84 87 font-lock-function-name-face)
422+
(88 88 nil)
423+
(89 89 font-lock-variable-name-face)
424+
(90 90 nil)
425+
(91 92 font-lock-keyword-face)
426+
(93 97 nil)
427+
(98 99 font-lock-variable-name-face)
428+
(100 118 nil)
429+
(119 121 font-lock-comment-delimiter-face)
430+
(122 130 font-lock-comment-face)
431+
(131 137 font-lock-function-name-face)
432+
(138 138 nil)
433+
(139 140 font-lock-variable-name-face)
434+
(141 141 nil)
435+
(142 145 font-lock-type-face)
436+
(146 148 nil)
437+
(149 150 font-lock-variable-name-face)
438+
(151 153 nil)
439+
(154 155 font-lock-variable-name-face)
440+
(156 156 nil)
441+
(157 162 font-lock-type-face)
442+
(163 163 nil)
443+
(164 170 font-lock-function-name-face)
444+
(171 171 nil)
445+
(172 172 font-lock-variable-name-face)
446+
(173 179 nil)
447+
(180 182 font-lock-comment-delimiter-face)
448+
(183 184 font-lock-comment-face)
449+
(185 193 font-lock-function-name-face)
450+
(194 195 nil)
451+
(196 196 font-lock-variable-name-face)
452+
(197 197 nil)
453+
(198 201 font-lock-type-face)
454+
(202 202 nil)
455+
(203 203 font-lock-keyword-face)
456+
(204 205 nil)
457+
(206 206 font-lock-variable-name-face)
458+
(207 210 nil)
459+
(211 213 font-lock-comment-delimiter-face)
460+
(214 221 font-lock-comment-face)
461+
(222 225 font-lock-function-name-face)
462+
(226 228 nil)
463+
(229 229 font-lock-variable-name-face)
464+
(230 232 nil)
465+
(233 233 font-lock-variable-name-face)
466+
(234 236 nil)
467+
(237 237 font-lock-variable-name-face)
468+
(238 238 nil)
469+
(239 239 font-lock-variable-name-face)
470+
(240 248 nil)
471+
(249 249 font-lock-variable-name-face)
472+
(250 260 nil)
473+
(261 261 font-lock-variable-name-face)
474+
(262 265 nil)
475+
(266 268 font-lock-comment-delimiter-face)
476+
(269 284 font-lock-comment-face)
477+
(285 296 font-lock-function-name-face)
478+
(297 297 nil)
479+
(298 298 font-lock-variable-name-face)
480+
(299 307 nil)
481+
(308 308 font-lock-variable-name-face)
482+
(309 309 nil)
483+
(310 319 font-lock-type-face)
484+
(320 322 nil)
485+
(323 324 font-lock-variable-name-face)
486+
(325 325 nil)
487+
(326 329 font-lock-type-face)
488+
(330 330 nil)
489+
(331 332 font-lock-variable-name-face)
490+
(333 335 nil)
491+
(336 347 font-lock-function-name-face)
492+
(348 348 nil)
493+
(349 349 font-lock-variable-name-face)
494+
(350 361 nil)
495+
(362 364 font-lock-comment-delimiter-face)
496+
(365 381 font-lock-comment-face)
497+
(382 384 font-lock-function-name-face)
498+
(385 385 nil)
499+
(386 387 font-lock-variable-name-face)
500+
(388 388 nil)
501+
(389 393 font-lock-type-face)
502+
(394 394 nil)
503+
(395 397 font-lock-type-face)
504+
(398 398 nil)
505+
(399 401 font-lock-function-name-face)
506+
(402 402 nil)
507+
(403 403 font-lock-variable-name-face)
508+
(404 406 nil)
509+
(407 407 font-lock-type-face)
510+
(408 414 nil))))

0 commit comments

Comments
 (0)