Skip to content

Commit c943e3f

Browse files
committed
feat(comments): support comment-start and variable keywords
1 parent 2e2550c commit c943e3f

2 files changed

Lines changed: 66 additions & 41 deletions

File tree

convention-comments.el

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,58 @@
1313
(defvar convention-comments-decoration-colors
1414
'("#1f77b4" "#ff7f0e" "#2ca02c" "#d62728" "#9467bd" "#17becf"
1515
"#bcbd22" "#7f7f7f" "#e377c2" "#ff6f61" "#30cfcf" "#b5bd00"
16-
"#a01920" "#4db6ac" "#9c89b8" "#8c564b" "#8dd35f""#6a5acd")
16+
"#a01920" "#4db6ac" "#9c89b8" "#8c564b" "#8dd35f" "#6a5acd")
1717
"List of decoration colors to alternate through.")
1818

19-
(defsubst convention-comments-syntax-elisp-keywords ()
20-
"Create conventional comments keywords for ELisp."
21-
`((,(rx (literal ";") (*? whitespace)
22-
(group (or "praise" "nitpick" "suggestion"
23-
"issue" "todo" "question"
24-
"thought" "chore" "note"))
19+
(defvar convention-comments-keywords
20+
'("praise" "nitpick" "suggestion"
21+
"issue" "todo" "question"
22+
"thought" "chore" "note")
23+
"Default used keywords.")
24+
25+
(defsubst convention-comments-syntax-keywords (comment-string)
26+
"Create conventional comments keywords for ELisp.
27+
Argument COMMENT-STRING represents one or more characters beginning a comment."
28+
(let ((plain
29+
(rx-to-string `(and
30+
(literal ,comment-string) (*? whitespace)
31+
(group (or ,@convention-comments-keywords))
2532
(*? not-newline)
2633
(group (literal ":"))
2734
(group (*? not-newline))
28-
line-end)
29-
(1 'font-lock-keyword-face t)
30-
(2 'bold t)
31-
(3 'italic append))
32-
(,(rx (literal ";") (*? whitespace)
33-
(or "praise" "nitpick" "suggestion"
34-
"issue" "todo" "question"
35-
"thought" "chore" "note")
35+
line-end)))
36+
(decorated
37+
(rx-to-string `(and
38+
(literal ,comment-string) (*? whitespace)
39+
(or ,@convention-comments-keywords)
3640
(*? not-newline)
3741
(group (literal "("))
3842
(*? not-newline)
3943
(group (literal ")"))
4044
(group (literal ":"))
4145
(group (*? not-newline))
42-
line-end)
43-
(1 'bold t)
44-
(2 'bold t)
45-
(3 'bold t)
46-
(4 'italic append))
47-
(,(rx (literal ";") (*? whitespace)
48-
(or "praise" "nitpick" "suggestion"
49-
"issue" "todo" "question"
50-
"thought" "chore" "note")
46+
line-end)))
47+
(decoration-anchor
48+
(rx-to-string `(and
49+
(literal ,comment-string) (*? whitespace)
50+
(or ,@convention-comments-keywords)
5151
(*? whitespace)
5252
(literal "(")
5353
(group (*? anychar))
5454
(literal ")")
55-
(literal ":"))
56-
(,(rx (+? (group (+ not-newline)) (*? (literal ","))))
55+
(literal ":"))))
56+
(decoration (rx (+? (group (+ not-newline)) (*? (literal ","))))))
57+
`((,plain
58+
(1 'font-lock-keyword-face t)
59+
(2 'bold t)
60+
(3 'italic append))
61+
(,decorated
62+
(1 'bold t)
63+
(2 'bold t)
64+
(3 'bold t)
65+
(4 'italic append))
66+
(,decoration-anchor
67+
(,decoration
5768
;; pre-match form
5869
(progn
5970
;; start matching in the parenthesis
@@ -80,29 +91,43 @@
8091
;; post-match form
8192
nil
8293
;; no group-matching props needed
83-
))))
94+
)))))
8495

85-
(defsubst convention-comments-set-syntax-elisp ()
86-
"Add conventional comments syntax in ELisp."
96+
(defsubst convention-comments-set-syntax (comment-string)
97+
"Add conventional comments syntax in ELisp.
98+
Argument COMMENT-STRING represents one or more characters beginning a comment."
8799
(convention-comments-syntax-defaults)
88-
(font-lock-add-keywords nil (convention-comments-syntax-elisp-keywords))
100+
(font-lock-add-keywords nil (convention-comments-syntax-keywords
101+
comment-string))
89102
(font-lock-update))
90103

91-
(defsubst convention-comments-unset-syntax-elisp ()
92-
"Remove conventional comments syntax in ELisp."
104+
(defsubst convention-comments-unset-syntax (comment-string)
105+
"Remove conventional comments syntax in ELisp.
106+
Argument COMMENT-STRING represents one or more characters beginning a comment."
93107
(convention-comments-syntax-defaults)
94-
(font-lock-remove-keywords nil (convention-comments-syntax-elisp-keywords))
108+
(font-lock-remove-keywords nil (convention-comments-syntax-keywords
109+
comment-string))
95110
(font-lock-update))
96111

97112
(defun convention-comments-syntax--activate ()
98113
"Add conventional comments syntax."
99-
(cond ((derived-mode-p 'emacs-lisp-mode)
100-
(convention-comments-set-syntax-elisp))))
114+
(if (and (boundp 'comment-start)
115+
comment-start
116+
(not (string= "" comment-start)))
117+
(convention-comments-set-syntax comment-start)
118+
(warn "convention: missing comment-start, fallback")
119+
(cond ((derived-mode-p 'emacs-lisp-mode)
120+
(convention-comments-set-syntax ";")))))
101121

102122
(defun convention-comments-syntax--deactivate ()
103123
"Remove conventional comments syntax."
104-
(cond ((derived-mode-p 'emacs-lisp-mode)
105-
(convention-comments-unset-syntax-elisp))))
124+
(if (and (boundp 'comment-start)
125+
comment-start
126+
(not (string= "" comment-start)))
127+
(convention-comments-unset-syntax comment-start)
128+
(warn "convention: missing comment-start, fallback")
129+
(cond ((derived-mode-p 'emacs-lisp-mode)
130+
(convention-comments-unset-syntax ";")))))
106131

107132

108133
(provide 'convention-comments)

convention-commits.el

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"#a01920" "#4db6ac" "#9c89b8" "#8c564b" "#8dd35f" "#6a5acd")
1616
"List of decoration colors to alternate through.")
1717

18-
(defvar convention-keywords
18+
(defvar convention-commits-keywords
1919
'("build" "chore" "ci"
2020
"docs" "feat" "fix"
2121
"perf" "refactor" "revert"
@@ -26,19 +26,19 @@
2626
"Create conventional comments keywords."
2727
(let ((plain
2828
(rx-to-string `(and
29-
string-start (group (or ,@convention-keywords)) (*? not-newline)
29+
string-start (group (or ,@convention-commits-keywords)) (*? not-newline)
3030
(group (? (literal "!"))) (group (literal ":"))
3131
(group (*? not-newline))
3232
line-end)))
3333
(decorated
3434
(rx-to-string `(and
35-
string-start (or ,@convention-keywords) (*? not-newline)
35+
string-start (or ,@convention-commits-keywords) (*? not-newline)
3636
(group (literal "(")) (*? not-newline) (group (literal ")"))
3737
(group (? (literal "!"))) (group (literal ":"))
3838
(group (*? not-newline)) line-end)))
3939
(decoration-anchor
4040
(rx-to-string `(and
41-
string-start (or ,@convention-keywords) (*? whitespace)
41+
string-start (or ,@convention-commits-keywords) (*? whitespace)
4242
(literal "(") (group (*? anychar)) (literal ")") (? (literal "!"))
4343
(literal ":"))))
4444
(decoration (rx (+? (group (+ not-newline)) (*? (literal ","))))))

0 commit comments

Comments
 (0)