Skip to content

Commit

Permalink
Migrate purescript-ds-create-imenu-index to hashtables
Browse files Browse the repository at this point in the history
The function has implemented a poor man's hash table by manually
enlisting keys and then jumping through the hoops by fetching them
from one place, converting values to symbols to values… In particular,
it was using `symbol-value` to map a symbol to its value, however the
symbol was a local variable, and `symbol-value` doesn't work with them
under lexical-binding, which the file was converted to since commit
9a9f550.

So fix the regression and simplify the code at the same time.

Fixes: #25
  • Loading branch information
Hi-Angel committed Nov 11, 2024
1 parent bc7d7e9 commit e42b147
Showing 1 changed file with 21 additions and 33 deletions.
54 changes: 21 additions & 33 deletions purescript-decl-scan.el
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
(require 'syntax)
(require 'cl-lib)
(require 'imenu)
(require 'subr-x)

(defgroup purescript-decl-scan nil
"PureScript declaration scanning (`imenu' support)."
Expand Down Expand Up @@ -453,6 +454,12 @@ positions and the type is one of the symbols \"variable\", \"datatype\",
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Declaration scanning via `imenu'.

(defmacro purescript-when-let (spec &rest body)
"A wrapper to silence `when-let' deprecation warning"
(if (fboundp 'when-let*)
(list 'when-let* spec (macroexp-progn body))
(with-no-warnings (list 'when-let spec (macroexp-progn body)))))

;;;###autoload
(defun purescript-ds-create-imenu-index ()
"Function for finding `imenu' declarations in PureScript mode.
Expand All @@ -462,11 +469,7 @@ datatypes) in a PureScript file for the `imenu' package."
;; These lists are nested using `(INDEX-TITLE . INDEX-ALIST)'.
(let* ((bird-literate (purescript-ds-bird-p))
(index-alist '())
(index-class-alist '()) ;; Classes
(index-var-alist '()) ;; Variables
(index-imp-alist '()) ;; Imports
(index-inst-alist '()) ;; Instances
(index-type-alist '()) ;; Datatypes
(imenu (make-hash-table :test 'equal))
;; Variables for showing progress.
(bufname (buffer-name))
(divisor-of-progress (max 1 (/ (buffer-size) 100)))
Expand All @@ -486,40 +489,25 @@ datatypes) in a PureScript file for the `imenu' package."
(name (car name-posns))
(posns (cdr name-posns))
(start-pos (car posns))
(type (cdr result))
;; Place `(name . start-pos)' in the correct alist.
(sym (cdr (assq type
'((variable . index-var-alist)
(datatype . index-type-alist)
(class . index-class-alist)
(import . index-imp-alist)
(instance . index-inst-alist))))))
(set sym (cons (cons name start-pos) (symbol-value sym))))))
(type (cdr result)))
(puthash type
(cons (cons name start-pos) (gethash type imenu '()))
imenu))))
;; Now sort all the lists, label them, and place them in one list.
(message "Sorting declarations in %s..." bufname)
(when index-type-alist
(push (cons "Datatypes"
(sort index-type-alist 'purescript-ds-imenu-label-cmp))
index-alist))
(when index-inst-alist
(push (cons "Instances"
(sort index-inst-alist 'purescript-ds-imenu-label-cmp))
index-alist))
(when index-imp-alist
(push (cons "Imports"
(sort index-imp-alist 'purescript-ds-imenu-label-cmp))
index-alist))
(when index-class-alist
(push (cons "Classes"
(sort index-class-alist 'purescript-ds-imenu-label-cmp))
index-alist))
(when index-var-alist
(dolist (type '((datatype . "Datatypes") (instance . "Instances")
(import . "Imports") (class . "Classes")))
(purescript-when-let ((curr-alist (gethash (car type) imenu)))
(push (cons (cdr type)
(sort curr-alist 'purescript-ds-imenu-label-cmp))
index-alist)))
(purescript-when-let ((var-alist (gethash 'variable imenu)))
(if purescript-decl-scan-bindings-as-variables
(push (cons "Variables"
(sort index-var-alist 'purescript-ds-imenu-label-cmp))
(sort var-alist 'purescript-ds-imenu-label-cmp))
index-alist)
(setq index-alist (append index-alist
(sort index-var-alist 'purescript-ds-imenu-label-cmp)))))
(sort var-alist 'purescript-ds-imenu-label-cmp)))))
(message "Sorting declarations in %s...done" bufname)
;; Return the alist.
index-alist))
Expand Down

0 comments on commit e42b147

Please sign in to comment.