From 19e2c198b13c8dd8c35d286f03787e2ee3dc24a1 Mon Sep 17 00:00:00 2001 From: Alexandre Grison Date: Tue, 7 Jan 2020 23:13:08 +0100 Subject: [PATCH] (clojure) Add support for global definitions Adds support for Clojure global definitions with class name `title`, including: * def * defonce * defprotocol * defstruct * defmulti * defmethod * defn * defn- * defmacro * deftype * defrecord --- AUTHORS.en.txt | 1 + src/languages/clojure.js | 75 +++++++++++-------- .../clojure/globals_definition.expect.txt | 61 +++++++++++++++ test/markup/clojure/globals_definition.txt | 61 +++++++++++++++ test/markup/clojure/hint_col.expect.txt | 2 +- .../markup/clojure/symbols-numbers.expect.txt | 2 +- 6 files changed, 170 insertions(+), 32 deletions(-) create mode 100644 test/markup/clojure/globals_definition.expect.txt create mode 100644 test/markup/clojure/globals_definition.txt diff --git a/AUTHORS.en.txt b/AUTHORS.en.txt index f312186d1b..a5523a3133 100644 --- a/AUTHORS.en.txt +++ b/AUTHORS.en.txt @@ -284,3 +284,4 @@ Contributors: - Thomas Reichel - G8t Guy - Samia Ali +- Alexandre Grison diff --git a/src/languages/clojure.js b/src/languages/clojure.js index 9e69379ef7..8ab9bfe0dc 100644 --- a/src/languages/clojure.js +++ b/src/languages/clojure.js @@ -2,43 +2,45 @@ Language: Clojure Description: Clojure syntax (based on lisp.js) Author: mfornos -Contributors: Martin Clausen +Contributors: Martin Clausen ; Alexandre Grison Website: https://clojure.org Category: lisp */ function(hljs) { + var globals = 'def defonce defprotocol defstruct defmulti defmethod defn defn- defmacro deftype defrecord'; var keywords = { 'builtin-name': // Clojure keywords - 'def defonce cond apply if-not if-let if not not= = < > <= >= == + / * - rem '+ - 'quot neg? pos? delay? symbol? keyword? true? false? integer? empty? coll? list? '+ - 'set? ifn? fn? associative? sequential? sorted? counted? reversible? number? decimal? '+ - 'class? distinct? isa? float? rational? reduced? ratio? odd? even? char? seq? vector? '+ - 'string? map? nil? contains? zero? instance? not-every? not-any? libspec? -> ->> .. . '+ - 'inc compare do dotimes mapcat take remove take-while drop letfn drop-last take-last '+ - 'drop-while while intern condp case reduced cycle split-at split-with repeat replicate '+ - 'iterate range merge zipmap declare line-seq sort comparator sort-by dorun doall nthnext '+ - 'nthrest partition eval doseq await await-for let agent atom send send-off release-pending-sends '+ - 'add-watch mapv filterv remove-watch agent-error restart-agent set-error-handler error-handler '+ - 'set-error-mode! error-mode shutdown-agents quote var fn loop recur throw try monitor-enter '+ - 'monitor-exit defmacro defn defn- macroexpand macroexpand-1 for dosync and or '+ - 'when when-not when-let comp juxt partial sequence memoize constantly complement identity assert '+ - 'peek pop doto proxy defstruct first rest cons defprotocol cast coll deftype defrecord last butlast '+ - 'sigs reify second ffirst fnext nfirst nnext defmulti defmethod meta with-meta ns in-ns create-ns import '+ - 'refer keys select-keys vals key val rseq name namespace promise into transient persistent! conj! '+ - 'assoc! dissoc! pop! disj! use class type num float double short byte boolean bigint biginteger '+ - 'bigdec print-method print-dup throw-if printf format load compile get-in update-in pr pr-on newline '+ - 'flush read slurp read-line subvec with-open memfn time re-find re-groups rand-int rand mod locking '+ - 'assert-valid-fdecl alias resolve ref deref refset swap! reset! set-validator! compare-and-set! alter-meta! '+ - 'reset-meta! commute get-validator alter ref-set ref-history-count ref-min-history ref-max-history ensure sync io! '+ - 'new next conj set! to-array future future-call into-array aset gen-class reduce map filter find empty '+ - 'hash-map hash-set sorted-map sorted-map-by sorted-set sorted-set-by vec vector seq flatten reverse assoc dissoc list '+ - 'disj get union difference intersection extend extend-type extend-protocol int nth delay count concat chunk chunk-buffer '+ - 'chunk-append chunk-first chunk-rest max min dec unchecked-inc-int unchecked-inc unchecked-dec-inc unchecked-dec unchecked-negate '+ - 'unchecked-add-int unchecked-add unchecked-subtract-int unchecked-subtract chunk-next chunk-cons chunked-seq? prn vary-meta '+ + globals + + 'cond apply if-not if-let if not not= = < > <= >= == + / * - rem ' + + 'quot neg? pos? delay? symbol? keyword? true? false? integer? empty? coll? list? ' + + 'set? ifn? fn? associative? sequential? sorted? counted? reversible? number? decimal? ' + + 'class? distinct? isa? float? rational? reduced? ratio? odd? even? char? seq? vector? ' + + 'string? map? nil? contains? zero? instance? not-every? not-any? libspec? -> ->> .. . ' + + 'inc compare do dotimes mapcat take remove take-while drop letfn drop-last take-last ' + + 'drop-while while intern condp case reduced cycle split-at split-with repeat replicate ' + + 'iterate range merge zipmap declare line-seq sort comparator sort-by dorun doall nthnext ' + + 'nthrest partition eval doseq await await-for let agent atom send send-off release-pending-sends ' + + 'add-watch mapv filterv remove-watch agent-error restart-agent set-error-handler error-handler ' + + 'set-error-mode! error-mode shutdown-agents quote var fn loop recur throw try monitor-enter ' + + 'monitor-exit macroexpand macroexpand-1 for dosync and or ' + + 'when when-not when-let comp juxt partial sequence memoize constantly complement identity assert ' + + 'peek pop doto proxy first rest cons cast coll last butlast ' + + 'sigs reify second ffirst fnext nfirst nnext meta with-meta ns in-ns create-ns import ' + + 'refer keys select-keys vals key val rseq name namespace promise into transient persistent! conj! ' + + 'assoc! dissoc! pop! disj! use class type num float double short byte boolean bigint biginteger ' + + 'bigdec print-method print-dup throw-if printf format load compile get-in update-in pr pr-on newline ' + + 'flush read slurp read-line subvec with-open memfn time re-find re-groups rand-int rand mod locking ' + + 'assert-valid-fdecl alias resolve ref deref refset swap! reset! set-validator! compare-and-set! alter-meta! ' + + 'reset-meta! commute get-validator alter ref-set ref-history-count ref-min-history ref-max-history ensure sync io! ' + + 'new next conj set! to-array future future-call into-array aset gen-class reduce map filter find empty ' + + 'hash-map hash-set sorted-map sorted-map-by sorted-set sorted-set-by vec vector seq flatten reverse assoc dissoc list ' + + 'disj get union difference intersection extend extend-type extend-protocol int nth delay count concat chunk chunk-buffer ' + + 'chunk-append chunk-first chunk-rest max min dec unchecked-inc-int unchecked-inc unchecked-dec-inc unchecked-dec unchecked-negate ' + + 'unchecked-add-int unchecked-add unchecked-subtract-int unchecked-subtract chunk-next chunk-cons chunked-seq? prn vary-meta ' + 'lazy-seq spread list* str find-keyword keyword symbol gensym force rationalize' - }; + }; var SYMBOLSTART = 'a-zA-Z_\\-!.?+*=<>&#\''; var SYMBOL_RE = '[' + SYMBOLSTART + '][' + SYMBOLSTART + '0-9/;:]*'; @@ -91,7 +93,20 @@ function(hljs) { }; var DEFAULT_CONTAINS = [LIST, STRING, HINT, HINT_COL, COMMENT, KEY, COLLECTION, NUMBER, LITERAL, SYMBOL]; - LIST.contains = [hljs.COMMENT('comment', ''), NAME, BODY]; + var GLOBAL = { + beginKeywords: globals, + end: '(\\[|\\#|\\d|"|:|\\{|\\)|\\(|$)', + excludeEnd: true, + contains: [ + { + className: 'title', + begin: SYMBOL_RE, + relevance: 0 + }, + ].concat(DEFAULT_CONTAINS) + }; + + LIST.contains = [hljs.COMMENT('comment', ''), GLOBAL, NAME, BODY]; BODY.contains = DEFAULT_CONTAINS; COLLECTION.contains = DEFAULT_CONTAINS; HINT_COL.contains = [COLLECTION]; @@ -100,5 +115,5 @@ function(hljs) { aliases: ['clj'], illegal: /\S/, contains: [LIST, STRING, HINT, HINT_COL, COMMENT, KEY, COLLECTION, NUMBER, LITERAL] - } + }; } diff --git a/test/markup/clojure/globals_definition.expect.txt b/test/markup/clojure/globals_definition.expect.txt new file mode 100644 index 0000000000..2be72ba64d --- /dev/null +++ b/test/markup/clojure/globals_definition.expect.txt @@ -0,0 +1,61 @@ +(ns playground + (:require + [clojure.string :as str])) + +; function +(defn clojure-function [args] + (let [string "multiline\nstring" + regexp #"regexp" + number 100,000 + booleans [false true] + keyword ::the-keyword] + ;; this is comment + (if true + (->> + (list [vector] {:map map} #{'set}))))) + +; global +(def some-var) +; another one +(def alternative-var "132") +; defonce +(defonce ^:private another-var #"foo") + +; private function +(defn- add [x y] (+ x y)) + +; protocols +(defprotocol Fly + "A simple protocol for flying" + (fly [this] "Method to fly")) + +(defrecord Bird [name species] + Fly + (fly [this] (str (:name this) " flies..."))) + +; multimethods +(defmulti service-charge (fn [acct] [(account-level acct) (:tag acct)])) +(defmethod service-charge [::acc/Basic ::acc/Checking] [_] 25) +(defmethod service-charge [::acc/Basic ::acc/Savings] [_] 10) +(defmethod service-charge [::acc/Premium ::acc/Account] [_] 0) + +; macros +(defmacro unless [pred a b] + `(if (not ~pred) ~a ~b)) + +(unless false (println "Will print") (println "Will not print")) + +; types +(deftype Circle [radius]) +(deftype Square [length width]) + +;; multimethods again +(defmulti area class) +(defmethod area Circle [c] + (* Math/PI (* (.radius c) (.radius c)))) +(defmethod area Square [s] + (* (.length s) (.width s))) + +;; create a couple shapes and get their area +(def myCircle (Circle. 10)) +(def mySquare (Square. 5 11)) diff --git a/test/markup/clojure/globals_definition.txt b/test/markup/clojure/globals_definition.txt new file mode 100644 index 0000000000..6faeee7942 --- /dev/null +++ b/test/markup/clojure/globals_definition.txt @@ -0,0 +1,61 @@ +(ns playground + (:require + [clojure.string :as str])) + +; function +(defn clojure-function [args] + (let [string "multiline\nstring" + regexp #"regexp" + number 100,000 + booleans [false true] + keyword ::the-keyword] + ;; this is comment + (if true + (->> + (list [vector] {:map map} #{'set}))))) + +; global +(def some-var) +; another one +(def alternative-var "132") +; defonce +(defonce ^:private another-var #"foo") + +; private function +(defn- add [x y] (+ x y)) + +; protocols +(defprotocol Fly + "A simple protocol for flying" + (fly [this] "Method to fly")) + +(defrecord Bird [name species] + Fly + (fly [this] (str (:name this) " flies..."))) + +; multimethods +(defmulti service-charge (fn [acct] [(account-level acct) (:tag acct)])) +(defmethod service-charge [::acc/Basic ::acc/Checking] [_] 25) +(defmethod service-charge [::acc/Basic ::acc/Savings] [_] 10) +(defmethod service-charge [::acc/Premium ::acc/Account] [_] 0) + +; macros +(defmacro unless [pred a b] + `(if (not ~pred) ~a ~b)) + +(unless false (println "Will print") (println "Will not print")) + +; types +(deftype Circle [radius]) +(deftype Square [length width]) + +;; multimethods again +(defmulti area class) +(defmethod area Circle [c] + (* Math/PI (* (.radius c) (.radius c)))) +(defmethod area Square [s] + (* (.length s) (.width s))) + +;; create a couple shapes and get their area +(def myCircle (Circle. 10)) +(def mySquare (Square. 5 11)) diff --git a/test/markup/clojure/hint_col.expect.txt b/test/markup/clojure/hint_col.expect.txt index 06e96c00c3..fd61cae7dd 100644 --- a/test/markup/clojure/hint_col.expect.txt +++ b/test/markup/clojure/hint_col.expect.txt @@ -4,7 +4,7 @@ (definterface Foo (foo [])) ;; annotation on type -(deftype ^{Deprecated true +(deftype ^{Deprecated true Retention RetentionPolicy/RUNTIME javax.annotation.processing.SupportedOptions ["foo" "bar" "baz"] javax.xml.ws.soap.Addressing {:enabled false :required true} diff --git a/test/markup/clojure/symbols-numbers.expect.txt b/test/markup/clojure/symbols-numbers.expect.txt index f9c48b2039..87595d111b 100644 --- a/test/markup/clojure/symbols-numbers.expect.txt +++ b/test/markup/clojure/symbols-numbers.expect.txt @@ -1 +1 @@ -(def +x [(a 1) +2 -3.0 y-5]) +(def +x [(a 1) +2 -3.0 y-5])