emacs-psci icon indicating copy to clipboard operation
emacs-psci copied to clipboard

load-current-file! doesn't load that file's imports to psci's scope

Open mpdairy opened this issue 5 years ago • 2 comments

It would be nice if, when you loaded a file, all the imports in the file would be in scope in the repl, so you could access anything that code in that file could also access. So if it imported Prelude, then when you loaded that file, all the functions in prelude would be available to use. However, it seems to only load the functions and types that are declared within that actual module. Is there a way to do that?

mpdairy avatar Jan 09 '20 18:01 mpdairy

@mpdairy I ran into the same annoyance and hacked together this bit of elisp, which seems to do what we both want, although I've barely tested it and it may have some unintended side effects:

(use-package psci
  :hook (purescript-mode . inferior-psci-mode)
  :config
  (defun spl/psci-send-import-lines! ()
    (interactive)
    (save-excursion
      (goto-char (point-min))
      (while (re-search-forward
              (rx (and line-start "import" (1+ space)
                       (group (and (1+ (any word "."))))
                       (opt (1+ (any space "\n")) "hiding")
                       (opt (1+ (any space "\n"))
                            "(" (0+ (any word "," space "\n")) ")")
                       (opt (1+ (any space "\n")) "as" (1+ space)
                            (group (and (1+ (any word ".")))))))
              nil t)
        (psci--run-psci-command!
         (replace-regexp-in-string "\n" " " (match-string 0)))))
    )
  (defun spl/psci-load-current-file-with-imports! ()
    (interactive)
    (psci/load-current-file!)
    (spl/psci-send-import-lines!))
  :bind (:map purescript-mode-map
              (("C-c l" . spl/psci-load-current-file-with-imports!)))
  )

I'll try this out for a bit and see how it goes.

Edit: Reconsidered my assumption that people would be using psc-ide-emacs and replaced psc-ide-import-regex with the actual regex from psc-ide.el for matching imports.

Edit 2: This isn't quite right, as it doesn't properly respect multi-line imports.

Edit 3: Revised the regex to handle multi-line imports.

shaunplee avatar Dec 29 '20 20:12 shaunplee

One week later, my above hack seems to be working as expected, although I'm not doing anything more complicated than the exercises in PureScript by Example.

shaunplee avatar Jan 05 '21 21:01 shaunplee