From d32dca5718509e733a0b930e035851d6b5cbaac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arthur=20Fayzrakhmanov=20=28=D0=90=D1=80=D1=82=D1=83=D1=80?= =?UTF-8?q?=20=D0=A4=D0=B0=D0=B9=D0=B7=D1=80=D0=B0=D1=85=D0=BC=D0=B0=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=29?= Date: Sun, 31 Jan 2016 02:12:50 +0500 Subject: [PATCH] Remove suggested imports completely Do not leave blank line when removing import statement. Now covers multiline imports properly when remove or comment out option selected. Tidy up tests a bit. --- haskell-interactive-mode.el | 29 ++++++++++++++++++++----- tests/interactive-haskell-mode-tests.el | 11 +++++----- 2 files changed, 29 insertions(+), 11 deletions(-) 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