Skip to content

Commit

Permalink
Add "housekeeping" commands
Browse files Browse the repository at this point in the history
Needs pdb commit d613857 or later, which exposes `db-stats`.
  • Loading branch information
greghendershott committed Aug 8, 2024
1 parent 0566de5 commit 940c0b5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
56 changes: 56 additions & 0 deletions racket-pdb.el
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,62 @@ Uses pdb to query for sites among multiple files."
:company-location (racket--xp-make-company-location-proc)
:company-doc-buffer (racket--xp-make-company-doc-buffer-proc))))

;;; Housekeeping

(defun racket-pdb-forget-file (filename)
"Tell pdb to forget analyses involving FILENAME."
(interactive "FForget file: ")
(racket--cmd/async nil `(pdb forget-path ,filename)))

(defun racket-pdb-add-directory (always directory import-depth)
"Tell pdb to analyze files in and under DIRECTORY.
Useful to add all files for a project. In general, pdb can
analyze files on-demand, including for commands like
`xref-find-definitions'. However commands like
`xref-find-references' can only find references in files that
have already analyzed, which is where proactive analysis can
help.
The analysis proceeds asynchronously. The results are stored in
the pdb database.
When IMPORT-DEPTH is greater than zero, it means to also analyze
imported files. For example a depth of 1 means to analyze only
immediate, direct imports. A very lage depth, like 1,000, means
to analyze imports until they reach modules like #%core or
#%runtime.
When ALWAYS is not nil -- as with a command prefix when used
interactively -- even if the database appears to have up-to-date
results, the analysis will be redone. "
(interactive "P\nDAnalyze files in and under directory:\nnImport depth: ")
(unless directory
(setq directory default-directory))
(racket--cmd/async nil `(pdb add-directory
,(and always t)
,directory
,import-depth)))

(defun racket-pdb-forget-directory (directory)
"Tell pdb to forget analyses involving files in and under DIRECTORY."
(interactive "GForget files in and under directory: ")
(racket--cmd/async nil `(pdb forget-directory ,directory)))

(defun racket-pdb-stats ()
"Show statistics about the pdb package's database."
(interactive)
(racket--cmd/async
nil `(pdb db-stats)
(lambda (str)
(help-setup-xref (list #'racket-pdb-stats)
(called-interactively-p 'interactive))
(with-help-window (help-buffer)
(with-current-buffer standard-output
(insert (propertize "pdb database statistics\n\n"
'font-lock-face 'package-help-section-name))
(insert str))))))

;;; Mode line status

(defvar-local racket--pdb-mode-status nil)
Expand Down
31 changes: 26 additions & 5 deletions racket/commands/pdb.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
racket/set
racket/string
syntax/parse/define
"../util.rkt")
"../util.rkt"
"../elisp.rkt")

(provide pdb-command)

Expand All @@ -27,20 +28,27 @@
(dynamic-require 'pdb 'id) ...)))])

(define-from-pdb available?
[analyze-path get-errors get-submodule-names get-completion-candidates
get-point-info get-doc-link get-require-path
use->def rename-sites])
[analyze-path forget-path
add-directory forget-directory
get-errors get-submodule-names get-completion-candidates
get-point-info get-doc-link get-require-path
use->def rename-sites
db-stats])

(define (pdb-command . args)
(match args
[`(available?) (available?)]
[`(analyze-path ,path ,code) (pdb-analyze-path path code)]
[`(forget-path ,path) (pdb-forget-path path)]
[`(add-directory ,path ,depth ,always) (pdb-add-directory path depth always)]
[`(forget-directory ,path) (pdb-forget-directory path)]
[`(submodules ,path ,pos) (pdb-submodules path pos)]
[`(completions ,path ,pos) (pdb-completions path pos)]
[`(point-info ,path ,pos ,beg ,end) (pdb-point-info path pos beg end)]
[`(doc-link ,path ,pos) (pdb-doc-link path pos)]
[`(visit ,path, pos) (pdb-visit path pos)]
[`(rename-sites ,path ,pos) (pdb-rename-sites path pos)]))
[`(rename-sites ,path ,pos) (pdb-rename-sites path pos)]
[`(db-stats) (db-stats)]))

(define (pdb-analyze-path path-str code-str)
(define path (string->path path-str))
Expand All @@ -49,6 +57,19 @@
`(break) ;abandoned due to newer request; ignore/cleanup
(list (cons 'errors (get-errors path)))))

(define (pdb-forget-path path-str)
(define path (string->path path-str))
(forget-path path))

(define (pdb-add-directory path-str depth always)
(define path (string->path path-str))
(define always? (as-racket-bool always))
(add-directory path #:import-depth depth #:always always?))

(define (pdb-forget-directory path-str)
(define path (string->path path-str))
(forget-directory))

(define (pdb-submodules path-str pos)
(define path (string->path path-str))
(get-submodule-names path pos))
Expand Down

0 comments on commit 940c0b5

Please sign in to comment.