Skip to content

Commit e42b147

Browse files
committed
Migrate purescript-ds-create-imenu-index to hashtables
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
1 parent bc7d7e9 commit e42b147

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

purescript-decl-scan.el

+21-33
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
(require 'syntax)
105105
(require 'cl-lib)
106106
(require 'imenu)
107+
(require 'subr-x)
107108

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

457+
(defmacro purescript-when-let (spec &rest body)
458+
"A wrapper to silence `when-let' deprecation warning"
459+
(if (fboundp 'when-let*)
460+
(list 'when-let* spec (macroexp-progn body))
461+
(with-no-warnings (list 'when-let spec (macroexp-progn body)))))
462+
456463
;;;###autoload
457464
(defun purescript-ds-create-imenu-index ()
458465
"Function for finding `imenu' declarations in PureScript mode.
@@ -462,11 +469,7 @@ datatypes) in a PureScript file for the `imenu' package."
462469
;; These lists are nested using `(INDEX-TITLE . INDEX-ALIST)'.
463470
(let* ((bird-literate (purescript-ds-bird-p))
464471
(index-alist '())
465-
(index-class-alist '()) ;; Classes
466-
(index-var-alist '()) ;; Variables
467-
(index-imp-alist '()) ;; Imports
468-
(index-inst-alist '()) ;; Instances
469-
(index-type-alist '()) ;; Datatypes
472+
(imenu (make-hash-table :test 'equal))
470473
;; Variables for showing progress.
471474
(bufname (buffer-name))
472475
(divisor-of-progress (max 1 (/ (buffer-size) 100)))
@@ -486,40 +489,25 @@ datatypes) in a PureScript file for the `imenu' package."
486489
(name (car name-posns))
487490
(posns (cdr name-posns))
488491
(start-pos (car posns))
489-
(type (cdr result))
490-
;; Place `(name . start-pos)' in the correct alist.
491-
(sym (cdr (assq type
492-
'((variable . index-var-alist)
493-
(datatype . index-type-alist)
494-
(class . index-class-alist)
495-
(import . index-imp-alist)
496-
(instance . index-inst-alist))))))
497-
(set sym (cons (cons name start-pos) (symbol-value sym))))))
492+
(type (cdr result)))
493+
(puthash type
494+
(cons (cons name start-pos) (gethash type imenu '()))
495+
imenu))))
498496
;; Now sort all the lists, label them, and place them in one list.
499497
(message "Sorting declarations in %s..." bufname)
500-
(when index-type-alist
501-
(push (cons "Datatypes"
502-
(sort index-type-alist 'purescript-ds-imenu-label-cmp))
503-
index-alist))
504-
(when index-inst-alist
505-
(push (cons "Instances"
506-
(sort index-inst-alist 'purescript-ds-imenu-label-cmp))
507-
index-alist))
508-
(when index-imp-alist
509-
(push (cons "Imports"
510-
(sort index-imp-alist 'purescript-ds-imenu-label-cmp))
511-
index-alist))
512-
(when index-class-alist
513-
(push (cons "Classes"
514-
(sort index-class-alist 'purescript-ds-imenu-label-cmp))
515-
index-alist))
516-
(when index-var-alist
498+
(dolist (type '((datatype . "Datatypes") (instance . "Instances")
499+
(import . "Imports") (class . "Classes")))
500+
(purescript-when-let ((curr-alist (gethash (car type) imenu)))
501+
(push (cons (cdr type)
502+
(sort curr-alist 'purescript-ds-imenu-label-cmp))
503+
index-alist)))
504+
(purescript-when-let ((var-alist (gethash 'variable imenu)))
517505
(if purescript-decl-scan-bindings-as-variables
518506
(push (cons "Variables"
519-
(sort index-var-alist 'purescript-ds-imenu-label-cmp))
507+
(sort var-alist 'purescript-ds-imenu-label-cmp))
520508
index-alist)
521509
(setq index-alist (append index-alist
522-
(sort index-var-alist 'purescript-ds-imenu-label-cmp)))))
510+
(sort var-alist 'purescript-ds-imenu-label-cmp)))))
523511
(message "Sorting declarations in %s...done" bufname)
524512
;; Return the alist.
525513
index-alist))

0 commit comments

Comments
 (0)