Skip to content

Commit 7c62b2f

Browse files
authored
Merge pull request #40 from Hi-Angel/more-tests2
Comment-highlight tests and fixing wrong doc-string highlight left from Haskell
2 parents e870a83 + 81d0cbc commit 7c62b2f

File tree

3 files changed

+162
-49
lines changed

3 files changed

+162
-49
lines changed

purescript-font-lock.el

+9-38
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ Returns keywords suitable for `font-lock-keywords'."
177177
;; record fields or other identifiers.
178178
(toplevel-keywords
179179
(rx line-start (zero-or-more whitespace)
180-
(group (or "type" "module" "import" "data" "class" "newtype"
180+
(group (or "type" "import" "data" "class" "newtype"
181181
"instance" "derive")
182182
word-end)))
183183
;; Reserved identifiers
@@ -186,7 +186,7 @@ Returns keywords suitable for `font-lock-keywords'."
186186
;; spec syntax, but they are not reserved.
187187
;; `_' can go in here since it has temporary word syntax.
188188
(regexp-opt
189-
'("ado" "case" "do" "else" "if" "in" "infix"
189+
'("ado" "case" "do" "else" "if" "in" "infix" "module"
190190
"infixl" "infixr" "let" "of" "then" "where" "_") 'words))
191191

192192
;; Top-level declarations
@@ -262,17 +262,6 @@ Returns keywords suitable for `font-lock-keywords'."
262262
(,sym 0 (if (eq (char-after (match-beginning 0)) ?:)
263263
purescript-constructor-face
264264
purescript-operator-face))))
265-
(unless (boundp 'font-lock-syntactic-keywords)
266-
(cl-case literate
267-
(bird
268-
(setq keywords
269-
`(("^[^>\n].*$" 0 purescript-comment-face t)
270-
,@keywords
271-
("^>" 0 purescript-default-face t))))
272-
((latex tex)
273-
(setq keywords
274-
`((purescript-fl-latex-comments 0 'font-lock-comment-face t)
275-
,@keywords)))))
276265
keywords))
277266

278267
;; The next three aren't used in Emacs 21.
@@ -363,9 +352,6 @@ that should be commented under LaTeX-style literate scripts."
363352
:type 'boolean
364353
:group 'purescript)
365354

366-
(defvar purescript-font-lock-seen-docstring nil)
367-
(make-variable-buffer-local 'purescript-font-lock-seen-docstring)
368-
369355
(defvar purescript-literate)
370356

371357
(defun purescript-syntactic-face-function (state)
@@ -383,31 +369,16 @@ that should be commented under LaTeX-style literate scripts."
383369
;; b) {-^ ... -}
384370
;; c) -- | ...
385371
;; d) -- ^ ...
386-
;; e) -- ...
387-
;; Where `e' is the tricky one: it is only a docstring comment if it
388-
;; follows immediately another docstring comment. Even an empty line
389-
;; breaks such a sequence of docstring comments. It is not clear if `e'
390-
;; can follow any other case, so I interpreted it as following only cases
391-
;; c,d,e (not a or b). In any case, this `e' is expensive since it
392-
;; requires extra work for each and every non-docstring comment, so I only
393-
;; go through the more expensive check if we've already seen a docstring
394-
;; comment in the buffer.
372+
373+
;; Worth pointing out purescript opted out of ability to continue
374+
;; docs-comment by omitting an empty line like in Haskell, see:
375+
;; https://github.com/purescript/documentation/blob/master/language/Syntax.md
376+
;; IOW, given a `-- | foo' line followed by `-- bar' line, the latter is a
377+
;; plain comment.
395378
((and purescript-font-lock-docstrings
396379
(save-excursion
397380
(goto-char (nth 8 state))
398-
(or (looking-at "\\(-- \\|{-\\)[ \\t]*[|^]")
399-
(and purescript-font-lock-seen-docstring
400-
(looking-at "-- ")
401-
(let ((doc nil)
402-
pos)
403-
(while (and (not doc)
404-
(setq pos (line-beginning-position))
405-
(forward-comment -1)
406-
(eq (line-beginning-position 2) pos)
407-
(looking-at "--\\( [|^]\\)?"))
408-
(setq doc (match-beginning 1)))
409-
doc)))))
410-
(set (make-local-variable 'purescript-font-lock-seen-docstring) t)
381+
(looking-at "\\(-- \\|{-\\)[ \\t]*[|^]")))
411382
'font-lock-doc-face)
412383
(t 'font-lock-comment-face)))
413384

purescript-mode.el

+1-10
Original file line numberDiff line numberDiff line change
@@ -322,16 +322,7 @@ see documentation for that variable for more details."
322322
(set (make-local-variable 'comment-end-skip) "[ \t]*\\(-}\\|\\s>\\)")
323323
(set (make-local-variable 'parse-sexp-ignore-comments) nil)
324324
(set (make-local-variable 'indent-line-function) 'purescript-mode-suggest-indent-choice)
325-
;; Set things up for font-lock.
326-
(set (make-local-variable 'font-lock-defaults)
327-
'(purescript-font-lock-choose-keywords
328-
nil nil ((?\' . "w") (?_ . "w")) nil
329-
(font-lock-syntactic-keywords
330-
. purescript-font-lock-choose-syntactic-keywords)
331-
(font-lock-syntactic-face-function
332-
. purescript-syntactic-face-function)
333-
;; Get help from font-lock-syntactic-keywords.
334-
(parse-sexp-lookup-properties . t)))
325+
(purescript-font-lock-defaults-create) ; set things up for font-lock.
335326
;; PureScript's layout rules mean that TABs have to be handled with extra care.
336327
;; The safer option is to avoid TABs. The second best is to make sure
337328
;; TABs stops are 8 chars apart, as mandated by the PureScript Report. --Stef

tests/purescript-font-lock-tests.el

+152-1
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,14 @@ hello
8080
"foo = \"\"\"
8181
# a string with hashtag
8282
# another # one
83+
-- not a comment --
84+
-- | not a comment
85+
{- not a comment -}
8386
\"\"\"
8487
"
8588
'((1 3 font-lock-function-name-face)
8689
(5 5 font-lock-variable-name-face)
87-
(7 55 font-lock-string-face))))
90+
(7 114 font-lock-string-face))))
8891

8992
(ert-deftest multiline-string-with-embedded-strings ()
9093
:expected-result :failed
@@ -96,3 +99,151 @@ this = \"still a string\"
9699
'((1 3 font-lock-function-name-face)
97100
(5 5 font-lock-variable-name-face)
98101
(7 37 font-lock-string-face))))
102+
103+
(ert-deftest docs-bar-comment-different-spacings ()
104+
(purescript-test-ranges
105+
"-- | Docs comment 1 space
106+
-- | Docs comment many spaces
107+
"
108+
'((1 57 font-lock-doc-face))))
109+
110+
(ert-deftest docs-bar-comment-continuation ()
111+
"Acc. to
112+
https://github.com/purescript/documentation/blob/master/language/Syntax.md
113+
PureScript explicitly doesn't support Haskell-style docs continuation
114+
where vertical bar is omitted"
115+
(purescript-test-ranges
116+
"-- | Docs start
117+
-- continue
118+
"
119+
'((1 16 font-lock-doc-face)
120+
(17 19 font-lock-comment-delimiter-face)
121+
(20 28 font-lock-comment-face))))
122+
123+
(ert-deftest docs-cap-comment-different-spacings ()
124+
(purescript-test-ranges
125+
"-- ^ Docs comment space
126+
-- ^ Docs comment many spaces
127+
"
128+
'((1 57 font-lock-doc-face))))
129+
130+
(ert-deftest multiline-comment ()
131+
(purescript-test-ranges
132+
"{-
133+
multiline comment
134+
-- | not a doc
135+
--| not a doc
136+
still comment
137+
-}
138+
noncomment
139+
{--}
140+
noncomment
141+
"
142+
'((1 64 font-lock-comment-face)
143+
(65 66 font-lock-comment-delimiter-face)
144+
(67 78 nil)
145+
(79 80 font-lock-comment-face)
146+
(81 82 font-lock-comment-delimiter-face)
147+
(83 93 nil))))
148+
149+
(ert-deftest multiline-comment-w-delimiter-inside ()
150+
:expected-result :failed
151+
(purescript-test-ranges
152+
"{- {-{- -} noncomment"
153+
'((1 6 font-lock-comment-face)
154+
(7 10 font-lock-comment-delimiter-face)
155+
(11 21 nil))))
156+
157+
(ert-deftest type-with-typenames-and--> ()
158+
(purescript-test-ranges
159+
"type Component props = Effect (props -> JSX)"
160+
'((1 4 font-lock-keyword-face)
161+
(5 5 nil)
162+
(6 14 font-lock-type-face)
163+
(15 21 nil)
164+
(22 22 font-lock-variable-name-face)
165+
(23 23 nil)
166+
(24 29 font-lock-type-face)
167+
(30 37 nil)
168+
(38 39 font-lock-variable-name-face)
169+
(40 40 nil)
170+
(41 43 font-lock-type-face)
171+
(44 45 nil))))
172+
173+
(ert-deftest module-in-different-locations ()
174+
(purescript-test-ranges
175+
"module React.Basic.Hooks ( Component, module React.Basic
176+
, module Data.Tuple.Nested ) where
177+
"
178+
'((1 6 font-lock-keyword-face)
179+
(7 7 nil)
180+
(8 24 font-lock-type-face)
181+
(25 27 nil)
182+
(28 36 font-lock-type-face)
183+
(37 38 nil)
184+
(39 44 font-lock-keyword-face)
185+
(45 45 nil)
186+
(46 56 font-lock-type-face)
187+
(57 84 nil)
188+
(85 90 font-lock-keyword-face)
189+
(91 91 nil)
190+
(92 108 font-lock-type-face)
191+
(109 111 nil)
192+
(112 116 font-lock-keyword-face)
193+
(117 117 nil))))
194+
195+
(ert-deftest func-decl-w-do-and-qualified-do ()
196+
(purescript-test-ranges
197+
"mkMyComponent :: Component {}
198+
mkMyComponent = do
199+
modalComp :: (NodeRef -> JSX) <- mkModal
200+
component \"mkMyComponent\" \\_ -> React.do
201+
dialogRef :: NodeRef <- newNodeRef
202+
pure $ R.label_ []
203+
"
204+
'((1 13 font-lock-function-name-face)
205+
(14 14 nil)
206+
(15 16 font-lock-variable-name-face)
207+
(17 17 nil)
208+
(18 26 font-lock-type-face)
209+
(27 30 nil)
210+
(31 43 font-lock-function-name-face)
211+
(44 44 nil)
212+
(45 45 font-lock-variable-name-face)
213+
(46 46 nil)
214+
(47 48 font-lock-keyword-face)
215+
(49 61 nil)
216+
(62 63 font-lock-variable-name-face)
217+
(64 65 nil)
218+
(66 72 font-lock-type-face)
219+
(73 73 nil)
220+
(74 75 font-lock-variable-name-face)
221+
(76 76 nil)
222+
(77 79 font-lock-type-face)
223+
(80 81 nil)
224+
(82 83 font-lock-variable-name-face)
225+
(84 104 nil)
226+
(105 119 font-lock-string-face)
227+
(120 120 nil)
228+
(121 121 font-lock-variable-name-face)
229+
(122 122 font-lock-keyword-face)
230+
(123 123 nil)
231+
(124 125 font-lock-variable-name-face)
232+
(126 126 nil)
233+
(127 131 font-lock-type-face)
234+
(132 132 font-lock-variable-name-face)
235+
(133 134 font-lock-keyword-face)
236+
(135 149 nil)
237+
(150 151 font-lock-variable-name-face)
238+
(152 152 nil)
239+
(153 159 font-lock-type-face)
240+
(160 160 nil)
241+
(161 162 font-lock-variable-name-face)
242+
(163 181 nil)
243+
(182 182 font-lock-variable-name-face)
244+
(183 183 nil)
245+
(184 184 font-lock-type-face)
246+
(185 185 font-lock-variable-name-face)
247+
(186 192 nil)
248+
(193 194 font-lock-type-face)
249+
(195 195 nil))))

0 commit comments

Comments
 (0)