Skip to content

Remove suggested imports completely #1121

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

Merged
merged 1 commit into from
Feb 1, 2016
Merged
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
29 changes: 23 additions & 6 deletions haskell-interactive-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ SESSION in specified FILE to remove IMPORT on given LINE."
(cl-case (read-event
(propertize (format "%sThe import line `%s' is redundant. Remove? (y, n, c: comment out) "
(if (not first)
"Please answer n, y or c: "
"Please answer y, n or c: "
"")
import)
'face
Expand All @@ -591,21 +591,38 @@ SESSION in specified FILE to remove IMPORT on given LINE."
(save-excursion
(goto-char (point-min))
(forward-line (1- line))
(goto-char (line-beginning-position))
(delete-region (line-beginning-position)
(line-end-position))))
(let ((bounds (haskell-interactive-mode--import-statement-bounds)))
(delete-region (car bounds) (cdr bounds))
(kill-line 1))))
(?n
(message "Ignoring redundant import %s" import))
(?c
(haskell-process-find-file session file)
(save-excursion
(goto-char (point-min))
(forward-line (1- line))
(goto-char (line-beginning-position))
(insert "-- "))))
(let ((bounds (haskell-interactive-mode--import-statement-bounds)))
(comment-region (car bounds) (cdr bounds))))))
;; unwind
(haskell-mode-toggle-interactive-prompt-state t))))

(defun haskell-interactive-mode--import-statement-bounds ()
"For internal use in `haskell-process-suggest-remove-import'.
This function supposed to be called having point placed on first
line of import statement, if this is a case it search import
statement bounds relying on layout and returns them as cons cell;
otherwise returns nil."
(save-excursion
(goto-char (line-beginning-position))
(when (looking-at-p (regexp-quote "import"))
(let ((a (point))
(z (line-end-position)))
(forward-line 1)
(while (looking-at-p (rx (and not-newline (1+ whitespace))))
(setq z (line-end-position))
(forward-line 1))
(cons a z)))))

(defun haskell-process-find-file (session file)
"Find the given file in the project."
(find-file (cond ((file-exists-p (concat (haskell-session-current-dir session) "/" file))
Expand Down
11 changes: 6 additions & 5 deletions tests/interactive-haskell-mode-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,21 @@
(require 'ert)
(require 'haskell-interactive-mode)

(defun should-match (str)
(should (eq 0 (string-match-p haskell-interactive-mode-error-regexp str))))

(ert-deftest haskell-interactive-error-regexp-test ()
"Tests the regexp `haskell-interactive-mode-error-regexp'"
(should (eq 0 (string-match-p haskell-interactive-mode-error-regexp
"/home/user/Test.hs:24:30:")))
(should (eq 0 (string-match-p haskell-interactive-mode-error-regexp
"Test.hs:5:18:")))
(should (eq 0 (string-match-p haskell-interactive-mode-error-regexp
"Test.hs:7:6: Not in scope: type constructor or class ‘Ty’")))
(should (eq 0 (string-match-p
haskell-interactive-mode-error-regexp
"Test.hs:7:6: Not in scope: type constructor or class ‘Ty’")))
(should (eq 0 (string-match-p haskell-interactive-mode-error-regexp
"Test.hs:9:5: Not in scope: ‘c’")))
(should (eq nil (string-match-p haskell-interactive-mode-error-regexp
;; leading space
" Test.hs:8:9:")))
)

(provide 'interactive-haskell-mode-tests)
;;; interactive-haskell-mode-tests.el ends here