Skip to content

Pick haddock root from .haddock-ref file #1859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ build-$(EMACS_VERSION)/build-flag : build-$(EMACS_VERSION) $(patsubst %.el,build
touch $@

check-%: tests/%-tests.el
$(BATCH) -l "$<" -f ert-run-tests-batch-and-exit;
$(BATCH) --eval $(INIT_PACKAGES) -l "$<" -f ert-run-tests-batch-and-exit;

check: compile $(AUTOLOADS) check-package-lint check-relint check-ert

Expand Down
2 changes: 1 addition & 1 deletion haskell-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
;; Copyright © 1992, 1997-1998 Simon Marlow, Graeme E Moss, and Tommy Thorn

;; Version: 17.5
;; Package-Requires: ((emacs "25.1"))
;; Package-Requires: ((emacs "25.1") (f "0.20.0"))
;; Keywords: haskell cabal ghc repl languages
;; URL: https://github.com/haskell/haskell-mode

Expand Down
37 changes: 37 additions & 0 deletions tests/w3m-haddock-tests.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
;;; w3m-haddock-tests.el --- Tests for w3m-haddocks -*- lexical-binding: t -*-
;;; Summary:
;;; Code:

(require 'ert)
(require 'f)
(require 'haskell)
(require 'w3m-haddock) ;; implementation under test

(defmacro with-temp-directory (tdir &rest body)
"Cleate TDIR temp directory and execute BODY."
`(let ((,tdir (make-temp-file "tmpdir" t)))
(unwind-protect
(progn ,@body))
(delete-directory ,tdir t)))

(ert-deftest w3m-haddock-discover-ref-test ()
(with-temp-directory
d
(let ((haddock-root (concat d "/nix/store/53195319531953-ghc-with-packages/share/doc")))
(make-directory haddock-root t)
(f-write-text haddock-root nil (concat d "/.haddock-ref"))
(make-directory (concat d "/src/a/b/c") t)
(f-write-text "module Main where" nil (concat d "/src/a/b/c/Main.hs"))
(find-file (concat d "/src/a/b/c/Main.hs"))
(should (equal haddock-root (w3m-haddock-discover-ref))))))

(ert-deftest w3m-haddock-discover-ref-nil-test ()
(with-temp-directory
d
(make-directory (concat d "/src/a/b/c") t)
(f-write-text "module Main where" nil (concat d "/src/a/b/c/Main.hs"))
(find-file (concat d "/src/a/b/c/Main.hs"))
(should (null (w3m-haddock-discover-ref)))))

(provide 'w3m-haddock-tests)
;;; w3m-haddock-tests.el ends here
31 changes: 27 additions & 4 deletions w3m-haddock.el
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
;;; Code:

(require 'cl-lib)
;; (require 'f)
(require 'haskell-mode)
(require 'haskell-font-lock)

Expand All @@ -40,6 +41,23 @@
"Face for quarantines."
:group 'haskell)

(defun w3m-haddock-discover-ref ()
"Return path form \".haddock-ref\" file or nil.

The file is picked up from the closest parent directory
relative to current file. If GHC is provided via nix-shell then path
to haddocks can be discovered in a shell hook and persisted in the project root:
\"echo $(dirname $(dirname $(which ghc)))/share/doc > .haddock-ref\"."
(let ((ap (buffer-file-name))
(r nil))
(while (and (null r) (setq ap (and ap (f-dirname ap))))
(let ((href (concat ap "/.haddock-ref")))
(when (file-exists-p href)
(setq href (string-trim (f-read-text href)))
(when (f-directory-p href)
(setq r href)))))
r))

(defcustom haskell-w3m-haddock-dirs
'("~/.cabal/share/doc/")
"The path to your cabal documentation dir.
Expand All @@ -54,26 +72,31 @@ You can rebind this if you're using hsenv by adding it to your

"
:group 'haskell
:type 'list)
:type '(list string))

(defvar w3m-haddock-entry-regex "^\\(\\(data\\|type\\) \\|[a-z].* :: \\)"
"Regex to match entry headings.")

(defun haskell-w3m-open-haddock ()
"Open a haddock page in w3m."
(interactive)
(let* ((entries (cl-remove-if (lambda (s) (string= s ""))
(let* (
(haddock-ref (w3m-haddock-discover-ref))
(haddock-dirs (if (null haddock-ref)
haskell-w3m-haddock-dirs
(cons haddock-ref haskell-w3m-haddock-dirs)))
(entries (cl-remove-if (lambda (s) (string= s ""))
(apply 'append (mapcar (lambda (dir)
(split-string (shell-command-to-string (concat "ls -1 " dir))

"\n"))
haskell-w3m-haddock-dirs))))
haddock-dirs))))
(package-dir (ido-completing-read
"Package: "
entries)))
(cond
((member package-dir entries)
(unless (cl-loop for dir in haskell-w3m-haddock-dirs
(unless (cl-loop for dir in haddock-dirs
when (w3m-haddock-find-index dir package-dir)
do (progn (w3m-browse-url (w3m-haddock-find-index dir package-dir)
t)
Expand Down