Skip to content

Commit 60b4a73

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 60b4a73

File tree

1 file changed

+15
-33
lines changed

1 file changed

+15
-33
lines changed

Diff for: purescript-decl-scan.el

+15-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)."
@@ -462,11 +463,7 @@ datatypes) in a PureScript file for the `imenu' package."
462463
;; These lists are nested using `(INDEX-TITLE . INDEX-ALIST)'.
463464
(let* ((bird-literate (purescript-ds-bird-p))
464465
(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
466+
(imenu (make-hash-table :test 'equal))
470467
;; Variables for showing progress.
471468
(bufname (buffer-name))
472469
(divisor-of-progress (max 1 (/ (buffer-size) 100)))
@@ -486,40 +483,25 @@ datatypes) in a PureScript file for the `imenu' package."
486483
(name (car name-posns))
487484
(posns (cdr name-posns))
488485
(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))))))
486+
(type (cdr result)))
487+
(puthash type
488+
(cons (cons name start-pos) (gethash type imenu '()))
489+
imenu))))
498490
;; Now sort all the lists, label them, and place them in one list.
499491
(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
492+
(dolist (type '((datatype . "Datatypes") (instance . "Instances")
493+
(import . "Imports") (class . "Classes")))
494+
(when-let ((curr-alist (gethash (car type) imenu)))
495+
(push (cons (cdr type)
496+
(sort curr-alist 'purescript-ds-imenu-label-cmp))
497+
index-alist)))
498+
(when-let ((var-alist (gethash 'variable imenu)))
517499
(if purescript-decl-scan-bindings-as-variables
518500
(push (cons "Variables"
519-
(sort index-var-alist 'purescript-ds-imenu-label-cmp))
501+
(sort var-alist 'purescript-ds-imenu-label-cmp))
520502
index-alist)
521503
(setq index-alist (append index-alist
522-
(sort index-var-alist 'purescript-ds-imenu-label-cmp)))))
504+
(sort var-alist 'purescript-ds-imenu-label-cmp)))))
523505
(message "Sorting declarations in %s...done" bufname)
524506
;; Return the alist.
525507
index-alist))

0 commit comments

Comments
 (0)