[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/geiser-guile 8f57a6a 034/284: Autodoc system revamped.
From: |
Philip Kaludercic |
Subject: |
[nongnu] elpa/geiser-guile 8f57a6a 034/284: Autodoc system revamped. |
Date: |
Sun, 1 Aug 2021 18:29:11 -0400 (EDT) |
branch: elpa/geiser-guile
commit 8f57a6ad7b612f9645b1ba088cd4f1fe1c72a51b
Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
Commit: Jose Antonio Ortega Ruiz <jao@gnu.org>
Autodoc system revamped.
---
geiser/emacs.scm | 2 +-
geiser/introspection.scm | 118 +++++++++++++++++++++++------------------------
2 files changed, 60 insertions(+), 60 deletions(-)
diff --git a/geiser/emacs.scm b/geiser/emacs.scm
index f440827..7f03be8 100644
--- a/geiser/emacs.scm
+++ b/geiser/emacs.scm
@@ -29,7 +29,7 @@
ge:compile
ge:compile-file
ge:load-file)
- #:re-export (ge:arguments
+ #:re-export (ge:autodoc
ge:completions
ge:symbol-location
ge:symbol-documentation
diff --git a/geiser/introspection.scm b/geiser/introspection.scm
index ca6afae..4b833d5 100644
--- a/geiser/introspection.scm
+++ b/geiser/introspection.scm
@@ -25,7 +25,7 @@
;;; Code:
(define-module (geiser introspection)
- #:export (arguments
+ #:export (autodoc
completions
symbol-location
symbol-documentation
@@ -33,23 +33,63 @@
module-children
module-location)
#:use-module (system vm program)
+ #:use-module (ice-9 regex)
#:use-module (ice-9 session)
#:use-module (ice-9 documentation)
#:use-module (srfi srfi-1))
-(define (arguments sym . syms)
- (let loop ((sym sym) (syms syms))
- (cond ((obj-args (symbol->obj sym)) => (lambda (args)
- (cons sym (apply args-alist
args))))
- ((null? syms) #f)
- (else (loop (car syms) (cdr syms))))))
+(define (autodoc form)
+ (cond ((null? form) #f)
+ ((symbol? form) (describe-application (list form)))
+ ((list? form)
+ (let ((lst (last form)))
+ (cond ((symbol? lst) (or (describe-application (list lst))
+ (describe-application form)))
+ ((list? lst)
+ (or (autodoc lst)
+ (autodoc (map (lambda (s) (if (list? s) (gensym) s))
form))))
+ (else (describe-application form)))))
+ (else #f)))
-(define (args-alist args opt module)
- (list (cons 'required args)
- (cons 'optional (or opt '()))
- (cons 'module (cond ((module? module) (module-name module))
- ((list? module) module)
- (else '())))))
+(define (describe-application form)
+ (let* ((fun (car form))
+ (args (obj-args (symbol->obj fun))))
+ (and args
+ (list (cons 'signature (signature fun args))
+ (cons 'position (find-position args form))
+ (cons 'module (symbol-module fun))))))
+
+(define (signature fun args)
+ (let ((req (assq-ref args 'required))
+ (opt (assq-ref args 'optional))
+ (key (assq-ref args 'keyword))
+ (rest (assq-ref args 'rest)))
+ (let ((sgn `(,fun ,@(or req '())
+ ,@(if opt (cons #:optional opt) '())
+ ,@(if key (cons #:key key) '()))))
+ (if rest `(,@sgn . ,rest) sgn))))
+
+(define (find-position args form)
+ (let* ((lf (length form))
+ (lf-1 (- lf 1)))
+ (if (= 1 lf) 0
+ (let ((req (length (or (assq-ref args 'required) '())))
+ (opt (length (or (assq-ref args 'optional) '())))
+ (keys (map (lambda (k) (symbol->keyword (if (list? k) (car k)
k)))
+ (or (assq-ref args 'keyword) '())))
+ (rest (assq-ref args 'rest)))
+ (cond ((<= lf (+ 1 req)) lf-1)
+ ((<= lf (+ 1 req opt)) (if (> opt 0) lf lf-1))
+ ((or (memq (last form) keys)
+ (memq (car (take-right form 2)) keys)) =>
+ (lambda (sl)
+ (+ 2 req
+ (if (> opt 0) (+ 1 opt) 0)
+ (- (length keys) (length sl)))))
+ (else (+ 1 req
+ (if (> opt 0) (+ 1 opt) 0)
+ (if (null? keys) 0 (+ 1 (length keys)))
+ (if rest 1 0))))))))
(define (symbol->obj sym)
(and (symbol? sym)
@@ -58,9 +98,9 @@
(define (obj-args obj)
(cond ((not obj) #f)
- ((program? obj) (program-args obj))
- ((procedure? obj) (procedure-args obj))
- ((macro? obj) (macro-args obj))
+ ((or (procedure? obj) (program? obj)) (procedure-arguments obj))
+ ((macro? obj) (or (obj-args (macro-transformer obj))
+ '((required ...))))
(else #f)))
(define (symbol-module sym)
@@ -70,47 +110,9 @@
(apropos-fold (lambda (module name var init)
(if (eq? name sym) (k (module-name module)) init))
#f
- (symbol->string sym)
+ (regexp-quote (symbol->string sym))
(apropos-fold-accessible (current-module)))))))
-(define (program-args program)
- (let* ((arity (program-arity program))
- (arg-no (first arity))
- (opt (> (second arity) 0))
- (args (map first (take (program-bindings program) arg-no))))
- (list (if opt (drop-right args 1) args)
- (and opt (last args))
- (program-module program))))
-
-(define (procedure-args proc)
- (let ((name (procedure-name proc)))
- (cond ((procedure-source proc) => (lambda (src)
- (procedure-args-from-source name src)))
- (else (let* ((arity (procedure-property proc 'arity))
- (req (first arity))
- (opt (third arity)))
- (list (map (lambda (n)
- (string->symbol (format "arg~A" (+ 1 n))))
- (iota req))
- (and opt 'rest)
- (and name (symbol-module name))))))))
-
-(define (procedure-args-from-source name src)
- (let ((formals (cadr src)))
- (cond ((list? formals) (list formals #f (symbol-module name)))
- ((pair? formals) (let ((req (car formals))
- (opt (cdr formals)))
- (list (if (list? req) req (list req))
- opt
- (symbol-module name))))
- (else #f))))
-
-(define (macro-args macro)
- (let ((prog (macro-transformer macro)))
- (if prog
- (obj-args prog)
- (list '(...) #f #f))))
-
(define (completions prefix)
(sort! (map symbol->string
(apropos-internal (string-append "^" prefix)))
@@ -147,10 +149,8 @@
(if doc (display doc))))))
(define (obj-signature sym obj)
- (let* ((args (obj-args obj))
- (req (and args (car args)))
- (opt (and args (cadr args))))
- (and args (if (not opt) `(,sym ,@req) `(,sym ,@req . ,opt)))))
+ (let ((args (obj-args obj)))
+ (and args (signature sym args))))
(define (symbol-documentation sym)
(let ((obj (symbol->obj sym)))
- [nongnu] elpa/geiser-guile ce78e11 069/284: Better xref display., (continued)
- [nongnu] elpa/geiser-guile ce78e11 069/284: Better xref display., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile 4eeb068 071/284: Module completion generalized and implemented for PLT., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile 63ebbc1 070/284: Using the new (system xref) interface., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile 54484ab 075/284: Fixes for module names reading and evaluation result display., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile b14ac49 079/284: Guile: rewriting stack trace captures - not yet complete., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile b32aed7 077/284: module-children -> module-exports., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile 19f55b0 097/284: New implementation registration mechanism, for the elisp side of things., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile c34d042 091/284: Yet another deklugdification: locals scanning moved to elisp., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile a329773 094/284: BSD relicensing: Guile code., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile 694539c 101/284: Guile: bug fix: a macro-transformer is not a good arity info source., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile 8f57a6a 034/284: Autodoc system revamped.,
Philip Kaludercic <=
- [nongnu] elpa/geiser-guile f48e83f 065/284: Accept a list as Guile binary., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile 4e1d945 088/284: Simpler, more correct and efficient autodoc implementation., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile 6d3f98b 085/284: Guile: unbreaking evaluation., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile 0e5a52b 090/284: Leftover removed., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile 58ae3a1 100/284: Cosmetics., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile 5807a76 105/284: Guile: adjustment to stack size display., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile a405f22 104/284: Guile: fix for file loading (we always compile them now)., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile 134606d 102/284: Guile: Support for multiple arities in autodoc., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile c34995d 110/284: Guile: support for the REPL debugger, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-guile 55c37c7 109/284: Guile: Minimal support for the new REPL debug mode., Philip Kaludercic, 2021/08/01