diff options
| author | Andrey Listopadov <andreyorst@gmail.com> | 2021-07-23 21:41:22 +0300 |
|---|---|---|
| committer | Andrey Listopadov <andreyorst@gmail.com> | 2021-07-23 21:41:22 +0300 |
| commit | 5cd4100a4e9e7b7cf77b21f2759f6401cd9a06d9 (patch) | |
| tree | 684fed91c5f78fff1c0c242306541f4c5efe71c4 | |
| parent | 859cf1388770f65d31002d3667977d6760c2e092 (diff) | |
fix: don't generate mulitarity arglist doc for single-arity functions
| -rw-r--r-- | .dir-locals.el | 6 | ||||
| -rw-r--r-- | CHANGELOG.md | 5 | ||||
| -rw-r--r-- | doc/cljlib.md | 102 | ||||
| -rw-r--r-- | init-macros.fnl | 14 | ||||
| -rw-r--r-- | tests/fn.fnl | 35 |
5 files changed, 89 insertions, 73 deletions
diff --git a/.dir-locals.el b/.dir-locals.el index af4b307..be31ff9 100644 --- a/.dir-locals.el +++ b/.dir-locals.el @@ -32,6 +32,12 @@ "loop")) word-end) 1 'font-lock-keyword-face)))) + (eval . (font-lock-add-keywords + 'fennel-mode + `((,(rx (syntax open-parenthesis) + (or "fn*" "defn" "defmulti") (1+ space) + (group (1+ (or (syntax word) (syntax symbol) "-" "_")))) + 1 'font-lock-function-name-face)))) (eval . (put 'when-meta 'fennel-indent-function 'defun)) (eval . (put 'defmethod 'fennel-indent-function 3)) (eval . (put 'defmulti 'bfennel-indent-function 'defun)) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5553189..7bf2237 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## Cljlib v0.5.5 (????-??-??) + +- Better generation of arglist docs for single-arity functions. + ## Cljlib v0.5.4 (2021-07-22) - Remove `when-meta` macro. @@ -51,4 +55,5 @@ First stable release of Fennel Cljlib. <!-- LocalWords: Cljlib namespace Memoization metatable metamethods + LocalWords: arglist arity --> diff --git a/doc/cljlib.md b/doc/cljlib.md index 6d7acc1..1e88856 100644 --- a/doc/cljlib.md +++ b/doc/cljlib.md @@ -225,7 +225,7 @@ Returns true if nums are in monotonically increasing order Function signature: ``` -(inc ([x])) +(inc [x]) ``` Increase number `x` by one @@ -234,7 +234,7 @@ Increase number `x` by one Function signature: ``` -(dec ([x])) +(dec [x]) ``` Decrease number `x` by one @@ -273,7 +273,7 @@ Deep comparison is used for tables which use tables as keys: Function signature: ``` -(map? ([tbl])) +(map? [tbl]) ``` Check whether `tbl` is an associative table. @@ -315,7 +315,7 @@ Empty tables created with [`hash-map`](#hash-map) will pass the test: Function signature: ``` -(vector? ([tbl])) +(vector? [tbl]) ``` Check whether `tbl` is an sequential table. @@ -357,7 +357,7 @@ Empty tables created with [`vector`](#vector-1) will pass the test: Function signature: ``` -(multifn? ([mf])) +(multifn? [mf]) ``` Test if `mf` is an instance of `multifn`. @@ -369,7 +369,7 @@ from `macros.fnl`. Function signature: ``` -(set? ([s])) +(set? [s]) ``` Test if `s` is either instance of a [`hash-set`](#hash-set) or [`ordered-set`](#ordered-set). @@ -387,7 +387,7 @@ Test if `x` is nil. Function signature: ``` -(zero? ([x])) +(zero? [x]) ``` Test if `x` is equal to zero. @@ -396,7 +396,7 @@ Test if `x` is equal to zero. Function signature: ``` -(pos? ([x])) +(pos? [x]) ``` Test if `x` is greater than zero. @@ -405,7 +405,7 @@ Test if `x` is greater than zero. Function signature: ``` -(neg? ([x])) +(neg? [x]) ``` Test if `x` is less than zero. @@ -414,7 +414,7 @@ Test if `x` is less than zero. Function signature: ``` -(even? ([x])) +(even? [x]) ``` Test if `x` is even. @@ -423,7 +423,7 @@ Test if `x` is even. Function signature: ``` -(odd? ([x])) +(odd? [x]) ``` Test if `x` is odd. @@ -432,7 +432,7 @@ Test if `x` is odd. Function signature: ``` -(string? ([x])) +(string? [x]) ``` Test if `x` is a string. @@ -441,7 +441,7 @@ Test if `x` is a string. Function signature: ``` -(boolean? ([x])) +(boolean? [x]) ``` Test if `x` is a Boolean @@ -450,7 +450,7 @@ Test if `x` is a Boolean Function signature: ``` -(true? ([x])) +(true? [x]) ``` Test if `x` is `true` @@ -459,7 +459,7 @@ Test if `x` is `true` Function signature: ``` -(false? ([x])) +(false? [x]) ``` Test if `x` is `false` @@ -468,7 +468,7 @@ Test if `x` is `false` Function signature: ``` -(int? ([x])) +(int? [x]) ``` Test if `x` is a number without floating point data. @@ -479,7 +479,7 @@ Number is rounded with `math.floor` and compared with original number. Function signature: ``` -(pos-int? ([x])) +(pos-int? [x]) ``` Test if `x` is a positive integer. @@ -488,7 +488,7 @@ Test if `x` is a positive integer. Function signature: ``` -(neg-int? ([x])) +(neg-int? [x]) ``` Test if `x` is a negative integer. @@ -497,7 +497,7 @@ Test if `x` is a negative integer. Function signature: ``` -(double? ([x])) +(double? [x]) ``` Test if `x` is a number with floating point data. @@ -506,7 +506,7 @@ Test if `x` is a number with floating point data. Function signature: ``` -(empty? ([x])) +(empty? [x]) ``` Check if collection is empty. @@ -515,7 +515,7 @@ Check if collection is empty. Function signature: ``` -(not-empty ([x])) +(not-empty [x]) ``` If `x` is empty, returns `nil`, otherwise `x`. @@ -524,7 +524,7 @@ If `x` is empty, returns `nil`, otherwise `x`. Function signature: ``` -(vector ([& args])) +(vector [& args]) ``` Constructs sequential table out of it's arguments. @@ -542,7 +542,7 @@ Sets additional metadata for function [`vector?`](#vector) to work. Function signature: ``` -(seq ([col])) +(seq [col]) ``` Create sequential table. @@ -582,7 +582,7 @@ Additionally you can use [`conj`](#conj) and [`apply`](#apply) with Function signature: ``` -(kvseq ([col])) +(kvseq [col]) ``` Transforms any table `col` to key-value sequence. @@ -591,7 +591,7 @@ Transforms any table `col` to key-value sequence. Function signature: ``` -(first ([col])) +(first [col]) ``` Return first element of a table. Calls [`seq`](#seq) on its argument. @@ -600,7 +600,7 @@ Return first element of a table. Calls [`seq`](#seq) on its argument. Function signature: ``` -(rest ([col])) +(rest [col]) ``` Returns table of all elements of a table but the first one. Calls @@ -610,7 +610,7 @@ Returns table of all elements of a table but the first one. Calls Function signature: ``` -(last ([col])) +(last [col]) ``` Returns the last element of a table. Calls [`seq`](#seq) on its argument. @@ -619,7 +619,7 @@ Returns the last element of a table. Calls [`seq`](#seq) on its argument. Function signature: ``` -(butlast ([col])) +(butlast [col]) ``` Returns everything but the last element of a table as a new @@ -683,7 +683,7 @@ Remove key `k` from set `s`. Function signature: ``` -(cons ([x tbl])) +(cons [x tbl]) ``` Insert `x` to `tbl` at the front. Calls [`seq`](#seq) on `tbl`. @@ -733,7 +733,7 @@ Reduce sequence of numbers with [`add`](#add) Function signature: ``` -(reduced ([x])) +(reduced [x]) ``` Wraps `x` in such a way so [`reduce`](#reduce) will terminate early @@ -765,7 +765,7 @@ valid number, but we've terminated right before we've reached it. Function signature: ``` -(reduce-kv ([f val tbl])) +(reduce-kv [f val tbl]) ``` Reduces an associative table using function `f` and initial value `val`. @@ -856,7 +856,7 @@ Basic `zipmap` implementation: Function signature: ``` -(filter ([pred col])) +(filter [pred col]) ``` Returns a sequential table of the items in `col` for which `pred` @@ -866,7 +866,7 @@ Returns a sequential table of the items in `col` for which `pred` Function signature: ``` -(every? ([pred tbl])) +(every? [pred tbl]) ``` Test if every item in `tbl` satisfies the `pred`. @@ -875,7 +875,7 @@ Test if every item in `tbl` satisfies the `pred`. Function signature: ``` -(some ([pred tbl])) +(some [pred tbl]) ``` Test if any item in `tbl` satisfies the `pred`. @@ -884,7 +884,7 @@ Test if any item in `tbl` satisfies the `pred`. Function signature: ``` -(not-any? ([pred tbl])) +(not-any? [pred tbl]) ``` Test if no item in `tbl` satisfy the `pred`. @@ -902,7 +902,7 @@ return range of of numbers from `lower` to `upper` with optional `step`. Function signature: ``` -(reverse ([tbl])) +(reverse [tbl]) ``` Returns table with same items as in `tbl` but in reverse order. @@ -911,7 +911,7 @@ Returns table with same items as in `tbl` but in reverse order. Function signature: ``` -(take ([n col])) +(take [n col]) ``` Returns a sequence of the first `n` items in `col`, or all items if @@ -921,7 +921,7 @@ there are fewer than `n`. Function signature: ``` -(nthrest ([col n])) +(nthrest [col n]) ``` Returns the nth rest of `col`, `col` when `n` is 0. @@ -979,7 +979,7 @@ Additional padding can be used to supply insufficient elements: Function signature: ``` -(identity ([x])) +(identity [x]) ``` Returns its argument. @@ -997,7 +997,7 @@ Compose functions. Function signature: ``` -(complement ([f])) +(complement [f]) ``` Takes a function `f` and returns the function that takes the same @@ -1008,7 +1008,7 @@ oppisite truth value. Function signature: ``` -(constantly ([x])) +(constantly [x]) ``` Returns a function that takes any number of arguments and returns `x`. @@ -1017,7 +1017,7 @@ Returns a function that takes any number of arguments and returns `x`. Function signature: ``` -(memoize ([f])) +(memoize [f]) ``` Returns a memoized version of a referentially transparent function. @@ -1071,7 +1071,7 @@ found in the table. Function signature: ``` -(keys ([tbl])) +(keys [tbl]) ``` Returns a sequence of the table's keys, in the same order as [`seq`](#seq). @@ -1080,7 +1080,7 @@ Returns a sequence of the table's keys, in the same order as [`seq`](#seq). Function signature: ``` -(vals ([tbl])) +(vals [tbl]) ``` Returns a sequence of the table's values, in the same order as [`seq`](#seq). @@ -1089,7 +1089,7 @@ Returns a sequence of the table's values, in the same order as [`seq`](#seq). Function signature: ``` -(find ([tbl key])) +(find [tbl key]) ``` Returns the map entry for `key`, or `nil` if key not present in `tbl`. @@ -1107,7 +1107,7 @@ Remove `key` from table `tbl`. Optionally takes more `keys`. Function signature: ``` -(remove-method ([multimethod dispatch-value])) +(remove-method [multimethod dispatch-value]) ``` Remove method from `multimethod` for given `dispatch-value`. @@ -1116,7 +1116,7 @@ Remove method from `multimethod` for given `dispatch-value`. Function signature: ``` -(remove-all-methods ([multimethod])) +(remove-all-methods [multimethod]) ``` Removes all of the methods of `multimethod` @@ -1125,7 +1125,7 @@ Removes all of the methods of `multimethod` Function signature: ``` -(methods ([multimethod])) +(methods [multimethod]) ``` Given a `multimethod`, returns a map of dispatch values -> dispatch fns @@ -1134,7 +1134,7 @@ Given a `multimethod`, returns a map of dispatch values -> dispatch fns Function signature: ``` -(get-method ([multimethod dispatch-value])) +(get-method [multimethod dispatch-value]) ``` Given a `multimethod` and a `dispatch-value`, returns the dispatch @@ -1145,7 +1145,7 @@ default. Function signature: ``` -(ordered-set ([& xs])) +(ordered-set [& xs]) ``` Create ordered set. @@ -1233,7 +1233,7 @@ same size: Function signature: ``` -(hash-set ([& xs])) +(hash-set [& xs]) ``` Create hash set. diff --git a/init-macros.fnl b/init-macros.fnl index 0b5fe65..a9762c8 100644 --- a/init-macros.fnl +++ b/init-macros.fnl @@ -224,11 +224,11 @@ this stuff will only work if you use `require-macros' instead of (string.format "%q" data) (tostring data)))) -(fn gen-arglist-doc [args method?] +(fn gen-arglist-doc [args method? multi?] (if (list? (. args 1)) (let [arglist []] (each [_ v (ipairs args)] - (let [arglist-doc (gen-arglist-doc v method?)] + (let [arglist-doc (gen-arglist-doc v method? (list? (. args 2)))] (when (next arglist-doc) (table.insert arglist (table.concat arglist-doc " "))))) (when (and (> (length (table.concat arglist " ")) 60) @@ -239,18 +239,20 @@ this stuff will only work if you use `require-macros' instead of (sequence? (. args 1)) (let [arglist [] + open (if multi? "([" "[") + close (if multi? "])" "]") args (if method? [(sym :self) (table.unpack (. args 1))] (. args 1)) len (length args)] (if (= len 0) - (table.insert arglist "([])") + (table.insert arglist (.. open close)) (each [i v (ipairs args)] (table.insert arglist (match i - (1 ? (= len 1)) (.. "([" (deep-tostring v) "])") - 1 (.. "([" (deep-tostring v)) - len (.. (deep-tostring v) "])") + (1 ? (= len 1)) (.. open (deep-tostring v) close) + 1 (.. open (deep-tostring v)) + len (.. (deep-tostring v) close) _ (deep-tostring v))))) arglist))) diff --git a/tests/fn.fnl b/tests/fn.fnl index 36fd395..a4b342b 100644 --- a/tests/fn.fnl +++ b/tests/fn.fnl @@ -4,46 +4,46 @@ (deftest fn* (testing "fn* meta" (fn* f - "docstring" + "single arity" [x] x) (assert-eq (meta f) - {:fnl/docstring "docstring" - :fnl/arglist ["([x])"]}) + {:fnl/docstring "single arity" + :fnl/arglist ["[x]"]}) (fn* f - "docstring" + "single empty arity" []) (assert-eq (meta f) - {:fnl/docstring "docstring" - :fnl/arglist ["([])"]}) + {:fnl/docstring "single empty arity" + :fnl/arglist ["[]"]}) (fn* f - "docstring" + "multiarity with single entry" ([x] x)) (assert-eq (meta f) - {:fnl/docstring "docstring" - :fnl/arglist ["([x])"]}) + {:fnl/docstring "multiarity with single entry" + :fnl/arglist ["[x]"]}) (fn* f - "docstring" + "multiarity" ([x] x) ([x y] (+ x y))) (assert-eq (meta f) - {:fnl/docstring "docstring" + {:fnl/docstring "multiarity" :fnl/arglist ["([x])" "([x y])"]}) (fn* f - "docstring" + "multiarity with one empty arity" ([]) ([x y] (+ x y))) (assert-eq (meta f) - {:fnl/docstring "docstring" + {:fnl/docstring "multiarity with one empty arity" :fnl/arglist ["([])" "([x y])"]}) (fn* f - "docstring" + "multiarity with two or more arity" ([x] x) ([x y] (+ x y)) ([x y & z] (+ x y))) (assert-eq (meta f) - {:fnl/docstring "docstring" + {:fnl/docstring "multiarity with two or more arity" :fnl/arglist ["([x])" "([x y])" "([x y & z])"]})) @@ -51,7 +51,10 @@ (testing "fn* doc destructuring" (fn* f [[a b c]]) (assert-eq (meta f) - {:fnl/arglist ["([[a b c]])"]}) + {:fnl/arglist ["[[a b c]]"]}) + (fn* f ([[a b c]])) + (assert-eq (meta f) + {:fnl/arglist ["[[a b c]]"]}) (fn* f ([[a b c]]) ([{: a}]) ([[{:a [a b c]}]])) (assert-eq (meta f) {:fnl/arglist ["([[a b c]])" |