Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add documentation macros #1415

Merged
merged 1 commit into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Changes from 0.13.0
* `while` loops may now contain an `else` clause, like `for` loops
* `xi` from `hy.extra.anaphoric` is now the `#%` tag macro
* `#%` works on any expression and has a new `&kwargs` parameter `%**`
* new `doc` macro and `#doc` tag macro

[ Bug Fixes ]
* Numeric literals are no longer parsed as symbols when followed by a dot
Expand Down
35 changes: 35 additions & 0 deletions docs/language/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,41 @@ Some example usage:
``do`` can accept any number of arguments, from 1 to n.


doc / #doc
----------

Documentation macro and tag macro.
Gets help for macros or tag macros, respectively.

.. code-block:: clj

=> (doc doc)
Help on function (doc) in module hy.core.macros:

(doc)(symbol)
macro documentation

Gets help for a macro function available in this module.
Use ``require`` to make other macros available.

Use ``#doc foo`` instead for help with tag macro ``#foo``.
Use ``(help foo)`` instead for help with runtime objects.

=> (doc comment)
Help on function (comment) in module hy.core.macros:

(comment)(*body)
Ignores body and always expands to None

=> #doc doc
Help on function #doc in module hy.core.macros:

#doc(symbol)
tag macro documentation

Gets help for a tag macro function available in this module.


def / setv
----------

Expand Down
39 changes: 39 additions & 0 deletions hy/core/macros.hy
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,49 @@ Such 'o!' params are availible within `body` as the equivalent 'g!' symbol."


(deftag @ [expr]
"with-decorator tag macro"
(setv decorators (cut expr None -1)
fndef (get expr -1))
`(with-decorator ~@decorators ~fndef))

(defmacro comment [&rest body]
"Ignores body and always expands to None"
None)

(defmacro doc [symbol]
"macro documentation

Gets help for a macro function available in this module.
Use ``require`` to make other macros available.

Use ``#doc foo`` instead for help with tag macro ``#foo``.
Use ``(help foo)`` instead for help with runtime objects."
`(try
(help (. (__import__ "hy")
macros
_hy_macros
[__name__]
['~symbol]))
(except [KeyError]
(help (. (__import__ "hy")
macros
_hy_macros
[None]
['~symbol])))))

(deftag doc [symbol]
"tag macro documentation

Gets help for a tag macro function available in this module."
`(try
(help (. (__import__ "hy")
macros
_hy_tag
[__name__]
['~symbol]))
(except [KeyError]
(help (. (__import__ "hy")
macros
_hy_tag
[None]
['~symbol])))))
2 changes: 2 additions & 0 deletions hy/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def macro(name):

"""
def _(fn):
fn.__name__ = "({})".format(name)
try:
argspec = getargspec(fn)
fn._hy_macro_pass_compiler = argspec.keywords is not None
Expand Down Expand Up @@ -63,6 +64,7 @@ def tag(name):

"""
def _(fn):
fn.__name__ = '#{}'.format(name)
module_name = fn.__module__
if module_name.startswith("hy.core"):
module_name = None
Expand Down