Skip to content

Commit 06286b0

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: purescript-emacs#25
1 parent d187b3d commit 06286b0

File tree

1 file changed

+14
-33
lines changed

1 file changed

+14
-33
lines changed

purescript-decl-scan.el

+14-33
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,7 @@ datatypes) in a PureScript file for the `imenu' package."
462462
;; These lists are nested using `(INDEX-TITLE . INDEX-ALIST)'.
463463
(let* ((bird-literate (purescript-ds-bird-p))
464464
(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
465+
(imenu (make-hash-table :test 'equal))
470466
;; Variables for showing progress.
471467
(bufname (buffer-name))
472468
(divisor-of-progress (max 1 (/ (buffer-size) 100)))
@@ -486,40 +482,25 @@ datatypes) in a PureScript file for the `imenu' package."
486482
(name (car name-posns))
487483
(posns (cdr name-posns))
488484
(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))))))
485+
(type (cdr result)))
486+
(puthash type
487+
(cons (cons name start-pos) (gethash type imenu '()))
488+
imenu))))
498489
;; Now sort all the lists, label them, and place them in one list.
499490
(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
491+
(dolist (type '((datatype . "Datatypes") (instance . "Instances")
492+
(import . "Imports") (class . "Classes")))
493+
(when-let* ((curr-alist (gethash (car type) imenu)))
494+
(push (cons (cdr type)
495+
(sort curr-alist 'purescript-ds-imenu-label-cmp))
496+
index-alist)))
497+
(when-let* ((var-alist (gethash 'variable imenu)))
517498
(if purescript-decl-scan-bindings-as-variables
518499
(push (cons "Variables"
519-
(sort index-var-alist 'purescript-ds-imenu-label-cmp))
500+
(sort var-alist 'purescript-ds-imenu-label-cmp))
520501
index-alist)
521502
(setq index-alist (append index-alist
522-
(sort index-var-alist 'purescript-ds-imenu-label-cmp)))))
503+
(sort var-alist 'purescript-ds-imenu-label-cmp)))))
523504
(message "Sorting declarations in %s...done" bufname)
524505
;; Return the alist.
525506
index-alist))

0 commit comments

Comments
 (0)