Skip to content

Commit 51a7af4

Browse files
joshgoebelagrison
andauthored
enh(clojure) Adds global definitions with class name title (#2419)
Adds support for Clojure global definitions with class name `title`, including: * def * defonce * defprotocol * defstruct * defmulti * defmethod * defn * defn- * defmacro * deftype * defrecord Original PR work by Alexandre Grison. Co-authored-by: Alexandre Grison <[email protected]>
1 parent f97acb6 commit 51a7af4

File tree

7 files changed

+173
-32
lines changed

7 files changed

+173
-32
lines changed

AUTHORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,4 @@ Contributors:
285285
- Thomas Reichel <[email protected]>
286286
- G8t Guy <[email protected]>
287287
- Samia Ali <[email protected]>
288+
- Alexandre Grison <[email protected]>

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Core Changes:
1616

1717
Language Improvements:
1818

19+
- enh(clojure) Add support for global definitions name (#2347) [Alexandre Grison][]
1920
- enh(fortran) Support Fortran 77 style comments (#2416) [Josh Goebel][]
2021
- (csharp) add support for `@identifier` style identifiers (#2414) [Josh Goebel][]
2122
- fix(elixir) Support function names with a slash (#2406) [Josh Goebel][]
@@ -45,6 +46,7 @@ Developer Tools:
4546

4647
- added Dockerfile for optionally developing with a container
4748

49+
[Alexandre Grison]: https://github.com/agrison
4850
[Josh Goebel]: https://github.com/yyyc514
4951
[Sam Miller]: https://github.com/smillerc
5052
[Robert Riebisch]: https://github.com/bttrx

src/languages/clojure.js

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,44 @@
22
Language: Clojure
33
Description: Clojure syntax (based on lisp.js)
44
Author: mfornos
5-
Contributors: Martin Clausen <[email protected]>
65
Website: https://clojure.org
76
Category: lisp
87
*/
98

109
export default function(hljs) {
10+
var globals = 'def defonce defprotocol defstruct defmulti defmethod defn- defn defmacro deftype defrecord';
1111
var keywords = {
1212
'builtin-name':
1313
// Clojure keywords
14-
'def defonce cond apply if-not if-let if not not= = < > <= >= == + / * - rem '+
15-
'quot neg? pos? delay? symbol? keyword? true? false? integer? empty? coll? list? '+
16-
'set? ifn? fn? associative? sequential? sorted? counted? reversible? number? decimal? '+
17-
'class? distinct? isa? float? rational? reduced? ratio? odd? even? char? seq? vector? '+
18-
'string? map? nil? contains? zero? instance? not-every? not-any? libspec? -> ->> .. . '+
19-
'inc compare do dotimes mapcat take remove take-while drop letfn drop-last take-last '+
20-
'drop-while while intern condp case reduced cycle split-at split-with repeat replicate '+
21-
'iterate range merge zipmap declare line-seq sort comparator sort-by dorun doall nthnext '+
22-
'nthrest partition eval doseq await await-for let agent atom send send-off release-pending-sends '+
23-
'add-watch mapv filterv remove-watch agent-error restart-agent set-error-handler error-handler '+
24-
'set-error-mode! error-mode shutdown-agents quote var fn loop recur throw try monitor-enter '+
25-
'monitor-exit defmacro defn defn- macroexpand macroexpand-1 for dosync and or '+
26-
'when when-not when-let comp juxt partial sequence memoize constantly complement identity assert '+
27-
'peek pop doto proxy defstruct first rest cons defprotocol cast coll deftype defrecord last butlast '+
28-
'sigs reify second ffirst fnext nfirst nnext defmulti defmethod meta with-meta ns in-ns create-ns import '+
29-
'refer keys select-keys vals key val rseq name namespace promise into transient persistent! conj! '+
30-
'assoc! dissoc! pop! disj! use class type num float double short byte boolean bigint biginteger '+
31-
'bigdec print-method print-dup throw-if printf format load compile get-in update-in pr pr-on newline '+
32-
'flush read slurp read-line subvec with-open memfn time re-find re-groups rand-int rand mod locking '+
33-
'assert-valid-fdecl alias resolve ref deref refset swap! reset! set-validator! compare-and-set! alter-meta! '+
34-
'reset-meta! commute get-validator alter ref-set ref-history-count ref-min-history ref-max-history ensure sync io! '+
35-
'new next conj set! to-array future future-call into-array aset gen-class reduce map filter find empty '+
36-
'hash-map hash-set sorted-map sorted-map-by sorted-set sorted-set-by vec vector seq flatten reverse assoc dissoc list '+
37-
'disj get union difference intersection extend extend-type extend-protocol int nth delay count concat chunk chunk-buffer '+
38-
'chunk-append chunk-first chunk-rest max min dec unchecked-inc-int unchecked-inc unchecked-dec-inc unchecked-dec unchecked-negate '+
39-
'unchecked-add-int unchecked-add unchecked-subtract-int unchecked-subtract chunk-next chunk-cons chunked-seq? prn vary-meta '+
14+
globals + ' ' +
15+
'cond apply if-not if-let if not not= = < > <= >= == + / * - rem ' +
16+
'quot neg? pos? delay? symbol? keyword? true? false? integer? empty? coll? list? ' +
17+
'set? ifn? fn? associative? sequential? sorted? counted? reversible? number? decimal? ' +
18+
'class? distinct? isa? float? rational? reduced? ratio? odd? even? char? seq? vector? ' +
19+
'string? map? nil? contains? zero? instance? not-every? not-any? libspec? -> ->> .. . ' +
20+
'inc compare do dotimes mapcat take remove take-while drop letfn drop-last take-last ' +
21+
'drop-while while intern condp case reduced cycle split-at split-with repeat replicate ' +
22+
'iterate range merge zipmap declare line-seq sort comparator sort-by dorun doall nthnext ' +
23+
'nthrest partition eval doseq await await-for let agent atom send send-off release-pending-sends ' +
24+
'add-watch mapv filterv remove-watch agent-error restart-agent set-error-handler error-handler ' +
25+
'set-error-mode! error-mode shutdown-agents quote var fn loop recur throw try monitor-enter ' +
26+
'monitor-exit macroexpand macroexpand-1 for dosync and or ' +
27+
'when when-not when-let comp juxt partial sequence memoize constantly complement identity assert ' +
28+
'peek pop doto proxy first rest cons cast coll last butlast ' +
29+
'sigs reify second ffirst fnext nfirst nnext meta with-meta ns in-ns create-ns import ' +
30+
'refer keys select-keys vals key val rseq name namespace promise into transient persistent! conj! ' +
31+
'assoc! dissoc! pop! disj! use class type num float double short byte boolean bigint biginteger ' +
32+
'bigdec print-method print-dup throw-if printf format load compile get-in update-in pr pr-on newline ' +
33+
'flush read slurp read-line subvec with-open memfn time re-find re-groups rand-int rand mod locking ' +
34+
'assert-valid-fdecl alias resolve ref deref refset swap! reset! set-validator! compare-and-set! alter-meta! ' +
35+
'reset-meta! commute get-validator alter ref-set ref-history-count ref-min-history ref-max-history ensure sync io! ' +
36+
'new next conj set! to-array future future-call into-array aset gen-class reduce map filter find empty ' +
37+
'hash-map hash-set sorted-map sorted-map-by sorted-set sorted-set-by vec vector seq flatten reverse assoc dissoc list ' +
38+
'disj get union difference intersection extend extend-type extend-protocol int nth delay count concat chunk chunk-buffer ' +
39+
'chunk-append chunk-first chunk-rest max min dec unchecked-inc-int unchecked-inc unchecked-dec-inc unchecked-dec unchecked-negate ' +
40+
'unchecked-add-int unchecked-add unchecked-subtract-int unchecked-subtract chunk-next chunk-cons chunked-seq? prn vary-meta ' +
4041
'lazy-seq spread list* str find-keyword keyword symbol gensym force rationalize'
41-
};
42+
};
4243

4344
var SYMBOLSTART = 'a-zA-Z_\\-!.?+*=<>&#\'';
4445
var SYMBOL_RE = '[' + SYMBOLSTART + '][' + SYMBOLSTART + '0-9/;:]*';
@@ -91,7 +92,22 @@ export default function(hljs) {
9192
};
9293
var DEFAULT_CONTAINS = [LIST, STRING, HINT, HINT_COL, COMMENT, KEY, COLLECTION, NUMBER, LITERAL, SYMBOL];
9394

94-
LIST.contains = [hljs.COMMENT('comment', ''), NAME, BODY];
95+
var GLOBAL = {
96+
beginKeywords: globals,
97+
end: '(\\[|\\#|\\d|"|:|\\{|\\)|\\(|$)',
98+
contains: [
99+
{
100+
className: 'title',
101+
begin: SYMBOL_RE,
102+
relevance: 0,
103+
excludeEnd: true,
104+
// we can only have a single title
105+
endsParent: true
106+
},
107+
].concat(DEFAULT_CONTAINS)
108+
};
109+
110+
LIST.contains = [hljs.COMMENT('comment', ''), GLOBAL, NAME, BODY];
95111
BODY.contains = DEFAULT_CONTAINS;
96112
COLLECTION.contains = DEFAULT_CONTAINS;
97113
HINT_COL.contains = [COLLECTION];
@@ -101,5 +117,5 @@ export default function(hljs) {
101117
aliases: ['clj'],
102118
illegal: /\S/,
103119
contains: [LIST, STRING, HINT, HINT_COL, COMMENT, KEY, COLLECTION, NUMBER, LITERAL]
104-
}
120+
};
105121
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
(<span class="hljs-name"><span class="hljs-builtin-name">ns</span></span> playground
2+
(<span class="hljs-symbol">:require</span>
3+
[clojure.string <span class="hljs-symbol">:as</span> str]))
4+
5+
<span class="hljs-comment">; function</span>
6+
(<span class="hljs-keyword">defn</span> <span class="hljs-title">clojure-function</span> [args]
7+
(<span class="hljs-name"><span class="hljs-builtin-name">let</span></span> [string <span class="hljs-string">"multiline\nstring"</span>
8+
regexp #<span class="hljs-string">"regexp"</span>
9+
number <span class="hljs-number">100</span>,<span class="hljs-number">000</span>
10+
booleans [<span class="hljs-literal">false</span> <span class="hljs-literal">true</span>]
11+
keyword <span class="hljs-symbol">::the-keyword</span>]
12+
<span class="hljs-comment">;; this is comment</span>
13+
(<span class="hljs-name"><span class="hljs-builtin-name">if</span></span> <span class="hljs-literal">true</span>
14+
(<span class="hljs-name"><span class="hljs-builtin-name">-&gt;&gt;</span></span>
15+
(<span class="hljs-name"><span class="hljs-builtin-name">list</span></span> [vector] {<span class="hljs-symbol">:map</span> map} #{'set})))))
16+
17+
<span class="hljs-comment">; global</span>
18+
(<span class="hljs-keyword">def</span> <span class="hljs-title">some-var</span>)
19+
<span class="hljs-comment">; another one</span>
20+
(<span class="hljs-keyword">def</span> <span class="hljs-title">alternative-var</span> <span class="hljs-string">"132"</span>)
21+
<span class="hljs-comment">; defonce</span>
22+
(<span class="hljs-keyword">defonce</span> ^<span class="hljs-symbol">:private</span> <span class="hljs-title">another-var</span> #<span class="hljs-string">"foo"</span>)
23+
24+
<span class="hljs-comment">; private function</span>
25+
(<span class="hljs-keyword">defn</span><span class="hljs-title">-</span> add [x y] (<span class="hljs-name"><span class="hljs-builtin-name">+</span></span> x y))
26+
27+
<span class="hljs-comment">; protocols</span>
28+
(<span class="hljs-keyword">defprotocol</span> <span class="hljs-title">Fly</span>
29+
<span class="hljs-string">"A simple protocol for flying"</span>
30+
(<span class="hljs-name">fly</span> [this] <span class="hljs-string">"Method to fly"</span>))
31+
32+
(<span class="hljs-keyword">defrecord</span> <span class="hljs-title">Bird</span> [name species]
33+
Fly
34+
(<span class="hljs-name">fly</span> [this] (<span class="hljs-name"><span class="hljs-builtin-name">str</span></span> (<span class="hljs-symbol">:name</span> this) <span class="hljs-string">" flies..."</span>)))
35+
36+
<span class="hljs-comment">; multimethods</span>
37+
(<span class="hljs-keyword">defmulti</span> <span class="hljs-title">service-charge</span> (<span class="hljs-name"><span class="hljs-builtin-name">fn</span></span> [acct] [(<span class="hljs-name">account-level</span> acct) (<span class="hljs-symbol">:tag</span> acct)]))
38+
(<span class="hljs-keyword">defmethod</span> <span class="hljs-title">service-charge</span> [<span class="hljs-symbol">::acc/Basic</span> <span class="hljs-symbol">::acc/Checking</span>] [_] <span class="hljs-number">25</span>)
39+
(<span class="hljs-keyword">defmethod</span> <span class="hljs-title">service-charge</span> [<span class="hljs-symbol">::acc/Basic</span> <span class="hljs-symbol">::acc/Savings</span>] [_] <span class="hljs-number">10</span>)
40+
(<span class="hljs-keyword">defmethod</span> <span class="hljs-title">service-charge</span> [<span class="hljs-symbol">::acc/Premium</span> <span class="hljs-symbol">::acc/Account</span>] [_] <span class="hljs-number">0</span>)
41+
42+
<span class="hljs-comment">; macros</span>
43+
(<span class="hljs-keyword">defmacro</span> <span class="hljs-title">unless</span> [pred a b]
44+
`(<span class="hljs-name"><span class="hljs-builtin-name">if</span></span> (<span class="hljs-name"><span class="hljs-builtin-name">not</span></span> ~pred) ~a ~b))
45+
46+
(<span class="hljs-name">unless</span> <span class="hljs-literal">false</span> (<span class="hljs-name">println</span> <span class="hljs-string">"Will print"</span>) (<span class="hljs-name">println</span> <span class="hljs-string">"Will not print"</span>))
47+
48+
<span class="hljs-comment">; types</span>
49+
(<span class="hljs-keyword">deftype</span> <span class="hljs-title">Circle</span> [radius])
50+
(<span class="hljs-keyword">deftype</span> <span class="hljs-title">Square</span> [length width])
51+
52+
<span class="hljs-comment">;; multimethods again</span>
53+
(<span class="hljs-keyword">defmulti</span> <span class="hljs-title">area</span> class)
54+
(<span class="hljs-keyword">defmethod</span> <span class="hljs-title">area</span> Circle [c]
55+
(<span class="hljs-name"><span class="hljs-builtin-name">*</span></span> Math/PI (<span class="hljs-name"><span class="hljs-builtin-name">*</span></span> (<span class="hljs-name">.radius</span> c) (<span class="hljs-name">.radius</span> c))))
56+
(<span class="hljs-keyword">defmethod</span> <span class="hljs-title">area</span> Square [s]
57+
(<span class="hljs-name"><span class="hljs-builtin-name">*</span></span> (<span class="hljs-name">.length</span> s) (<span class="hljs-name">.width</span> s)))
58+
59+
<span class="hljs-comment">;; create a couple shapes and get their area</span>
60+
(<span class="hljs-keyword">def</span> <span class="hljs-title">myCircle</span> (<span class="hljs-name">Circle.</span> <span class="hljs-number">10</span>))
61+
(<span class="hljs-keyword">def</span> <span class="hljs-title">mySquare</span> (<span class="hljs-name">Square.</span> <span class="hljs-number">5</span> <span class="hljs-number">11</span>))
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
(ns playground
2+
(:require
3+
[clojure.string :as str]))
4+
5+
; function
6+
(defn clojure-function [args]
7+
(let [string "multiline\nstring"
8+
regexp #"regexp"
9+
number 100,000
10+
booleans [false true]
11+
keyword ::the-keyword]
12+
;; this is comment
13+
(if true
14+
(->>
15+
(list [vector] {:map map} #{'set})))))
16+
17+
; global
18+
(def some-var)
19+
; another one
20+
(def alternative-var "132")
21+
; defonce
22+
(defonce ^:private another-var #"foo")
23+
24+
; private function
25+
(defn- add [x y] (+ x y))
26+
27+
; protocols
28+
(defprotocol Fly
29+
"A simple protocol for flying"
30+
(fly [this] "Method to fly"))
31+
32+
(defrecord Bird [name species]
33+
Fly
34+
(fly [this] (str (:name this) " flies...")))
35+
36+
; multimethods
37+
(defmulti service-charge (fn [acct] [(account-level acct) (:tag acct)]))
38+
(defmethod service-charge [::acc/Basic ::acc/Checking] [_] 25)
39+
(defmethod service-charge [::acc/Basic ::acc/Savings] [_] 10)
40+
(defmethod service-charge [::acc/Premium ::acc/Account] [_] 0)
41+
42+
; macros
43+
(defmacro unless [pred a b]
44+
`(if (not ~pred) ~a ~b))
45+
46+
(unless false (println "Will print") (println "Will not print"))
47+
48+
; types
49+
(deftype Circle [radius])
50+
(deftype Square [length width])
51+
52+
;; multimethods again
53+
(defmulti area class)
54+
(defmethod area Circle [c]
55+
(* Math/PI (* (.radius c) (.radius c))))
56+
(defmethod area Square [s]
57+
(* (.length s) (.width s)))
58+
59+
;; create a couple shapes and get their area
60+
(def myCircle (Circle. 10))
61+
(def mySquare (Square. 5 11))

test/markup/clojure/hint_col.expect.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
(<span class="hljs-name">definterface</span> Foo (<span class="hljs-name">foo</span> []))
55

66
<span class="hljs-comment">;; annotation on type</span>
7-
(<span class="hljs-name"><span class="hljs-builtin-name">deftype</span></span> <span class="hljs-comment">^{Deprecated true
7+
(<span class="hljs-keyword">deftype</span> <span class="hljs-comment">^{Deprecated true
88
Retention RetentionPolicy/RUNTIME
99
javax.annotation.processing.SupportedOptions [<span class="hljs-string">"foo"</span> <span class="hljs-string">"bar"</span> <span class="hljs-string">"baz"</span>]
1010
javax.xml.ws.soap.Addressing {<span class="hljs-symbol">:enabled</span> <span class="hljs-literal">false</span> <span class="hljs-symbol">:required</span> <span class="hljs-literal">true</span>}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(<span class="hljs-name"><span class="hljs-builtin-name">def</span></span> +x [(<span class="hljs-name">a</span> <span class="hljs-number">1</span>) <span class="hljs-number">+2</span> <span class="hljs-number">-3.0</span> y-5])
1+
(<span class="hljs-keyword">def</span> <span class="hljs-title">+x</span> [(<span class="hljs-name">a</span> <span class="hljs-number">1</span>) <span class="hljs-number">+2</span> <span class="hljs-number">-3.0</span> y-5])

0 commit comments

Comments
 (0)