emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/go-mode 37181ae 021/495: add feature for removing unused i


From: ELPA Syncer
Subject: [nongnu] elpa/go-mode 37181ae 021/495: add feature for removing unused imports
Date: Sat, 7 Aug 2021 09:04:36 -0400 (EDT)

branch: elpa/go-mode
commit 37181ae561593f8c9aa9c590ec440db96b20362c
Author: Dominik Honnef <dominikh@fork-bomb.org>
Commit: Dominik Honnef <dominikh@fork-bomb.org>

    add feature for removing unused imports
---
 go-mode.el | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/go-mode.el b/go-mode.el
index 6592b5e..63676ea 100644
--- a/go-mode.el
+++ b/go-mode.el
@@ -612,5 +612,26 @@ If no list exists yet, one will be created if possible."
                   (go--directory-dirs pkgdir))))
       (go-root-and-paths)))) 'string<))
 
+(defun go-unused-imports-lines ()
+  ;; TODO handle *_test.gi files
+  (reverse (remove nil
+                   (mapcar
+                    (lambda (line)
+                      (if (string-match "^\\(.+\\):\\([[:digit:]]+\\): 
imported and not used: \".+\"$" line)
+                          (if (string= (file-truename (match-string 1 line)) 
buffer-file-truename)
+                              (string-to-number (match-string 2 line)))))
+                    (split-string (shell-command-to-string "go build -o 
/dev/null") "\n")))))
+
+(defun go-remove-unused-imports (arg)
+  (interactive "P")
+  (save-excursion
+    (let ((lines (go-unused-imports-lines)))
+      (dolist (import lines)
+        (goto-line import)
+        (beginning-of-line)
+        (if arg
+            (comment-region (line-beginning-position) (line-end-position))
+          (kill-line)))
+      (message "Removed %d imports" (length lines)))))
 
 (provide 'go-mode)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]