[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/go-mode 4371669 029/495: Merge branch 'feature/remove_unus
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/go-mode 4371669 029/495: Merge branch 'feature/remove_unused_imports' |
Date: |
Sat, 7 Aug 2021 09:04:37 -0400 (EDT) |
branch: elpa/go-mode
commit 4371669f2ba0af8fcdeb469b76d9e42a223e2fe3
Merge: cf557a3 22427ec
Author: Dominik Honnef <dominikh@fork-bomb.org>
Commit: Dominik Honnef <dominikh@fork-bomb.org>
Merge branch 'feature/remove_unused_imports'
---
README.md | 1 +
go-mode.el | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
2 files changed, 55 insertions(+), 7 deletions(-)
diff --git a/README.md b/README.md
index 451ab68..1e98677 100644
--- a/README.md
+++ b/README.md
@@ -33,6 +33,7 @@ Features
- Support for `C-M-h` (`mark-defun`)
- Two functions for sending code to the Playground (`go-play-buffer` and
`go-play-region`)
- A function for adding imports, including tab completion (`go-import-add`,
bound to `C-c C-a`)
+- A function for removing or commenting unused imports
(`go-remove-unused-imports`)
- A function for jumping to the file's imports (`go-goto-imports`)
- Adds basic support for imenu (functions and variables)
diff --git a/go-mode.el b/go-mode.el
index d48b9e2..1657111 100644
--- a/go-mode.el
+++ b/go-mode.el
@@ -540,23 +540,40 @@ link in the kill ring."
When called with a prefix argument asks for an alternative name
to import the package as.
-If no list exists yet, one will be created if possible."
+If no list exists yet, one will be created if possible.
+
+If an identical import has been commented, it will be
+uncommented, otherwise a new import will be added."
+
+ ;; - If there's a matching `// import "foo"`, uncomment it
+ ;; - If we're in an import() block and there's a matching `"foo"`, uncomment
it
+ ;; - Otherwise add a new import, with the appropriate syntax
(interactive
(list
current-prefix-arg
(completing-read "Package: " (go-packages))))
(save-excursion
- (let (as line)
+ (let (as line import-start)
(if arg
(setq as (read-from-minibuffer "Import as: ")))
(if as
(setq line (format "%s \"%s\"" as import))
(setq line (format "\"%s\"" import)))
- (case (go-goto-imports)
- ('fail (message "Could not find a place to add import."))
- ('block (insert "\n\t" line))
- ('single (insert "import " line "\n"))
- ('none (insert "\nimport (\n\t" line "\n)\n"))))))
+
+ (beginning-of-buffer)
+ (if (re-search-forward (concat "^// import " line "$") nil t)
+ (uncomment-region (line-beginning-position) (line-end-position))
+ (case (go-goto-imports)
+ ('fail (message "Could not find a place to add import."))
+ ('block
+ (save-excursion
+ (re-search-backward "^import (")
+ (setq import-start (point)))
+ (if (re-search-backward (concat "^[[:space:]]+// " line "$")
import-start t)
+ (uncomment-region (line-beginning-position)
(line-end-position))
+ (insert "\n\t" line)))
+ ('single (insert "import " line "\n"))
+ ('none (insert "\nimport (\n\t" line "\n)\n")))))))
(defun go--directory-dirs (dir)
(if (file-directory-p dir)
@@ -618,5 +635,35 @@ 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 ()
+ (let (cmd)
+ (if (string-match "_test\.go$" buffer-file-truename)
+ (setq cmd "go test -c")
+ (setq cmd "go build -o /dev/null"))
+ (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 cmd) "\n"))))))
+
+(defun go-remove-unused-imports (arg)
+ "Removes all unused imports. If ARG is non-nil, unused imports
+will be commented, otherwise they will be removed completely."
+ (interactive "P")
+ (save-excursion
+ (let ((cur-buffer (current-buffer)) lines)
+ (save-some-buffers nil (lambda () (equal cur-buffer (current-buffer))))
+ (if (buffer-modified-p)
+ (message "Cannot operate on unsaved buffer")
+ (setq 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)
- [nongnu] elpa/go-mode 54a102e 046/495: do not move cursor to beginning of line after indentation, (continued)
- [nongnu] elpa/go-mode 54a102e 046/495: do not move cursor to beginning of line after indentation, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 1128e71 052/495: do not try to turn off flymake if flymake isn't loaded, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 903768b 053/495: tweak godoc to behave more like help in emacs, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode e78baea 054/495: do not leave point at beginning of line when (not) indenting a comment, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode a5164ac 055/495: Fix indentation for code after multi-line function signature, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 72cf126 056/495: fix indentation for multi-line if expressions, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode cfc3d50 058/495: add more test cases, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 63929d2 085/495: get rid of go--flatten, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 850986e 022/495: Do not loop forever in go-beginning-of-defun if point is in comment and there's no function definition before point, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode b91c53e 025/495: save buffer before trying to remove unused imports, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 4371669 029/495: Merge branch 'feature/remove_unused_imports',
ELPA Syncer <=
- [nongnu] elpa/go-mode cd13093 034/495: use inhibit-read-only instead of toggle-read-only, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode b6dfdd4 037/495: Closes gh-5, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 9dbdf22 041/495: Do not skip over strings when checking for dangling operator, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 95c9519 049/495: when searching for anchor for indentation, do not limit search to one line, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 399393a 057/495: remove dead code, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode ee10287 059/495: Revert "Fix indentation for code after multi-line function signature", ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 79d5860 073/495: improve fontification, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 49b9355 074/495: do not change indentation if in multiline string, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 522543f 077/495: replace several defuns with macros, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 4aeb058 078/495: add cache to go-previous-line-has-dangling-op-p, ELPA Syncer, 2021/08/07