diff --git a/haskell-interactive-mode.el b/haskell-interactive-mode.el index e19ed015b..c5f281853 100644 --- a/haskell-interactive-mode.el +++ b/haskell-interactive-mode.el @@ -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 @@ -591,9 +591,9 @@ 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 @@ -601,11 +601,28 @@ 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)) - (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)) diff --git a/tests/interactive-haskell-mode-tests.el b/tests/interactive-haskell-mode-tests.el index f87e1eec3..a7e416a0f 100644 --- a/tests/interactive-haskell-mode-tests.el +++ b/tests/interactive-haskell-mode-tests.el @@ -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